Bug 14167: (QA followup) Adjust category handling in Koha::Logger
[koha.git] / t / db_dependent / Branch.t
blob9ca3f56de2005691cca90b8fedbf134d0dd0dc0f
1 #!/usr/bin/perl
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
10 # version.
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>.
19 use Modern::Perl;
21 use C4::Context;
22 use Data::Dumper;
24 use Test::More tests => 36;
26 use C4::Branch;
28 BEGIN {
29 use FindBin;
30 use lib $FindBin::Bin;
31 use_ok('C4::Branch');
33 can_ok(
34 'C4::Branch', qw(
35 GetBranchCategory
36 GetBranchName
37 GetBranch
38 GetBranches
39 GetBranchesLoop
40 GetBranchDetail
41 get_branchinfos_of
42 ModBranch
43 CheckBranchCategorycode
44 GetBranchInfo
45 GetCategoryTypes
46 GetBranchCategories
47 GetBranchesInCategory
48 ModBranchCategoryInfo
49 DelBranch
50 DelBranchCategory
51 CheckCategoryUnique
52 mybranch
53 GetBranchesCount)
57 # Start transaction
58 my $dbh = C4::Context->dbh;
59 $dbh->{AutoCommit} = 0;
60 $dbh->{RaiseError} = 1;
62 # clear the slate
63 $dbh->do('DELETE FROM branchcategories');
65 # Start test
67 my $count = GetBranchesCount();
68 like( $count, '/^\d+$/', "the count is a number" );
70 #add 2 branches
71 my $b1 = {
72 add => 1,
73 branchcode => 'BRA',
74 branchname => 'BranchA',
75 branchaddress1 => 'adr1A',
76 branchaddress2 => 'adr2A',
77 branchaddress3 => 'adr3A',
78 branchzip => 'zipA',
79 branchcity => 'cityA',
80 branchstate => 'stateA',
81 branchcountry => 'countryA',
82 branchphone => 'phoneA',
83 branchfax => 'faxA',
84 branchemail => 'emailA',
85 branchreplyto => 'emailreply',
86 branchreturnpath => 'branchreturn',
87 branchurl => 'urlA',
88 branchip => 'ipA',
89 branchprinter => undef,
90 branchnotes => 'noteA',
91 opac_info => 'opacA'
93 my $b2 = {
94 branchcode => 'BRB',
95 branchname => 'BranchB',
96 branchaddress1 => 'adr1B',
97 branchaddress2 => 'adr2B',
98 branchaddress3 => 'adr3B',
99 branchzip => 'zipB',
100 branchcity => 'cityB',
101 branchstate => 'stateB',
102 branchcountry => 'countryB',
103 branchphone => 'phoneB',
104 branchfax => 'faxB',
105 branchemail => 'emailB',
106 branchreplyto => 'emailreply',
107 branchreturnpath => 'branchreturn',
108 branchurl => 'urlB',
109 branchip => 'ipB',
110 branchprinter => undef,
111 branchnotes => 'noteB',
112 opac_info => 'opacB',
114 ModBranch($b1);
115 is( ModBranch($b2), undef, 'the field add is missing' );
117 $b2->{add} = 1;
118 ModBranch($b2);
119 is( GetBranchesCount(), $count + 2, "two branches added" );
121 #Test DelBranch
123 is( DelBranch( $b2->{branchcode} ), 1, "One row affected" );
124 is( GetBranchesCount(), $count + 1, "branch BRB deleted" );
126 #Test GetBranchName
127 is( GetBranchName( $b1->{branchcode} ),
128 $b1->{branchname}, "GetBranchName returns the right name" );
130 #Test GetBranchDetail
131 my $branchdetail = GetBranchDetail( $b1->{branchcode} );
132 $branchdetail->{add} = 1;
133 $b1->{issuing} = undef; # Not used in DB
134 is_deeply( $branchdetail, $b1, 'branchdetail is right' );
136 #Test Getbranches
137 my $branches = GetBranches();
138 is( scalar( keys %$branches ),
139 GetBranchesCount(), "GetBranches returns the right number of branches" );
141 #Test ModBranch
143 $b1 = {
144 branchcode => 'BRA',
145 branchname => 'BranchA modified',
146 branchaddress1 => 'adr1A modified',
147 branchaddress2 => 'adr2A modified',
148 branchaddress3 => 'adr3A modified',
149 branchzip => 'zipA modified',
150 branchcity => 'cityA modified',
151 branchstate => 'stateA modified',
152 branchcountry => 'countryA modified',
153 branchphone => 'phoneA modified',
154 branchfax => 'faxA modified',
155 branchemail => 'emailA modified',
156 branchreplyto => 'emailreply modified',
157 branchreturnpath => 'branchreturn modified',
158 branchurl => 'urlA modified',
159 branchip => 'ipA modified',
160 branchprinter => undef,
161 branchnotes => 'notesA modified',
162 opac_info => 'opacA modified'
165 ModBranch($b1);
166 is( GetBranchesCount(), $count + 1,
167 "A branch has been modified, no new branch added" );
168 $branchdetail = GetBranchDetail( $b1->{branchcode} );
169 $b1->{issuing} = undef;
170 is_deeply( $branchdetail, $b1 , "GetBranchDetail gives the details of BRA");
172 #Test categories
173 my $categories = GetBranchCategories();
174 my $count_cat = scalar( @$categories );
176 my $cat1 = {
177 add => 1,
178 categorycode => 'CAT1',
179 categoryname => 'catname1',
180 codedescription => 'catdesc1',
181 categorytype => 'cattype1',
182 show_in_pulldown => 1
184 my $cat2 = {
185 add => 1,
186 categorycode => 'CAT2',
187 categoryname => 'catname2',
188 categorytype => 'catype2',
189 codedescription => 'catdesc2',
190 show_in_pulldown => 1
193 my %new_category = (
194 categorycode => 'LIBCATCODE',
195 categoryname => 'library category name',
196 codedescription => 'library category code description',
197 categorytype => 'searchdomain',
198 show_in_pulldown => 1,
201 ModBranchCategoryInfo({
202 add => 1,
203 %new_category,
206 ModBranchCategoryInfo($cat1);
207 ModBranchCategoryInfo($cat2);
209 $categories = GetBranchCategories();
210 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
211 delete $cat1->{add};
212 delete $cat2->{add};
213 delete $new_category{add};
214 is_deeply($categories, [ $cat1,$cat2,\%new_category ], 'retrieve all expected library categories (bug 10515)');
216 #test GetBranchCategory
217 my $cat1detail = GetBranchCategory( $cat1->{categorycode} );
218 delete $cat1->{add};
219 is_deeply( $cat1detail, $cat1, 'CAT1 details are right' );
220 my $category = GetBranchCategory('LIBCATCODE');
221 is_deeply($category, \%new_category, 'fetched newly added library category');
223 #Test DelBranchCategory
224 my $del = DelBranchCategory( $cat2->{categorycode} );
225 is( $del, 1, 'One row affected' );
227 $categories = GetBranchCategories();
228 is( scalar( @$categories ), $count_cat + 2, "Category CAT2 deleted" );
230 my $cat2detail = GetBranchCategory( $cat2->{categorycode} );
231 is( $cat2detail, undef, 'CAT2 doesnt exist' );
233 $category = GetBranchCategory();
234 is($category, undef, 'retrieve library category only if code is supplied (bug 10515)');
236 #Test CheckBranchCategoryCode
237 my $check1 = CheckBranchCategorycode( $cat1->{categorycode} );
238 my $check2 = CheckBranchCategorycode( $cat2->{categorycode} );
239 like( $check1, '/^\d+$/', "CheckBranchCategorycode returns a number" );
241 $b2->{CAT1} = 1;
242 ModBranch($b2);
243 is( GetBranchesCount(), $count + 2, 'BRB added' );
245 CheckBranchCategorycode( $cat1->{categorycode} ),
246 $check1 + 1,
247 'BRB added to CAT1'
250 #Test GetBranchInfo
251 my $b1info = GetBranchInfo( $b1->{branchcode} );
252 $b1->{categories} = [];
253 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
255 my $b2info = GetBranchInfo( $b2->{branchcode} );
256 my @cat = ( $cat1->{categorycode} );
257 delete $b2->{add};
258 delete $b2->{CAT1};
259 $b2->{issuing} = undef;
260 $b2->{categories} = \@cat;
261 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
263 ModBranchCategoryInfo({add => 1,%$cat2});
264 $categories = GetBranchCategories();
265 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
266 $b2 = {
267 branchcode => 'BRB',
268 branchname => 'BranchB',
269 branchaddress1 => 'adr1B',
270 branchaddress2 => 'adr2B',
271 branchaddress3 => 'adr3B',
272 branchzip => 'zipB',
273 branchcity => 'cityB',
274 branchstate => 'stateB',
275 branchcountry => 'countryB',
276 branchphone => 'phoneB',
277 branchfax => 'faxB',
278 branchemail => 'emailB',
279 branchreplyto => 'emailreply',
280 branchreturnpath => 'branchreturn',
281 branchurl => 'urlB',
282 branchip => 'ipB',
283 branchprinter => undef,
284 branchnotes => 'noteB',
285 opac_info => 'opacB',
286 CAT1 => 1,
287 CAT2 => 1
289 ModBranch($b2);
290 $b2info = GetBranchInfo( $b2->{branchcode} );
292 CheckBranchCategorycode( $cat2->{categorycode} ),
293 $check2 + 1,
294 'BRB added to CAT2'
296 push( @cat, $cat2->{categorycode} );
297 delete $b2->{CAT1};
298 delete $b2->{CAT2};
299 $b2->{issuing} = undef;
300 $b2->{categories} = \@cat;
301 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
303 #Test GetBranchesInCategory
304 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
305 my @b = ( $b2->{branchcode} );
306 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
308 my $b3 = {
309 add => 1,
310 branchcode => 'BRC',
311 branchname => 'BranchC',
312 branchaddress1 => 'adr1C',
313 branchaddress2 => 'adr2C',
314 branchaddress3 => 'adr3C',
315 branchzip => 'zipC',
316 branchcity => 'cityC',
317 branchstate => 'stateC',
318 branchcountry => 'countryC',
319 branchphone => 'phoneC',
320 branchfax => 'faxC',
321 branchemail => 'emailC',
322 branchurl => 'urlC',
323 branchip => 'ipC',
324 branchprinter => undef,
325 branchnotes => 'noteC',
326 opac_info => 'opacC',
327 CAT1 => 1,
328 CAT2 => 1
330 ModBranch($b3);
331 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
332 push( @b, $b3->{branchcode} );
333 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
335 CheckBranchCategorycode( $cat1->{categorycode} ),
336 $check1 + 2,
337 'BRC has been added to CAT1'
340 #Test CheckCategoryUnique
341 is( CheckCategoryUnique('CAT2'), 0, 'CAT2 exists' );
342 is( CheckCategoryUnique('CAT_NO_EXISTS'), 1, 'CAT_NO_EXISTS doesnt exist' );
344 #Test GetCategoryTypes
345 my @category_types = GetCategoryTypes();
346 is_deeply(\@category_types, [ 'searchdomain', 'properties' ], 'received expected library category types');
348 $categories = GetBranchCategories(undef, undef, 'LIBCATCODE');
349 is_deeply($categories, [ {%$cat1}, {%$cat2},{ %new_category, selected => 1 } ], 'retrieve expected, eselected library category (bug 10515)');
351 #TODO later: test mybranchine and onlymine
352 # Actually we cannot mock C4::Context->userenv in unit tests
354 #Test GetBranchesLoop
355 my $loop = GetBranchesLoop;
356 is( scalar(@$loop), GetBranchesCount(), 'There is the right number of branches' );
358 # End transaction
359 $dbh->rollback;