Lines 16-64
Link Here
|
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
|
|
19 |
use Data::Dumper qw( Dumper ); |
19 |
use Test::More tests => 1; |
20 |
use Test::More tests => 1; |
20 |
|
21 |
|
21 |
use Koha::AllowList; |
22 |
use Koha::AllowList; |
22 |
|
23 |
|
23 |
subtest 'AllowList' => sub { |
24 |
subtest 'AllowList' => sub { |
24 |
plan tests => 13; |
25 |
plan tests => 17; |
25 |
|
26 |
|
26 |
my $allowlist = Koha::AllowList->new; |
27 |
my $defaults = [ 'col1' ]; |
27 |
$allowlist->load; |
28 |
my $allowlist = Koha::AllowList->new({ defaults => $defaults }); |
28 |
is( $allowlist->apply, undef, 'No input returns undef' ); |
29 |
is( $allowlist->apply, undef, 'No input returns undef' ); |
29 |
is( %{$allowlist->apply({ input => {} })}, 0, 'Empty hash returns nothing blocked' ); |
30 |
is( %{$allowlist->apply({ input => {} })}, 0, 'Empty hash returns empty hash' ); |
30 |
|
31 |
|
31 |
# Test return from apply on hashrefs |
32 |
# Test apply on hashrefs |
32 |
my @input = ( col1 => 1, col2 => 2, col3 => 3, col4 => 4 ); |
33 |
my $input = { col1 => 1, col2 => 2, col3 => 3, col4 => 4 }; |
33 |
my $blocked = $allowlist->apply({ input => { @input } }); |
34 |
$allowlist->remove('col1'); # results in empty allow list |
|
|
35 |
my $blocked = {}; |
36 |
$allowlist->apply({ input => $input, dump => $blocked }); |
34 |
is( keys %$blocked, 4, 'Empty list blocks all' ); |
37 |
is( keys %$blocked, 4, 'Empty list blocks all' ); |
35 |
$allowlist->add( 'col1' ); |
38 |
$allowlist->reset; # Back to col1 |
36 |
$blocked = $allowlist->apply({ input => { @input } }); |
39 |
$blocked = {}; |
|
|
40 |
$allowlist->apply({ input => $input, dump => $blocked }); |
37 |
is( keys %$blocked, 3, 'One field allowed' ); |
41 |
is( keys %$blocked, 3, 'One field allowed' ); |
38 |
is( $blocked->{col1}, undef, 'And that is col1' ); |
42 |
is( $blocked->{col1}, undef, 'And that is col1' ); |
39 |
$allowlist->remove( 'col1' ); |
|
|
40 |
$blocked = $allowlist->apply({ input => { @input } }); |
41 |
is( keys %$blocked, 4, 'Back to 4 after removing col1' ); |
42 |
$allowlist->add( 'col2' ); |
43 |
$allowlist->add( 'col2' ); |
43 |
$allowlist->unload; |
44 |
$blocked = {}; |
44 |
$blocked = $allowlist->apply({ input => { @input } }); |
45 |
my $result = $allowlist->apply({ input => $input, dump => $blocked }); |
45 |
is( keys %$blocked, 4, 'Same after unloading' ); |
46 |
is( keys %$blocked, 2, 'Two fields blocked' ); |
|
|
47 |
is( keys %$result, 2, 'Two fields allowed' ); |
48 |
is( $result->{col2}, 2, 'Check a result' ); |
49 |
$allowlist->reset; # Undo col2 |
50 |
$blocked = {}; |
51 |
$allowlist->apply({ input => $input, dump => $blocked }); |
52 |
is( keys %$blocked, 3, 'Back to three blocked' ); |
46 |
|
53 |
|
47 |
# Test dry run |
54 |
# Test check method |
48 |
my $input2 = { col1 => 1, col2 => 2, col3 => 3, col4 => 4 }; |
55 |
$blocked = {}; |
|
|
56 |
$input = { col1 => 1, col2 => 2, col3 => 3, col4 => 4 }; |
49 |
$allowlist->add( 'col1', 'col3' ); |
57 |
$allowlist->add( 'col1', 'col3' ); |
50 |
$blocked = $allowlist->check({ input => $input2 }); |
58 |
is( $allowlist->check({ input => $input, dump => $blocked }), q{}, 'Check returns false' ); |
51 |
is( keys %$blocked, 2, 'Expected two entries' ); |
59 |
is( keys %$blocked, 2, 'Dumped 2 entries' ); |
52 |
is( keys %$input2, 4, 'Expected no change in input2' ); |
60 |
$allowlist->add( 'col2', 'col4' ); |
53 |
$blocked = $allowlist->apply({ input => $input2 }); |
61 |
is( $allowlist->check({ input => $input }), 1, 'Check returns true' ); |
54 |
is( keys %$input2, 2, 'Apply removed entries now' ); |
|
|
55 |
|
62 |
|
56 |
# Test array |
63 |
# Test on arrays |
57 |
@input = ( 'col1', 'col2', 'col3', 'col4', 'col5' ); |
64 |
$allowlist->remove( 'col2', 'col3' ); |
58 |
$blocked = $allowlist->check({ input => [ @input ] }); |
65 |
$input = [ 'col1', 'col2', 'col3', 'col4', 'col5' ]; |
|
|
66 |
$blocked = {}; |
67 |
$result = $allowlist->apply({ input => $input, dump => $blocked }); |
68 |
is( @$result, 2, 'Expected 2 allowed entries' ); |
69 |
is( ( grep { $_ eq 'col1' } @$result ), 1, 'col1 allowed' ); |
59 |
is( keys %$blocked, 3, 'Expected three blocked entries' ); |
70 |
is( keys %$blocked, 3, 'Expected three blocked entries' ); |
60 |
$input2 = [ @input ]; |
71 |
is( $blocked->{col2}, 1, 'col2 blocked' ); |
61 |
$blocked = $allowlist->apply({ input => $input2 }); |
72 |
|
62 |
is( keys %$blocked, 3, 'Expected three blocked entries again' ); |
73 |
# Test keys method |
63 |
is( @$input2, 2, 'Expect two entries now in input2' ); |
74 |
is( join(',', $allowlist->keys), 'col1,col4', 'List current entries' ); |
64 |
}; |
75 |
}; |
65 |
- |
|
|