3 # This file is part of Koha.
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
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.
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>.
19 use Test::More tests => 3;
23 use C4::Biblio qw(AddBiblio);
27 use List::MoreUtils qw(any);
29 use t::lib::TestBuilder;
33 use_ok('Koha::Template::Plugin::Branches');
36 my $schema = Koha::Database->schema;
37 my $builder = t::lib::TestBuilder->new;
39 subtest 'all() tests' => sub {
43 $schema->storage->txn_begin;
45 my $library = $builder->build({
48 branchcode => 'MYLIBRARY',
49 branchname => 'My sweet library'
52 my $another_library = $builder->build({
55 branchcode => 'ANOTHERLIB',
59 my $plugin = Koha::Template::Plugin::Branches->new();
60 ok($plugin, "initialized Branches plugin");
62 my $name = $plugin->GetName($library->{branchcode});
63 is($name, $library->{branchname}, 'retrieved expected name for library');
65 $name = $plugin->GetName('__ANY__');
66 is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
68 $name = $plugin->GetName(undef);
69 is($name, '', 'received empty string as name of NULL/undefined library code');
71 is($plugin->GetLoggedInBranchcode(), '', 'no active library code if there is no active user session');
72 is($plugin->GetLoggedInBranchname(), '', 'no active library name if there is no active user session');
74 t::lib::Mocks::mock_userenv({ branchcode => 'MYLIBRARY', branchname => 'My sweet library' });
75 is($plugin->GetLoggedInBranchcode(), 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library code');
76 is($plugin->GetLoggedInBranchname(), 'My sweet library', 'GetLoggedInBranchname() returns active library name');
78 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
79 my $libraries = $plugin->all();
80 ok( scalar(@$libraries) > 1, 'If IndependentBranches is not set, all libraries should be returned' );
81 is( grep ( { $_->{branchcode} eq 'MYLIBRARY' and $_->{selected} == 1 } @$libraries ), 1, 'Without selected parameter, my library should be preselected' );
82 is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and not exists $_->{selected} } @$libraries ), 1, 'Without selected parameter, other library should not be preselected' );
83 $libraries = $plugin->all( { selected => 'ANOTHERLIB' } );
84 is( grep ( { $_->{branchcode} eq 'MYLIBRARY' and not exists $_->{selected} } @$libraries ), 1, 'With selected parameter, my library should not be preselected' );
85 is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and $_->{selected} == 1 } @$libraries ), 1, 'With selected parameter, other library should be preselected' );
86 $libraries = $plugin->all( { selected => '' } );
87 is( grep ( { exists $_->{selected} } @$libraries ), 0, 'With selected parameter set to an empty string, no library should be preselected' );
89 my $total = @{$plugin->all};
90 my $pickupable = @{$plugin->all( { search_params => { pickup_location => 1 } }) };
91 my $yet_another_library = $builder->build({
94 branchcode => 'CANTPICKUP',
98 is(@{$plugin->all( { search_params => { pickup_location => 1 } }) }, $pickupable,
99 'Adding a new library with pickups'
100 .' disabled does not increase the amount returned by ->pickup_locations');
101 is(@{$plugin->all}, $total+1, 'However, adding a new library increases'
102 .' the total amount gotten with ->all');
104 t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
105 $libraries = $plugin->all();
106 is( scalar(@$libraries), 1, 'If IndependentBranches is set, only 1 library should be returned' );
107 $libraries = $plugin->all( { unfiltered => 1 } );
108 ok( scalar(@$libraries) > 1, 'If IndependentBranches is set, all libraries should be returned if the unfiltered flag is set' );
110 $schema->storage->txn_rollback;
113 subtest 'pickup_locations() tests' => sub {
117 $schema->storage->txn_begin;
119 Koha::Libraries->search->update({ pickup_location => 0 });
121 my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
122 my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
123 my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
125 my $plugin = Koha::Template::Plugin::Branches->new();
126 my $pickup_locations = $plugin->pickup_locations();
128 is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
130 $pickup_locations = $plugin->pickup_locations({ search_params => { item => undef }});
131 is( scalar @{$pickup_locations}, 3, 'item parameter not a ref, fallback to general search' );
133 $pickup_locations = $plugin->pickup_locations({ search_params => { biblio => undef }});
134 is( scalar @{$pickup_locations}, 3, 'biblio parameter not a ref, fallback to general search' );
136 my $item_class = Test::MockModule->new('Koha::Item');
140 return Koha::Libraries->search(
141 { branchcode => $library_1->branchcode } );
145 my $item = $builder->build_sample_item();
146 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
148 $pickup_locations = $plugin->pickup_locations(
149 { search_params => { item => $item, patron => Koha::Patron->new } } );
151 is( scalar @{$pickup_locations}, 1, 'Only the library returned by $item->pickup_locations is returned' );
152 is( $pickup_locations->[0]->{branchcode}, $library_1->branchcode, 'Not cheating' );
154 my $biblio_class = Test::MockModule->new('Koha::Biblio');
162 my $biblio = $builder->build_sample_biblio();
164 $pickup_locations = $plugin->pickup_locations(
165 { search_params => { biblio => $biblio, patron => Koha::Patron->new } } );
167 is( scalar @{$pickup_locations}, 1, 'Only the library returned by $biblio->pickup_locations is returned' );
168 is( $pickup_locations->[0]->{branchcode}, $library_2->branchcode, 'Not cheating' );
170 subtest 'selected tests' => sub {
174 t::lib::Mocks::mock_userenv({ branchcode => $library_2->branchcode });
176 $pickup_locations = $plugin->pickup_locations();
178 is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
179 foreach my $pickup_location (@{ $pickup_locations }) {
180 next unless exists $pickup_location->{selected} and $pickup_location->{selected} == 1;
181 is( $pickup_location->{branchcode}, $library_2->branchcode, 'The right library is marked as selected' );
184 $pickup_locations = $plugin->pickup_locations({ selected => $library_3->branchcode });
186 is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
187 foreach my $pickup_location (@{ $pickup_locations }) {
188 next unless exists $pickup_location->{selected} and $pickup_location->{selected} == 1;
189 is( $pickup_location->{branchcode}, $library_3->branchcode, 'The right library is marked as selected' );
193 $schema->storage->txn_rollback;