51c629e6f2dc922eb51c21d1675d29cd01ad2a78
[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 => 16;
25
26 use C4::Branch;
27 use Koha::Database;
28 use Koha::Library;
29 use Koha::Libraries;
30 use Koha::LibraryCategories;
31
32 BEGIN {
33     use FindBin;
34     use lib $FindBin::Bin;
35     use_ok('C4::Branch');
36 }
37 can_ok(
38     'C4::Branch', qw(
39       GetBranch
40       GetBranches
41       GetBranchesLoop
42       mybranch
43       )
44 );
45
46 my $schema = Koha::Database->new->schema;
47 $schema->storage->txn_begin;
48
49 my $dbh = C4::Context->dbh;
50
51 # clear the slate
52 $dbh->do('DELETE FROM branchcategories');
53
54 # Start test
55
56 my $count = Koha::Libraries->search->count;
57 like( $count, '/^\d+$/', "the count is a number" );
58
59 #add 2 branches
60 my $b1 = {
61     branchcode     => 'BRA',
62     branchname     => 'BranchA',
63     branchaddress1 => 'adr1A',
64     branchaddress2 => 'adr2A',
65     branchaddress3 => 'adr3A',
66     branchzip      => 'zipA',
67     branchcity     => 'cityA',
68     branchstate    => 'stateA',
69     branchcountry  => 'countryA',
70     branchphone    => 'phoneA',
71     branchfax      => 'faxA',
72     branchemail    => 'emailA',
73     branchreplyto  => 'emailreply',
74     branchreturnpath => 'branchreturn',
75     branchurl      => 'urlA',
76     branchip       => 'ipA',
77     branchprinter  => undef,
78     branchnotes    => 'noteA',
79     opac_info      => 'opacA',
80     issuing        => undef,
81 };
82 my $b2 = {
83     branchcode     => 'BRB',
84     branchname     => 'BranchB',
85     branchaddress1 => 'adr1B',
86     branchaddress2 => 'adr2B',
87     branchaddress3 => 'adr3B',
88     branchzip      => 'zipB',
89     branchcity     => 'cityB',
90     branchstate    => 'stateB',
91     branchcountry  => 'countryB',
92     branchphone    => 'phoneB',
93     branchfax      => 'faxB',
94     branchemail    => 'emailB',
95     branchreplyto  => 'emailreply',
96     branchreturnpath => 'branchreturn',
97     branchurl      => 'urlB',
98     branchip       => 'ipB',
99     branchprinter  => undef,
100     branchnotes    => 'noteB',
101     opac_info      => 'opacB',
102     issuing        => undef,
103 };
104 Koha::Library->new($b1)->store;
105 Koha::Library->new($b2)->store;
106
107 is( Koha::Libraries->search->count, $count + 2, "two branches added" );
108
109 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1,          "One row affected" );
110 is( Koha::Libraries->search->count,             $count + 1, "branch BRB deleted" );
111
112 #Test Getbranches
113 my $branches = GetBranches();
114 is( scalar( keys %$branches ),
115     Koha::Libraries->search->count, "GetBranches returns the right number of branches" );
116
117 #Test modify a library
118
119 $b1 = {
120     branchcode     => 'BRA',
121     branchname     => 'BranchA modified',
122     branchaddress1 => 'adr1A modified',
123     branchaddress2 => 'adr2A modified',
124     branchaddress3 => 'adr3A modified',
125     branchzip      => 'zipA modified',
126     branchcity     => 'cityA modified',
127     branchstate    => 'stateA modified',
128     branchcountry  => 'countryA modified',
129     branchphone    => 'phoneA modified',
130     branchfax      => 'faxA modified',
131     branchemail    => 'emailA modified',
132     branchreplyto  => 'emailreply modified',
133     branchreturnpath => 'branchreturn modified',
134     branchurl      => 'urlA modified',
135     branchip       => 'ipA modified',
136     branchprinter  => undef,
137     branchnotes    => 'notesA modified',
138     opac_info      => 'opacA modified',
139     issuing        => undef,
140 };
141
142 Koha::Libraries->find($b1->{branchcode})->set($b1)->store;
143 is( Koha::Libraries->search->count, $count + 1,
144     "A branch has been modified, no new branch added" );
145
146 #Test categories
147 my $count_cat  = Koha::LibraryCategories->search->count;
148
149 my $cat1 = {
150     categorycode     => 'CAT1',
151     categoryname     => 'catname1',
152     codedescription  => 'catdesc1',
153     categorytype     => 'cattype1',
154     show_in_pulldown => 1
155 };
156 my $cat2 = {
157     categorycode     => 'CAT2',
158     categoryname     => 'catname2',
159     categorytype     => 'catype2',
160     codedescription  => 'catdesc2',
161     show_in_pulldown => 1
162 };
163
164 my %new_category = (
165     categorycode     => 'LIBCATCODE',
166     categoryname     => 'library category name',
167     codedescription  => 'library category code description',
168     categorytype     => 'searchdomain',
169     show_in_pulldown => 1,
170 );
171
172 Koha::LibraryCategory->new(\%new_category)->store;
173 Koha::LibraryCategory->new($cat1)->store;
174 Koha::LibraryCategory->new($cat2)->store;
175
176 my $categories = Koha::LibraryCategories->search;
177 is( $categories->count, $count_cat + 3, "Two categories added" );
178
179 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
180 is( $del, 1, 'One row affected' );
181
182 is( Koha::LibraryCategories->search->count, $count_cat + 2, "Category CAT 2 deleted" );
183
184 my $b2_stored = Koha::Library->new($b2)->store;
185 my $CAT1 = Koha::LibraryCategories->find('CAT1');
186 $b2_stored->add_to_categories([$CAT1]);
187 is( Koha::Libraries->search->count, $count + 2, 'BRB added' );
188
189 my $b1info = Koha::Libraries->find( $b1->{branchcode} );
190 is_deeply( $b1info->get_categories->count, 0, 'BRA has no categories' );
191
192 my $b2info = Koha::Libraries->find( $b2->{branchcode} );
193 is_deeply( $b2info->get_categories->count, 1, 'BRB has the category CAT1' );
194
195 Koha::LibraryCategory->new($cat2)->store;
196 is( Koha::LibraryCategories->search->count, $count_cat + 3, "Two categories added" );
197
198 #TODO later: test mybranchine and onlymine
199 # Actually we cannot mock C4::Context->userenv in unit tests
200
201 #Test GetBranchesLoop
202 my $loop = GetBranchesLoop;
203 is( scalar(@$loop), Koha::Libraries->search->count, 'There is the right number of branches' );
204
205 $schema->storage->txn_rollback;