Bug 15758: Koha::Libraries - Do not select an option if selected is defined
[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 => 15;
20
21 use C4::Context;
22 use Koha::Database;
23
24 use t::lib::TestBuilder;
25
26 BEGIN {
27     use_ok('Koha::Template::Plugin::Branches');
28 }
29
30 my $schema = Koha::Database->schema;
31 $schema->storage->txn_begin;
32 my $builder = t::lib::TestBuilder->new;
33 my $library = $builder->build({
34     source => 'Branch',
35     value => {
36         branchcode => 'MYLIBRARY',
37     }
38 });
39 my $another_library = $builder->build({
40     source => 'Branch',
41     value => {
42         branchcode => 'ANOTHERLIB',
43     }
44 });
45
46 my $plugin = Koha::Template::Plugin::Branches->new();
47 ok($plugin, "initialized Branches plugin");
48
49 my $name = $plugin->GetName($library->{branchcode});
50 is($name, $library->{branchname}, 'retrieved expected name for library');
51
52 $name = $plugin->GetName('__ANY__');
53 is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
54
55 $name = $plugin->GetName(undef);
56 is($name, '', 'received empty string as name of NULL/undefined library code');
57
58 $library = $plugin->GetLoggedInBranchcode();
59 is($library, '', 'no active library if there is no active user session');
60
61 C4::Context->_new_userenv('DUMMY_SESSION_ID');
62 C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
63 $library = $plugin->GetLoggedInBranchcode();
64 is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
65
66 C4::Context->set_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 C4::Context->set_preference( 'IndependentBranches', 1 );
78 $libraries = $plugin->all();
79 is( scalar(@$libraries), 1, 'If IndependentBranches is set, only 1 library should be returned' );
80 $libraries = $plugin->all( { unfiltered => 1 } );
81 ok( scalar(@$libraries) > 1, 'If IndependentBranches is set, all libraries should be returned if the unfiltered flag is set' );