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 => 15;
24 use t::lib::TestBuilder;
27 use_ok('Koha::Template::Plugin::Branches');
30 my $schema = Koha::Database->schema;
31 $schema->storage->txn_begin;
32 my $builder = t::lib::TestBuilder->new;
33 my $library = $builder->build({
36 branchcode => 'MYLIBRARY',
39 my $another_library = $builder->build({
42 branchcode => 'ANOTHERLIB',
46 my $plugin = Koha::Template::Plugin::Branches->new();
47 ok($plugin, "initialized Branches plugin");
49 my $name = $plugin->GetName($library->{branchcode});
50 is($name, $library->{branchname}, 'retrieved expected name for library');
52 $name = $plugin->GetName('__ANY__');
53 is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
55 $name = $plugin->GetName(undef);
56 is($name, '', 'received empty string as name of NULL/undefined library code');
58 $library = $plugin->GetLoggedInBranchcode();
59 is($library, '', 'no active library if there is no active user session');
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');
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' );
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' );