View | Details | Raw Unified | Return to bug 15758
Collapse All | Expand All

(-)a/t/db_dependent/Template/Plugin/Branches.t (-30 / +54 lines)
Lines 1-25 Link Here
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
1
use Modern::Perl;
17
use Modern::Perl;
2
18
3
use Test::More tests => 11;
19
use Test::More tests => 14;
4
20
5
use C4::Context;
21
use C4::Context;
6
use Koha::Library;
22
use Koha::Database;
7
use Koha::Libraries;
23
8
use Koha::Template::Plugin::Branches;
24
use t::lib::TestBuilder;
9
25
10
my $dbh = C4::Context->dbh;
26
BEGIN {
11
$dbh->{AutoCommit} = 0;
27
    use_ok('Koha::Template::Plugin::Branches');
12
$dbh->{RaiseError} = 1;
13
14
for my $i ( 1 .. 5 ) {
15
    Koha::Library->new(
16
{
17
        branchcode     => "test_br_$i",
18
        branchname     => "test_br_$i",
19
}
20
    )->store;
21
}
28
}
22
my $library = Koha::Libraries->search->next->unblessed;
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
});
23
45
24
my $plugin = Koha::Template::Plugin::Branches->new();
46
my $plugin = Koha::Template::Plugin::Branches->new();
25
ok($plugin, "initialized Branches plugin");
47
ok($plugin, "initialized Branches plugin");
Lines 35-54 is($name, '', 'received empty string as name of NULL/undefined library code'); Link Here
35
57
36
$library = $plugin->GetLoggedInBranchcode();
58
$library = $plugin->GetLoggedInBranchcode();
37
is($library, '', 'no active library if there is no active user session');
59
is($library, '', 'no active library if there is no active user session');
60
38
C4::Context->_new_userenv('DUMMY_SESSION_ID');
61
C4::Context->_new_userenv('DUMMY_SESSION_ID');
39
C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
62
C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
40
$library = $plugin->GetLoggedInBranchcode();
63
$library = $plugin->GetLoggedInBranchcode();
41
is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
64
is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
42
65
43
my $branches = $plugin->all;
66
C4::Context->set_preference( 'IndependentBranches', 0 );
44
my $test_branches = [ grep { $_->{branchcode} =~ m|^test_br_| } @$branches ];
67
my $libraries = $plugin->all();
45
is( scalar( @$test_branches ), 5, 'Plugin Branches should return the branches' );
68
ok( scalar(@$libraries) > 1, 'If IndependentBranches is not set, all libraries should be returned' );
46
my $selected_branches = [ grep { $_->{selected} } @$branches ];
69
is( grep ( { $_->{branchcode} eq 'MYLIBRARY'  and $_->{selected} == 1 } @$libraries ),       1, 'Without selected parameter, my library should be preselected' );
47
is( scalar( @$selected_branches ), 0, 'Plugin Branches should not select a branch if not needed' );
70
is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and not exists $_->{selected} } @$libraries ), 1, 'Without selected parameter, other library should not be preselected' );
48
71
$libraries = $plugin->all( { selected => 'ANOTHERLIB' } );
49
$branches = $plugin->all({selected => 'test_br_3'});
72
is( grep ( { $_->{branchcode} eq 'MYLIBRARY'  and not exists $_->{selected} } @$libraries ), 1, 'With selected parameter, my library should not be preselected' );
50
$test_branches = [ grep { $_->{branchcode} =~ m|^test_br_| } @$branches ];
73
is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and $_->{selected} == 1 } @$libraries ),       1, 'With selected parameter, other library should be preselected' );
51
is( scalar( @$test_branches ), 5, 'Plugin Branches should return the branches if selected passed' );
74
52
$selected_branches = [ grep { $_->{selected} } @$branches ];
75
C4::Context->set_preference( 'IndependentBranches', 1 );
53
is( scalar( @$selected_branches ), 1, 'Plugin Branches should return only 1 selected if passed' );
76
$libraries = $plugin->all();
54
is( $selected_branches->[0]->{branchcode}, 'test_br_3', 'Plugin Branches should select the good one' );
77
is( scalar(@$libraries), 1, 'If IndependentBranches is set, only 1 library should be returned' );
78
$libraries = $plugin->all( { unfiltered => 1 } );
79
ok( scalar(@$libraries) > 1, 'If IndependentBranches is set, all libraries should be returned if the unfiltered flag is set' );
55
- 

Return to bug 15758