Bug 12744 - Set language in staff client should have 'Cancel' link
[koha.git] / t / db_dependent / Auth.t
blob079779fdfc4f8e4ecf0561996302d30f8ae5643d
1 #!/usr/bin/perl
3 # This Koha test module is a stub!
4 # Add more tests here!!!
6 use Modern::Perl;
8 use CGI;
9 use Test::MockModule;
10 use List::MoreUtils qw/all any none/;
11 use Test::More tests => 6;
12 use C4::Members;
13 use Koha::AuthUtils qw/hash_password/;
15 BEGIN {
16 use_ok('C4::Auth');
19 my $dbh = C4::Context->dbh;
21 # Start transaction
22 $dbh->{AutoCommit} = 0;
23 $dbh->{RaiseError} = 1;
26 # get_template_and_user tests
28 { # Tests for the language URL parameter
30 sub MockedCheckauth {
31 my ($query,$authnotrequired,$flagsrequired,$type) = @_;
32 # return vars
33 my $userid = 'cobain';
34 my $sessionID = 234;
35 # we don't need to bother about permissions for this test
36 my $flags = {
37 superlibrarian => 1, acquisition => 0,
38 borrow => 0, borrowers => 0,
39 catalogue => 1, circulate => 0,
40 coursereserves => 0, editauthorities => 0,
41 editcatalogue => 0, management => 0,
42 parameters => 0, permissions => 0,
43 plugins => 0, reports => 0,
44 reserveforothers => 0, serials => 0,
45 staffaccess => 0, tools => 0,
46 updatecharges => 0
49 my $session_cookie = $query->cookie(
50 -name => 'CGISESSID',
51 -value => 'nirvana',
52 -HttpOnly => 1
55 return ( $userid, $session_cookie, $sessionID, $flags );
58 # Mock checkauth, build the scenario
59 my $auth = new Test::MockModule( 'C4::Auth' );
60 $auth->mock( 'checkauth', \&MockedCheckauth );
62 # Make sure 'EnableOpacSearchHistory' is set
63 C4::Context->set_preference('EnableOpacSearchHistory',1);
64 # Enable es-ES for the OPAC and staff interfaces
65 C4::Context->set_preference('opaclanguages','en,es-ES');
66 C4::Context->set_preference('language','en,es-ES');
68 # we need a session cookie
69 $ENV{"SERVER_PORT"} = 80;
70 $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
72 my $query = new CGI;
73 $query->param('language','es-ES');
75 my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
77 template_name => "about.tt",
78 query => $query,
79 type => "opac",
80 authnotrequired => 1,
81 flagsrequired => { catalogue => 1 },
82 debug => 1
86 ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
87 'BZ9735: the cookies array is flat' );
89 # new query, with non-existent language (we only have en and es-ES)
90 $query->param('language','tomas');
92 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
94 template_name => "about.tt",
95 query => $query,
96 type => "opac",
97 authnotrequired => 1,
98 flagsrequired => { catalogue => 1 },
99 debug => 1
103 ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
104 'BZ9735: invalid language, it is not set');
106 ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
107 'BZ9735: invalid language, then default to en');
110 my $hash1 = hash_password('password');
111 my $hash2 = hash_password('password');
113 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
114 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');
116 $dbh->rollback;