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 => 24;
28 use Koha::LibraryCategories;
32 use lib $FindBin::Bin;
54 my $dbh = C4::Context->dbh;
55 $dbh->{AutoCommit} = 0;
56 $dbh->{RaiseError} = 1;
59 $dbh->do('DELETE FROM branchcategories');
63 my $count = GetBranchesCount();
64 like( $count, '/^\d+$/', "the count is a number" );
70 branchname => 'BranchA',
71 branchaddress1 => 'adr1A',
72 branchaddress2 => 'adr2A',
73 branchaddress3 => 'adr3A',
75 branchcity => 'cityA',
76 branchstate => 'stateA',
77 branchcountry => 'countryA',
78 branchphone => 'phoneA',
80 branchemail => 'emailA',
81 branchreplyto => 'emailreply',
82 branchreturnpath => 'branchreturn',
85 branchprinter => undef,
86 branchnotes => 'noteA',
91 branchname => 'BranchB',
92 branchaddress1 => 'adr1B',
93 branchaddress2 => 'adr2B',
94 branchaddress3 => 'adr3B',
96 branchcity => 'cityB',
97 branchstate => 'stateB',
98 branchcountry => 'countryB',
99 branchphone => 'phoneB',
101 branchemail => 'emailB',
102 branchreplyto => 'emailreply',
103 branchreturnpath => 'branchreturn',
106 branchprinter => undef,
107 branchnotes => 'noteB',
108 opac_info => 'opacB',
111 is( ModBranch($b2), undef, 'the field add is missing' );
115 is( GetBranchesCount(), $count + 2, "two branches added" );
117 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1, "One row affected" );
118 is( GetBranchesCount(), $count + 1, "branch BRB deleted" );
121 is( GetBranchName( $b1->{branchcode} ),
122 $b1->{branchname}, "GetBranchName returns the right name" );
124 #Test GetBranchDetail
125 my $branchdetail = GetBranchDetail( $b1->{branchcode} );
126 $branchdetail->{add} = 1;
127 $b1->{issuing} = undef; # Not used in DB
128 is_deeply( $branchdetail, $b1, 'branchdetail is right' );
131 my $branches = GetBranches();
132 is( scalar( keys %$branches ),
133 GetBranchesCount(), "GetBranches returns the right number of branches" );
139 branchname => 'BranchA modified',
140 branchaddress1 => 'adr1A modified',
141 branchaddress2 => 'adr2A modified',
142 branchaddress3 => 'adr3A modified',
143 branchzip => 'zipA modified',
144 branchcity => 'cityA modified',
145 branchstate => 'stateA modified',
146 branchcountry => 'countryA modified',
147 branchphone => 'phoneA modified',
148 branchfax => 'faxA modified',
149 branchemail => 'emailA modified',
150 branchreplyto => 'emailreply modified',
151 branchreturnpath => 'branchreturn modified',
152 branchurl => 'urlA modified',
153 branchip => 'ipA modified',
154 branchprinter => undef,
155 branchnotes => 'notesA modified',
156 opac_info => 'opacA modified'
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");
167 my $count_cat = Koha::LibraryCategories->search->count;
171 categorycode => 'CAT1',
172 categoryname => 'catname1',
173 codedescription => 'catdesc1',
174 categorytype => 'cattype1',
175 show_in_pulldown => 1
179 categorycode => 'CAT2',
180 categoryname => 'catname2',
181 categorytype => 'catype2',
182 codedescription => 'catdesc2',
183 show_in_pulldown => 1
187 categorycode => 'LIBCATCODE',
188 categoryname => 'library category name',
189 codedescription => 'library category code description',
190 categorytype => 'searchdomain',
191 show_in_pulldown => 1,
194 ModBranchCategoryInfo({
199 ModBranchCategoryInfo($cat1);
200 ModBranchCategoryInfo($cat2);
202 my $categories = Koha::LibraryCategories->search;
203 is( $categories->count, $count_cat + 3, "Two categories added" );
206 delete $new_category{add};
208 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
209 is( $del, 1, 'One row affected' );
211 is( Koha::LibraryCategories->search->count, $count_cat + 2, "Category CAT 2 deleted" );
215 is( GetBranchesCount(), $count + 2, 'BRB added' );
218 my $b1info = GetBranchInfo( $b1->{branchcode} );
219 $b1->{categories} = [];
220 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
222 my $b2info = GetBranchInfo( $b2->{branchcode} );
223 my @cat = ( $cat1->{categorycode} );
226 $b2->{issuing} = undef;
227 $b2->{categories} = \@cat;
228 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
230 ModBranchCategoryInfo({add => 1,%$cat2});
231 is( Koha::LibraryCategories->search->count, $count_cat + 3, "Two catgories added" );
234 branchname => 'BranchB',
235 branchaddress1 => 'adr1B',
236 branchaddress2 => 'adr2B',
237 branchaddress3 => 'adr3B',
239 branchcity => 'cityB',
240 branchstate => 'stateB',
241 branchcountry => 'countryB',
242 branchphone => 'phoneB',
244 branchemail => 'emailB',
245 branchreplyto => 'emailreply',
246 branchreturnpath => 'branchreturn',
249 branchprinter => undef,
250 branchnotes => 'noteB',
251 opac_info => 'opacB',
256 $b2info = GetBranchInfo( $b2->{branchcode} );
257 push( @cat, $cat2->{categorycode} );
260 $b2->{issuing} = undef;
261 $b2->{categories} = \@cat;
262 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
264 #Test GetBranchesInCategory
265 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
266 my @b = ( $b2->{branchcode} );
267 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
272 branchname => 'BranchC',
273 branchaddress1 => 'adr1C',
274 branchaddress2 => 'adr2C',
275 branchaddress3 => 'adr3C',
277 branchcity => 'cityC',
278 branchstate => 'stateC',
279 branchcountry => 'countryC',
280 branchphone => 'phoneC',
282 branchemail => 'emailC',
285 branchprinter => undef,
286 branchnotes => 'noteC',
287 opac_info => 'opacC',
292 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
293 push( @b, $b3->{branchcode} );
294 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
296 #Test GetCategoryTypes
297 my @category_types = GetCategoryTypes();
298 is_deeply(\@category_types, [ 'searchdomain', 'properties' ], 'received expected library category types');
300 #TODO later: test mybranchine and onlymine
301 # Actually we cannot mock C4::Context->userenv in unit tests
303 #Test GetBranchesLoop
304 my $loop = GetBranchesLoop;
305 is( scalar(@$loop), GetBranchesCount(), 'There is the right number of branches' );