Bug 15629: Koha::Libraries - Remove GetBranchesInCategory (3)
[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 => 21;
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       ModBranch
43       GetBranchInfo
44       mybranch
45       )
46 );
47
48
49 # Start transaction
50 my $dbh = C4::Context->dbh;
51 $dbh->{AutoCommit} = 0;
52 $dbh->{RaiseError} = 1;
53
54 # clear the slate
55 $dbh->do('DELETE FROM branchcategories');
56
57 # Start test
58
59 my $count = Koha::Libraries->search->count;
60 like( $count, '/^\d+$/', "the count is a number" );
61
62 #add 2 branches
63 my $b1 = {
64     add            => 1,
65     branchcode     => 'BRA',
66     branchname     => 'BranchA',
67     branchaddress1 => 'adr1A',
68     branchaddress2 => 'adr2A',
69     branchaddress3 => 'adr3A',
70     branchzip      => 'zipA',
71     branchcity     => 'cityA',
72     branchstate    => 'stateA',
73     branchcountry  => 'countryA',
74     branchphone    => 'phoneA',
75     branchfax      => 'faxA',
76     branchemail    => 'emailA',
77     branchreplyto  => 'emailreply',
78     branchreturnpath => 'branchreturn',
79     branchurl      => 'urlA',
80     branchip       => 'ipA',
81     branchprinter  => undef,
82     branchnotes    => 'noteA',
83     opac_info      => 'opacA'
84 };
85 my $b2 = {
86     branchcode     => 'BRB',
87     branchname     => 'BranchB',
88     branchaddress1 => 'adr1B',
89     branchaddress2 => 'adr2B',
90     branchaddress3 => 'adr3B',
91     branchzip      => 'zipB',
92     branchcity     => 'cityB',
93     branchstate    => 'stateB',
94     branchcountry  => 'countryB',
95     branchphone    => 'phoneB',
96     branchfax      => 'faxB',
97     branchemail    => 'emailB',
98     branchreplyto  => 'emailreply',
99     branchreturnpath => 'branchreturn',
100     branchurl      => 'urlB',
101     branchip       => 'ipB',
102     branchprinter  => undef,
103     branchnotes    => 'noteB',
104     opac_info      => 'opacB',
105 };
106 ModBranch($b1);
107 is( ModBranch($b2), undef, 'the field add is missing' );
108
109 $b2->{add} = 1;
110 ModBranch($b2);
111 is( Koha::Libraries->search->count, $count + 2, "two branches added" );
112
113 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1,          "One row affected" );
114 is( Koha::Libraries->search->count,             $count + 1, "branch BRB deleted" );
115
116 #Test GetBranchName
117 is( GetBranchName( $b1->{branchcode} ),
118     $b1->{branchname}, "GetBranchName returns the right name" );
119
120 #Test GetBranchDetail
121 my $branchdetail = GetBranchDetail( $b1->{branchcode} );
122 $branchdetail->{add} = 1;
123 $b1->{issuing}       = undef;    # Not used in DB
124 is_deeply( $branchdetail, $b1, 'branchdetail is right' );
125
126 #Test Getbranches
127 my $branches = GetBranches();
128 is( scalar( keys %$branches ),
129     Koha::Libraries->search->count, "GetBranches returns the right number of branches" );
130
131 #Test ModBranch
132
133 $b1 = {
134     branchcode     => 'BRA',
135     branchname     => 'BranchA modified',
136     branchaddress1 => 'adr1A modified',
137     branchaddress2 => 'adr2A modified',
138     branchaddress3 => 'adr3A modified',
139     branchzip      => 'zipA modified',
140     branchcity     => 'cityA modified',
141     branchstate    => 'stateA modified',
142     branchcountry  => 'countryA modified',
143     branchphone    => 'phoneA modified',
144     branchfax      => 'faxA modified',
145     branchemail    => 'emailA modified',
146     branchreplyto  => 'emailreply modified',
147     branchreturnpath => 'branchreturn modified',
148     branchurl      => 'urlA modified',
149     branchip       => 'ipA modified',
150     branchprinter  => undef,
151     branchnotes    => 'notesA modified',
152     opac_info      => 'opacA modified'
153 };
154
155 ModBranch($b1);
156 is( Koha::Libraries->search->count, $count + 1,
157     "A branch has been modified, no new branch added" );
158 $branchdetail = GetBranchDetail( $b1->{branchcode} );
159 $b1->{issuing} = undef;
160 is_deeply( $branchdetail, $b1 , "GetBranchDetail gives the details of BRA");
161
162 #Test categories
163 my $count_cat  = Koha::LibraryCategories->search->count;
164
165 my $cat1 = {
166     categorycode     => 'CAT1',
167     categoryname     => 'catname1',
168     codedescription  => 'catdesc1',
169     categorytype     => 'cattype1',
170     show_in_pulldown => 1
171 };
172 my $cat2 = {
173     categorycode     => 'CAT2',
174     categoryname     => 'catname2',
175     categorytype     => 'catype2',
176     codedescription  => 'catdesc2',
177     show_in_pulldown => 1
178 };
179
180 my %new_category = (
181     categorycode     => 'LIBCATCODE',
182     categoryname     => 'library category name',
183     codedescription  => 'library category code description',
184     categorytype     => 'searchdomain',
185     show_in_pulldown => 1,
186 );
187
188 Koha::LibraryCategory->new(\%new_category)->store;
189 Koha::LibraryCategory->new($cat1)->store;
190 Koha::LibraryCategory->new($cat2)->store;
191
192 my $categories = Koha::LibraryCategories->search;
193 is( $categories->count, $count_cat + 3, "Two categories added" );
194
195 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
196 is( $del, 1, 'One row affected' );
197
198 is( Koha::LibraryCategories->search->count, $count_cat + 2, "Category CAT 2 deleted" );
199
200 $b2->{CAT1} = 1;
201 ModBranch($b2);
202 is( Koha::Libraries->search->count, $count + 2, 'BRB added' );
203
204 #Test GetBranchInfo
205 my $b1info = GetBranchInfo( $b1->{branchcode} );
206 $b1->{categories} = [];
207 is_deeply( @$b1info[0], $b1, 'BRA has no categories' );
208
209 my $b2info = GetBranchInfo( $b2->{branchcode} );
210 my @cat    = ( $cat1->{categorycode} );
211 delete $b2->{add};
212 delete $b2->{CAT1};
213 $b2->{issuing}    = undef;
214 $b2->{categories} = \@cat;
215 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' );
216
217 Koha::LibraryCategory->new($cat2)->store;
218 is( Koha::LibraryCategories->search->count, $count_cat + 3, "Two categories added" );
219 $b2 = {
220     branchcode     => 'BRB',
221     branchname     => 'BranchB',
222     branchaddress1 => 'adr1B',
223     branchaddress2 => 'adr2B',
224     branchaddress3 => 'adr3B',
225     branchzip      => 'zipB',
226     branchcity     => 'cityB',
227     branchstate    => 'stateB',
228     branchcountry  => 'countryB',
229     branchphone    => 'phoneB',
230     branchfax      => 'faxB',
231     branchemail    => 'emailB',
232     branchreplyto  => 'emailreply',
233     branchreturnpath => 'branchreturn',
234     branchurl      => 'urlB',
235     branchip       => 'ipB',
236     branchprinter  => undef,
237     branchnotes    => 'noteB',
238     opac_info      => 'opacB',
239     CAT1           => 1,
240     CAT2           => 1
241 };
242 ModBranch($b2);
243 $b2info = GetBranchInfo( $b2->{branchcode} );
244 push( @cat, $cat2->{categorycode} );
245 delete $b2->{CAT1};
246 delete $b2->{CAT2};
247 $b2->{issuing}    = undef;
248 $b2->{categories} = \@cat;
249 is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' );
250
251 #TODO later: test mybranchine and onlymine
252 # Actually we cannot mock C4::Context->userenv in unit tests
253
254 #Test GetBranchesLoop
255 my $loop = GetBranchesLoop;
256 is( scalar(@$loop), Koha::Libraries->search->count, 'There is the right number of branches' );
257
258 # End transaction
259 $dbh->rollback;
260