Bug 21912: Add tests for Koha::Objects->search
[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 => 6;
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 # Stockrotation relationship testing
57
58 my $new_library_sr = $builder->build({ source => 'Branch' });
59
60 $builder->build({
61     source => 'Stockrotationstage',
62     value  => { branchcode_id => $new_library_sr->{branchcode} },
63 });
64 $builder->build({
65     source => 'Stockrotationstage',
66     value  => { branchcode_id => $new_library_sr->{branchcode} },
67 });
68 $builder->build({
69     source => 'Stockrotationstage',
70     value  => { branchcode_id => $new_library_sr->{branchcode} },
71 });
72
73 my $srstages = Koha::Libraries->find($new_library_sr->{branchcode})
74     ->stockrotationstages;
75 is( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this branch');
76
77 isa_ok( $srstages->next, 'Koha::StockRotationStage', "Relationship correctly creates Koha::Objects." );
78
79 $schema->storage->txn_rollback;
80
81 subtest '->get_effective_marcorgcode' => sub {
82
83     plan tests => 4;
84
85     $schema->storage->txn_begin;
86
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 } });
91
92     t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
93
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');
98
99     t::lib::Mocks::mock_preference('MARCOrgCode', 'Blah');
100     is( $library_2->get_effective_marcorgcode, 'Blah',
101        'Fallback is always MARCOrgCode syspref');
102
103     $library_2->marcorgcode('ThisIsACode')->store();
104     is( $library_2->get_effective_marcorgcode, 'ThisIsACode',
105        'Pick library_2 code');
106
107     $schema->storage->txn_rollback;
108 };