Bug 10132: (QA followup) Organize tests in subtest
[srvgit] / 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 => 10;
23
24 use Koha::Library;
25 use Koha::Libraries;
26 use Koha::LibraryCategory;
27 use Koha::LibraryCategories;
28 use Koha::Database;
29
30 use t::lib::Mocks;
31 use t::lib::TestBuilder;
32
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35
36 my $builder = t::lib::TestBuilder->new;
37 my $nb_of_libraries = Koha::Libraries->search->count;
38 my $nb_of_categories = Koha::LibraryCategories->search->count;
39 my $new_library_1 = Koha::Library->new({
40     branchcode => 'my_bc_1',
41     branchname => 'my_branchname_1',
42     branchnotes => 'my_branchnotes_1',
43     marcorgcode => 'US-MyLib',
44 })->store;
45 my $new_library_2 = Koha::Library->new({
46     branchcode => 'my_bc_2',
47     branchname => 'my_branchname_2',
48     branchnotes => 'my_branchnotes_2',
49 })->store;
50 my $new_category_1 = Koha::LibraryCategory->new({
51     categorycode => 'my_cc_1',
52     categoryname => 'my_categoryname_1',
53     codedescription => 'my_codedescription_1',
54     categorytype => 'properties',
55 } )->store;
56 my $new_category_2 = Koha::LibraryCategory->new( {
57           categorycode    => 'my_cc_2',
58           categoryname    => 'my_categoryname_2',
59           codedescription => 'my_codedescription_2',
60           categorytype    => 'searchdomain',
61 } )->store;
62 my $new_category_3 = Koha::LibraryCategory->new( {
63           categorycode    => 'my_cc_3',
64           categoryname    => 'my_categoryname_3',
65           codedescription => 'my_codedescription_3',
66           categorytype    => 'searchdomain',
67 } )->store;
68
69 is( Koha::Libraries->search->count,         $nb_of_libraries + 2,  'The 2 libraries should have been added' );
70 is( Koha::LibraryCategories->search->count, $nb_of_categories + 3, 'The 3 library categories should have been added' );
71
72 $new_library_1->add_to_categories( [$new_category_1] );
73 $new_library_2->add_to_categories( [$new_category_2] );
74 my $retrieved_library_1 = Koha::Libraries->find( $new_library_1->branchcode );
75 is( $retrieved_library_1->branchname, $new_library_1->branchname, 'Find a library by branchcode should return the correct library' );
76 is( Koha::Libraries->find( $new_library_1->branchcode )->get_categories->count, 1, '1 library should have been linked to the category 1' );
77
78 $retrieved_library_1->update_categories( [ $new_category_2, $new_category_3 ] );
79 is( Koha::Libraries->find( $new_library_1->branchcode )->get_categories->count, 2, '2 libraries should have been linked to the category 2' );
80
81 my $retrieved_category_2 = Koha::LibraryCategories->find( $new_category_2->categorycode );
82 is( $retrieved_category_2->libraries->count, 2, '2 libraries should have been linked to the category_2' );
83 is( $retrieved_category_2->categorycode, uc('my_cc_2'), 'The Koha::LibraryCategory constructor should have upercased the categorycode' );
84
85 $retrieved_library_1->delete;
86 is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
87
88 $retrieved_category_2->delete;
89 is( Koha::LibraryCategories->search->count, $nb_of_categories + 2, 'Delete should have deleted the library category' );
90
91 $schema->storage->txn_rollback;
92
93 subtest '->get_effective_marcorgcode' => sub {
94
95     plan tests => 4;
96
97     $schema->storage->txn_begin;
98
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 } });
103
104     t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
105
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');
110
111     t::lib::Mocks::mock_preference('MARCOrgCode', 'Blah');
112     is( $library_2->get_effective_marcorgcode, 'Blah',
113        'Fallback is always MARCOrgCode syspref');
114
115     $library_2->marcorgcode('ThisIsACode')->store();
116     is( $library_2->get_effective_marcorgcode, 'ThisIsACode',
117        'Pick library_2 code');
118
119     $schema->storage->txn_rollback;
120 };