Bug 15295: Koha::Libraries - Remove CheckBranchCategorycode
[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 => 30;
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       GetBranchInfo
46       GetCategoryTypes
47       GetBranchCategories
48       GetBranchesInCategory
49       ModBranchCategoryInfo
50       mybranch
51       GetBranchesCount)
52 );
53
54
55 # Start transaction
56 my $dbh = C4::Context->dbh;
57 $dbh->{AutoCommit} = 0;
58 $dbh->{RaiseError} = 1;
59
60 # clear the slate
61 $dbh->do('DELETE FROM branchcategories');
62
63 # Start test
64
65 my $count = GetBranchesCount();
66 like( $count, '/^\d+$/', "the count is a number" );
67
68 #add 2 branches
69 my $b1 = {
70     add            => 1,
71     branchcode     => 'BRA',
72     branchname     => 'BranchA',
73     branchaddress1 => 'adr1A',
74     branchaddress2 => 'adr2A',
75     branchaddress3 => 'adr3A',
76     branchzip      => 'zipA',
77     branchcity     => 'cityA',
78     branchstate    => 'stateA',
79     branchcountry  => 'countryA',
80     branchphone    => 'phoneA',
81     branchfax      => 'faxA',
82     branchemail    => 'emailA',
83     branchreplyto  => 'emailreply',
84     branchreturnpath => 'branchreturn',
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     branchreplyto  => 'emailreply',
105     branchreturnpath => 'branchreturn',
106     branchurl      => 'urlB',
107     branchip       => 'ipB',
108     branchprinter  => undef,
109     branchnotes    => 'noteB',
110     opac_info      => 'opacB',
111 };
112 ModBranch($b1);
113 is( ModBranch($b2), undef, 'the field add is missing' );
114
115 $b2->{add} = 1;
116 ModBranch($b2);
117 is( GetBranchesCount(), $count + 2, "two branches added" );
118
119 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 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     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'
159 };
160
161 ModBranch($b1);
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");
167
168 #Test categories
169 my $categories = GetBranchCategories();
170 my $count_cat  = scalar( @$categories );
171
172 my $cat1 = {
173     add              => 1,
174     categorycode     => 'CAT1',
175     categoryname     => 'catname1',
176     codedescription  => 'catdesc1',
177     categorytype     => 'cattype1',
178     show_in_pulldown => 1
179 };
180 my $cat2 = {
181     add              => 1,
182     categorycode     => 'CAT2',
183     categoryname     => 'catname2',
184     categorytype     => 'catype2',
185     codedescription  => 'catdesc2',
186     show_in_pulldown => 1
187 };
188
189 my %new_category = (
190     categorycode     => 'LIBCATCODE',
191     categoryname     => 'library category name',
192     codedescription  => 'library category code description',
193     categorytype     => 'searchdomain',
194     show_in_pulldown => 1,
195 );
196
197 ModBranchCategoryInfo({
198     add => 1,
199     %new_category,
200 });
201
202 ModBranchCategoryInfo($cat1);
203 ModBranchCategoryInfo($cat2);
204
205 $categories = GetBranchCategories();
206 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
207 delete $cat1->{add};
208 delete $cat2->{add};
209 delete $new_category{add};
210 is_deeply($categories, [ $cat1,$cat2,\%new_category ], 'retrieve all expected library categories (bug 10515)');
211
212 #test GetBranchCategory
213 my $cat1detail = GetBranchCategory( $cat1->{categorycode} );
214 delete $cat1->{add};
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');
218
219 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
220 is( $del, 1, 'One row affected' );
221
222 $categories = GetBranchCategories();
223 is( scalar( @$categories ), $count_cat + 2, "Category  CAT2 deleted" );
224
225 my $cat2detail = GetBranchCategory( $cat2->{categorycode} );
226 is( $cat2detail, undef, 'CAT2 doesnt exist' );
227
228 $category = GetBranchCategory();
229 is($category, undef, 'retrieve library category only if code is supplied (bug 10515)');
230
231 $b2->{CAT1} = 1;
232 ModBranch($b2);
233 is( GetBranchesCount(), $count + 2, 'BRB added' );
234
235 #Test GetBranchInfo
236 my $b1info = GetBranchInfo( $b1->{branchcode} );
237 $b1->{categories} = [];
238 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
239
240 my $b2info = GetBranchInfo( $b2->{branchcode} );
241 my @cat    = ( $cat1->{categorycode} );
242 delete $b2->{add};
243 delete $b2->{CAT1};
244 $b2->{issuing}    = undef;
245 $b2->{categories} = \@cat;
246 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
247
248 ModBranchCategoryInfo({add => 1,%$cat2});
249 $categories = GetBranchCategories();
250 is( scalar( @$categories ), $count_cat + 3, "Two categories added" );
251 $b2 = {
252     branchcode     => 'BRB',
253     branchname     => 'BranchB',
254     branchaddress1 => 'adr1B',
255     branchaddress2 => 'adr2B',
256     branchaddress3 => 'adr3B',
257     branchzip      => 'zipB',
258     branchcity     => 'cityB',
259     branchstate    => 'stateB',
260     branchcountry  => 'countryB',
261     branchphone    => 'phoneB',
262     branchfax      => 'faxB',
263     branchemail    => 'emailB',
264     branchreplyto  => 'emailreply',
265     branchreturnpath => 'branchreturn',
266     branchurl      => 'urlB',
267     branchip       => 'ipB',
268     branchprinter  => undef,
269     branchnotes    => 'noteB',
270     opac_info      => 'opacB',
271     CAT1           => 1,
272     CAT2           => 1
273 };
274 ModBranch($b2);
275 $b2info = GetBranchInfo( $b2->{branchcode} );
276 push( @cat, $cat2->{categorycode} );
277 delete $b2->{CAT1};
278 delete $b2->{CAT2};
279 $b2->{issuing}    = undef;
280 $b2->{categories} = \@cat;
281 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
282
283 #Test GetBranchesInCategory
284 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
285 my @b      = ( $b2->{branchcode} );
286 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
287
288 my $b3 = {
289     add            => 1,
290     branchcode     => 'BRC',
291     branchname     => 'BranchC',
292     branchaddress1 => 'adr1C',
293     branchaddress2 => 'adr2C',
294     branchaddress3 => 'adr3C',
295     branchzip      => 'zipC',
296     branchcity     => 'cityC',
297     branchstate    => 'stateC',
298     branchcountry  => 'countryC',
299     branchphone    => 'phoneC',
300     branchfax      => 'faxC',
301     branchemail    => 'emailC',
302     branchurl      => 'urlC',
303     branchip       => 'ipC',
304     branchprinter  => undef,
305     branchnotes    => 'noteC',
306     opac_info      => 'opacC',
307     CAT1           => 1,
308     CAT2           => 1
309 };
310 ModBranch($b3);
311 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
312 push( @b, $b3->{branchcode} );
313 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
314
315 #Test GetCategoryTypes
316 my @category_types = GetCategoryTypes();
317 is_deeply(\@category_types, [ 'searchdomain', 'properties' ], 'received expected library category types');
318
319 $categories = GetBranchCategories(undef, undef, 'LIBCATCODE');
320 is_deeply($categories, [ {%$cat1}, {%$cat2},{ %new_category, selected => 1 } ], 'retrieve expected, eselected library category (bug 10515)');
321
322 #TODO later: test mybranchine and onlymine
323 # Actually we cannot mock C4::Context->userenv in unit tests
324
325 #Test GetBranchesLoop
326 my $loop = GetBranchesLoop;
327 is( scalar(@$loop), GetBranchesCount(), 'There is the right number of branches' );
328
329 # End transaction
330 $dbh->rollback;
331