Bug 32721: (QA follow up) - fix QA issues
[koha.git] / t / db_dependent / Template / Plugin / Branches.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
16
17 use Modern::Perl;
18
19 use Test::More tests => 4;
20 use Test::MockModule;
21
22 use C4::Context;
23 use C4::Biblio qw(AddBiblio);
24 use Koha::Database;
25
26 use Clone qw(clone);
27 use List::MoreUtils qw(any);
28
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 BEGIN {
33     use_ok('Koha::Template::Plugin::Branches');
34 }
35
36 my $schema  = Koha::Database->schema;
37 my $builder = t::lib::TestBuilder->new;
38
39 subtest 'all() tests' => sub {
40
41     plan tests => 19;
42
43     $schema->storage->txn_begin;
44
45     my $library = $builder->build({
46         source => 'Branch',
47         value => {
48             branchcode => 'MYLIBRARY',
49             branchname => 'My sweet library'
50         }
51     });
52     my $another_library = $builder->build({
53         source => 'Branch',
54         value => {
55             branchcode => 'ANOTHERLIB',
56         }
57     });
58
59     my $plugin = Koha::Template::Plugin::Branches->new();
60     ok($plugin, "initialized Branches plugin");
61
62     my $name = $plugin->GetName($library->{branchcode});
63     is($name, $library->{branchname}, 'retrieved expected name for library');
64
65     $name = $plugin->GetName('__ANY__');
66     is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
67
68     $name = $plugin->GetName(undef);
69     is($name, '', 'received empty string as name of NULL/undefined library code');
70
71     $name = $plugin->GetName(q{});
72     is($name, '', 'received empty string as name of empty string library code');
73
74     is($plugin->GetLoggedInBranchcode(), '', 'no active library code if there is no active user session');
75     is($plugin->GetLoggedInBranchname(), '', 'no active library name if there is no active user session');
76
77     t::lib::Mocks::mock_userenv({ branchcode => 'MYLIBRARY', branchname => 'My sweet library' });
78     is($plugin->GetLoggedInBranchcode(), 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library code');
79     is($plugin->GetLoggedInBranchname(), 'My sweet library', 'GetLoggedInBranchname() returns active library name');
80
81     t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
82     my $libraries = $plugin->all();
83     ok( scalar(@$libraries) > 1, 'If IndependentBranches is not set, all libraries should be returned' );
84     is( grep ( { $_->{branchcode} eq 'MYLIBRARY'  and $_->{selected} == 1 } @$libraries ),       1, 'Without selected parameter, my library should be preselected' );
85     is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and not exists $_->{selected} } @$libraries ), 1, 'Without selected parameter, other library should not be preselected' );
86     $libraries = $plugin->all( { selected => 'ANOTHERLIB' } );
87     is( grep ( { $_->{branchcode} eq 'MYLIBRARY'  and not exists $_->{selected} } @$libraries ), 1, 'With selected parameter, my library should not be preselected' );
88     is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and $_->{selected} == 1 } @$libraries ),       1, 'With selected parameter, other library should be preselected' );
89     $libraries = $plugin->all( { selected => '' } );
90     is( grep ( { exists $_->{selected} } @$libraries ), 0, 'With selected parameter set to an empty string, no library should be preselected' );
91
92     my $total = @{$plugin->all};
93     my $pickupable = @{$plugin->all( { search_params => { pickup_location => 1 } }) };
94     my $yet_another_library = $builder->build({
95         source => 'Branch',
96         value => {
97             branchcode => 'CANTPICKUP',
98             pickup_location => 0,
99         }
100     });
101     is(@{$plugin->all( { search_params => { pickup_location => 1 } }) }, $pickupable,
102        'Adding a new library with pickups'
103        .' disabled does not increase the amount returned by ->pickup_locations');
104     is(@{$plugin->all}, $total+1, 'However, adding a new library increases'
105        .' the total amount gotten with ->all');
106
107     t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
108     $libraries = $plugin->all();
109     is( scalar(@$libraries), 1, 'If IndependentBranches is set, only 1 library should be returned' );
110     $libraries = $plugin->all( { unfiltered => 1 } );
111     ok( scalar(@$libraries) > 1, 'If IndependentBranches is set, all libraries should be returned if the unfiltered flag is set' );
112
113     $schema->storage->txn_rollback;
114 };
115
116 subtest 'pickup_locations() tests' => sub {
117
118     plan tests => 9;
119
120     $schema->storage->txn_begin;
121
122     Koha::Libraries->search->update({ pickup_location => 0 });
123
124     my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
125     my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
126     my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
127
128     my $plugin           = Koha::Template::Plugin::Branches->new();
129     my $pickup_locations = $plugin->pickup_locations();
130
131     is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
132
133     $pickup_locations = $plugin->pickup_locations({ search_params => { item => undef }});
134     is( scalar @{$pickup_locations}, 3, 'item parameter not a ref, fallback to general search' );
135
136     $pickup_locations = $plugin->pickup_locations({ search_params => { biblio => undef }});
137     is( scalar @{$pickup_locations}, 3, 'biblio parameter not a ref, fallback to general search' );
138
139     my $item_class = Test::MockModule->new('Koha::Item');
140     $item_class->mock(
141         'pickup_locations',
142         sub {
143             return Koha::Libraries->search(
144                 { branchcode => $library_1->branchcode } );
145         }
146     );
147
148     my $item   = $builder->build_sample_item();
149     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
150
151     $pickup_locations = $plugin->pickup_locations(
152         { search_params => { item => $item, patron => Koha::Patron->new } } );
153
154     is( scalar @{$pickup_locations}, 1, 'Only the library returned by $item->pickup_locations is returned' );
155     is( $pickup_locations->[0]->{branchcode}, $library_1->branchcode, 'Not cheating' );
156
157     my $biblio_class = Test::MockModule->new('Koha::Biblio');
158     $biblio_class->mock(
159         'pickup_locations',
160         sub {
161             return Koha::Libraries->search(
162                 { branchcode => $library_2->branchcode } );
163         }
164     );
165
166     my $biblio = $builder->build_sample_biblio();
167
168     $pickup_locations = $plugin->pickup_locations(
169         { search_params => { biblio => $biblio, patron => Koha::Patron->new } } );
170
171     is( scalar @{$pickup_locations}, 1, 'Only the library returned by $biblio->pickup_locations is returned' );
172     is( $pickup_locations->[0]->{branchcode}, $library_2->branchcode, 'Not cheating' );
173
174     subtest 'Koha::Item->pickup_locations and Koha::Biblio->pickup_locations empty tests' => sub {
175
176         plan tests => 2;
177
178         my $biblio_class = Test::MockModule->new('Koha::Biblio');
179         $biblio_class->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty } );
180
181         my $biblio = $builder->build_sample_biblio;
182
183         my @pickup_locations = @{$plugin->pickup_locations({ search_params => { biblio => $biblio->id } })};
184         is( scalar @pickup_locations, 0, 'No pickup locations returned' );
185
186         my $item_class = Test::MockModule->new('Koha::Item');
187         $item_class->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty } );
188
189         my $item = $builder->build_sample_item;
190
191         @pickup_locations = @{$plugin->pickup_locations({ search_params => { item => $item->id } })};
192         is( scalar @pickup_locations, 0, 'No pickup locations returned' );
193     };
194
195     subtest 'selected tests' => sub {
196
197         plan tests => 4;
198
199         t::lib::Mocks::mock_userenv({ branchcode => $library_2->branchcode });
200
201         $pickup_locations = $plugin->pickup_locations();
202
203         is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
204         foreach my $pickup_location (@{ $pickup_locations }) {
205             next unless exists $pickup_location->{selected} and $pickup_location->{selected} == 1;
206             is( $pickup_location->{branchcode}, $library_2->branchcode, 'The right library is marked as selected' );
207         }
208
209         $pickup_locations = $plugin->pickup_locations({ selected => $library_3->branchcode });
210
211         is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
212         foreach my $pickup_location (@{ $pickup_locations }) {
213             next unless exists $pickup_location->{selected} and $pickup_location->{selected} == 1;
214             is( $pickup_location->{branchcode}, $library_3->branchcode, 'The right library is marked as selected' );
215         }
216     };
217
218     $schema->storage->txn_rollback;
219 };
220
221 subtest 'branch specific js and css' => sub {
222
223     plan tests => 2;
224
225     $schema->storage->txn_begin;
226
227     my $newbranch = $builder->build({
228         source => 'Branch',
229         value => {
230             branchcode => 'AAA',
231             branchname => 'Specific Branch',
232             userjs => 'console.log(\'Hello World\');',
233             usercss => 'body { background-color: blue; }'
234         }
235     });
236
237     my $plugin = Koha::Template::Plugin::Branches->new();
238
239     my $userjs = $plugin->GetBranchSpecificJS($newbranch->{branchcode});
240     is($userjs, $newbranch->{userjs},'received correct JS string from function');
241
242     my $usercss = $plugin->GetBranchSpecificCSS($newbranch->{branchcode});
243     is($usercss, $newbranch->{usercss},'received correct CSS string from function');
244
245     $schema->storage->txn_rollback;
246 };