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