|
Lines 2-8
Link Here
|
| 2 |
|
2 |
|
| 3 |
use Modern::Perl; |
3 |
use Modern::Perl; |
| 4 |
|
4 |
|
| 5 |
use Test::More tests => 26; |
5 |
use CGI qw ( -utf8 ); |
|
|
6 |
use Test::MockModule; |
| 7 |
use List::MoreUtils qw/all any none/; |
| 8 |
use t::lib::Mocks; |
| 9 |
use t::lib::TestBuilder; |
| 10 |
|
| 11 |
use C4::Auth; |
| 12 |
use Koha::AuthUtils qw/hash_password/; |
| 13 |
use Koha::Database; |
| 14 |
|
| 15 |
use Test::More tests => 27; |
| 6 |
use Test::Warn; |
16 |
use Test::Warn; |
| 7 |
use URI::Escape; |
17 |
use URI::Escape; |
| 8 |
use List::Util qw( shuffle ); |
18 |
use List::Util qw( shuffle ); |
|
Lines 345-350
sub delete_all {
Link Here
|
| 345 |
}); |
355 |
}); |
| 346 |
} |
356 |
} |
| 347 |
|
357 |
|
|
|
358 |
subtest 'LoadSearchHistoryToTheFirstLoggedUser working' => sub { |
| 359 |
plan tests =>2; |
| 360 |
|
| 361 |
my $query = new CGI; |
| 362 |
|
| 363 |
my $schema = Koha::Database->schema; |
| 364 |
my $builder = t::lib::TestBuilder->new; |
| 365 |
|
| 366 |
# Borrower Creation |
| 367 |
my $hash = hash_password('password'); |
| 368 |
our $patron = $builder->build( { source => 'Borrower' } ); |
| 369 |
Koha::Patrons->find( $patron->{borrowernumber} )->update_password( $patron->{userid}, $hash ); |
| 370 |
|
| 371 |
my $session = C4::Auth::get_session(""); |
| 372 |
$session->flush; |
| 373 |
|
| 374 |
sub myMockedget_from_session { |
| 375 |
my $expected_recent_searches = [ |
| 376 |
{ |
| 377 |
'time' => 1374978877, |
| 378 |
'query_cgi' => 'cgi_test', |
| 379 |
'total' => 2, |
| 380 |
'query_desc' => 'kw,wrdl: history, ' |
| 381 |
} |
| 382 |
]; |
| 383 |
return @{$expected_recent_searches}; |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
my $getfrom = new Test::MockModule( 'C4::Search::History' ); |
| 388 |
$getfrom->mock( 'get_from_session', \&myMockedget_from_session ); |
| 389 |
|
| 390 |
my $cgi = new Test::MockModule( 'CGI'); |
| 391 |
$cgi->mock('cookie', sub { |
| 392 |
my ($self, $key) = @_; |
| 393 |
if (!ref($key) && $key eq 'CGISESSID'){ |
| 394 |
return 'ID'; |
| 395 |
} |
| 396 |
}); |
| 397 |
|
| 398 |
sub MockedCheckauth { |
| 399 |
my ($query,$authnotrequired,$flagsrequired,$type) = @_; |
| 400 |
my $userid = $patron->{userid}; |
| 401 |
my $sessionID = 234; |
| 402 |
my $flags = { |
| 403 |
superlibrarian => 1, acquisition => 0, |
| 404 |
borrowers => 0, |
| 405 |
catalogue => 1, circulate => 0, |
| 406 |
coursereserves => 0, editauthorities => 0, |
| 407 |
editcatalogue => 0, management => 0, |
| 408 |
parameters => 0, permissions => 0, |
| 409 |
plugins => 0, reports => 0, |
| 410 |
reserveforothers => 0, serials => 0, |
| 411 |
staffaccess => 0, tools => 0, |
| 412 |
updatecharges => 0 |
| 413 |
}; |
| 414 |
|
| 415 |
my $session_cookie = $query->cookie( |
| 416 |
-name => 'CGISESSID', |
| 417 |
-value => '9884013ae2c441d12e0bc9376242d2a8', |
| 418 |
-HttpOnly => 1 |
| 419 |
); |
| 420 |
return ( $userid, $session_cookie, $sessionID, $flags ); |
| 421 |
} |
| 422 |
|
| 423 |
# Mock checkauth |
| 424 |
my $auth = new Test::MockModule( 'C4::Auth' ); |
| 425 |
$auth->mock( 'checkauth', \&MockedCheckauth ); |
| 426 |
|
| 427 |
$query->param('koha_login_context', 'opac'); |
| 428 |
$query->param('userid', $patron->{userid}); |
| 429 |
$query->param('password', 'password'); |
| 430 |
|
| 431 |
# Test when the syspref is disabled |
| 432 |
t::lib::Mocks::mock_preference('LoadSearchHistoryToTheFirstLoggedUser', 0); |
| 433 |
my $result = $schema->resultset('SearchHistory')->search()->count; |
| 434 |
|
| 435 |
my ( $template, $loggedinuser, $cookies ) = get_template_and_user( |
| 436 |
{ |
| 437 |
template_name => "opac-user.tt", |
| 438 |
query => $query, |
| 439 |
type => "opac", |
| 440 |
authnotrequired => 0, |
| 441 |
debug => 1 |
| 442 |
} |
| 443 |
); |
| 444 |
|
| 445 |
my $result2 = $schema->resultset('SearchHistory')->search()->count; |
| 446 |
is($result2, $result, 'no new search added to borrower'); |
| 447 |
|
| 448 |
# Test when the syspref is enabled |
| 449 |
t::lib::Mocks::mock_preference('LoadSearchHistoryToTheFirstLoggedUser', 1); |
| 450 |
$query->param('koha_login_context', 'opac'); |
| 451 |
$query->param('userid', $patron->{userid}); |
| 452 |
$query->param('password', 'password'); |
| 453 |
$query->cookie( |
| 454 |
-name => 'CGISESSID', |
| 455 |
-value => $session->id, |
| 456 |
-HttpOnly => 1 |
| 457 |
); |
| 458 |
|
| 459 |
$result = $schema->resultset('SearchHistory')->search()->count; |
| 460 |
|
| 461 |
( $template, $loggedinuser, $cookies ) = get_template_and_user( |
| 462 |
{ |
| 463 |
template_name => "opac-user.tt", |
| 464 |
query => $query, |
| 465 |
type => "opac", |
| 466 |
authnotrequired => 0, |
| 467 |
debug => 1 |
| 468 |
} |
| 469 |
); |
| 470 |
|
| 471 |
$result2 = $schema->resultset('SearchHistory')->search()->count; |
| 472 |
is($result2, $result+1, 'new search added to borrower'); |
| 473 |
}; |
| 474 |
|
| 348 |
$dbh->rollback; |
475 |
$dbh->rollback; |
| 349 |
|
476 |
|
| 350 |
done_testing; |
477 |
done_testing; |
| 351 |
- |
|
|