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 => 36;
28 use Koha::LibraryCategories;
32 use lib $FindBin::Bin;
45 CheckBranchCategorycode
58 my $dbh = C4::Context->dbh;
59 $dbh->{AutoCommit} = 0;
60 $dbh->{RaiseError} = 1;
63 $dbh->do('DELETE FROM branchcategories');
67 my $count = GetBranchesCount();
68 like( $count, '/^\d+$/', "the count is a number" );
74 branchname => 'BranchA',
75 branchaddress1 => 'adr1A',
76 branchaddress2 => 'adr2A',
77 branchaddress3 => 'adr3A',
79 branchcity => 'cityA',
80 branchstate => 'stateA',
81 branchcountry => 'countryA',
82 branchphone => 'phoneA',
84 branchemail => 'emailA',
85 branchreplyto => 'emailreply',
86 branchreturnpath => 'branchreturn',
89 branchprinter => undef,
90 branchnotes => 'noteA',
95 branchname => 'BranchB',
96 branchaddress1 => 'adr1B',
97 branchaddress2 => 'adr2B',
98 branchaddress3 => 'adr3B',
100 branchcity => 'cityB',
101 branchstate => 'stateB',
102 branchcountry => 'countryB',
103 branchphone => 'phoneB',
105 branchemail => 'emailB',
106 branchreplyto => 'emailreply',
107 branchreturnpath => 'branchreturn',
110 branchprinter => undef,
111 branchnotes => 'noteB',
112 opac_info => 'opacB',
115 is( ModBranch($b2), undef, 'the field add is missing' );
119 is( GetBranchesCount(), $count + 2, "two branches added" );
121 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1, "One row affected" );
122 is( GetBranchesCount(), $count + 1, "branch BRB deleted" );
125 is( GetBranchName( $b1->{branchcode} ),
126 $b1->{branchname}, "GetBranchName returns the right name" );
128 #Test GetBranchDetail
129 my $branchdetail = GetBranchDetail( $b1->{branchcode} );
130 $branchdetail->{add} = 1;
131 $b1->{issuing} = undef; # Not used in DB
132 is_deeply( $branchdetail, $b1, 'branchdetail is right' );
135 my $branches = GetBranches();
136 is( scalar( keys %$branches ),
137 GetBranchesCount(), "GetBranches returns the right number of branches" );
143 branchname => 'BranchA modified',
144 branchaddress1 => 'adr1A modified',
145 branchaddress2 => 'adr2A modified',
146 branchaddress3 => 'adr3A modified',
147 branchzip => 'zipA modified',
148 branchcity => 'cityA modified',
149 branchstate => 'stateA modified',
150 branchcountry => 'countryA modified',
151 branchphone => 'phoneA modified',
152 branchfax => 'faxA modified',
153 branchemail => 'emailA modified',
154 branchreplyto => 'emailreply modified',
155 branchreturnpath => 'branchreturn modified',
156 branchurl => 'urlA modified',
157 branchip => 'ipA modified',
158 branchprinter => undef,
159 branchnotes => 'notesA modified',
160 opac_info => 'opacA modified'
164 is( GetBranchesCount(), $count + 1,
165 "A branch has been modified, no new branch added" );
166 $branchdetail = GetBranchDetail( $b1->{branchcode} );
167 $b1->{issuing} = undef;
168 is_deeply( $branchdetail, $b1 , "GetBranchDetail gives the details of BRA");
171 my $categories = GetBranchCategories();
172 my $count_cat = scalar( @$categories );
176 categorycode => 'CAT1',
177 categoryname => 'catname1',
178 codedescription => 'catdesc1',
179 categorytype => 'cattype1',
180 show_in_pulldown => 1
184 categorycode => 'CAT2',
185 categoryname => 'catname2',
186 categorytype => 'catype2',
187 codedescription => 'catdesc2',
188 show_in_pulldown => 1
192 categorycode => 'LIBCATCODE',
193 categoryname => 'library category name',
194 codedescription => 'library category code description',
195 categorytype => 'searchdomain',
196 show_in_pulldown => 1,
199 ModBranchCategoryInfo({
204 ModBranchCategoryInfo($cat1);
205 ModBranchCategoryInfo($cat2);
207 $categories = GetBranchCategories();
208 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
211 delete $new_category{add};
212 is_deeply($categories, [ $cat1,$cat2,\%new_category ], 'retrieve all expected library categories (bug 10515)');
214 #test GetBranchCategory
215 my $cat1detail = GetBranchCategory( $cat1->{categorycode} );
217 is_deeply( $cat1detail, $cat1, 'CAT1 details are right' );
218 my $category = GetBranchCategory('LIBCATCODE');
219 is_deeply($category, \%new_category, 'fetched newly added library category');
221 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
222 is( $del, 1, 'One row affected' );
224 $categories = GetBranchCategories();
225 is( scalar( @$categories ), $count_cat + 2, "Category CAT2 deleted" );
227 my $cat2detail = GetBranchCategory( $cat2->{categorycode} );
228 is( $cat2detail, undef, 'CAT2 doesnt exist' );
230 $category = GetBranchCategory();
231 is($category, undef, 'retrieve library category only if code is supplied (bug 10515)');
233 #Test CheckBranchCategoryCode
234 my $check1 = CheckBranchCategorycode( $cat1->{categorycode} );
235 my $check2 = CheckBranchCategorycode( $cat2->{categorycode} );
236 like( $check1, '/^\d+$/', "CheckBranchCategorycode returns a number" );
240 is( GetBranchesCount(), $count + 2, 'BRB added' );
242 CheckBranchCategorycode( $cat1->{categorycode} ),
248 my $b1info = GetBranchInfo( $b1->{branchcode} );
249 $b1->{categories} = [];
250 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
252 my $b2info = GetBranchInfo( $b2->{branchcode} );
253 my @cat = ( $cat1->{categorycode} );
256 $b2->{issuing} = undef;
257 $b2->{categories} = \@cat;
258 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
260 ModBranchCategoryInfo({add => 1,%$cat2});
261 $categories = GetBranchCategories();
262 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
265 branchname => 'BranchB',
266 branchaddress1 => 'adr1B',
267 branchaddress2 => 'adr2B',
268 branchaddress3 => 'adr3B',
270 branchcity => 'cityB',
271 branchstate => 'stateB',
272 branchcountry => 'countryB',
273 branchphone => 'phoneB',
275 branchemail => 'emailB',
276 branchreplyto => 'emailreply',
277 branchreturnpath => 'branchreturn',
280 branchprinter => undef,
281 branchnotes => 'noteB',
282 opac_info => 'opacB',
287 $b2info = GetBranchInfo( $b2->{branchcode} );
289 CheckBranchCategorycode( $cat2->{categorycode} ),
293 push( @cat, $cat2->{categorycode} );
296 $b2->{issuing} = undef;
297 $b2->{categories} = \@cat;
298 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
300 #Test GetBranchesInCategory
301 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
302 my @b = ( $b2->{branchcode} );
303 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
308 branchname => 'BranchC',
309 branchaddress1 => 'adr1C',
310 branchaddress2 => 'adr2C',
311 branchaddress3 => 'adr3C',
313 branchcity => 'cityC',
314 branchstate => 'stateC',
315 branchcountry => 'countryC',
316 branchphone => 'phoneC',
318 branchemail => 'emailC',
321 branchprinter => undef,
322 branchnotes => 'noteC',
323 opac_info => 'opacC',
328 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
329 push( @b, $b3->{branchcode} );
330 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
332 CheckBranchCategorycode( $cat1->{categorycode} ),
334 'BRC has been added to CAT1'
337 #Test CheckCategoryUnique
338 is( CheckCategoryUnique('CAT2'), 0, 'CAT2 exists' );
339 is( CheckCategoryUnique('CAT_NO_EXISTS'), 1, 'CAT_NO_EXISTS doesnt exist' );
341 #Test GetCategoryTypes
342 my @category_types = GetCategoryTypes();
343 is_deeply(\@category_types, [ 'searchdomain', 'properties' ], 'received expected library category types');
345 $categories = GetBranchCategories(undef, undef, 'LIBCATCODE');
346 is_deeply($categories, [ {%$cat1}, {%$cat2},{ %new_category, selected => 1 } ], 'retrieve expected, eselected library category (bug 10515)');
348 #TODO later: test mybranchine and onlymine
349 # Actually we cannot mock C4::Context->userenv in unit tests
351 #Test GetBranchesLoop
352 my $loop = GetBranchesLoop;
353 is( scalar(@$loop), GetBranchesCount(), 'There is the right number of branches' );