Bug 12478: Take the FacetMaxCount pref into account
[koha.git] / t / db_dependent / ILSDI_Services.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Members qw/AddMember GetMember GetBorrowercategory/;
6 use Koha::Libraries;
7 use CGI qw ( -utf8 );
8
9 use Test::More tests => 16;
10 use t::lib::Mocks;
11 use t::lib::TestBuilder;
12
13 BEGIN {
14     use_ok('C4::ILSDI::Services');
15 }
16
17 my $dbh = C4::Context->dbh;
18
19 # Start transaction
20 $dbh->{AutoCommit} = 0;
21 $dbh->{RaiseError} = 1;
22
23 # Create patron
24 my %data = (
25     firstname =>  'my firstname',
26     surname => 'my surname',
27     categorycode => 'UT',
28     branchcode => 'UT',
29     cardnumber => 'ilsdi-cardnumber',
30     userid => 'ilsdi-userid',
31     password => 'ilsdi-password',
32 );
33
34 # Crate patron category
35 unless ( GetBorrowercategory('UT') ) {
36     $dbh->do("INSERT INTO categories
37     (categorycode,description,enrolmentperiod,upperagelimit,enrolmentfee,overduenoticerequired,reservefee,category_type,default_privacy)
38         VALUES
39     ('UT','Unit tester',99,99,0.000000,1,0.000000,'C','default');");
40 }
41
42 # Create library
43 unless ( Koha::Libraries->find('UT') ) {
44     $dbh->do("INSERT INTO branches (branchcode,branchname) VALUES ('UT','Unit test library');");
45 }
46
47
48 my $borrowernumber = AddMember(%data);
49 my $borrower = GetMember( borrowernumber => $borrowernumber );
50
51 { # AuthenticatePatron test
52
53     my $query = new CGI;
54     $query->param('username',$borrower->{'userid'});
55     $query->param('password','ilsdi-password');
56
57     my $reply = C4::ILSDI::Services::AuthenticatePatron($query);
58     is($reply->{'id'}, $borrowernumber, "userid and password - Patron authenticated");
59     is($reply->{'code'}, undef, "Error code undef");
60
61     $query->param('password','ilsdi-passworD');
62     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
63     is($reply->{'code'}, 'PatronNotFound', "userid and wrong password - PatronNotFound");
64     is($reply->{'id'}, undef, "id undef");
65
66     $query->param('password','ilsdi-password');
67     $query->param('username','wrong-ilsdi-useriD');
68     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
69     is($reply->{'code'}, 'PatronNotFound', "non-existing userid - PatronNotFound");
70     is($reply->{'id'}, undef, "id undef");
71
72     $query->param('username',uc($borrower->{'userid'}));
73     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
74     is($reply->{'id'}, $borrowernumber, "userid is not case sensitive - Patron authenticated");
75     is($reply->{'code'}, undef, "Error code undef");
76
77     $query->param('username',$borrower->{'cardnumber'});
78     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
79     is($reply->{'id'}, $borrowernumber, "cardnumber and password - Patron authenticated");
80     is($reply->{'code'}, undef, "Error code undef");
81
82     $query->param('password','ilsdi-passworD');
83     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
84     is($reply->{'code'}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount");
85     is($reply->{'id'}, undef, "id undef");
86
87     $query->param('username','randomcardnumber1234');
88     $query->param('password','ilsdi-password');
89     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
90     is($reply->{'code'}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound");
91     is($reply->{'id'}, undef, "id undef");
92
93 }
94
95 # End transaction
96 $dbh->rollback;
97 $dbh->{AutoCommit} = 1;
98 $dbh->{RaiseError} = 0;
99
100 my $schema = Koha::Database->schema;
101 $schema->storage->txn_begin;
102
103 $schema->resultset( 'Issue' )->delete_all;
104 $schema->resultset( 'Borrower' )->delete_all;
105 $schema->resultset( 'BorrowerAttribute' )->delete_all;
106 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
107 $schema->resultset( 'Category' )->delete_all;
108 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
109 $schema->resultset( 'Branch' )->delete_all;
110
111
112 { # GetPatronInfo/GetBorrowerAttributes test for extended patron attributes:
113
114     # Configure Koha to enable ILS-DI server and extended attributes:
115     t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
116     t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
117
118     my $builder = t::lib::TestBuilder->new;
119
120     # Set up a library/branch for our user to belong to:
121     my $lib = $builder->build( {
122         source => 'Branch',
123         value => {
124             branchcode => 'T_ILSDI',
125         }
126     } );
127
128     # Create a new category for user to belong to:
129     my $cat = $builder->build( {
130         source => 'Category',
131         value  => {
132             category_type                 => 'A',
133             BlockExpiredPatronOpacActions => -1,
134         }
135     } );
136
137     # Create a new attribute type:
138     my $attr_type = $builder->build( {
139         source => 'BorrowerAttributeType',
140         value  => {
141             code                      => 'DOORCODE',
142             opac_display              => 0,
143             authorised_value_category => '',
144             class                     => '',
145         }
146     } );
147
148     # Create a new user:
149     my $brwr = $builder->build( {
150         source => 'Borrower',
151         value  => {
152             categorycode => $cat->{'categorycode'},
153             branchcode   => $lib,
154         }
155     } );
156
157     # Authorised value:
158     my $auth = $builder->build( {
159         source => 'AuthorisedValue',
160         value  => {
161             category => $cat->{'categorycode'}
162         }
163     } );
164
165     # Set the new attribute for our user:
166     my $attr = $builder->build( {
167         source => 'BorrowerAttribute',
168         value  => {
169             borrowernumber => $brwr->{'borrowernumber'},
170             code           => $attr_type->{'code'},
171             attribute      => '1337',
172             password       => undef,
173         }
174     } );
175
176     # Prepare and send web request for IL-SDI server:
177     my $query = new CGI;
178     $query->param( 'service', 'GetPatronInfo' );
179     $query->param( 'patron_id', $brwr->{'borrowernumber'} );
180     $query->param( 'show_attributes', '1' );
181
182     my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
183
184     # Build a structure for comparison:
185     my $cmp = {
186         category_code     => $attr_type->{'category_code'},
187         class             => $attr_type->{'class'},
188         code              => $attr->{'code'},
189         description       => $attr_type->{'description'},
190         display_checkout  => $attr_type->{'display_checkout'},
191         password          => undef,
192         value             => $attr->{'attribute'},
193         value_description => undef,
194     };
195
196     # Check results:
197     is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
198 }
199
200 # Cleanup
201 $schema->storage->txn_rollback;