Bug 20404: Fix Patrons/Import.t
[koha.git] / t / db_dependent / Koha / Libraries.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 4;
23
24 use Koha::Library;
25 use Koha::Libraries;
26 use Koha::Database;
27
28 use t::lib::Mocks;
29 use t::lib::TestBuilder;
30
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
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',
41 })->store;
42 my $new_library_2 = Koha::Library->new({
43     branchcode => 'my_bc_2',
44     branchname => 'my_branchname_2',
45     branchnotes => 'my_branchnotes_2',
46 })->store;
47
48 is( Koha::Libraries->search->count,         $nb_of_libraries + 2,  'The 2 libraries should have been added' );
49
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' );
52
53 $retrieved_library_1->delete;
54 is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
55
56 $schema->storage->txn_rollback;
57
58 subtest '->get_effective_marcorgcode' => sub {
59
60     plan tests => 4;
61
62     $schema->storage->txn_begin;
63
64     my $library_1 = $builder->build_object({ class => 'Koha::Libraries',
65                                              value => { marcorgcode => 'US-MyLib' } });
66     my $library_2 = $builder->build_object({ class => 'Koha::Libraries',
67                                              value => { marcorgcode => undef } });
68
69     t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
70
71     is( $library_1->get_effective_marcorgcode, 'US-MyLib',
72        'If defined, use library\'s own marc org code');
73     is( $library_2->get_effective_marcorgcode, 'US-Default',
74        'If not defined library\' marc org code, use the one from system preferences');
75
76     t::lib::Mocks::mock_preference('MARCOrgCode', 'Blah');
77     is( $library_2->get_effective_marcorgcode, 'Blah',
78        'Fallback is always MARCOrgCode syspref');
79
80     $library_2->marcorgcode('ThisIsACode')->store();
81     is( $library_2->get_effective_marcorgcode, 'ThisIsACode',
82        'Pick library_2 code');
83
84     $schema->storage->txn_rollback;
85 };