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