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 => 30;
28 use Koha::LibraryCategories;
32 use lib $FindBin::Bin;
56 my $dbh = C4::Context->dbh;
57 $dbh->{AutoCommit} = 0;
58 $dbh->{RaiseError} = 1;
61 $dbh->do('DELETE FROM branchcategories');
65 my $count = GetBranchesCount();
66 like( $count, '/^\d+$/', "the count is a number" );
72 branchname => 'BranchA',
73 branchaddress1 => 'adr1A',
74 branchaddress2 => 'adr2A',
75 branchaddress3 => 'adr3A',
77 branchcity => 'cityA',
78 branchstate => 'stateA',
79 branchcountry => 'countryA',
80 branchphone => 'phoneA',
82 branchemail => 'emailA',
83 branchreplyto => 'emailreply',
84 branchreturnpath => 'branchreturn',
87 branchprinter => undef,
88 branchnotes => 'noteA',
93 branchname => 'BranchB',
94 branchaddress1 => 'adr1B',
95 branchaddress2 => 'adr2B',
96 branchaddress3 => 'adr3B',
98 branchcity => 'cityB',
99 branchstate => 'stateB',
100 branchcountry => 'countryB',
101 branchphone => 'phoneB',
103 branchemail => 'emailB',
104 branchreplyto => 'emailreply',
105 branchreturnpath => 'branchreturn',
108 branchprinter => undef,
109 branchnotes => 'noteB',
110 opac_info => 'opacB',
113 is( ModBranch($b2), undef, 'the field add is missing' );
117 is( GetBranchesCount(), $count + 2, "two branches added" );
119 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1, "One row affected" );
120 is( GetBranchesCount(), $count + 1, "branch BRB deleted" );
123 is( GetBranchName( $b1->{branchcode} ),
124 $b1->{branchname}, "GetBranchName returns the right name" );
126 #Test GetBranchDetail
127 my $branchdetail = GetBranchDetail( $b1->{branchcode} );
128 $branchdetail->{add} = 1;
129 $b1->{issuing} = undef; # Not used in DB
130 is_deeply( $branchdetail, $b1, 'branchdetail is right' );
133 my $branches = GetBranches();
134 is( scalar( keys %$branches ),
135 GetBranchesCount(), "GetBranches returns the right number of branches" );
141 branchname => 'BranchA modified',
142 branchaddress1 => 'adr1A modified',
143 branchaddress2 => 'adr2A modified',
144 branchaddress3 => 'adr3A modified',
145 branchzip => 'zipA modified',
146 branchcity => 'cityA modified',
147 branchstate => 'stateA modified',
148 branchcountry => 'countryA modified',
149 branchphone => 'phoneA modified',
150 branchfax => 'faxA modified',
151 branchemail => 'emailA modified',
152 branchreplyto => 'emailreply modified',
153 branchreturnpath => 'branchreturn modified',
154 branchurl => 'urlA modified',
155 branchip => 'ipA modified',
156 branchprinter => undef,
157 branchnotes => 'notesA modified',
158 opac_info => 'opacA modified'
162 is( GetBranchesCount(), $count + 1,
163 "A branch has been modified, no new branch added" );
164 $branchdetail = GetBranchDetail( $b1->{branchcode} );
165 $b1->{issuing} = undef;
166 is_deeply( $branchdetail, $b1 , "GetBranchDetail gives the details of BRA");
169 my $categories = GetBranchCategories();
170 my $count_cat = scalar( @$categories );
174 categorycode => 'CAT1',
175 categoryname => 'catname1',
176 codedescription => 'catdesc1',
177 categorytype => 'cattype1',
178 show_in_pulldown => 1
182 categorycode => 'CAT2',
183 categoryname => 'catname2',
184 categorytype => 'catype2',
185 codedescription => 'catdesc2',
186 show_in_pulldown => 1
190 categorycode => 'LIBCATCODE',
191 categoryname => 'library category name',
192 codedescription => 'library category code description',
193 categorytype => 'searchdomain',
194 show_in_pulldown => 1,
197 ModBranchCategoryInfo({
202 ModBranchCategoryInfo($cat1);
203 ModBranchCategoryInfo($cat2);
205 $categories = GetBranchCategories();
206 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
209 delete $new_category{add};
210 is_deeply($categories, [ $cat1,$cat2,\%new_category ], 'retrieve all expected library categories (bug 10515)');
212 #test GetBranchCategory
213 my $cat1detail = GetBranchCategory( $cat1->{categorycode} );
215 is_deeply( $cat1detail, $cat1, 'CAT1 details are right' );
216 my $category = GetBranchCategory('LIBCATCODE');
217 is_deeply($category, \%new_category, 'fetched newly added library category');
219 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
220 is( $del, 1, 'One row affected' );
222 $categories = GetBranchCategories();
223 is( scalar( @$categories ), $count_cat + 2, "Category CAT2 deleted" );
225 my $cat2detail = GetBranchCategory( $cat2->{categorycode} );
226 is( $cat2detail, undef, 'CAT2 doesnt exist' );
228 $category = GetBranchCategory();
229 is($category, undef, 'retrieve library category only if code is supplied (bug 10515)');
233 is( GetBranchesCount(), $count + 2, 'BRB added' );
236 my $b1info = GetBranchInfo( $b1->{branchcode} );
237 $b1->{categories} = [];
238 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
240 my $b2info = GetBranchInfo( $b2->{branchcode} );
241 my @cat = ( $cat1->{categorycode} );
244 $b2->{issuing} = undef;
245 $b2->{categories} = \@cat;
246 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
248 ModBranchCategoryInfo({add => 1,%$cat2});
249 $categories = GetBranchCategories();
250 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
253 branchname => 'BranchB',
254 branchaddress1 => 'adr1B',
255 branchaddress2 => 'adr2B',
256 branchaddress3 => 'adr3B',
258 branchcity => 'cityB',
259 branchstate => 'stateB',
260 branchcountry => 'countryB',
261 branchphone => 'phoneB',
263 branchemail => 'emailB',
264 branchreplyto => 'emailreply',
265 branchreturnpath => 'branchreturn',
268 branchprinter => undef,
269 branchnotes => 'noteB',
270 opac_info => 'opacB',
275 $b2info = GetBranchInfo( $b2->{branchcode} );
276 push( @cat, $cat2->{categorycode} );
279 $b2->{issuing} = undef;
280 $b2->{categories} = \@cat;
281 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
283 #Test GetBranchesInCategory
284 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
285 my @b = ( $b2->{branchcode} );
286 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
291 branchname => 'BranchC',
292 branchaddress1 => 'adr1C',
293 branchaddress2 => 'adr2C',
294 branchaddress3 => 'adr3C',
296 branchcity => 'cityC',
297 branchstate => 'stateC',
298 branchcountry => 'countryC',
299 branchphone => 'phoneC',
301 branchemail => 'emailC',
304 branchprinter => undef,
305 branchnotes => 'noteC',
306 opac_info => 'opacC',
311 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
312 push( @b, $b3->{branchcode} );
313 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
315 #Test GetCategoryTypes
316 my @category_types = GetCategoryTypes();
317 is_deeply(\@category_types, [ 'searchdomain', 'properties' ], 'received expected library category types');
319 $categories = GetBranchCategories(undef, undef, 'LIBCATCODE');
320 is_deeply($categories, [ {%$cat1}, {%$cat2},{ %new_category, selected => 1 } ], 'retrieve expected, eselected library category (bug 10515)');
322 #TODO later: test mybranchine and onlymine
323 # Actually we cannot mock C4::Context->userenv in unit tests
325 #Test GetBranchesLoop
326 my $loop = GetBranchesLoop;
327 is( scalar(@$loop), GetBranchesCount(), 'There is the right number of branches' );