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