Lines 19-27
Link Here
|
19 |
|
19 |
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
use CGI; |
21 |
use CGI; |
22 |
use Data::Dumper qw(Dumper); |
22 |
|
|
|
23 |
#use Data::Dumper qw(Dumper); |
23 |
use Test::NoWarnings; |
24 |
use Test::NoWarnings; |
24 |
use Test::More tests => 4; |
25 |
use Test::More tests => 5; |
25 |
|
26 |
|
26 |
use t::lib::Mocks; |
27 |
use t::lib::Mocks; |
27 |
|
28 |
|
Lines 29-57
use C4::Context;
Link Here
|
29 |
use Koha::CookieManager; |
30 |
use Koha::CookieManager; |
30 |
|
31 |
|
31 |
subtest 'new' => sub { |
32 |
subtest 'new' => sub { |
32 |
plan tests => 3; |
33 |
plan tests => 4; |
33 |
|
34 |
|
34 |
t::lib::Mocks::mock_config( Koha::CookieManager::DENY_LIST_VAR, 'just_one' ); |
35 |
t::lib::Mocks::mock_config( Koha::CookieManager::KEEP_COOKIE_CONF_VAR, 'just_one' ); |
35 |
my $cmgr = Koha::CookieManager->new; |
36 |
my $cmgr = Koha::CookieManager->new; |
36 |
is( scalar keys %{ $cmgr->{_remove_unless} }, 1, 'one entry' ); |
37 |
is( scalar @{ $cmgr->{_keep_list} }, 1, 'one entry to keep' ); |
37 |
is( exists $cmgr->{_secure}, 1, 'secure key found' ); |
38 |
is( exists $cmgr->{_secure}, 1, 'secure key found' ); |
38 |
|
39 |
|
39 |
t::lib::Mocks::mock_config( Koha::CookieManager::DENY_LIST_VAR, [ 'two', 'entries' ] ); |
40 |
t::lib::Mocks::mock_config( Koha::CookieManager::KEEP_COOKIE_CONF_VAR, [ 'two', 'entries' ] ); |
|
|
41 |
t::lib::Mocks::mock_config( Koha::CookieManager::REMOVE_COOKIE_CONF_VAR, ['test'] ); |
40 |
$cmgr = Koha::CookieManager->new; |
42 |
$cmgr = Koha::CookieManager->new; |
41 |
is( scalar keys %{ $cmgr->{_remove_unless} }, 2, 'two entries' ); |
43 |
is( scalar @{ $cmgr->{_keep_list} }, 2, 'two entries to keep' ); |
|
|
44 |
is( scalar @{ $cmgr->{_remove_list} }, 1, 'one entry to remove' ); |
42 |
}; |
45 |
}; |
43 |
|
46 |
|
44 |
subtest 'clear_unless' => sub { |
47 |
subtest 'clear_unless' => sub { |
45 |
plan tests => 17; |
48 |
plan tests => 14; |
46 |
|
49 |
|
47 |
t::lib::Mocks::mock_config( Koha::CookieManager::DENY_LIST_VAR, [ 'aap', 'noot' ] ); |
50 |
t::lib::Mocks::mock_config( Koha::CookieManager::KEEP_COOKIE_CONF_VAR, [ 'aap', 'noot' ] ); |
|
|
51 |
t::lib::Mocks::mock_config( Koha::CookieManager::REMOVE_COOKIE_CONF_VAR, ['mies'] ); |
48 |
|
52 |
|
49 |
my $q = CGI->new; |
53 |
my $q = CGI->new; |
50 |
my $cmgr = Koha::CookieManager->new; |
54 |
my $cmgr = Koha::CookieManager->new; |
51 |
|
55 |
|
52 |
my $cookie1 = $q->cookie( -name => 'aap', -value => 'aap', -expires => '+1d' ); |
56 |
my $cookie1 = $q->cookie( -name => 'aap', -value => 'aap', -expires => '+1d' ); |
53 |
my $cookie2 = $q->cookie( -name => 'noot', -value => 'noot' ); |
57 |
my $cookie2 = $q->cookie( -name => 'noot', -value => 'noot' ); |
54 |
my $cookie3 = $q->cookie( -name => 'wim', -value => q{wim}, -HttpOnly => 1 ); |
58 |
my $cookie3 = $q->cookie( -name => 'wim', -value => q{wim}, -HttpOnly => 0 ); |
55 |
my $cookie4 = $q->cookie( -name => 'aap', -value => q{aap2}, -HttpOnly => 1 ); |
59 |
my $cookie4 = $q->cookie( -name => 'aap', -value => q{aap2}, -HttpOnly => 1 ); |
56 |
my $list = [ $cookie1, $cookie2, $cookie3, $cookie4, 'mies', 'zus' ]; # 4 cookies, 2 names |
60 |
my $list = [ $cookie1, $cookie2, $cookie3, $cookie4, 'mies', 'zus' ]; # 4 cookies, 2 names |
57 |
|
61 |
|
Lines 59-112
subtest 'clear_unless' => sub {
Link Here
|
59 |
is( @{ $cmgr->clear_unless }, 0, 'Empty list' ); |
63 |
is( @{ $cmgr->clear_unless }, 0, 'Empty list' ); |
60 |
is( @{ $cmgr->clear_unless( { hash => 1 }, ['array'], $q ) }, 0, 'Empty list for invalid arguments' ); |
64 |
is( @{ $cmgr->clear_unless( { hash => 1 }, ['array'], $q ) }, 0, 'Empty list for invalid arguments' ); |
61 |
|
65 |
|
62 |
# Pass list, expect 5 cookies (3 cleared, last aap kept) |
66 |
# Pass list, expecting 4 cookies (2 kept, 1 untouched, 1 cleared); duplicate aap and zus discarded |
63 |
my @rv = @{ $cmgr->clear_unless(@$list) }; |
67 |
my @rv = @{ $cmgr->clear_unless(@$list) }; |
64 |
is( @rv, 5, '5 expected' ); |
68 |
is( @rv, 4, '4 expected' ); |
65 |
is( $rv[0]->name, 'noot', '1st cookie' ); |
69 |
is( $rv[0]->name, 'noot', '1st cookie' ); |
66 |
is( $rv[1]->name, 'wim', '2nd cookie' ); |
70 |
is( $rv[1]->name, 'wim', '2nd cookie' ); |
67 |
is( $rv[2]->name, 'aap', '3rd cookie' ); |
71 |
is( $rv[2]->name, 'aap', '3rd cookie' ); |
68 |
is( $rv[3]->name, 'mies', '4th cookie' ); |
72 |
is( $rv[3]->name, 'mies', '4th cookie' ); |
69 |
is( $rv[4]->name, 'zus', '5th cookie' ); |
73 |
is( $rv[0]->value, q{noot}, 'noot kept' ); |
70 |
is( $rv[0]->value, q{noot}, 'noot not empty' ); |
74 |
is( $rv[1]->value, q{wim}, 'wim untouched' ); |
71 |
is( $rv[1]->value, q{}, 'wim empty' ); |
75 |
is( $rv[2]->value, q{aap2}, 'aap kept, last entry' ); |
72 |
is( $rv[2]->value, q{aap2}, 'aap not empty' ); |
76 |
is( $rv[3]->value, q{}, 'mies cleared' ); |
73 |
is( $rv[3]->value, q{}, 'mies empty' ); |
77 |
is( $rv[1]->httponly, undef, 'wim still not httponly' ); |
74 |
is( $rv[4]->value, q{}, 'zus empty' ); |
|
|
75 |
is( $rv[1]->httponly, 0, 'cleared wim is not httponly' ); |
76 |
is( $rv[2]->httponly, 1, 'aap httponly' ); |
78 |
is( $rv[2]->httponly, 1, 'aap httponly' ); |
77 |
|
79 |
|
78 |
# Test with numeric suffix (via regex) |
80 |
# Test with prefix (note trailing underscore) |
79 |
t::lib::Mocks::mock_config( Koha::CookieManager::DENY_LIST_VAR, ['catalogue_editor_\d+'] ); |
81 |
t::lib::Mocks::mock_config( Koha::CookieManager::KEEP_COOKIE_CONF_VAR, 'catalogue_editor_' ); |
|
|
82 |
t::lib::Mocks::mock_config( Koha::CookieManager::REMOVE_COOKIE_CONF_VAR, 'catalogue_editor' ); |
80 |
$cmgr = Koha::CookieManager->new; |
83 |
$cmgr = Koha::CookieManager->new; |
81 |
$cookie1 = $q->cookie( -name => 'catalogue_editor_abc', -value => '1', -expires => '+1y' ); |
84 |
$cookie1 = $q->cookie( -name => 'catalogue_editor', -value => '1' ); |
82 |
$cookie2 = $q->cookie( -name => 'catalogue_editor_345', -value => '1', -expires => '+1y' ); |
85 |
$cookie2 = $q->cookie( -name => 'catalogue_editor2', -value => '2' ); |
83 |
$cookie3 = $q->cookie( -name => 'catalogue_editor_', -value => '1', -expires => '+1y' ); |
86 |
$cookie3 = $q->cookie( -name => 'catalogue_editor_3', -value => '3' ); |
84 |
$cookie4 = $q->cookie( -name => 'catalogue_editor_123x', -value => '1', -expires => '+1y' ); |
87 |
|
85 |
|
88 |
$list = [ $cookie1, $cookie2, $cookie3, 'catalogue_editor4' ]; |
86 |
$list = [ $cookie1, $cookie2, $cookie3, $cookie4 ]; |
89 |
my $result = [ map { defined( $_->max_age ) ? () : $_->name } @{ $cmgr->clear_unless(@$list) } ]; |
87 |
@rv = @{ $cmgr->clear_unless(@$list) }; |
90 |
is_deeply( $result, ['catalogue_editor_3'], 'Only cookie3 is kept (not expired)' ); |
88 |
is_deeply( |
91 |
}; |
89 |
[ map { $_->value ? $_->name : () } @rv ], |
|
|
90 |
['catalogue_editor_345'], |
91 |
'Cookie2 should be found only' |
92 |
); |
93 |
|
94 |
# Test with another regex (yes, highly realistic examples :) |
95 |
t::lib::Mocks::mock_config( Koha::CookieManager::DENY_LIST_VAR, ['next_\w+_number\d{2}_(now|never)'] ); |
96 |
$cmgr = Koha::CookieManager->new; |
97 |
my $cookie5; |
98 |
$cookie1 = $q->cookie( -name => 'next_mynewword_number99_never', -value => '1', -expires => '+1y' ); #fine |
99 |
$cookie2 = $q->cookie( -name => 'prefixed_next_mynewword_number99_never', -value => '1', -expires => '+1y' ) |
100 |
; # wrong prefix |
101 |
$cookie3 = $q->cookie( -name => 'next_mynew-word_number99_never', -value => '1', -expires => '+1y' ) |
102 |
; # wrong: hyphen in word |
103 |
$cookie4 = |
104 |
$q->cookie( -name => 'mynewword_number999_never', -value => '1', -expires => '+1y' ); # wrong: three digits |
105 |
$cookie5 = |
106 |
$q->cookie( -name => 'next_mynewword_number99_always', -value => '1', -expires => '+1y' ); # wrong: always |
107 |
@rv = @{ $cmgr->clear_unless( $cookie1, $cookie2, $cookie3, $cookie4, $cookie5 ) }; |
108 |
is_deeply( [ map { $_->value ? $_->name : () } @rv ], ['next_mynewword_number99_never'], 'Only cookie1 matched' ); |
109 |
|
92 |
|
|
|
93 |
subtest 'path exception' => sub { |
94 |
plan tests => 4; |
95 |
|
96 |
t::lib::Mocks::mock_config( Koha::CookieManager::REMOVE_COOKIE_CONF_VAR, ['always_show_holds'] ); |
97 |
my $q = CGI->new; |
98 |
my $cmgr = Koha::CookieManager->new; |
99 |
my $cookie1 = $q->cookie( -name => 'always_show_holds', -value => 'DO', path => '/cgi-bin/koha/reserve' ); |
100 |
my @rv = @{ $cmgr->clear_unless($cookie1) }; |
101 |
is( $rv[0]->name, 'always_show_holds', 'Check name' ); |
102 |
is( $rv[0]->path, '/cgi-bin/koha/reserve', 'Check path' ); |
103 |
is( $rv[0]->max_age, 0, 'Check max_age' ); |
104 |
my $cookie2 = $q->cookie( -name => 'always_show_holds', -value => 'DONT' ); # default path |
105 |
@rv = @{ $cmgr->clear_unless($cookie2) }; |
106 |
is( $rv[0]->path, '/cgi-bin/koha/reserve', 'Check path cookie2, corrected here' ); |
110 |
}; |
107 |
}; |
111 |
|
108 |
|
112 |
subtest 'replace_in_list' => sub { |
109 |
subtest 'replace_in_list' => sub { |
113 |
- |
|
|