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/;
26 my $schema = Koha
::Database
->schema;
27 $schema->storage->txn_begin;
28 my $builder = t
::lib
::TestBuilder
->new;
29 my $dbh = C4
::Context
->dbh;
31 my $hash1 = hash_password
('password');
32 my $hash2 = hash_password
('password');
34 { # tests no_set_userenv parameter
35 my $patron = $builder->build( { source
=> 'Borrower' } );
36 Koha
::Patrons
->find( $patron->{borrowernumber
} )->update_password( $patron->{userid
}, $hash1 );
37 my $library = $builder->build(
43 ok
( checkpw
( $dbh, $patron->{userid
}, 'password', undef, undef, 1 ), 'checkpw returns true' );
44 is
( C4
::Context
->userenv, undef, 'Userenv should be undef as required' );
45 C4
::Context
->_new_userenv('DUMMY SESSION');
46 C4
::Context
->set_userenv(0,0,0,'firstname','surname', $library->{branchcode
}, 'Library 1', 0, '', '');
47 is
( C4
::Context
->userenv->{branch
}, $library->{branchcode
}, 'Userenv gives correct branch' );
48 ok
( checkpw
( $dbh, $patron->{userid
}, 'password', undef, undef, 1 ), 'checkpw returns true' );
49 is
( C4
::Context
->userenv->{branch
}, $library->{branchcode
}, 'Userenv branch is preserved if no_set_userenv is true' );
50 ok
( checkpw
( $dbh, $patron->{userid
}, 'password', undef, undef, 0 ), 'checkpw still returns true' );
51 isnt
( C4
::Context
->userenv->{branch
}, $library->{branchcode
}, 'Userenv branch is overwritten if no_set_userenv is false' );
54 # get_template_and_user tests
56 { # Tests for the language URL parameter
59 my ($query,$authnotrequired,$flagsrequired,$type) = @_;
61 my $userid = 'cobain';
63 # we don't need to bother about permissions for this test
65 superlibrarian
=> 1, acquisition
=> 0,
67 catalogue
=> 1, circulate
=> 0,
68 coursereserves
=> 0, editauthorities
=> 0,
69 editcatalogue
=> 0, management
=> 0,
70 parameters
=> 0, permissions
=> 0,
71 plugins
=> 0, reports
=> 0,
72 reserveforothers
=> 0, serials
=> 0,
73 staffaccess
=> 0, tools
=> 0,
77 my $session_cookie = $query->cookie(
83 return ( $userid, $session_cookie, $sessionID, $flags );
86 # Mock checkauth, build the scenario
87 my $auth = new Test
::MockModule
( 'C4::Auth' );
88 $auth->mock( 'checkauth', \
&MockedCheckauth
);
90 # Make sure 'EnableOpacSearchHistory' is set
91 t
::lib
::Mocks
::mock_preference
('EnableOpacSearchHistory',1);
92 # Enable es-ES for the OPAC and staff interfaces
93 t
::lib
::Mocks
::mock_preference
('opaclanguages','en,es-ES');
94 t
::lib
::Mocks
::mock_preference
('language','en,es-ES');
96 # we need a session cookie
97 $ENV{"SERVER_PORT"} = 80;
98 $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
101 $query->param('language','es-ES');
103 my ( $template, $loggedinuser, $cookies ) = get_template_and_user
(
105 template_name
=> "about.tt",
108 authnotrequired
=> 1,
109 flagsrequired
=> { catalogue
=> 1 },
114 ok
( ( all
{ ref($_) eq 'CGI::Cookie' } @
$cookies ),
115 'BZ9735: the cookies array is flat' );
117 # new query, with non-existent language (we only have en and es-ES)
118 $query->param('language','tomas');
120 ( $template, $loggedinuser, $cookies ) = get_template_and_user
(
122 template_name
=> "about.tt",
125 authnotrequired
=> 1,
126 flagsrequired
=> { catalogue
=> 1 },
131 ok
( ( none
{ $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @
$cookies ),
132 'BZ9735: invalid language, it is not set');
134 ok
( ( any
{ $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @
$cookies ),
135 'BZ9735: invalid language, then default to en');
137 for my $template_name (
139 ../../../../../../../../../../../../../../../etc/passwd
140 test/../../../../../../../../../../../../../../etc/passwd
142 test/does_not_finished_by_tt_t
146 ( $template, $loggedinuser, $cookies ) = get_template_and_user
(
148 template_name
=> $template_name,
151 authnotrequired
=> 1,
152 flagsrequired
=> { catalogue
=> 1 },
156 like
( $@
, qr
(^bad template path
), 'The file $template_name should not be accessible' );
158 ( $template, $loggedinuser, $cookies ) = get_template_and_user
(
160 template_name
=> 'errors/errorpage.tt',
163 authnotrequired
=> 1,
164 flagsrequired
=> { catalogue
=> 1 },
167 my $file_exists = ( -f
$template->{filename
} ) ?
1 : 0;
168 is
( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
171 # Check that there is always an OPACBaseURL set.
172 my $input = CGI
->new();
173 my ( $template1, $borrowernumber, $cookie );
174 ( $template1, $borrowernumber, $cookie ) = get_template_and_user
(
176 template_name
=> "opac-detail.tt",
179 authnotrequired
=> 1,
183 ok
( ( any
{ 'OPACBaseURL' eq $_ } keys %{$template1->{VARS
}} ),
184 'OPACBaseURL is in OPAC template' );
187 ( $template2, $borrowernumber, $cookie ) = get_template_and_user
(
189 template_name
=> "catalogue/detail.tt",
192 authnotrequired
=> 1,
196 ok
( ( any
{ 'OPACBaseURL' eq $_ } keys %{$template2->{VARS
}} ),
197 'OPACBaseURL is in Staff template' );
199 ok
(C4
::Auth
::checkpw_hash
('password', $hash1), 'password validates with first hash');
200 ok
(C4
::Auth
::checkpw_hash
('password', $hash2), 'password validates with second hash');