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 => 6;
29 use t::lib::TestBuilder;
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
34 my $builder = t::lib::TestBuilder->new;
35 my $nb_of_libraries = Koha::Libraries->search->count;
36 my $new_library_1 = Koha::Library->new({
37 branchcode => 'my_bc_1',
38 branchname => 'my_branchname_1',
39 branchnotes => 'my_branchnotes_1',
40 marcorgcode => 'US-MyLib',
42 my $new_library_2 = Koha::Library->new({
43 branchcode => 'my_bc_2',
44 branchname => 'my_branchname_2',
45 branchnotes => 'my_branchnotes_2',
48 is( Koha::Libraries->search->count, $nb_of_libraries + 2, 'The 2 libraries should have been added' );
50 my $retrieved_library_1 = Koha::Libraries->find( $new_library_1->branchcode );
51 is( $retrieved_library_1->branchname, $new_library_1->branchname, 'Find a library by branchcode should return the correct library' );
53 $retrieved_library_1->delete;
54 is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
56 # Stockrotation relationship testing
58 my $new_library_sr = $builder->build({ source => 'Branch' });
61 source => 'Stockrotationstage',
62 value => { branchcode_id => $new_library_sr->{branchcode} },
65 source => 'Stockrotationstage',
66 value => { branchcode_id => $new_library_sr->{branchcode} },
69 source => 'Stockrotationstage',
70 value => { branchcode_id => $new_library_sr->{branchcode} },
73 my $srstages = Koha::Libraries->find($new_library_sr->{branchcode})
74 ->stockrotationstages;
75 is( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this branch');
77 isa_ok( $srstages->next, 'Koha::StockRotationStage', "Relationship correctly creates Koha::Objects." );
79 $schema->storage->txn_rollback;
81 subtest '->get_effective_marcorgcode' => sub {
85 $schema->storage->txn_begin;
87 my $library_1 = $builder->build_object({ class => 'Koha::Libraries',
88 value => { marcorgcode => 'US-MyLib' } });
89 my $library_2 = $builder->build_object({ class => 'Koha::Libraries',
90 value => { marcorgcode => undef } });
92 t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
94 is( $library_1->get_effective_marcorgcode, 'US-MyLib',
95 'If defined, use library\'s own marc org code');
96 is( $library_2->get_effective_marcorgcode, 'US-Default',
97 'If not defined library\' marc org code, use the one from system preferences');
99 t::lib::Mocks::mock_preference('MARCOrgCode', 'Blah');
100 is( $library_2->get_effective_marcorgcode, 'Blah',
101 'Fallback is always MARCOrgCode syspref');
103 $library_2->marcorgcode('ThisIsACode')->store();
104 is( $library_2->get_effective_marcorgcode, 'ThisIsACode',
105 'Pick library_2 code');
107 $schema->storage->txn_rollback;