3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use List
::MoreUtils qw
/all any none/;
23 use Test
::More tests
=> 2;
25 use t
::lib
::TestBuilder
;
28 use Koha
::AuthUtils qw
/hash_password/;
33 my $schema = Koha
::Database
->schema;
34 $schema->storage->txn_begin;
35 my $builder = t
::lib
::TestBuilder
->new;
38 my $hash = hash_password
('password');
39 my $patron = $builder->build( { source
=> 'Borrower' } );
40 Koha
::Patrons
->find( $patron->{borrowernumber
} )->update_password( $patron->{userid
}, $hash );
42 my $session = C4
::Auth
::get_session
("");
45 sub myMockedget_from_session
{
46 my $expected_recent_searches = [
49 'query_cgi' => 'cgi_test',
51 'query_desc' => 'kw,wrdl: history, '
54 return @
{$expected_recent_searches};
58 my $getfrom = new Test
::MockModule
( 'C4::Search::History' );
59 $getfrom->mock( 'get_from_session', \
&myMockedget_from_session
);
61 my $cgi = new Test
::MockModule
( 'CGI');
62 $cgi->mock('cookie', sub {
63 my ($self, $key) = @_;
64 if (!ref($key) && $key eq 'CGISESSID'){
70 my ($query,$authnotrequired,$flagsrequired,$type) = @_;
71 my $userid = $patron->{userid
};
74 superlibrarian
=> 1, acquisition
=> 0,
76 catalogue
=> 1, circulate
=> 0,
77 coursereserves
=> 0, editauthorities
=> 0,
78 editcatalogue
=> 0, management
=> 0,
79 parameters
=> 0, permissions
=> 0,
80 plugins
=> 0, reports
=> 0,
81 reserveforothers
=> 0, serials
=> 0,
82 staffaccess
=> 0, tools
=> 0,
86 my $session_cookie = $query->cookie(
88 -value
=> '9884013ae2c441d12e0bc9376242d2a8',
91 return ( $userid, $session_cookie, $sessionID, $flags );
95 my $auth = new Test
::MockModule
( 'C4::Auth' );
96 $auth->mock( 'checkauth', \
&MockedCheckauth
);
98 $query->param('koha_login_context', 'opac');
99 $query->param('userid', $patron->{userid
});
100 $query->param('password', 'password');
102 # Test when the syspref is disabled
103 t
::lib
::Mocks
::mock_preference
('addSearchHistoryToTheFirstLoggedUser', 0);
104 my $result = $schema->resultset('SearchHistory')->search()->count;
106 my ( $template, $loggedinuser, $cookies ) = get_template_and_user
(
108 template_name
=> "opac-user.tt",
111 authnotrequired
=> 0,
116 my $result2 = $schema->resultset('SearchHistory')->search()->count;
117 is
($result2, $result, 'no new search added to borrower');
119 # Test when the syspref is enabled
120 t
::lib
::Mocks
::mock_preference
('addSearchHistoryToTheFirstLoggedUser', 1);
121 $query->param('koha_login_context', 'opac');
122 $query->param('userid', $patron->{userid
});
123 $query->param('password', 'password');
125 -name
=> 'CGISESSID',
126 -value
=> $session->id,
130 $result = $schema->resultset('SearchHistory')->search()->count;
132 ( $template, $loggedinuser, $cookies ) = get_template_and_user
(
134 template_name
=> "opac-user.tt",
137 authnotrequired
=> 0,
142 $result2 = $schema->resultset('SearchHistory')->search()->count;
143 is
($result2, $result+1, 'new search added to borrower');
146 $result = $schema->resultset('SearchHistory')->search(undef, { query_cgi
=> 'cgi_test'});
147 $result->delete_all();