3 # This Koha test module is a stub!
4 # Add more tests here!!!
10 use List::MoreUtils qw/all any none/;
11 use Test::More tests => 20;
14 use t::lib::TestBuilder;
16 use C4::Auth qw(checkpw);
18 use Koha::AuthUtils qw/hash_password/;
25 my $schema = Koha::Database->schema;
26 $schema->storage->txn_begin;
27 my $builder = t::lib::TestBuilder->new;
28 my $dbh = C4::Context->dbh;
30 my $hash1 = hash_password('password');
31 my $hash2 = hash_password('password');
33 { # tests no_set_userenv parameter
34 my $patron = $builder->build( { source => 'Borrower' } );
35 changepassword( $patron->{userid}, $patron->{borrowernumber}, $hash1 );
36 my $library = $builder->build(
42 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
43 is( C4::Context->userenv, undef, 'Userenv should be undef as required' );
44 C4::Context->_new_userenv('DUMMY SESSION');
45 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Library 1', 0, '', '');
46 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv gives correct branch' );
47 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
48 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is preserved if no_set_userenv is true' );
49 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 0 ), 'checkpw still returns true' );
50 isnt( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is overwritten if no_set_userenv is false' );
53 # get_template_and_user tests
55 { # Tests for the language URL parameter
58 my ($query,$authnotrequired,$flagsrequired,$type) = @_;
60 my $userid = 'cobain';
62 # we don't need to bother about permissions for this test
64 superlibrarian => 1, acquisition => 0,
66 catalogue => 1, circulate => 0,
67 coursereserves => 0, editauthorities => 0,
68 editcatalogue => 0, management => 0,
69 parameters => 0, permissions => 0,
70 plugins => 0, reports => 0,
71 reserveforothers => 0, serials => 0,
72 staffaccess => 0, tools => 0,
76 my $session_cookie = $query->cookie(
82 return ( $userid, $session_cookie, $sessionID, $flags );
85 # Mock checkauth, build the scenario
86 my $auth = new Test::MockModule( 'C4::Auth' );
87 $auth->mock( 'checkauth', \&MockedCheckauth );
89 # Make sure 'EnableOpacSearchHistory' is set
90 t::lib::Mocks::mock_preference('EnableOpacSearchHistory',1);
91 # Enable es-ES for the OPAC and staff interfaces
92 t::lib::Mocks::mock_preference('opaclanguages','en,es-ES');
93 t::lib::Mocks::mock_preference('language','en,es-ES');
95 # we need a session cookie
96 $ENV{"SERVER_PORT"} = 80;
97 $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
100 $query->param('language','es-ES');
102 my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
104 template_name => "about.tt",
107 authnotrequired => 1,
108 flagsrequired => { catalogue => 1 },
113 ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
114 'BZ9735: the cookies array is flat' );
116 # new query, with non-existent language (we only have en and es-ES)
117 $query->param('language','tomas');
119 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
121 template_name => "about.tt",
124 authnotrequired => 1,
125 flagsrequired => { catalogue => 1 },
130 ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
131 'BZ9735: invalid language, it is not set');
133 ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
134 'BZ9735: invalid language, then default to en');
136 for my $template_name (
138 ../../../../../../../../../../../../../../../etc/passwd
139 test/../../../../../../../../../../../../../../etc/passwd
141 test/does_not_finished_by_tt_t
145 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
147 template_name => $template_name,
150 authnotrequired => 1,
151 flagsrequired => { catalogue => 1 },
155 like ( $@, qr(^bad template path), 'The file $template_name should not be accessible' );
157 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
159 template_name => 'errors/errorpage.tt',
162 authnotrequired => 1,
163 flagsrequired => { catalogue => 1 },
166 my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
167 is ( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
170 # Check that there is always an OPACBaseURL set.
171 my $input = CGI->new();
172 my ( $template1, $borrowernumber, $cookie );
173 ( $template1, $borrowernumber, $cookie ) = get_template_and_user(
175 template_name => "opac-detail.tt",
178 authnotrequired => 1,
182 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
183 'OPACBaseURL is in OPAC template' );
186 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
188 template_name => "catalogue/detail.tt",
191 authnotrequired => 1,
195 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
196 'OPACBaseURL is in Staff template' );
198 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
199 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');