Bug 11543: (followup) add one more test
[koha.git] / t / db_dependent / Branch.t
1 #!/usr/bin/perl
2
3 # Copyright 2013 Equinox Software, Inc.
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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>.
18
19 use Modern::Perl;
20
21 use C4::Context;
22 use Data::Dumper;
23
24 use Test::More tests => 36;
25
26 use C4::Branch;
27
28 BEGIN {
29     use FindBin;
30     use lib $FindBin::Bin;
31     use_ok('C4::Branch');
32 }
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)
54 );
55
56
57 # Start transaction
58 my $dbh = C4::Context->dbh;
59 $dbh->{AutoCommit} = 0;
60 $dbh->{RaiseError} = 1;
61
62 # clear the slate
63 $dbh->do('DELETE FROM branchcategories');
64
65 # Start test
66
67 my $count = GetBranchesCount();
68 like( $count, '/^\d+$/', "the count is a number" );
69
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     branchurl      => 'urlA',
86     branchip       => 'ipA',
87     branchprinter  => undef,
88     branchnotes    => 'noteA',
89     opac_info      => 'opacA'
90 };
91 my $b2 = {
92     branchcode     => 'BRB',
93     branchname     => 'BranchB',
94     branchaddress1 => 'adr1B',
95     branchaddress2 => 'adr2B',
96     branchaddress3 => 'adr3B',
97     branchzip      => 'zipB',
98     branchcity     => 'cityB',
99     branchstate    => 'stateB',
100     branchcountry  => 'countryB',
101     branchphone    => 'phoneB',
102     branchfax      => 'faxB',
103     branchemail    => 'emailB',
104     branchurl      => 'urlB',
105     branchip       => 'ipB',
106     branchprinter  => undef,
107     branchnotes    => 'noteB',
108     opac_info      => 'opacB',
109 };
110 ModBranch($b1);
111 is( ModBranch($b2), undef, 'the field add is missing' );
112
113 $b2->{add} = 1;
114 ModBranch($b2);
115 is( GetBranchesCount(), $count + 2, "two branches added" );
116
117 #Test DelBranch
118
119 is( DelBranch( $b2->{branchcode} ), 1,          "One row affected" );
120 is( GetBranchesCount(),             $count + 1, "branch BRB deleted" );
121
122 #Test GetBranchName
123 is( GetBranchName( $b1->{branchcode} ),
124     $b1->{branchname}, "GetBranchName returns the right name" );
125
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' );
131
132 #Test Getbranches
133 my $branches = GetBranches();
134 is( scalar( keys %$branches ),
135     GetBranchesCount(), "GetBranches returns the right number of branches" );
136
137 #Test ModBranch
138
139 $b1 = {
140     branchcode     => 'BRA',
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     branchurl      => 'urlA modified',
153     branchip       => 'ipA modified',
154     branchprinter  => undef,
155     branchnotes    => 'notesA modified',
156     opac_info      => 'opacA modified'
157 };
158
159 ModBranch($b1);
160 is( GetBranchesCount(), $count + 1,
161     "A branch has been modified, no new branch added" );
162 $branchdetail = GetBranchDetail( $b1->{branchcode} );
163 $b1->{issuing} = undef;
164 is_deeply( $branchdetail, $b1 , "GetBranchDetail gives the details of BRA");
165
166 #Test categories
167 my $categories = GetBranchCategories();
168 my $count_cat  = scalar( @$categories );
169
170 my $cat1 = {
171     add              => 1,
172     categorycode     => 'CAT1',
173     categoryname     => 'catname1',
174     codedescription  => 'catdesc1',
175     categorytype     => 'cattype1',
176     show_in_pulldown => 1
177 };
178 my $cat2 = {
179     add              => 1,
180     categorycode     => 'CAT2',
181     categoryname     => 'catname2',
182     categorytype     => 'catype2',
183     codedescription  => 'catdesc2',
184     show_in_pulldown => 1
185 };
186
187 my %new_category = (
188     categorycode     => 'LIBCATCODE',
189     categoryname     => 'library category name',
190     codedescription  => 'library category code description',
191     categorytype     => 'searchdomain',
192     show_in_pulldown => 1,
193 );
194
195 ModBranchCategoryInfo({
196     add => 1,
197     %new_category,
198 });
199
200 ModBranchCategoryInfo($cat1);
201 ModBranchCategoryInfo($cat2);
202
203 $categories = GetBranchCategories();
204 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
205 delete $cat1->{add};
206 delete $cat2->{add};
207 delete $new_category{add};
208 is_deeply($categories, [ $cat1,$cat2,\%new_category ], 'retrieve all expected library categories (bug 10515)');
209
210 #test GetBranchCategory
211 my $cat1detail = GetBranchCategory( $cat1->{categorycode} );
212 delete $cat1->{add};
213 is_deeply( $cat1detail, $cat1, 'CAT1 details are right' );
214 my $category = GetBranchCategory('LIBCATCODE');
215 is_deeply($category, \%new_category, 'fetched newly added library category');
216
217 #Test DelBranchCategory
218 my $del = DelBranchCategory( $cat2->{categorycode} );
219 is( $del, 1, 'One row affected' );
220
221 $categories = GetBranchCategories();
222 is( scalar( @$categories ), $count_cat + 2, "Category  CAT2 deleted" );
223
224 my $cat2detail = GetBranchCategory( $cat2->{categorycode} );
225 is( $cat2detail, undef, 'CAT2 doesnt exist' );
226
227 $category = GetBranchCategory();
228 is($category, undef, 'retrieve library category only if code is supplied (bug 10515)');
229
230 #Test CheckBranchCategoryCode
231 my $check1 = CheckBranchCategorycode( $cat1->{categorycode} );
232 my $check2 = CheckBranchCategorycode( $cat2->{categorycode} );
233 like( $check1, '/^\d+$/', "CheckBranchCategorycode returns a number" );
234
235 $b2->{CAT1} = 1;
236 ModBranch($b2);
237 is( GetBranchesCount(), $count + 2, 'BRB added' );
238 is(
239     CheckBranchCategorycode( $cat1->{categorycode} ),
240     $check1 + 1,
241     'BRB added to CAT1'
242 );
243
244 #Test GetBranchInfo
245 my $b1info = GetBranchInfo( $b1->{branchcode} );
246 $b1->{categories} = [];
247 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
248
249 my $b2info = GetBranchInfo( $b2->{branchcode} );
250 my @cat    = ( $cat1->{categorycode} );
251 delete $b2->{add};
252 delete $b2->{CAT1};
253 $b2->{issuing}    = undef;
254 $b2->{categories} = \@cat;
255 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
256
257 ModBranchCategoryInfo({add => 1,%$cat2});
258 $categories = GetBranchCategories();
259 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
260 $b2 = {
261     branchcode     => 'BRB',
262     branchname     => 'BranchB',
263     branchaddress1 => 'adr1B',
264     branchaddress2 => 'adr2B',
265     branchaddress3 => 'adr3B',
266     branchzip      => 'zipB',
267     branchcity     => 'cityB',
268     branchstate    => 'stateB',
269     branchcountry  => 'countryB',
270     branchphone    => 'phoneB',
271     branchfax      => 'faxB',
272     branchemail    => 'emailB',
273     branchurl      => 'urlB',
274     branchip       => 'ipB',
275     branchprinter  => undef,
276     branchnotes    => 'noteB',
277     opac_info      => 'opacB',
278     CAT1           => 1,
279     CAT2           => 1
280 };
281 ModBranch($b2);
282 $b2info = GetBranchInfo( $b2->{branchcode} );
283 is(
284     CheckBranchCategorycode( $cat2->{categorycode} ),
285     $check2 + 1,
286     'BRB added to CAT2'
287 );
288 push( @cat, $cat2->{categorycode} );
289 delete $b2->{CAT1};
290 delete $b2->{CAT2};
291 $b2->{issuing}    = undef;
292 $b2->{categories} = \@cat;
293 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
294
295 #Test GetBranchesInCategory
296 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
297 my @b      = ( $b2->{branchcode} );
298 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
299
300 my $b3 = {
301     add            => 1,
302     branchcode     => 'BRC',
303     branchname     => 'BranchC',
304     branchaddress1 => 'adr1C',
305     branchaddress2 => 'adr2C',
306     branchaddress3 => 'adr3C',
307     branchzip      => 'zipC',
308     branchcity     => 'cityC',
309     branchstate    => 'stateC',
310     branchcountry  => 'countryC',
311     branchphone    => 'phoneC',
312     branchfax      => 'faxC',
313     branchemail    => 'emailC',
314     branchurl      => 'urlC',
315     branchip       => 'ipC',
316     branchprinter  => undef,
317     branchnotes    => 'noteC',
318     opac_info      => 'opacC',
319     CAT1           => 1,
320     CAT2           => 1
321 };
322 ModBranch($b3);
323 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
324 push( @b, $b3->{branchcode} );
325 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
326 is(
327     CheckBranchCategorycode( $cat1->{categorycode} ),
328     $check1 + 2,
329     'BRC has been added to CAT1'
330 );
331
332 #Test CheckCategoryUnique
333 is( CheckCategoryUnique('CAT2'),          0, 'CAT2 exists' );
334 is( CheckCategoryUnique('CAT_NO_EXISTS'), 1, 'CAT_NO_EXISTS doesnt exist' );
335
336 #Test GetCategoryTypes
337 my @category_types = GetCategoryTypes();
338 is_deeply(\@category_types, [ 'searchdomain', 'properties' ], 'received expected library category types');
339
340 $categories = GetBranchCategories(undef, undef, 'LIBCATCODE');
341 is_deeply($categories, [ {%$cat1}, {%$cat2},{ %new_category, selected => 1 } ], 'retrieve expected, eselected library category (bug 10515)');
342
343 #TODO later: test mybranchine and onlymine
344 # Actually we cannot mock C4::Context->userenv in unit tests
345
346 #Test GetBranchesLoop
347 my $loop = GetBranchesLoop;
348 is( scalar(@$loop), GetBranchesCount(), 'There is the right number of branches' );
349
350 # End transaction
351 $dbh->rollback;
352