3 # Copyright PTFS Europe 2020
5 # This file is part of Koha.
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
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.
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>.
20 use Test::More tests => 4;
22 use t::lib::TestBuilder;
29 use_ok('Koha::Template::Plugin::Registers');
32 my $schema = Koha::Database->schema;
33 my $builder = t::lib::TestBuilder->new;
35 subtest 'session_register_id' => sub {
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" );
48 C4::Context->_new_userenv(undef);
51 subtest 'session_register_name' => sub {
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" );
64 C4::Context->_new_userenv(undef);
67 subtest 'all() tests' => sub {
71 $schema->storage->txn_begin;
73 my $library1 = $builder->build_object(
75 class => 'Koha::Libraries'
78 my $register1 = $builder->build_object(
80 class => 'Koha::Cash::Registers',
82 branch => $library1->branchcode,
87 my $register2 = $builder->build_object(
89 class => 'Koha::Cash::Registers',
91 branch => $library1->branchcode,
97 my $library2 = $builder->build_object(
99 class => 'Koha::Libraries'
102 my $register3 = $builder->build_object(
104 class => 'Koha::Cash::Registers',
106 branch => $library2->branchcode
111 my $plugin = Koha::Template::Plugin::Registers->new();
112 ok( $plugin, "Plugin initialized" );
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)" );
122 $result = $plugin->all( { filters => { current_branch => 1 } } );
123 is( ref($result), 'ARRAY',
124 "Return arrayref (no userenv, filters: current_branch)" );
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)" );
137 $result = $plugin->all( { filters => { current_branch => 1 } } );
138 is( ref($result), 'ARRAY',
139 "Return arrayref (userenv: branchcode, filters: current_branch)" );
141 scalar( @{$result} ),
143 "Array contains 2 branch registers (userenv: branchcode, filters: current_branch)"
145 for my $register ( @{$result} ) {
146 is( $register->{selected}, 0,
147 "Register is not selected (userenv: branchcode, filters: current_branch)"
151 t::lib::Mocks::mock_userenv(
152 { branchcode => $library1->branchcode, register_id => $register2->id }
154 $result = $plugin->all( { filters => { current_branch => 1 } } );
155 is( ref($result), 'ARRAY',
156 "Return arrayref (userenv: branchcode + register_id, filters: current_branch)"
159 scalar( @{$result} ),
161 "Array contains 2 branch registers (userenv: branchcode + register_id, filters: current_branch)"
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)"
170 $schema->storage->txn_rollback;