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;
52 my $dbh = C4::Context->dbh;
53 $dbh->{AutoCommit} = 0;
54 $dbh->{RaiseError} = 1;
57 $dbh->do('DELETE FROM branchcategories');
61 my $count = Koha::Libraries->search->count;
62 like( $count, '/^\d+$/', "the count is a number" );
68 branchname => 'BranchA',
69 branchaddress1 => 'adr1A',
70 branchaddress2 => 'adr2A',
71 branchaddress3 => 'adr3A',
73 branchcity => 'cityA',
74 branchstate => 'stateA',
75 branchcountry => 'countryA',
76 branchphone => 'phoneA',
78 branchemail => 'emailA',
79 branchreplyto => 'emailreply',
80 branchreturnpath => 'branchreturn',
83 branchprinter => undef,
84 branchnotes => 'noteA',
89 branchname => 'BranchB',
90 branchaddress1 => 'adr1B',
91 branchaddress2 => 'adr2B',
92 branchaddress3 => 'adr3B',
94 branchcity => 'cityB',
95 branchstate => 'stateB',
96 branchcountry => 'countryB',
97 branchphone => 'phoneB',
99 branchemail => 'emailB',
100 branchreplyto => 'emailreply',
101 branchreturnpath => 'branchreturn',
104 branchprinter => undef,
105 branchnotes => 'noteB',
106 opac_info => 'opacB',
109 is( ModBranch($b2), undef, 'the field add is missing' );
113 is( Koha::Libraries->search->count, $count + 2, "two branches added" );
115 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1, "One row affected" );
116 is( Koha::Libraries->search->count, $count + 1, "branch BRB deleted" );
119 is( GetBranchName( $b1->{branchcode} ),
120 $b1->{branchname}, "GetBranchName returns the right name" );
122 #Test GetBranchDetail
123 my $branchdetail = GetBranchDetail( $b1->{branchcode} );
124 $branchdetail->{add} = 1;
125 $b1->{issuing} = undef; # Not used in DB
126 is_deeply( $branchdetail, $b1, 'branchdetail is right' );
129 my $branches = GetBranches();
130 is( scalar( keys %$branches ),
131 Koha::Libraries->search->count, "GetBranches returns the right number of branches" );
137 branchname => 'BranchA modified',
138 branchaddress1 => 'adr1A modified',
139 branchaddress2 => 'adr2A modified',
140 branchaddress3 => 'adr3A modified',
141 branchzip => 'zipA modified',
142 branchcity => 'cityA modified',
143 branchstate => 'stateA modified',
144 branchcountry => 'countryA modified',
145 branchphone => 'phoneA modified',
146 branchfax => 'faxA modified',
147 branchemail => 'emailA modified',
148 branchreplyto => 'emailreply modified',
149 branchreturnpath => 'branchreturn modified',
150 branchurl => 'urlA modified',
151 branchip => 'ipA modified',
152 branchprinter => undef,
153 branchnotes => 'notesA modified',
154 opac_info => 'opacA modified'
158 is( Koha::Libraries->search->count, $count + 1,
159 "A branch has been modified, no new branch added" );
160 $branchdetail = GetBranchDetail( $b1->{branchcode} );
161 $b1->{issuing} = undef;
162 is_deeply( $branchdetail, $b1 , "GetBranchDetail gives the details of BRA");
165 my $count_cat = Koha::LibraryCategories->search->count;
168 categorycode => 'CAT1',
169 categoryname => 'catname1',
170 codedescription => 'catdesc1',
171 categorytype => 'cattype1',
172 show_in_pulldown => 1
175 categorycode => 'CAT2',
176 categoryname => 'catname2',
177 categorytype => 'catype2',
178 codedescription => 'catdesc2',
179 show_in_pulldown => 1
183 categorycode => 'LIBCATCODE',
184 categoryname => 'library category name',
185 codedescription => 'library category code description',
186 categorytype => 'searchdomain',
187 show_in_pulldown => 1,
190 Koha::LibraryCategory->new(\%new_category)->store;
191 Koha::LibraryCategory->new($cat1)->store;
192 Koha::LibraryCategory->new($cat2)->store;
194 my $categories = Koha::LibraryCategories->search;
195 is( $categories->count, $count_cat + 3, "Two categories added" );
197 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
198 is( $del, 1, 'One row affected' );
200 is( Koha::LibraryCategories->search->count, $count_cat + 2, "Category CAT 2 deleted" );
204 is( Koha::Libraries->search->count, $count + 2, 'BRB added' );
207 my $b1info = GetBranchInfo( $b1->{branchcode} );
208 $b1->{categories} = [];
209 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
211 my $b2info = GetBranchInfo( $b2->{branchcode} );
212 my @cat = ( $cat1->{categorycode} );
215 $b2->{issuing} = undef;
216 $b2->{categories} = \@cat;
217 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
219 Koha::LibraryCategory->new($cat2)->store;
220 is( Koha::LibraryCategories->search->count, $count_cat + 3, "Two categories added" );
223 branchname => 'BranchB',
224 branchaddress1 => 'adr1B',
225 branchaddress2 => 'adr2B',
226 branchaddress3 => 'adr3B',
228 branchcity => 'cityB',
229 branchstate => 'stateB',
230 branchcountry => 'countryB',
231 branchphone => 'phoneB',
233 branchemail => 'emailB',
234 branchreplyto => 'emailreply',
235 branchreturnpath => 'branchreturn',
238 branchprinter => undef,
239 branchnotes => 'noteB',
240 opac_info => 'opacB',
245 $b2info = GetBranchInfo( $b2->{branchcode} );
246 push( @cat, $cat2->{categorycode} );
249 $b2->{issuing} = undef;
250 $b2->{categories} = \@cat;
251 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
253 #Test GetBranchesInCategory
254 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
255 my @b = ( $b2->{branchcode} );
256 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
261 branchname => 'BranchC',
262 branchaddress1 => 'adr1C',
263 branchaddress2 => 'adr2C',
264 branchaddress3 => 'adr3C',
266 branchcity => 'cityC',
267 branchstate => 'stateC',
268 branchcountry => 'countryC',
269 branchphone => 'phoneC',
271 branchemail => 'emailC',
274 branchprinter => undef,
275 branchnotes => 'noteC',
276 opac_info => 'opacC',
281 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
282 push( @b, $b3->{branchcode} );
283 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
285 #TODO later: test mybranchine and onlymine
286 # Actually we cannot mock C4::Context->userenv in unit tests
288 #Test GetBranchesLoop
289 my $loop = GetBranchesLoop;
290 is( scalar(@$loop), Koha::Libraries->search->count, 'There is the right number of branches' );