Bug 18089 - All XSLT testing singleBranchMode = 0 fails to show even if install has...
[koha.git] / t / db_dependent / ILSDI_Services.t
blob88e8181d04a64f7d8a4a74f4593ee6965da72ecd
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use CGI qw ( -utf8 );
22 use Test::More tests => 3;
23 use Test::MockModule;
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
27 use Koha::AuthUtils;
29 BEGIN {
30 use_ok('C4::ILSDI::Services');
33 my $schema = Koha::Database->schema;
34 my $dbh = C4::Context->dbh;
35 my $builder = t::lib::TestBuilder->new;
37 subtest 'AuthenticatePatron test' => sub {
39 plan tests => 14;
41 $schema->storage->txn_begin;
43 my $plain_password = 'tomasito';
45 my $borrower = $builder->build({
46 source => 'Borrower',
47 value => {
48 password => Koha::AuthUtils::hash_password( $plain_password )
50 });
52 my $query = new CGI;
53 $query->param( 'username', $borrower->{userid});
54 $query->param( 'password', $plain_password);
56 my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
57 is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
58 is( $reply->{code}, undef, "Error code undef");
60 $query->param('password','ilsdi-passworD');
61 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
62 is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" );
63 is( $reply->{id}, undef, "id undef");
65 $query->param( 'password', $plain_password );
66 $query->param( 'username', 'wrong-ilsdi-useriD' );
67 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
68 is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" );
69 is( $reply->{id}, undef, "id undef");
71 $query->param( 'username', uc( $borrower->{userid} ));
72 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
73 is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" );
74 is( $reply->{code}, undef, "Error code undef");
76 $query->param( 'username', $borrower->{cardnumber} );
77 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
78 is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" );
79 is( $reply->{code}, undef, "Error code undef" );
81 $query->param( 'password', 'ilsdi-passworD' );
82 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
83 is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" );
84 is( $reply->{id}, undef, "id undef" );
86 $query->param( 'username', 'randomcardnumber1234' );
87 $query->param( 'password', $plain_password );
88 $reply = C4::ILSDI::Services::AuthenticatePatron($query);
89 is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
90 is( $reply->{id}, undef, "id undef");
92 $schema->storage->txn_rollback;
96 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
98 plan tests => 2;
100 $schema->storage->txn_begin;
102 $schema->resultset( 'Issue' )->delete_all;
103 $schema->resultset( 'Borrower' )->delete_all;
104 $schema->resultset( 'BorrowerAttribute' )->delete_all;
105 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
106 $schema->resultset( 'Category' )->delete_all;
107 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
108 $schema->resultset( 'Branch' )->delete_all;
110 # Configure Koha to enable ILS-DI server and extended attributes:
111 t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
112 t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
114 # Set up a library/branch for our user to belong to:
115 my $lib = $builder->build( {
116 source => 'Branch',
117 value => {
118 branchcode => 'T_ILSDI',
120 } );
122 # Create a new category for user to belong to:
123 my $cat = $builder->build( {
124 source => 'Category',
125 value => {
126 category_type => 'A',
127 BlockExpiredPatronOpacActions => -1,
129 } );
131 # Create a new attribute type:
132 my $attr_type = $builder->build( {
133 source => 'BorrowerAttributeType',
134 value => {
135 code => 'DOORCODE',
136 opac_display => 0,
137 authorised_value_category => '',
138 class => '',
140 } );
142 # Create a new user:
143 my $brwr = $builder->build( {
144 source => 'Borrower',
145 value => {
146 categorycode => $cat->{'categorycode'},
147 branchcode => $lib->{'branchcode'},
149 } );
151 # Authorised value:
152 my $auth = $builder->build( {
153 source => 'AuthorisedValue',
154 value => {
155 category => $cat->{'categorycode'}
157 } );
159 # Set the new attribute for our user:
160 my $attr = $builder->build( {
161 source => 'BorrowerAttribute',
162 value => {
163 borrowernumber => $brwr->{'borrowernumber'},
164 code => $attr_type->{'code'},
165 attribute => '1337',
167 } );
169 my $members = Test::MockModule->new('C4::Members');
170 $members->mock( 'GetMemberAccountBalance', sub { return ( 10, 10, 0 ); } );
172 # Prepare and send web request for IL-SDI server:
173 my $query = new CGI;
174 $query->param( 'service', 'GetPatronInfo' );
175 $query->param( 'patron_id', $brwr->{'borrowernumber'} );
176 $query->param( 'show_attributes', '1' );
178 my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
180 # Build a structure for comparison:
181 my $cmp = {
182 category_code => $attr_type->{'category_code'},
183 class => $attr_type->{'class'},
184 code => $attr->{'code'},
185 description => $attr_type->{'description'},
186 display_checkout => $attr_type->{'display_checkout'},
187 value => $attr->{'attribute'},
188 value_description => undef,
191 is( $reply->{'charges'}, '10.00',
192 'The \'charges\' attribute should be correctly filled (bug 17836)' );
194 # Check results:
195 is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
197 # Cleanup
198 $schema->storage->txn_rollback;