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

(-)a/Koha/Template/Plugin/Registers.pm (-3 / +2 lines)
Lines 86-94 sub all { Link Here
86
86
87
    my $filters = $params->{filters} // {};
87
    my $filters = $params->{filters} // {};
88
    my $where;
88
    my $where;
89
    $where->{branch} =
89
    $where->{branch} = C4::Context->userenv->{'branch'}
90
      C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef
90
      if ( $filters->{current_branch} && C4::Context->userenv );
91
      if $filters->{current_branch};
92
    my $registers = Koha::Cash::Registers->search($where)->unblessed();
91
    my $registers = Koha::Cash::Registers->search($where)->unblessed();
93
    for my $register ( @{$registers} ) {
92
    for my $register ( @{$registers} ) {
94
        $register->{selected} = ( defined( $self->session_register_id )
93
        $register->{selected} = ( defined( $self->session_register_id )
(-)a/t/db_dependent/Koha/Template/Plugin/Registers.t (+173 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright PTFS Europe 2020
4
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, see <http://www.gnu.org/licenses>.
18
19
use Modern::Perl;
20
use Test::More tests => 4;
21
22
use t::lib::TestBuilder;
23
use t::lib::Mocks;
24
25
use C4::Context;
26
use Koha::Database;
27
28
BEGIN {
29
    use_ok('Koha::Template::Plugin::Registers');
30
}
31
32
my $schema  = Koha::Database->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
subtest 'session_register_id' => sub {
36
37
    plan tests => 3;
38
39
    my $plugin = Koha::Template::Plugin::Registers->new();
40
    ok( $plugin, "Plugin initialized" );
41
    is( $plugin->session_register_id,
42
        '', "Returns empty string if no userenv is set" );
43
    t::lib::Mocks::mock_userenv( { register_id => '1' } );
44
    is( $plugin->session_register_id,
45
        '1', "Returns the register id when set in the userenv" );
46
47
    # Unset the userenv
48
    C4::Context->_new_userenv(undef);
49
};
50
51
subtest 'session_register_name' => sub {
52
53
    plan tests => 3;
54
55
    my $plugin = Koha::Template::Plugin::Registers->new();
56
    ok( $plugin, "Plugin initialized" );
57
    is( $plugin->session_register_name,
58
        '', "Returns empty string if no userenv is set" );
59
    t::lib::Mocks::mock_userenv( { register_name => 'Register One' } );
60
    is( $plugin->session_register_name,
61
        'Register One', "Returns the register name when set in the userenv" );
62
63
    # Unset the userenv
64
    C4::Context->_new_userenv(undef);
65
};
66
67
subtest 'all() tests' => sub {
68
69
    plan tests => 20;
70
71
    $schema->storage->txn_begin;
72
73
    my $library1 = $builder->build_object(
74
        {
75
            class => 'Koha::Libraries'
76
        }
77
    );
78
    my $register1 = $builder->build_object(
79
        {
80
            class => 'Koha::Cash::Registers',
81
            value => {
82
                branch         => $library1->branchcode,
83
                branch_default => 0
84
            }
85
        }
86
    );
87
    my $register2 = $builder->build_object(
88
        {
89
            class => 'Koha::Cash::Registers',
90
            value => {
91
                branch         => $library1->branchcode,
92
                branch_default => 1
93
            }
94
        }
95
    );
96
97
    my $library2 = $builder->build_object(
98
        {
99
            class => 'Koha::Libraries'
100
        }
101
    );
102
    my $register3 = $builder->build_object(
103
        {
104
            class => 'Koha::Cash::Registers',
105
            value => {
106
                branch => $library2->branchcode
107
            }
108
        }
109
    );
110
111
    my $plugin = Koha::Template::Plugin::Registers->new();
112
    ok( $plugin, "Plugin initialized" );
113
114
    my $result = $plugin->all;
115
    is( ref($result), 'ARRAY', "Return arrayref (no userenv, no filters)" );
116
    is( scalar( @{$result} ),
117
        3, "Array contains all 3 registers (no userenv, no filters)" );
118
    for my $register ( @{$result} ) {
119
        is( $register->{selected}, 0, "Register is not selected (no userenv)" );
120
    }
121
122
    $result = $plugin->all( { filters => { current_branch => 1 } } );
123
    is( ref($result), 'ARRAY',
124
        "Return arrayref (no userenv, filters: current_branch)" );
125
126
    t::lib::Mocks::mock_userenv( { branchcode => $library1->branchcode } );
127
    $result = $plugin->all;
128
    is( ref($result), 'ARRAY',
129
        "Return arrayref (userenv: branchcode, no filters)" );
130
    is( scalar( @{$result} ),
131
        3, "Array contains all 3 registers (userenv: branchcode, no filters)" );
132
    for my $register ( @{$result} ) {
133
        is( $register->{selected}, 0,
134
            "Register is not selected (userenv: branchcode, no filters)" );
135
    }
136
137
    $result = $plugin->all( { filters => { current_branch => 1 } } );
138
    is( ref($result), 'ARRAY',
139
        "Return arrayref (userenv: branchcode, filters: current_branch)" );
140
    is(
141
        scalar( @{$result} ),
142
        2,
143
"Array contains 2 branch registers (userenv: branchcode, filters: current_branch)"
144
    );
145
    for my $register ( @{$result} ) {
146
        is( $register->{selected}, 0,
147
"Register is not selected (userenv: branchcode, filters: current_branch)"
148
        );
149
    }
150
151
    t::lib::Mocks::mock_userenv(
152
        { branchcode => $library1->branchcode, register_id => $register2->id }
153
    );
154
    $result = $plugin->all( { filters => { current_branch => 1 } } );
155
    is( ref($result), 'ARRAY',
156
"Return arrayref (userenv: branchcode + register_id, filters: current_branch)"
157
    );
158
    is(
159
        scalar( @{$result} ),
160
        2,
161
"Array contains 2 branch registers (userenv: branchcode + register_id, filters: current_branch)"
162
    );
163
    for my $register ( @{$result} ) {
164
        my $selected = ( $register->{id} == $register2->id ) ? 1 : 0;
165
        is( $register->{selected}, $selected,
166
"Register is selected $selected (userenv: brancode, filters: current_branch)"
167
        );
168
    }
169
170
    $schema->storage->txn_rollback;
171
};
172
173
1;
(-)a/t/lib/Mocks.pm (-2 / +10 lines)
Lines 60-68 sub mock_userenv { Link Here
60
    my $branchname   = $params->{branchname}   || $userenv->{branchname};
60
    my $branchname   = $params->{branchname}   || $userenv->{branchname};
61
    my $flags        = $params->{flags}        || $userenv->{flags}          || 0;
61
    my $flags        = $params->{flags}        || $userenv->{flags}          || 0;
62
    my $emailaddress = $params->{emailaddress} || $userenv->{emailaddress};
62
    my $emailaddress = $params->{emailaddress} || $userenv->{emailaddress};
63
    my $desk_id       = $params->{desk_id}       || $userenv->{desk_id};
64
    my $desk_name     = $params->{desk_name}     || $userenv->{desk_name};
65
    my $register_id   = $params->{register_id}   || $userenv->{register_id};
66
    my $register_name = $params->{register_name} || $userenv->{register_name};
63
    my ( $shibboleth );
67
    my ( $shibboleth );
64
68
65
    C4::Context->set_userenv($usernum, $userid, $cardnumber, $firstname, $surname, $branchcode, $branchname, $flags, $emailaddress, $shibboleth );
69
    C4::Context->set_userenv(
70
        $usernum,      $userid,     $cardnumber, $firstname,
71
        $surname,      $branchcode, $branchname, $flags,
72
        $emailaddress, $shibboleth, $desk_id,    $desk_name,
73
        $register_id,  $register_name
74
    );
66
}
75
}
67
76
68
1;
77
1;
69
- 

Return to bug 24786