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

(-)a/C4/Auth.pm (-1 / +1 lines)
Lines 260-266 sub get_template_and_user { Link Here
260
            $template->param(ShowOpacRecentSearchLink => 1);
260
            $template->param(ShowOpacRecentSearchLink => 1);
261
            }
261
            }
262
262
263
            # And if there's a cookie with searches performed when the user was not logged in,
263
            # And if there are searches performed when the user was not logged in,
264
            # we add them to the logged-in search history
264
            # we add them to the logged-in search history
265
            my @recentSearches = ParseSearchHistorySession($in->{'query'});
265
            my @recentSearches = ParseSearchHistorySession($in->{'query'});
266
            if (@recentSearches) {
266
            if (@recentSearches) {
(-)a/opac/opac-search-history.pl (-4 / +3 lines)
Lines 44-61 my ($template, $loggedinuser, $cookie)= get_template_and_user({template_name => Link Here
44
                                debug => 1,
44
                                debug => 1,
45
                                });
45
                                });
46
46
47
# If the user is not logged in, we deal with the cookie
47
# If the user is not logged in, we deal with the session
48
if (!$loggedinuser) {
48
if (!$loggedinuser) {
49
49
50
    # Deleting search history
50
    # Deleting search history
51
    if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
51
    if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
52
        # Deleting cookie's content
52
        # Deleting session's search history
53
        SetSearchHistorySession($cgi, []);
53
        SetSearchHistorySession($cgi, []);
54
54
55
        # Redirecting to this same url with the cookie in the headers so it's deleted immediately
55
        # Redirecting to this same url so the user won't see the search history link in the header
56
        my $uri = $cgi->url();
56
        my $uri = $cgi->url();
57
        print $cgi->redirect(-uri => $uri);
57
        print $cgi->redirect(-uri => $uri);
58
59
    # Showing search history
58
    # Showing search history
60
    } else {
59
    } else {
61
60
(-)a/opac/opac-search.pl (-3 / +1 lines)
Lines 614-620 for (my $i=0;$i<@servers;$i++) { Link Here
614
        }
614
        }
615
615
616
        # Opac search history
616
        # Opac search history
617
        my $newsearchcookie;
618
        if (C4::Context->preference('EnableOpacSearchHistory')) {
617
        if (C4::Context->preference('EnableOpacSearchHistory')) {
619
            my @recentSearches = ParseSearchHistorySession($cgi);
618
            my @recentSearches = ParseSearchHistorySession($cgi);
620
619
Lines 638-648 for (my $i=0;$i<@servers;$i++) { Link Here
638
                }
637
                }
639
638
640
                shift @recentSearches if (@recentSearches > 15);
639
                shift @recentSearches if (@recentSearches > 15);
641
                # Pushing the cookie back
642
                SetSearchHistorySession($cgi, \@recentSearches);
640
                SetSearchHistorySession($cgi, \@recentSearches);
643
            }
641
            }
644
            else {
642
            else {
645
                # To the session (the user is logged in)
643
                # To the database (the user is logged in)
646
                if (!$offset) {
644
                if (!$offset) {
647
                    AddSearchHistory($borrowernumber, $cgi->cookie("CGISESSID"), $query_desc_history, $query_cgi_history, $total);
645
                    AddSearchHistory($borrowernumber, $cgi->cookie("CGISESSID"), $query_desc_history, $query_cgi_history, $total);
648
                    $template->param(ShowOpacRecentSearchLink => 1);
646
                    $template->param(ShowOpacRecentSearchLink => 1);
(-)a/t/db_dependent/Auth_ParseSearchHistoryCookie.t (-43 lines)
Lines 1-43 Link Here
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;
(-)a/t/db_dependent/Auth_SearchHistorySession.t (-1 / +52 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use strict;
4
use warnings;
5
6
use Test::More tests => 4;
7
8
use_ok('C4::Auth', qw/ParseSearchHistorySession SetSearchHistorySession get_session/);
9
10
my $expected_recent_searches = [
11
    {
12
        'time' => 1374978877,
13
        'query_cgi' => 'idx=&q=history&branch_group_limit=',
14
        'total' => 2,
15
        'query_desc' => 'kw,wrdl: history, '
16
    }
17
];
18
19
# Create new session and put its id into CGISESSID cookie
20
my $session = get_session("");
21
$session->flush;
22
my $input = new CookieSimulator({CGISESSID => $session->id});
23
24
my @recent_searches = ParseSearchHistorySession($input);
25
is_deeply(\@recent_searches, [], 'at start, there is no recent searches');
26
27
SetSearchHistorySession($input, $expected_recent_searches);
28
@recent_searches = ParseSearchHistorySession($input);
29
is_deeply(\@recent_searches, $expected_recent_searches, 'recent searches set and retrieved successfully');
30
31
SetSearchHistorySession($input, []);
32
@recent_searches = ParseSearchHistorySession($input);
33
is_deeply(\@recent_searches, [], 'recent searches emptied successfully');
34
35
# Delete session
36
$session->delete;
37
$session->flush;
38
39
package CookieSimulator;
40
41
sub new {
42
    my ($class, $hashref) = @_;
43
    my $val = $hashref;
44
    return bless $val, $class;
45
}
46
47
sub cookie {
48
    my ($self, $name) = @_;
49
    return $self->{$name};
50
}
51
52
1;

Return to bug 10952