Bug 29234: Further clean Z3950 Tests
[srvgit] / t / db_dependent / Patron / Borrower_Debarments.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Context;
6 use Koha::Database;
7 use Koha::Patrons;
8
9 use t::lib::TestBuilder;
10
11 use Test::More tests => 34;
12
13 use_ok('Koha::Patron::Debarments');
14
15 my $schema = Koha::Database->schema;
16 $schema->storage->txn_begin;
17 my $builder = t::lib::TestBuilder->new;
18 my $dbh = C4::Context->dbh;
19
20 my $library = $builder->build({
21     source => 'Branch',
22 });
23
24 my $patron_category = $builder->build({ source => 'Category' });
25 my $patron = $builder->build_object(
26     {
27         class  => 'Koha::Patrons',
28         value => {
29             firstname    => 'my firstname',
30             surname      => 'my surname',
31             categorycode => $patron_category->{categorycode},
32             branchcode   => $library->{branchcode},
33         }
34     }
35 );
36 my $borrowernumber = $patron->borrowernumber;
37
38 my $success = Koha::Patron::Debarments::AddDebarment({
39     borrowernumber => $borrowernumber,
40     expiration => '9999-06-10',
41     type => 'MANUAL',
42     comment => 'Test 1',
43 });
44 is( $success, 1, "AddDebarment returned true" );
45
46
47 my $restrictions = $patron->restrictions;
48 my $THE_restriction = $restrictions->next;
49 is( $restrictions->count, 1, '$patron->restrictions returns 1 restriction' );
50 is( $THE_restriction->type->code, 'MANUAL', "Correctly stored 'type'" );
51 is( $THE_restriction->expiration, '9999-06-10', "Correctly stored 'expiration'" );
52 is( $THE_restriction->comment, 'Test 1', "Correctly stored 'comment'" );
53
54
55 $success = Koha::Patron::Debarments::AddDebarment({
56     borrowernumber => $borrowernumber,
57     comment => 'Test 2',
58 });
59
60 $restrictions = $patron->restrictions;
61 $THE_restriction = $restrictions->last;
62 is( $restrictions->count, 2, '$patron->restrictions returns 2 restrictions' );
63 is( $THE_restriction->type->code, 'MANUAL', "Correctly stored 'type'" );
64 is( $THE_restriction->expiration, undef, "Correctly stored debarrment with no expiration" );
65 is( $THE_restriction->comment, 'Test 2', "Correctly stored 'comment'" );
66
67
68 Koha::Patron::Debarments::ModDebarment({
69     borrower_debarment_id => $THE_restriction->borrower_debarment_id,
70     comment => 'Test 3',
71     expiration => '9998-06-10',
72 });
73
74 $restrictions = $patron->restrictions;
75 $THE_restriction = $restrictions->last;
76 is( $restrictions->count, 2, '$patron->restrictions returns 2 restrictions' );
77 is( $THE_restriction->comment, 'Test 3', "ModDebarment functions correctly" );
78
79 $patron = $patron->get_from_storage;
80 is( $patron->debarred, '9999-06-10', "Field borrowers.debarred set correctly" );
81 is( $patron->debarredcomment, "Test 1\nTest 3", "Field borrowers.debarredcomment set correctly" );
82
83
84 Koha::Patron::Debarments::AddUniqueDebarment({
85     borrowernumber => $borrowernumber,
86     type           => 'OVERDUES'
87 });
88
89 $restrictions = $patron->restrictions->search(
90     {
91         type => 'OVERDUES',
92     }
93 );
94 $THE_restriction = $restrictions->next;
95 is( $restrictions->count, 1, '$patron->restrictions->search({ type => "OVERDUES" }) returns 1 OVERDUES restriction after running AddUniqueDebarment once' );
96 is( $THE_restriction->type->code, 'OVERDUES', "AddOverduesDebarment created new debarment correctly" );
97
98 Koha::Patron::Debarments::AddUniqueDebarment({
99     borrowernumber => $borrowernumber,
100     expiration => '9999-11-09',
101     type => 'OVERDUES'
102 });
103
104 $restrictions = $patron->restrictions->search(
105     {
106         type => 'OVERDUES',
107     }
108 );
109 $THE_restriction = $restrictions->next;
110 is( $restrictions->count, 1, '$patron->restrictions->search({ type => "OVERDUES" }) returns 1 OVERDUES restriction after running AddUniqueDebarent twice' );
111 is( $THE_restriction->expiration, '9999-11-09', "AddUniqueDebarment updated the OVERDUES restriction correctly" );
112
113
114 my $delUniqueDebarment = Koha::Patron::Debarments::DelUniqueDebarment({
115 });
116 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the arguments 'borrowernumber' and 'type' returns undef" );
117
118 $restrictions = $patron->restrictions->search(
119     {
120         type => 'OVERDUES',
121     }
122 );
123 is( $restrictions->count, 1, "DelUniqueDebarment without the arguments 'borrowernumber' and 'type' does not delete the debarment" );
124
125 $delUniqueDebarment = Koha::Patron::Debarments::DelUniqueDebarment({
126     borrowernumber => $borrowernumber,
127 });
128 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the argument 'type' returns undef" );
129
130 $restrictions = $patron->restrictions->search(
131     {
132         type => 'OVERDUES',
133     }
134 );
135 is( $restrictions->count, 1, "DelUniqueDebarment without the argument 'type' does not delete the debarment" );
136
137 $delUniqueDebarment = Koha::Patron::Debarments::DelUniqueDebarment({
138     type => 'OVERDUES'
139 });
140 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the argument 'borrowernumber' returns undef" );
141
142 $restrictions = $patron->restrictions->search(
143     {
144         type => 'OVERDUES',
145     }
146 );
147 is( $restrictions->count, 1, "DelUniqueDebarment without the argument 'borrowerumber' does not delete the debarment" );
148
149 $delUniqueDebarment = Koha::Patron::Debarments::DelUniqueDebarment({
150     borrowernumber => $borrowernumber,
151     type => 'SUSPENSION',
152 });
153 is( $delUniqueDebarment, undef, "DelUniqueDebarment with wrong arguments returns undef" );
154
155 $restrictions = $patron->restrictions->search(
156     {
157         type => 'OVERDUES',
158     }
159 );
160 is( $restrictions->count, 1, "DelUniqueDebarment with wrong arguments does not delete the debarment" );
161
162 $delUniqueDebarment = Koha::Patron::Debarments::DelUniqueDebarment({
163     borrowernumber => $borrowernumber,
164     type => 'OVERDUES',
165 });
166 is( $delUniqueDebarment, 1, "DelUniqueDebarment returns 1" );
167
168 $restrictions = $patron->restrictions->search(
169     {
170         type => 'OVERDUES',
171     }
172 );
173 is( $restrictions->count, 0, "DelUniqueDebarment functions correctly" );
174
175 $restrictions = $patron->restrictions;
176 while ( my $restriction = $restrictions->next ) {
177     Koha::Patron::Debarments::DelDebarment( $restriction->borrower_debarment_id );
178 }
179
180 $restrictions = $patron->restrictions;
181 is( $restrictions->count, 0, "DelDebarment functions correctly" );
182
183 $dbh->do(q|UPDATE borrowers SET debarred = '1970-01-01'|);
184 is( Koha::Patrons->find( $borrowernumber )->is_debarred, undef, 'A patron with a debarred date in the past is not debarred' );
185
186 $dbh->do(q|UPDATE borrowers SET debarred = NULL|);
187 is( Koha::Patrons->find( $borrowernumber )->is_debarred, undef, 'A patron without a debarred date is not debarred' );
188
189 $dbh->do(q|UPDATE borrowers SET debarred = '9999-12-31'|); # Note: Change this test before the first of January 10000!
190 is( Koha::Patrons->find( $borrowernumber )->is_debarred, '9999-12-31', 'A patron with a debarred date in the future is debarred' );
191
192 # Test patrons merge
193 my $borrowernumber2 = Koha::Patron->new(
194     {
195         firstname    => 'my firstname bis',
196         surname      => 'my surname bis',
197         categorycode => $patron_category->{categorycode},
198         branchcode   => $library->{branchcode},
199     }
200 )->store->borrowernumber;
201 my $debarreddate2    = '9999-06-10'; # Be sure to be in the future
202 my $debarredcomment2 = 'Test merge';
203 Koha::Patron::Debarments::AddDebarment(
204     {
205         borrowernumber => $borrowernumber2,
206         expiration     => $debarreddate2,
207         type           => 'MANUAL',
208         comment        => $debarredcomment2,
209     }
210 );
211 my $borrowernumber3 = Koha::Patron->new(
212     {
213         firstname    => 'my firstname ter',
214         surname      => 'my surname ter',
215         categorycode => $patron_category->{categorycode},
216         branchcode   => $library->{branchcode},
217     }
218 )->store->borrowernumber;
219 Koha::Patrons->find($borrowernumber3)->merge_with( [$borrowernumber2] );
220 is( Koha::Patrons->find($borrowernumber3)->debarred,
221     $debarreddate2, 'Koha::Patron->merge_with() transfers well debarred' );
222 is( Koha::Patrons->find($borrowernumber3)->debarredcomment,
223     $debarredcomment2, 'Koha::Patron->merge_with() transfers well debarredcomment' );