Changing CanBookBeRenewed to pass back the reason a renewal cannot proceed
[koha.git] / C4 / SIP / t / 03checkout.t
1 #!/usr/bin/perl
2 # checkout: test Checkout Response
3
4 use strict;
5 use warnings;
6 use Clone qw(clone);
7
8 use Sip::Constants qw(:all);
9
10 use SIPtest qw($datepat $textpat $instid $currency $user_barcode
11                $item_barcode $item_title
12                $item_diacritic_barcode $item_diacritic_title
13                $item_diacritic_owner);
14
15 my $patron_enable_template = {
16     id => 'Renew All: prep: enable patron permissions',
17     msg => "2520060102    084238AO$instid|AA$user_barcode|",
18     pat => qr/^26 {4}[ Y]{10}000$datepat/o,
19     fields => [],
20 };
21
22 my $patron_disable_template = {
23     id => 'Checkout: block patron (prep to test checkout denied)',
24     msg => "01N20060102    084238AO$instid|ALHe's a jerk|AA$user_barcode|",
25     # response to block patron is a patron status message
26     pat => qr/^24Y{4}[ Y]{10}000$datepat/o,
27     fields => [], };
28
29 my $checkin_template = {
30                         id => 'Checkout: cleanup: check in item',
31                         msg => "09N20050102    08423620060113    084235APUnder the bed|AO$instid|AB$item_barcode|ACterminal password|",
32                         pat => qr/^101YNN$datepat/o,
33                         fields => [],
34                        };
35
36 my $checkout_test_template = {
37     id => 'Checkout: valid item, valid patron',
38     msg => "11YN20060329    203000                  AO$instid|AA$user_barcode|AB$item_barcode|AC|",
39     pat => qr/^121NNY$datepat/,
40     fields => [
41                $SIPtest::field_specs{(FID_INST_ID)},
42                $SIPtest::field_specs{(FID_SCREEN_MSG)},
43                $SIPtest::field_specs{(FID_PRINT_LINE)},
44                { field    => FID_PATRON_ID,
45                  pat      => qr/^$user_barcode$/o,
46                  required => 1, },
47                { field    => FID_ITEM_ID,
48                  pat      => qr/^$item_barcode$/o,
49                  required => 1, },
50                { field    => FID_TITLE_ID,
51                  pat      => qr/^$item_title\s*$/o,
52                  required => 1, },
53                { field    => FID_DUE_DATE,
54                  pat      => $textpat,
55                  required => 1, },
56                { field    => FID_FEE_TYPE,
57                  pat      => qr/^\d{2}$/,
58                  required => 0, },
59                { field    => FID_SECURITY_INHIBIT,
60                  pat      => qr/^[YN]$/,
61                  required => 0, },
62                { field    => FID_CURRENCY,
63                  pat      => qr/^$currency$/o,
64                  required => 0, },
65                { field    => FID_FEE_AMT,
66                  pat      => qr/^[.0-9]+$/,
67                  required => 0, },
68                { field    => FID_MEDIA_TYPE,
69                  pat      => qr/^\d{3}$/,
70                  required => 0, },
71                { field    => FID_ITEM_PROPS,
72                  pat      => $textpat,
73                  required => 0, },
74                { field    => FID_TRANSACTION_ID,
75                  pat      => $textpat,
76                  required => 0, },
77                ], };
78
79 my @tests = (
80              $SIPtest::login_test,
81              $SIPtest::sc_status_test,
82              clone($checkout_test_template),
83              # Don't check the item in, because we're about to test renew
84              );
85
86 my $test;
87
88 ## Renewal OK
89 ## Test this by checking out exactly the same book a second time.
90 ## The only difference should be that the "Renewal OK" flag should now
91 ## be 'Y'.
92 #$test = clone($checkout_test_template);
93 #$test->{id} = 'Checkout: patron renewal';
94 #$test->{pat} = qr/^121YNY$datepat/;
95 #
96 #push @tests, $test;
97
98 # NOW check it in
99
100 push @tests, $checkin_template;
101
102 # Valid Patron, item with diacritical in the title
103 $test = clone($checkout_test_template);
104
105 $test->{id} = 'Checkout: valid patron, diacritical character in title';
106 $test->{msg} =~ s/AB$item_barcode/AB$item_diacritic_barcode/;
107
108 foreach my $i (0 .. (scalar @{$test->{fields}})-1) {
109     my $field =  $test->{fields}[$i];
110
111     if ($field->{field} eq FID_ITEM_ID) {
112         $field->{pat} = qr/^$item_diacritic_barcode$/;
113     } elsif ($field->{field} eq FID_TITLE_ID) {
114         $field->{pat} = qr/^$item_diacritic_title\s*$/;
115     } elsif ($field->{field} eq FID_OWNER) {
116         $field->{pat} = qr/^$item_diacritic_owner$/;
117     }
118 }
119
120 push @tests, $test;
121
122 $test = clone($checkin_template);
123 $test->{msg} =~ s/AB$item_barcode/AB$item_diacritic_barcode/;
124 push @tests, $test;
125
126 # Valid Patron, Invalid Item_id
127 $test = clone($checkout_test_template);
128
129 $test->{id} = 'Checkout: valid patron, invalid item';
130 $test->{msg} =~ s/AB$item_barcode/ABno-barcode/o;
131 $test->{pat} = qr/^120NUN$datepat/;
132 delete $test->{fields};
133 $test->{fields} = [
134                    $SIPtest::field_specs{(FID_INST_ID)},
135                    $SIPtest::field_specs{(FID_SCREEN_MSG)},
136                    $SIPtest::field_specs{(FID_PRINT_LINE)},
137                    { field    => FID_PATRON_ID,
138                      pat      => qr/^$user_barcode$/o,
139                      required => 1, },
140                    { field    => FID_ITEM_ID,
141                      pat      => qr/^no-barcode$/,
142                      required => 1, },
143                    { field    => FID_TITLE_ID,
144                      pat      => qr/^$/,
145                      required => 1, },
146                    { field    => FID_DUE_DATE,
147                      pat      => qr/^$/,
148                      required => 1, },
149                    { field    => FID_VALID_PATRON,
150                      pat      => qr/^Y$/,
151                      required => 1, },
152                    ];
153
154 push @tests, $test;
155
156 # Invalid patron, valid item
157 $test = clone($checkout_test_template);
158 $test->{id} = 'Checkout: invalid patron, valid item';
159 $test->{msg} =~ s/AA$user_barcode/AAberick/;
160 $test->{pat} = qr/^120NUN$datepat/;
161 delete $test->{fields};
162 $test->{fields} = [
163                    $SIPtest::field_specs{(FID_INST_ID)},
164                    $SIPtest::field_specs{(FID_SCREEN_MSG)},
165                    $SIPtest::field_specs{(FID_PRINT_LINE)},
166                    { field    => FID_PATRON_ID,
167                      pat      => qr/^berick$/,
168                      required => 1, },
169                    { field    => FID_ITEM_ID,
170                      pat      => qr/^$item_barcode$/o,
171                      required => 1, },
172                    { field    => FID_TITLE_ID,
173                      pat      => qr/^$item_title\s*$/o,
174                      required => 1, },
175                    { field    => FID_DUE_DATE,
176                      pat      => qr/^$/,
177                      required => 1, },
178                    { field    => FID_VALID_PATRON,
179                      pat      => qr/^N$/,
180                      required => 1, },
181                    ];
182
183 push @tests, $test;
184
185 # Needed: tests for blocked patrons, patrons with excessive
186 # fines/fees, magnetic media, charging fees to borrow items.
187
188 ## Blocked patron
189 #$test = clone($checkout_test_template);
190 #$test->{id} = 'Checkout: Blocked patron';
191 #$test->{pat} = qr/^120NUN$datepat/;
192 #delete $test->{fields};
193 #$test->{fields} = [
194 #                  $SIPtest::field_specs{(FID_INST_ID)},
195 #                  $SIPtest::field_specs{(FID_SCREEN_MSG)},
196 #                  $SIPtest::field_specs{(FID_PRINT_LINE)},
197 #                  { field    => FID_PATRON_ID,
198 #                    pat      => qr/^$user_barcode$/,
199 #                    required => 1, },
200 #                  { field    => FID_VALID_PATRON,
201 #                    pat      => qr/^Y$/,
202 #                    required => 1, },
203 #                 ];
204 #
205 #push @tests, $patron_disable_template, $test, $patron_enable_template;
206 #
207 SIPtest::run_sip_tests(@tests);
208
209 1;