3 # Copyright 2013 Equinox Software, Inc.
5 # This file is part of Koha.
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
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.
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>.
24 use Test::More tests => 17;
30 use Koha::LibraryCategories;
34 use lib $FindBin::Bin;
47 my $schema = Koha::Database->new->schema;
48 $schema->storage->txn_begin;
50 my $dbh = C4::Context->dbh;
53 $dbh->do('DELETE FROM branchcategories');
57 my $count = Koha::Libraries->search->count;
58 like( $count, '/^\d+$/', "the count is a number" );
63 branchname => 'BranchA',
64 branchaddress1 => 'adr1A',
65 branchaddress2 => 'adr2A',
66 branchaddress3 => 'adr3A',
68 branchcity => 'cityA',
69 branchstate => 'stateA',
70 branchcountry => 'countryA',
71 branchphone => 'phoneA',
73 branchemail => 'emailA',
74 branchreplyto => 'emailreply',
75 branchreturnpath => 'branchreturn',
78 branchprinter => undef,
79 branchnotes => 'noteA',
85 branchname => 'BranchB',
86 branchaddress1 => 'adr1B',
87 branchaddress2 => 'adr2B',
88 branchaddress3 => 'adr3B',
90 branchcity => 'cityB',
91 branchstate => 'stateB',
92 branchcountry => 'countryB',
93 branchphone => 'phoneB',
95 branchemail => 'emailB',
96 branchreplyto => 'emailreply',
97 branchreturnpath => 'branchreturn',
100 branchprinter => undef,
101 branchnotes => 'noteB',
102 opac_info => 'opacB',
105 Koha::Library->new($b1)->store;
106 Koha::Library->new($b2)->store;
108 is( Koha::Libraries->search->count, $count + 2, "two branches added" );
110 is( Koha::Libraries->find( $b2->{branchcode} )->delete, 1, "One row affected" );
111 is( Koha::Libraries->search->count, $count + 1, "branch BRB deleted" );
114 is( GetBranchName( $b1->{branchcode} ),
115 $b1->{branchname}, "GetBranchName returns the right name" );
118 my $branches = GetBranches();
119 is( scalar( keys %$branches ),
120 Koha::Libraries->search->count, "GetBranches returns the right number of branches" );
122 #Test modify a library
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',
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" );
152 my $count_cat = Koha::LibraryCategories->search->count;
155 categorycode => 'CAT1',
156 categoryname => 'catname1',
157 codedescription => 'catdesc1',
158 categorytype => 'cattype1',
159 show_in_pulldown => 1
162 categorycode => 'CAT2',
163 categoryname => 'catname2',
164 categorytype => 'catype2',
165 codedescription => 'catdesc2',
166 show_in_pulldown => 1
170 categorycode => 'LIBCATCODE',
171 categoryname => 'library category name',
172 codedescription => 'library category code description',
173 categorytype => 'searchdomain',
174 show_in_pulldown => 1,
177 Koha::LibraryCategory->new(\%new_category)->store;
178 Koha::LibraryCategory->new($cat1)->store;
179 Koha::LibraryCategory->new($cat2)->store;
181 my $categories = Koha::LibraryCategories->search;
182 is( $categories->count, $count_cat + 3, "Two categories added" );
184 my $del = Koha::LibraryCategories->find( $cat2->{categorycode} )->delete;
185 is( $del, 1, 'One row affected' );
187 is( Koha::LibraryCategories->search->count, $count_cat + 2, "Category CAT 2 deleted" );
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' );
194 my $b1info = Koha::Libraries->find( $b1->{branchcode} );
195 is_deeply( $b1info->get_categories->count, 0, 'BRA has no categories' );
197 my $b2info = Koha::Libraries->find( $b2->{branchcode} );
198 is_deeply( $b2info->get_categories->count, 1, 'BRB has the category CAT1' );
200 Koha::LibraryCategory->new($cat2)->store;
201 is( Koha::LibraryCategories->search->count, $count_cat + 3, "Two categories added" );
203 #TODO later: test mybranchine and onlymine
204 # Actually we cannot mock C4::Context->userenv in unit tests
206 #Test GetBranchesLoop
207 my $loop = GetBranchesLoop;
208 is( scalar(@$loop), Koha::Libraries->search->count, 'There is the right number of branches' );
210 $schema->storage->txn_rollback;