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 => 18;
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 # get_template_and_user tests
32 { # Tests for the language URL parameter
35 my ($query,$authnotrequired,$flagsrequired,$type) = @_;
37 my $userid = 'cobain';
39 # we don't need to bother about permissions for this test
41 superlibrarian => 1, acquisition => 0,
43 catalogue => 1, circulate => 0,
44 coursereserves => 0, editauthorities => 0,
45 editcatalogue => 0, management => 0,
46 parameters => 0, permissions => 0,
47 plugins => 0, reports => 0,
48 reserveforothers => 0, serials => 0,
49 staffaccess => 0, tools => 0,
53 my $session_cookie = $query->cookie(
59 return ( $userid, $session_cookie, $sessionID, $flags );
62 # Mock checkauth, build the scenario
63 my $auth = new Test::MockModule( 'C4::Auth' );
64 $auth->mock( 'checkauth', \&MockedCheckauth );
66 # Make sure 'EnableOpacSearchHistory' is set
67 t::lib::Mocks::mock_preference('EnableOpacSearchHistory',1);
68 # Enable es-ES for the OPAC and staff interfaces
69 t::lib::Mocks::mock_preference('opaclanguages','en,es-ES');
70 t::lib::Mocks::mock_preference('language','en,es-ES');
72 # we need a session cookie
73 $ENV{"SERVER_PORT"} = 80;
74 $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
77 $query->param('language','es-ES');
79 my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
81 template_name => "about.tt",
85 flagsrequired => { catalogue => 1 },
90 ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
91 'BZ9735: the cookies array is flat' );
93 # new query, with non-existent language (we only have en and es-ES)
94 $query->param('language','tomas');
96 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
98 template_name => "about.tt",
101 authnotrequired => 1,
102 flagsrequired => { catalogue => 1 },
107 ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
108 'BZ9735: invalid language, it is not set');
110 ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
111 'BZ9735: invalid language, then default to en');
113 for my $template_name (
115 ../../../../../../../../../../../../../../../etc/passwd
116 test/../../../../../../../../../../../../../../etc/passwd
118 test/does_not_finished_by_tt_t
122 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
124 template_name => $template_name,
127 authnotrequired => 1,
128 flagsrequired => { catalogue => 1 },
132 like ( $@, qr(^bad template path), 'The file $template_name should not be accessible' );
134 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
136 template_name => 'errors/errorpage.tt',
139 authnotrequired => 1,
140 flagsrequired => { catalogue => 1 },
143 my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
144 is ( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
147 # Check that there is always an OPACBaseURL set.
148 my $input = CGI->new();
149 my ( $template1, $borrowernumber, $cookie );
150 ( $template1, $borrowernumber, $cookie ) = get_template_and_user(
152 template_name => "opac-detail.tt",
155 authnotrequired => 1,
159 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
160 'OPACBaseURL is in OPAC template' );
163 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
165 template_name => "catalogue/detail.tt",
168 authnotrequired => 1,
172 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
173 'OPACBaseURL is in Staff template' );
175 my $hash1 = hash_password('password');
176 my $hash2 = hash_password('password');
178 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
179 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');
181 my $patron = $builder->build( { source => 'Borrower' } );
182 changepassword( $patron->{userid}, $patron->{borrowernumber}, $hash1 );
183 my $library = $builder->build(
189 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Library 1', 0, '', '');
190 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv gives correct branch' );
191 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
192 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is preserved if no_set_userenv is true' );
193 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 0 ), 'checkpw still returns true' );
194 isnt( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is overwritten if no_set_userenv is false' );