3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 9;
29 use Koha::Item::Transfer::Limits;
34 use Koha::CirculationRules;
37 use t::lib::TestBuilder;
39 my $schema = Koha::Database->new->schema;
40 $schema->storage->txn_begin;
42 # Cleanup default_branch_item_rules
43 my $dbh = C4::Context->dbh;
44 $dbh->do('DELETE FROM circulation_rules');
46 my $builder = t::lib::TestBuilder->new;
47 my $nb_of_libraries = Koha::Libraries->search->count;
48 my $new_library_1 = Koha::Library->new({
49 branchcode => 'my_bc_1',
50 branchname => 'my_branchname_1',
51 branchnotes => 'my_branchnotes_1',
52 marcorgcode => 'US-MyLib',
54 my $new_library_2 = Koha::Library->new({
55 branchcode => 'my_bc_2',
56 branchname => 'my_branchname_2',
57 branchnotes => 'my_branchnotes_2',
60 is( Koha::Libraries->search->count, $nb_of_libraries + 2, 'The 2 libraries should have been added' );
62 my $retrieved_library_1 = Koha::Libraries->find( $new_library_1->branchcode );
63 is( $retrieved_library_1->branchname, $new_library_1->branchname, 'Find a library by branchcode should return the correct library' );
65 $retrieved_library_1->delete;
66 is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
68 # Stockrotation relationship testing
70 my $new_library_sr = $builder->build({ source => 'Branch' });
73 source => 'Stockrotationstage',
74 value => { branchcode_id => $new_library_sr->{branchcode} },
77 source => 'Stockrotationstage',
78 value => { branchcode_id => $new_library_sr->{branchcode} },
81 source => 'Stockrotationstage',
82 value => { branchcode_id => $new_library_sr->{branchcode} },
85 my $srstages = Koha::Libraries->find($new_library_sr->{branchcode})
86 ->stockrotationstages;
87 is( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this branch');
89 isa_ok( $srstages->next, 'Koha::StockRotationStage', "Relationship correctly creates Koha::Objects." );
91 $schema->storage->txn_rollback;
93 subtest '->get_effective_marcorgcode' => sub {
97 $schema->storage->txn_begin;
99 my $library_1 = $builder->build_object({ class => 'Koha::Libraries',
100 value => { marcorgcode => 'US-MyLib' } });
101 my $library_2 = $builder->build_object({ class => 'Koha::Libraries',
102 value => { marcorgcode => undef } });
104 t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
106 is( $library_1->get_effective_marcorgcode, 'US-MyLib',
107 'If defined, use library\'s own marc org code');
108 is( $library_2->get_effective_marcorgcode, 'US-Default',
109 'If not defined library\' marc org code, use the one from system preferences');
111 t::lib::Mocks::mock_preference('MARCOrgCode', 'Blah');
112 is( $library_2->get_effective_marcorgcode, 'Blah',
113 'Fallback is always MARCOrgCode syspref');
115 $library_2->marcorgcode('ThisIsACode')->store();
116 is( $library_2->get_effective_marcorgcode, 'ThisIsACode',
117 'Pick library_2 code');
119 $schema->storage->txn_rollback;
122 subtest '->inbound_email_address' => sub {
126 $schema->storage->txn_begin;
128 my $library_1 = $builder->build_object(
130 class => 'Koha::Libraries',
132 branchemail => 'from@mybranc.com',
133 branchreplyto => 'reply@mybranch.com'
138 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'admin@mylibrary.com' );
139 t::lib::Mocks::mock_preference( 'ReplytoDefault', 'reply@mylibrary.com' );
141 is( $library_1->inbound_email_address, $library_1->branchreplyto,
142 'If defined, use branches replyto address');
144 $library_1->branchreplyto(undef)->store();
145 is( $library_1->inbound_email_address, $library_1->branchemail,
146 'Fallback to branches email address when branchreplyto is undefined');
148 $library_1->branchemail(undef)->store();
149 is( $library_1->inbound_email_address, 'reply@mylibrary.com',
150 'Fallback to ReplytoDefault email address when branchreplyto and branchemail are undefined');
152 t::lib::Mocks::mock_preference( 'ReplytoDefault', '' );
153 is( $library_1->inbound_email_address, 'admin@mylibrary.com',
154 'Fallback to KohaAdminEmailAddress email address when branchreplyto, branchemail and ReplytoDefault are undefined');
156 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', '' );
157 is( $library_1->inbound_email_address, undef,
158 'Return undef when email address when branchreplyto, branchemail, ReplytoDefault and KohaAdminEmailAddress are undefined');
159 $schema->storage->txn_rollback;
162 subtest 'cash_registers' => sub {
165 $schema->storage->txn_begin;
167 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
168 my $register1 = $builder->build_object(
170 class => 'Koha::Cash::Registers',
171 value => { branch => $library->branchcode },
174 my $register2 = $builder->build_object(
176 class => 'Koha::Cash::Registers',
177 value => { branch => $library->branchcode },
181 my $registers = $library->cash_registers;
182 is( ref($registers), 'Koha::Cash::Registers',
183 'Koha::Library->cash_registers should return a set of Koha::Cash::Registers'
185 is( $registers->count, 2,
186 'Koha::Library->cash_registers should return the correct cash registers'
190 is( $library->cash_registers->next->id, $register2->id,
191 'Koha::Library->cash_registers should return the correct cash registers'
194 $schema->storage->txn_rollback;
197 subtest 'get_hold_libraries and validate_hold_sibling' => sub {
201 $schema->storage->txn_begin;
203 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
204 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
205 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
207 my $root = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
208 my $g1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library1->branchcode } } );
209 my $g2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library2->branchcode } } );
211 my @hold_libraries = ($library1, $library2);
213 my @result = $library1->get_hold_libraries();
215 ok(scalar(@result) == 2, 'get_hold_libraries returns 2 libraries');
217 my %map = map {$_->branchcode, 1} @result;
219 foreach my $hold_library ( @hold_libraries ) {
220 ok(exists $map{$hold_library->branchcode}, 'library in hold group');
223 ok($library1->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is a valid hold sibling');
224 ok(!$library1->validate_hold_sibling( { branchcode => $library3->branchcode } ), 'Library 3 is not a valid hold sibling');
226 $schema->storage->txn_rollback;