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 => 17;
24 use t::lib::TestBuilder;
28 use_ok('Koha::Template::Plugin::Branches');
31 my $schema = Koha::Database->schema;
32 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new;
34 my $library = $builder->build({
37 branchcode => 'MYLIBRARY',
40 my $another_library = $builder->build({
43 branchcode => 'ANOTHERLIB',
47 my $plugin = Koha::Template::Plugin::Branches->new();
48 ok($plugin, "initialized Branches plugin");
50 my $name = $plugin->GetName($library->{branchcode});
51 is($name, $library->{branchname}, 'retrieved expected name for library');
53 $name = $plugin->GetName('__ANY__');
54 is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
56 $name = $plugin->GetName(undef);
57 is($name, '', 'received empty string as name of NULL/undefined library code');
59 $library = $plugin->GetLoggedInBranchcode();
60 is($library, '', 'no active library if there is no active user session');
62 t::lib::Mocks::mock_userenv({ branchcode => 'MYLIBRARY' });
63 $library = $plugin->GetLoggedInBranchcode();
64 is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
66 t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
67 my $libraries = $plugin->all();
68 ok( scalar(@$libraries) > 1, 'If IndependentBranches is not set, all libraries should be returned' );
69 is( grep ( { $_->{branchcode} eq 'MYLIBRARY' and $_->{selected} == 1 } @$libraries ), 1, 'Without selected parameter, my library should be preselected' );
70 is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and not exists $_->{selected} } @$libraries ), 1, 'Without selected parameter, other library should not be preselected' );
71 $libraries = $plugin->all( { selected => 'ANOTHERLIB' } );
72 is( grep ( { $_->{branchcode} eq 'MYLIBRARY' and not exists $_->{selected} } @$libraries ), 1, 'With selected parameter, my library should not be preselected' );
73 is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and $_->{selected} == 1 } @$libraries ), 1, 'With selected parameter, other library should be preselected' );
74 $libraries = $plugin->all( { selected => '' } );
75 is( grep ( { exists $_->{selected} } @$libraries ), 0, 'With selected parameter set to an empty string, no library should be preselected' );
77 my $total = @{$plugin->all};
78 my $pickupable = @{$plugin->all( { search_params => { pickup_location => 1 } }) };
79 my $yet_another_library = $builder->build({
82 branchcode => 'CANTPICKUP',
86 is(@{$plugin->all( { search_params => { pickup_location => 1 } }) }, $pickupable,
87 'Adding a new library with pickups'
88 .' disabled does not increase the amount returned by ->pickup_locations');
89 is(@{$plugin->all}, $total+1, 'However, adding a new library increases'
90 .' the total amount gotten with ->all');
92 t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
93 $libraries = $plugin->all();
94 is( scalar(@$libraries), 1, 'If IndependentBranches is set, only 1 library should be returned' );
95 $libraries = $plugin->all( { unfiltered => 1 } );
96 ok( scalar(@$libraries) > 1, 'If IndependentBranches is set, all libraries should be returned if the unfiltered flag is set' );