Bug 15295: Koha::Libraries - Remove DelBranch and DelBranchCategory
[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 use Koha::Libraries;
28 use Koha::LibraryCategories;
29
30 BEGIN {
31     use FindBin;
32     use lib $FindBin::Bin;
33     use_ok('C4::Branch');
34 }
35 can_ok(
36     'C4::Branch', qw(
37       GetBranchCategory
38       GetBranchName
39       GetBranch
40       GetBranches
41       GetBranchesLoop
42       GetBranchDetail
43       get_branchinfos_of
44       ModBranch
45       CheckBranchCategorycode
46       GetBranchInfo
47       GetCategoryTypes
48       GetBranchCategories
49       GetBranchesInCategory
50       ModBranchCategoryInfo
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     branchreplyto  => 'emailreply',
86     branchreturnpath => 'branchreturn',
87     branchurl      => 'urlA',
88     branchip       => 'ipA',
89     branchprinter  => undef,
90     branchnotes    => 'noteA',
91     opac_info      => 'opacA'
92 };
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',
113 };
114 ModBranch($b1);
115 is( ModBranch($b2), undef, 'the field add is missing' );
116
117 $b2->{add} = 1;
118 ModBranch($b2);
119 is( GetBranchesCount(), $count + 2, "two branches added" );
120
121 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1,          "One row affected" );
122 is( GetBranchesCount(),             $count + 1, "branch BRB deleted" );
123
124 #Test GetBranchName
125 is( GetBranchName( $b1->{branchcode} ),
126     $b1->{branchname}, "GetBranchName returns the right name" );
127
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' );
133
134 #Test Getbranches
135 my $branches = GetBranches();
136 is( scalar( keys %$branches ),
137     GetBranchesCount(), "GetBranches returns the right number of branches" );
138
139 #Test ModBranch
140
141 $b1 = {
142     branchcode     => 'BRA',
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'
161 };
162
163 ModBranch($b1);
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");
169
170 #Test categories
171 my $categories = GetBranchCategories();
172 my $count_cat  = scalar( @$categories );
173
174 my $cat1 = {
175     add              => 1,
176     categorycode     => 'CAT1',
177     categoryname     => 'catname1',
178     codedescription  => 'catdesc1',
179     categorytype     => 'cattype1',
180     show_in_pulldown => 1
181 };
182 my $cat2 = {
183     add              => 1,
184     categorycode     => 'CAT2',
185     categoryname     => 'catname2',
186     categorytype     => 'catype2',
187     codedescription  => 'catdesc2',
188     show_in_pulldown => 1
189 };
190
191 my %new_category = (
192     categorycode     => 'LIBCATCODE',
193     categoryname     => 'library category name',
194     codedescription  => 'library category code description',
195     categorytype     => 'searchdomain',
196     show_in_pulldown => 1,
197 );
198
199 ModBranchCategoryInfo({
200     add => 1,
201     %new_category,
202 });
203
204 ModBranchCategoryInfo($cat1);
205 ModBranchCategoryInfo($cat2);
206
207 $categories = GetBranchCategories();
208 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
209 delete $cat1->{add};
210 delete $cat2->{add};
211 delete $new_category{add};
212 is_deeply($categories, [ $cat1,$cat2,\%new_category ], 'retrieve all expected library categories (bug 10515)');
213
214 #test GetBranchCategory
215 my $cat1detail = GetBranchCategory( $cat1->{categorycode} );
216 delete $cat1->{add};
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');
220
221 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
222 is( $del, 1, 'One row affected' );
223
224 $categories = GetBranchCategories();
225 is( scalar( @$categories ), $count_cat + 2, "Category  CAT2 deleted" );
226
227 my $cat2detail = GetBranchCategory( $cat2->{categorycode} );
228 is( $cat2detail, undef, 'CAT2 doesnt exist' );
229
230 $category = GetBranchCategory();
231 is($category, undef, 'retrieve library category only if code is supplied (bug 10515)');
232
233 #Test CheckBranchCategoryCode
234 my $check1 = CheckBranchCategorycode( $cat1->{categorycode} );
235 my $check2 = CheckBranchCategorycode( $cat2->{categorycode} );
236 like( $check1, '/^\d+$/', "CheckBranchCategorycode returns a number" );
237
238 $b2->{CAT1} = 1;
239 ModBranch($b2);
240 is( GetBranchesCount(), $count + 2, 'BRB added' );
241 is(
242     CheckBranchCategorycode( $cat1->{categorycode} ),
243     $check1 + 1,
244     'BRB added to CAT1'
245 );
246
247 #Test GetBranchInfo
248 my $b1info = GetBranchInfo( $b1->{branchcode} );
249 $b1->{categories} = [];
250 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
251
252 my $b2info = GetBranchInfo( $b2->{branchcode} );
253 my @cat    = ( $cat1->{categorycode} );
254 delete $b2->{add};
255 delete $b2->{CAT1};
256 $b2->{issuing}    = undef;
257 $b2->{categories} = \@cat;
258 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
259
260 ModBranchCategoryInfo({add => 1,%$cat2});
261 $categories = GetBranchCategories();
262 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
263 $b2 = {
264     branchcode     => 'BRB',
265     branchname     => 'BranchB',
266     branchaddress1 => 'adr1B',
267     branchaddress2 => 'adr2B',
268     branchaddress3 => 'adr3B',
269     branchzip      => 'zipB',
270     branchcity     => 'cityB',
271     branchstate    => 'stateB',
272     branchcountry  => 'countryB',
273     branchphone    => 'phoneB',
274     branchfax      => 'faxB',
275     branchemail    => 'emailB',
276     branchreplyto  => 'emailreply',
277     branchreturnpath => 'branchreturn',
278     branchurl      => 'urlB',
279     branchip       => 'ipB',
280     branchprinter  => undef,
281     branchnotes    => 'noteB',
282     opac_info      => 'opacB',
283     CAT1           => 1,
284     CAT2           => 1
285 };
286 ModBranch($b2);
287 $b2info = GetBranchInfo( $b2->{branchcode} );
288 is(
289     CheckBranchCategorycode( $cat2->{categorycode} ),
290     $check2 + 1,
291     'BRB added to CAT2'
292 );
293 push( @cat, $cat2->{categorycode} );
294 delete $b2->{CAT1};
295 delete $b2->{CAT2};
296 $b2->{issuing}    = undef;
297 $b2->{categories} = \@cat;
298 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
299
300 #Test GetBranchesInCategory
301 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
302 my @b      = ( $b2->{branchcode} );
303 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
304
305 my $b3 = {
306     add            => 1,
307     branchcode     => 'BRC',
308     branchname     => 'BranchC',
309     branchaddress1 => 'adr1C',
310     branchaddress2 => 'adr2C',
311     branchaddress3 => 'adr3C',
312     branchzip      => 'zipC',
313     branchcity     => 'cityC',
314     branchstate    => 'stateC',
315     branchcountry  => 'countryC',
316     branchphone    => 'phoneC',
317     branchfax      => 'faxC',
318     branchemail    => 'emailC',
319     branchurl      => 'urlC',
320     branchip       => 'ipC',
321     branchprinter  => undef,
322     branchnotes    => 'noteC',
323     opac_info      => 'opacC',
324     CAT1           => 1,
325     CAT2           => 1
326 };
327 ModBranch($b3);
328 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
329 push( @b, $b3->{branchcode} );
330 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
331 is(
332     CheckBranchCategorycode( $cat1->{categorycode} ),
333     $check1 + 2,
334     'BRC has been added to CAT1'
335 );
336
337 #Test CheckCategoryUnique
338 is( CheckCategoryUnique('CAT2'),          0, 'CAT2 exists' );
339 is( CheckCategoryUnique('CAT_NO_EXISTS'), 1, 'CAT_NO_EXISTS doesnt exist' );
340
341 #Test GetCategoryTypes
342 my @category_types = GetCategoryTypes();
343 is_deeply(\@category_types, [ 'searchdomain', 'properties' ], 'received expected library category types');
344
345 $categories = GetBranchCategories(undef, undef, 'LIBCATCODE');
346 is_deeply($categories, [ {%$cat1}, {%$cat2},{ %new_category, selected => 1 } ], 'retrieve expected, eselected library category (bug 10515)');
347
348 #TODO later: test mybranchine and onlymine
349 # Actually we cannot mock C4::Context->userenv in unit tests
350
351 #Test GetBranchesLoop
352 my $loop = GetBranchesLoop;
353 is( scalar(@$loop), GetBranchesCount(), 'There is the right number of branches' );
354
355 # End transaction
356 $dbh->rollback;
357