do some validation of the KohaOpacRecentSearches cookie
[koha.git] / t / Auth_ParseSearchHistoryCookie.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 3;
7
8 use_ok('C4::Auth', qw/ParseSearchHistoryCookie/);
9
10 my $valid_cookie = "%5B%7B%22time%22%3A1374978877%2C%22query_cgi%22%3A%22idx%3D%26q%3Dhistory%26branch_group_limit%3D%22%2C%22total%22%3A2%2C%22query_desc%22%3A%22kw%2Cwrdl%3A%20history%2C%20%22%7D%5D";
11 my $expected_recent_searches = [
12     {
13         'time' => 1374978877,
14         'query_cgi' => 'idx=&q=history&branch_group_limit=',
15         'total' => 2,
16         'query_desc' => 'kw,wrdl: history, '
17     }
18 ];
19
20 my $input = CookieSimulator->new($valid_cookie);
21 my @recent_searches = ParseSearchHistoryCookie($input);
22 is_deeply(\@recent_searches, $expected_recent_searches, 'parsed valid search history cookie value');
23
24 # simulate bit of a Storable-based search history cookie
25 my $invalid_cookie = "%04%08%0812345";
26 $input = CookieSimulator->new($invalid_cookie);
27 @recent_searches = ParseSearchHistoryCookie($input);
28 is_deeply(\@recent_searches, [], 'got back empty search history list if given invalid cookie');
29
30 package CookieSimulator;
31
32 sub new {
33     my ($class, $str) = @_;
34     my $val = [ $str ];
35     return bless $val, $class;
36 }
37
38 sub cookie {
39     my $self = shift;
40     return $self->[0];
41 }
42
43 1;