Bug 15295: Koha::Libraries - Remove GetCategoryTypes
[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 => 23;
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       GetBranchName
38       GetBranch
39       GetBranches
40       GetBranchesLoop
41       GetBranchDetail
42       get_branchinfos_of
43       ModBranch
44       GetBranchInfo
45       GetBranchesInCategory
46       ModBranchCategoryInfo
47       mybranch
48       GetBranchesCount)
49 );
50
51
52 # Start transaction
53 my $dbh = C4::Context->dbh;
54 $dbh->{AutoCommit} = 0;
55 $dbh->{RaiseError} = 1;
56
57 # clear the slate
58 $dbh->do('DELETE FROM branchcategories');
59
60 # Start test
61
62 my $count = GetBranchesCount();
63 like( $count, '/^\d+$/', "the count is a number" );
64
65 #add 2 branches
66 my $b1 = {
67     add            => 1,
68     branchcode     => 'BRA',
69     branchname     => 'BranchA',
70     branchaddress1 => 'adr1A',
71     branchaddress2 => 'adr2A',
72     branchaddress3 => 'adr3A',
73     branchzip      => 'zipA',
74     branchcity     => 'cityA',
75     branchstate    => 'stateA',
76     branchcountry  => 'countryA',
77     branchphone    => 'phoneA',
78     branchfax      => 'faxA',
79     branchemail    => 'emailA',
80     branchreplyto  => 'emailreply',
81     branchreturnpath => 'branchreturn',
82     branchurl      => 'urlA',
83     branchip       => 'ipA',
84     branchprinter  => undef,
85     branchnotes    => 'noteA',
86     opac_info      => 'opacA'
87 };
88 my $b2 = {
89     branchcode     => 'BRB',
90     branchname     => 'BranchB',
91     branchaddress1 => 'adr1B',
92     branchaddress2 => 'adr2B',
93     branchaddress3 => 'adr3B',
94     branchzip      => 'zipB',
95     branchcity     => 'cityB',
96     branchstate    => 'stateB',
97     branchcountry  => 'countryB',
98     branchphone    => 'phoneB',
99     branchfax      => 'faxB',
100     branchemail    => 'emailB',
101     branchreplyto  => 'emailreply',
102     branchreturnpath => 'branchreturn',
103     branchurl      => 'urlB',
104     branchip       => 'ipB',
105     branchprinter  => undef,
106     branchnotes    => 'noteB',
107     opac_info      => 'opacB',
108 };
109 ModBranch($b1);
110 is( ModBranch($b2), undef, 'the field add is missing' );
111
112 $b2->{add} = 1;
113 ModBranch($b2);
114 is( GetBranchesCount(), $count + 2, "two branches added" );
115
116 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1,          "One row affected" );
117 is( GetBranchesCount(),             $count + 1, "branch BRB deleted" );
118
119 #Test GetBranchName
120 is( GetBranchName( $b1->{branchcode} ),
121     $b1->{branchname}, "GetBranchName returns the right name" );
122
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' );
128
129 #Test Getbranches
130 my $branches = GetBranches();
131 is( scalar( keys %$branches ),
132     GetBranchesCount(), "GetBranches returns the right number of branches" );
133
134 #Test ModBranch
135
136 $b1 = {
137     branchcode     => 'BRA',
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'
156 };
157
158 ModBranch($b1);
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");
164
165 #Test categories
166 my $count_cat  = Koha::LibraryCategories->search->count;
167
168 my $cat1 = {
169     add              => 1,
170     categorycode     => 'CAT1',
171     categoryname     => 'catname1',
172     codedescription  => 'catdesc1',
173     categorytype     => 'cattype1',
174     show_in_pulldown => 1
175 };
176 my $cat2 = {
177     add              => 1,
178     categorycode     => 'CAT2',
179     categoryname     => 'catname2',
180     categorytype     => 'catype2',
181     codedescription  => 'catdesc2',
182     show_in_pulldown => 1
183 };
184
185 my %new_category = (
186     categorycode     => 'LIBCATCODE',
187     categoryname     => 'library category name',
188     codedescription  => 'library category code description',
189     categorytype     => 'searchdomain',
190     show_in_pulldown => 1,
191 );
192
193 ModBranchCategoryInfo({
194     add => 1,
195     %new_category,
196 });
197
198 ModBranchCategoryInfo($cat1);
199 ModBranchCategoryInfo($cat2);
200
201 my $categories = Koha::LibraryCategories->search;
202 is( $categories->count, $count_cat + 3, "Two categories added" );
203 delete $cat1->{add};
204 delete $cat2->{add};
205 delete $new_category{add};
206
207 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
208 is( $del, 1, 'One row affected' );
209
210 is( Koha::LibraryCategories->search->count, $count_cat + 2, "Category CAT 2 deleted" );
211
212 $b2->{CAT1} = 1;
213 ModBranch($b2);
214 is( GetBranchesCount(), $count + 2, 'BRB added' );
215
216 #Test GetBranchInfo
217 my $b1info = GetBranchInfo( $b1->{branchcode} );
218 $b1->{categories} = [];
219 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
220
221 my $b2info = GetBranchInfo( $b2->{branchcode} );
222 my @cat    = ( $cat1->{categorycode} );
223 delete $b2->{add};
224 delete $b2->{CAT1};
225 $b2->{issuing}    = undef;
226 $b2->{categories} = \@cat;
227 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
228
229 ModBranchCategoryInfo({add => 1,%$cat2});
230 is( Koha::LibraryCategories->search->count, $count_cat + 3, "Two catgories added" );
231 $b2 = {
232     branchcode     => 'BRB',
233     branchname     => 'BranchB',
234     branchaddress1 => 'adr1B',
235     branchaddress2 => 'adr2B',
236     branchaddress3 => 'adr3B',
237     branchzip      => 'zipB',
238     branchcity     => 'cityB',
239     branchstate    => 'stateB',
240     branchcountry  => 'countryB',
241     branchphone    => 'phoneB',
242     branchfax      => 'faxB',
243     branchemail    => 'emailB',
244     branchreplyto  => 'emailreply',
245     branchreturnpath => 'branchreturn',
246     branchurl      => 'urlB',
247     branchip       => 'ipB',
248     branchprinter  => undef,
249     branchnotes    => 'noteB',
250     opac_info      => 'opacB',
251     CAT1           => 1,
252     CAT2           => 1
253 };
254 ModBranch($b2);
255 $b2info = GetBranchInfo( $b2->{branchcode} );
256 push( @cat, $cat2->{categorycode} );
257 delete $b2->{CAT1};
258 delete $b2->{CAT2};
259 $b2->{issuing}    = undef;
260 $b2->{categories} = \@cat;
261 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
262
263 #Test GetBranchesInCategory
264 my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
265 my @b      = ( $b2->{branchcode} );
266 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' );
267
268 my $b3 = {
269     add            => 1,
270     branchcode     => 'BRC',
271     branchname     => 'BranchC',
272     branchaddress1 => 'adr1C',
273     branchaddress2 => 'adr2C',
274     branchaddress3 => 'adr3C',
275     branchzip      => 'zipC',
276     branchcity     => 'cityC',
277     branchstate    => 'stateC',
278     branchcountry  => 'countryC',
279     branchphone    => 'phoneC',
280     branchfax      => 'faxC',
281     branchemail    => 'emailC',
282     branchurl      => 'urlC',
283     branchip       => 'ipC',
284     branchprinter  => undef,
285     branchnotes    => 'noteC',
286     opac_info      => 'opacC',
287     CAT1           => 1,
288     CAT2           => 1
289 };
290 ModBranch($b3);
291 $brCat1 = GetBranchesInCategory( $cat1->{categorycode} );
292 push( @b, $b3->{branchcode} );
293 is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' );
294
295 #TODO later: test mybranchine and onlymine
296 # Actually we cannot mock C4::Context->userenv in unit tests
297
298 #Test GetBranchesLoop
299 my $loop = GetBranchesLoop;
300 is( scalar(@$loop), GetBranchesCount(), 'There is the right number of branches' );
301
302 # End transaction
303 $dbh->rollback;
304