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 => 23;
28 use Koha::LibraryCategories;
32 use lib $FindBin::Bin;
53 my $dbh = C4::Context->dbh;
54 $dbh->{AutoCommit} = 0;
55 $dbh->{RaiseError} = 1;
58 $dbh->do('DELETE FROM branchcategories');
62 my $count = GetBranchesCount();
63 like( $count, '/^\d+$/', "the count is a number" );
69 branchname => 'BranchA',
70 branchaddress1 => 'adr1A',
71 branchaddress2 => 'adr2A',
72 branchaddress3 => 'adr3A',
74 branchcity => 'cityA',
75 branchstate => 'stateA',
76 branchcountry => 'countryA',
77 branchphone => 'phoneA',
79 branchemail => 'emailA',
80 branchreplyto => 'emailreply',
81 branchreturnpath => 'branchreturn',
84 branchprinter => undef,
85 branchnotes => 'noteA',
90 branchname => 'BranchB',
91 branchaddress1 => 'adr1B',
92 branchaddress2 => 'adr2B',
93 branchaddress3 => 'adr3B',
95 branchcity => 'cityB',
96 branchstate => 'stateB',
97 branchcountry => 'countryB',
98 branchphone => 'phoneB',
100 branchemail => 'emailB',
101 branchreplyto => 'emailreply',
102 branchreturnpath => 'branchreturn',
105 branchprinter => undef,
106 branchnotes => 'noteB',
107 opac_info => 'opacB',
110 is( ModBranch($b2), undef, 'the field add is missing' );
114 is( GetBranchesCount(), $count + 2, "two branches added" );
116 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1, "One row affected" );
117 is( GetBranchesCount(), $count + 1, "branch BRB deleted" );
120 is( GetBranchName( $b1->{branchcode} ),
121 $b1->{branchname}, "GetBranchName returns the right name" );
123 #Test GetBranchDetail
124 my $branchdetail = GetBranchDetail( $b1->{branchcode} );
125 $branchdetail->{add} = 1;
126 $b1->{issuing} = undef; # Not used in DB
127 is_deeply( $branchdetail, $b1, 'branchdetail is right' );
130 my $branches = GetBranches();
131 is( scalar( keys %$branches ),
132 GetBranchesCount(), "GetBranches returns the right number of branches" );
138 branchname => 'BranchA modified',
139 branchaddress1 => 'adr1A modified',
140 branchaddress2 => 'adr2A modified',
141 branchaddress3 => 'adr3A modified',
142 branchzip => 'zipA modified',
143 branchcity => 'cityA modified',
144 branchstate => 'stateA modified',
145 branchcountry => 'countryA modified',
146 branchphone => 'phoneA modified',
147 branchfax => 'faxA modified',
148 branchemail => 'emailA modified',
149 branchreplyto => 'emailreply modified',
150 branchreturnpath => 'branchreturn modified',
151 branchurl => 'urlA modified',
152 branchip => 'ipA modified',
153 branchprinter => undef,
154 branchnotes => 'notesA modified',
155 opac_info => 'opacA modified'
159 is( GetBranchesCount(), $count + 1,
160 "A branch has been modified, no new branch added" );
161 $branchdetail = GetBranchDetail( $b1->{branchcode} );
162 $b1->{issuing} = undef;
163 is_deeply( $branchdetail, $b1 , "GetBranchDetail gives the details of BRA");
166 my $count_cat = Koha::LibraryCategories->search->count;
170 categorycode => 'CAT1',
171 categoryname => 'catname1',
172 codedescription => 'catdesc1',
173 categorytype => 'cattype1',
174 show_in_pulldown => 1
178 categorycode => 'CAT2',
179 categoryname => 'catname2',
180 categorytype => 'catype2',
181 codedescription => 'catdesc2',
182 show_in_pulldown => 1
186 categorycode => 'LIBCATCODE',
187 categoryname => 'library category name',
188 codedescription => 'library category code description',
189 categorytype => 'searchdomain',
190 show_in_pulldown => 1,
193 ModBranchCategoryInfo({
198 ModBranchCategoryInfo($cat1);
199 ModBranchCategoryInfo($cat2);
201 my $categories = Koha::LibraryCategories->search;
202 is( $categories->count, $count_cat + 3, "Two categories added" );
205 delete $new_category{add};
207 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
208 is( $del, 1, 'One row affected' );
210 is( Koha::LibraryCategories->search->count, $count_cat + 2, "Category CAT 2 deleted" );
214 is( GetBranchesCount(), $count + 2, 'BRB added' );
217 my $b1info = GetBranchInfo( $b1->{branchcode} );
218 $b1->{categories} = [];
219 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
221 my $b2info = GetBranchInfo( $b2->{branchcode} );
222 my @cat = ( $cat1->{categorycode} );
225 $b2->{issuing} = undef;
226 $b2->{categories} = \@cat;
227 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
229 ModBranchCategoryInfo({add => 1,%$cat2});
230 is( Koha::LibraryCategories->search->count, $count_cat + 3, "Two catgories added" );
233 branchname => 'BranchB',
234 branchaddress1 => 'adr1B',
235 branchaddress2 => 'adr2B',
236 branchaddress3 => 'adr3B',
238 branchcity => 'cityB',
239 branchstate => 'stateB',
240 branchcountry => 'countryB',
241 branchphone => 'phoneB',
243 branchemail => 'emailB',
244 branchreplyto => 'emailreply',
245 branchreturnpath => 'branchreturn',
248 branchprinter => undef,
249 branchnotes => 'noteB',
250 opac_info => 'opacB',
255 $b2info = GetBranchInfo( $b2->{branchcode} );
256 push( @cat, $cat2->{categorycode} );
259 $b2->{issuing} = undef;
260 $b2->{categories} = \@cat;
261 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
263 #Test GetBranchesInCategory
264 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
265 my @b = ( $b2->{branchcode} );
266 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
271 branchname => 'BranchC',
272 branchaddress1 => 'adr1C',
273 branchaddress2 => 'adr2C',
274 branchaddress3 => 'adr3C',
276 branchcity => 'cityC',
277 branchstate => 'stateC',
278 branchcountry => 'countryC',
279 branchphone => 'phoneC',
281 branchemail => 'emailC',
284 branchprinter => undef,
285 branchnotes => 'noteC',
286 opac_info => 'opacC',
291 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
292 push( @b, $b3->{branchcode} );
293 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
295 #TODO later: test mybranchine and onlymine
296 # Actually we cannot mock C4::Context->userenv in unit tests
298 #Test GetBranchesLoop
299 my $loop = GetBranchesLoop;
300 is( scalar(@$loop), GetBranchesCount(), 'There is the right number of branches' );