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