Bug 15628: Remove get_branchinfos_of vestiges
[koha.git] / t / db_dependent / Branch.t
blob7c7ed75784eb757fad682b1ba3ac7cde52b852ba
1 #!/usr/bin/perl
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
10 # version.
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>.
19 use Modern::Perl;
21 use C4::Context;
22 use Data::Dumper;
24 use Test::More tests => 23;
26 use C4::Branch;
27 use Koha::Libraries;
28 use Koha::LibraryCategories;
30 BEGIN {
31 use FindBin;
32 use lib $FindBin::Bin;
33 use_ok('C4::Branch');
35 can_ok(
36 'C4::Branch', qw(
37 GetBranchName
38 GetBranch
39 GetBranches
40 GetBranchesLoop
41 GetBranchDetail
42 ModBranch
43 GetBranchInfo
44 GetBranchesInCategory
45 mybranch
50 # Start transaction
51 my $dbh = C4::Context->dbh;
52 $dbh->{AutoCommit} = 0;
53 $dbh->{RaiseError} = 1;
55 # clear the slate
56 $dbh->do('DELETE FROM branchcategories');
58 # Start test
60 my $count = Koha::Libraries->search->count;
61 like( $count, '/^\d+$/', "the count is a number" );
63 #add 2 branches
64 my $b1 = {
65 add => 1,
66 branchcode => 'BRA',
67 branchname => 'BranchA',
68 branchaddress1 => 'adr1A',
69 branchaddress2 => 'adr2A',
70 branchaddress3 => 'adr3A',
71 branchzip => 'zipA',
72 branchcity => 'cityA',
73 branchstate => 'stateA',
74 branchcountry => 'countryA',
75 branchphone => 'phoneA',
76 branchfax => 'faxA',
77 branchemail => 'emailA',
78 branchreplyto => 'emailreply',
79 branchreturnpath => 'branchreturn',
80 branchurl => 'urlA',
81 branchip => 'ipA',
82 branchprinter => undef,
83 branchnotes => 'noteA',
84 opac_info => 'opacA'
86 my $b2 = {
87 branchcode => 'BRB',
88 branchname => 'BranchB',
89 branchaddress1 => 'adr1B',
90 branchaddress2 => 'adr2B',
91 branchaddress3 => 'adr3B',
92 branchzip => 'zipB',
93 branchcity => 'cityB',
94 branchstate => 'stateB',
95 branchcountry => 'countryB',
96 branchphone => 'phoneB',
97 branchfax => 'faxB',
98 branchemail => 'emailB',
99 branchreplyto => 'emailreply',
100 branchreturnpath => 'branchreturn',
101 branchurl => 'urlB',
102 branchip => 'ipB',
103 branchprinter => undef,
104 branchnotes => 'noteB',
105 opac_info => 'opacB',
107 ModBranch($b1);
108 is( ModBranch($b2), undef, 'the field add is missing' );
110 $b2->{add} = 1;
111 ModBranch($b2);
112 is( Koha::Libraries->search->count, $count + 2, "two branches added" );
114 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1, "One row affected" );
115 is( Koha::Libraries->search->count, $count + 1, "branch BRB deleted" );
117 #Test GetBranchName
118 is( GetBranchName( $b1->{branchcode} ),
119 $b1->{branchname}, "GetBranchName returns the right name" );
121 #Test GetBranchDetail
122 my $branchdetail = GetBranchDetail( $b1->{branchcode} );
123 $branchdetail->{add} = 1;
124 $b1->{issuing} = undef; # Not used in DB
125 is_deeply( $branchdetail, $b1, 'branchdetail is right' );
127 #Test Getbranches
128 my $branches = GetBranches();
129 is( scalar( keys %$branches ),
130 Koha::Libraries->search->count, "GetBranches returns the right number of branches" );
132 #Test ModBranch
134 $b1 = {
135 branchcode => 'BRA',
136 branchname => 'BranchA modified',
137 branchaddress1 => 'adr1A modified',
138 branchaddress2 => 'adr2A modified',
139 branchaddress3 => 'adr3A modified',
140 branchzip => 'zipA modified',
141 branchcity => 'cityA modified',
142 branchstate => 'stateA modified',
143 branchcountry => 'countryA modified',
144 branchphone => 'phoneA modified',
145 branchfax => 'faxA modified',
146 branchemail => 'emailA modified',
147 branchreplyto => 'emailreply modified',
148 branchreturnpath => 'branchreturn modified',
149 branchurl => 'urlA modified',
150 branchip => 'ipA modified',
151 branchprinter => undef,
152 branchnotes => 'notesA modified',
153 opac_info => 'opacA modified'
156 ModBranch($b1);
157 is( Koha::Libraries->search->count, $count + 1,
158 "A branch has been modified, no new branch added" );
159 $branchdetail = GetBranchDetail( $b1->{branchcode} );
160 $b1->{issuing} = undef;
161 is_deeply( $branchdetail, $b1 , "GetBranchDetail gives the details of BRA");
163 #Test categories
164 my $count_cat = Koha::LibraryCategories->search->count;
166 my $cat1 = {
167 categorycode => 'CAT1',
168 categoryname => 'catname1',
169 codedescription => 'catdesc1',
170 categorytype => 'cattype1',
171 show_in_pulldown => 1
173 my $cat2 = {
174 categorycode => 'CAT2',
175 categoryname => 'catname2',
176 categorytype => 'catype2',
177 codedescription => 'catdesc2',
178 show_in_pulldown => 1
181 my %new_category = (
182 categorycode => 'LIBCATCODE',
183 categoryname => 'library category name',
184 codedescription => 'library category code description',
185 categorytype => 'searchdomain',
186 show_in_pulldown => 1,
189 Koha::LibraryCategory->new(\%new_category)->store;
190 Koha::LibraryCategory->new($cat1)->store;
191 Koha::LibraryCategory->new($cat2)->store;
193 my $categories = Koha::LibraryCategories->search;
194 is( $categories->count, $count_cat + 3, "Two categories added" );
196 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
197 is( $del, 1, 'One row affected' );
199 is( Koha::LibraryCategories->search->count, $count_cat + 2, "Category CAT 2 deleted" );
201 $b2->{CAT1} = 1;
202 ModBranch($b2);
203 is( Koha::Libraries->search->count, $count + 2, 'BRB added' );
205 #Test GetBranchInfo
206 my $b1info = GetBranchInfo( $b1->{branchcode} );
207 $b1->{categories} = [];
208 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
210 my $b2info = GetBranchInfo( $b2->{branchcode} );
211 my @cat = ( $cat1->{categorycode} );
212 delete $b2->{add};
213 delete $b2->{CAT1};
214 $b2->{issuing} = undef;
215 $b2->{categories} = \@cat;
216 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
218 Koha::LibraryCategory->new($cat2)->store;
219 is( Koha::LibraryCategories->search->count, $count_cat + 3, "Two categories added" );
220 $b2 = {
221 branchcode => 'BRB',
222 branchname => 'BranchB',
223 branchaddress1 => 'adr1B',
224 branchaddress2 => 'adr2B',
225 branchaddress3 => 'adr3B',
226 branchzip => 'zipB',
227 branchcity => 'cityB',
228 branchstate => 'stateB',
229 branchcountry => 'countryB',
230 branchphone => 'phoneB',
231 branchfax => 'faxB',
232 branchemail => 'emailB',
233 branchreplyto => 'emailreply',
234 branchreturnpath => 'branchreturn',
235 branchurl => 'urlB',
236 branchip => 'ipB',
237 branchprinter => undef,
238 branchnotes => 'noteB',
239 opac_info => 'opacB',
240 CAT1 => 1,
241 CAT2 => 1
243 ModBranch($b2);
244 $b2info = GetBranchInfo( $b2->{branchcode} );
245 push( @cat, $cat2->{categorycode} );
246 delete $b2->{CAT1};
247 delete $b2->{CAT2};
248 $b2->{issuing} = undef;
249 $b2->{categories} = \@cat;
250 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
252 #Test GetBranchesInCategory
253 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
254 my @b = ( $b2->{branchcode} );
255 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
257 my $b3 = {
258 add => 1,
259 branchcode => 'BRC',
260 branchname => 'BranchC',
261 branchaddress1 => 'adr1C',
262 branchaddress2 => 'adr2C',
263 branchaddress3 => 'adr3C',
264 branchzip => 'zipC',
265 branchcity => 'cityC',
266 branchstate => 'stateC',
267 branchcountry => 'countryC',
268 branchphone => 'phoneC',
269 branchfax => 'faxC',
270 branchemail => 'emailC',
271 branchurl => 'urlC',
272 branchip => 'ipC',
273 branchprinter => undef,
274 branchnotes => 'noteC',
275 opac_info => 'opacC',
276 CAT1 => 1,
277 CAT2 => 1
279 ModBranch($b3);
280 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
281 push( @b, $b3->{branchcode} );
282 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
284 #TODO later: test mybranchine and onlymine
285 # Actually we cannot mock C4::Context->userenv in unit tests
287 #Test GetBranchesLoop
288 my $loop = GetBranchesLoop;
289 is( scalar(@$loop), Koha::Libraries->search->count, 'There is the right number of branches' );
291 # End transaction
292 $dbh->rollback;