]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Template/Plugin/Branches.t
Bug 21817: Centralize the mock of userenv from tests
[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 => 17;
20
21 use C4::Context;
22 use Koha::Database;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 BEGIN {
28     use_ok('Koha::Template::Plugin::Branches');
29 }
30
31 my $schema = Koha::Database->schema;
32 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new;
34 my $library = $builder->build({
35     source => 'Branch',
36     value => {
37         branchcode => 'MYLIBRARY',
38     }
39 });
40 my $another_library = $builder->build({
41     source => 'Branch',
42     value => {
43         branchcode => 'ANOTHERLIB',
44     }
45 });
46
47 my $plugin = Koha::Template::Plugin::Branches->new();
48 ok($plugin, "initialized Branches plugin");
49
50 my $name = $plugin->GetName($library->{branchcode});
51 is($name, $library->{branchname}, 'retrieved expected name for library');
52
53 $name = $plugin->GetName('__ANY__');
54 is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
55
56 $name = $plugin->GetName(undef);
57 is($name, '', 'received empty string as name of NULL/undefined library code');
58
59 $library = $plugin->GetLoggedInBranchcode();
60 is($library, '', 'no active library if there is no active user session');
61
62 t::lib::Mocks::mock_userenv({ branchcode => 'MYLIBRARY' });
63 $library = $plugin->GetLoggedInBranchcode();
64 is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
65
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' );
76
77 my $total = @{$plugin->all};
78 my $pickupable = @{$plugin->all( { search_params => { pickup_location => 1 } }) };
79 my $yet_another_library = $builder->build({
80     source => 'Branch',
81     value => {
82         branchcode => 'CANTPICKUP',
83         pickup_location => 0,
84     }
85 });
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');
91
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' );