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