3 # Copyright 2013 Equinox Software, Inc.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Test::More tests => 23;
28 use Koha::LibraryCategories;
32 use lib $FindBin::Bin;
51 my $dbh = C4::Context->dbh;
52 $dbh->{AutoCommit} = 0;
53 $dbh->{RaiseError} = 1;
56 $dbh->do('DELETE FROM branchcategories');
60 my $count = Koha::Libraries->search->count;
61 like( $count, '/^\d+$/', "the count is a number" );
67 branchname => 'BranchA',
68 branchaddress1 => 'adr1A',
69 branchaddress2 => 'adr2A',
70 branchaddress3 => 'adr3A',
72 branchcity => 'cityA',
73 branchstate => 'stateA',
74 branchcountry => 'countryA',
75 branchphone => 'phoneA',
77 branchemail => 'emailA',
78 branchreplyto => 'emailreply',
79 branchreturnpath => 'branchreturn',
82 branchprinter => undef,
83 branchnotes => 'noteA',
88 branchname => 'BranchB',
89 branchaddress1 => 'adr1B',
90 branchaddress2 => 'adr2B',
91 branchaddress3 => 'adr3B',
93 branchcity => 'cityB',
94 branchstate => 'stateB',
95 branchcountry => 'countryB',
96 branchphone => 'phoneB',
98 branchemail => 'emailB',
99 branchreplyto => 'emailreply',
100 branchreturnpath => 'branchreturn',
103 branchprinter => undef,
104 branchnotes => 'noteB',
105 opac_info => 'opacB',
108 is( ModBranch($b2), undef, 'the field add is missing' );
112 is( Koha::Libraries->search->count, $count + 2, "two branches added" );
114 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1, "One row affected" );
115 is( Koha::Libraries->search->count, $count + 1, "branch BRB deleted" );
118 is( GetBranchName( $b1->{branchcode} ),
119 $b1->{branchname}, "GetBranchName returns the right name" );
121 #Test GetBranchDetail
122 my $branchdetail = GetBranchDetail( $b1->{branchcode} );
123 $branchdetail->{add} = 1;
124 $b1->{issuing} = undef; # Not used in DB
125 is_deeply( $branchdetail, $b1, 'branchdetail is right' );
128 my $branches = GetBranches();
129 is( scalar( keys %$branches ),
130 Koha::Libraries->search->count, "GetBranches returns the right number of branches" );
136 branchname => 'BranchA modified',
137 branchaddress1 => 'adr1A modified',
138 branchaddress2 => 'adr2A modified',
139 branchaddress3 => 'adr3A modified',
140 branchzip => 'zipA modified',
141 branchcity => 'cityA modified',
142 branchstate => 'stateA modified',
143 branchcountry => 'countryA modified',
144 branchphone => 'phoneA modified',
145 branchfax => 'faxA modified',
146 branchemail => 'emailA modified',
147 branchreplyto => 'emailreply modified',
148 branchreturnpath => 'branchreturn modified',
149 branchurl => 'urlA modified',
150 branchip => 'ipA modified',
151 branchprinter => undef,
152 branchnotes => 'notesA modified',
153 opac_info => 'opacA modified'
157 is( Koha::Libraries->search->count, $count + 1,
158 "A branch has been modified, no new branch added" );
159 $branchdetail = GetBranchDetail( $b1->{branchcode} );
160 $b1->{issuing} = undef;
161 is_deeply( $branchdetail, $b1 , "GetBranchDetail gives the details of BRA");
164 my $count_cat = Koha::LibraryCategories->search->count;
167 categorycode => 'CAT1',
168 categoryname => 'catname1',
169 codedescription => 'catdesc1',
170 categorytype => 'cattype1',
171 show_in_pulldown => 1
174 categorycode => 'CAT2',
175 categoryname => 'catname2',
176 categorytype => 'catype2',
177 codedescription => 'catdesc2',
178 show_in_pulldown => 1
182 categorycode => 'LIBCATCODE',
183 categoryname => 'library category name',
184 codedescription => 'library category code description',
185 categorytype => 'searchdomain',
186 show_in_pulldown => 1,
189 Koha::LibraryCategory->new(\%new_category)->store;
190 Koha::LibraryCategory->new($cat1)->store;
191 Koha::LibraryCategory->new($cat2)->store;
193 my $categories = Koha::LibraryCategories->search;
194 is( $categories->count, $count_cat + 3, "Two categories added" );
196 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
197 is( $del, 1, 'One row affected' );
199 is( Koha::LibraryCategories->search->count, $count_cat + 2, "Category CAT 2 deleted" );
203 is( Koha::Libraries->search->count, $count + 2, 'BRB added' );
206 my $b1info = GetBranchInfo( $b1->{branchcode} );
207 $b1->{categories} = [];
208 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
210 my $b2info = GetBranchInfo( $b2->{branchcode} );
211 my @cat = ( $cat1->{categorycode} );
214 $b2->{issuing} = undef;
215 $b2->{categories} = \@cat;
216 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
218 Koha::LibraryCategory->new($cat2)->store;
219 is( Koha::LibraryCategories->search->count, $count_cat + 3, "Two categories added" );
222 branchname => 'BranchB',
223 branchaddress1 => 'adr1B',
224 branchaddress2 => 'adr2B',
225 branchaddress3 => 'adr3B',
227 branchcity => 'cityB',
228 branchstate => 'stateB',
229 branchcountry => 'countryB',
230 branchphone => 'phoneB',
232 branchemail => 'emailB',
233 branchreplyto => 'emailreply',
234 branchreturnpath => 'branchreturn',
237 branchprinter => undef,
238 branchnotes => 'noteB',
239 opac_info => 'opacB',
244 $b2info = GetBranchInfo( $b2->{branchcode} );
245 push( @cat, $cat2->{categorycode} );
248 $b2->{issuing} = undef;
249 $b2->{categories} = \@cat;
250 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
252 #Test GetBranchesInCategory
253 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
254 my @b = ( $b2->{branchcode} );
255 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
260 branchname => 'BranchC',
261 branchaddress1 => 'adr1C',
262 branchaddress2 => 'adr2C',
263 branchaddress3 => 'adr3C',
265 branchcity => 'cityC',
266 branchstate => 'stateC',
267 branchcountry => 'countryC',
268 branchphone => 'phoneC',
270 branchemail => 'emailC',
273 branchprinter => undef,
274 branchnotes => 'noteC',
275 opac_info => 'opacC',
280 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
281 push( @b, $b3->{branchcode} );
282 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
284 #TODO later: test mybranchine and onlymine
285 # Actually we cannot mock C4::Context->userenv in unit tests
287 #Test GetBranchesLoop
288 my $loop = GetBranchesLoop;
289 is( scalar(@$loop), Koha::Libraries->search->count, 'There is the right number of branches' );