9 use t::lib::TestBuilder;
11 use Test::More tests => 34;
13 use_ok('Koha::Patron::Debarments');
15 my $schema = Koha::Database->schema;
16 $schema->storage->txn_begin;
17 my $builder = t::lib::TestBuilder->new;
18 my $dbh = C4::Context->dbh;
20 my $library = $builder->build({
24 my $patron_category = $builder->build({ source => 'Category' });
25 my $patron = $builder->build_object(
27 class => 'Koha::Patrons',
29 firstname => 'my firstname',
30 surname => 'my surname',
31 categorycode => $patron_category->{categorycode},
32 branchcode => $library->{branchcode},
36 my $borrowernumber = $patron->borrowernumber;
38 my $success = Koha::Patron::Debarments::AddDebarment({
39 borrowernumber => $borrowernumber,
40 expiration => '9999-06-10',
44 is( $success, 1, "AddDebarment returned true" );
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'" );
55 $success = Koha::Patron::Debarments::AddDebarment({
56 borrowernumber => $borrowernumber,
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'" );
68 Koha::Patron::Debarments::ModDebarment({
69 borrower_debarment_id => $THE_restriction->borrower_debarment_id,
71 expiration => '9998-06-10',
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" );
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" );
84 Koha::Patron::Debarments::AddUniqueDebarment({
85 borrowernumber => $borrowernumber,
89 $restrictions = $patron->restrictions->search(
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" );
98 Koha::Patron::Debarments::AddUniqueDebarment({
99 borrowernumber => $borrowernumber,
100 expiration => '9999-11-09',
104 $restrictions = $patron->restrictions->search(
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" );
114 my $delUniqueDebarment = Koha::Patron::Debarments::DelUniqueDebarment({
116 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the arguments 'borrowernumber' and 'type' returns undef" );
118 $restrictions = $patron->restrictions->search(
123 is( $restrictions->count, 1, "DelUniqueDebarment without the arguments 'borrowernumber' and 'type' does not delete the debarment" );
125 $delUniqueDebarment = Koha::Patron::Debarments::DelUniqueDebarment({
126 borrowernumber => $borrowernumber,
128 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the argument 'type' returns undef" );
130 $restrictions = $patron->restrictions->search(
135 is( $restrictions->count, 1, "DelUniqueDebarment without the argument 'type' does not delete the debarment" );
137 $delUniqueDebarment = Koha::Patron::Debarments::DelUniqueDebarment({
140 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the argument 'borrowernumber' returns undef" );
142 $restrictions = $patron->restrictions->search(
147 is( $restrictions->count, 1, "DelUniqueDebarment without the argument 'borrowerumber' does not delete the debarment" );
149 $delUniqueDebarment = Koha::Patron::Debarments::DelUniqueDebarment({
150 borrowernumber => $borrowernumber,
151 type => 'SUSPENSION',
153 is( $delUniqueDebarment, undef, "DelUniqueDebarment with wrong arguments returns undef" );
155 $restrictions = $patron->restrictions->search(
160 is( $restrictions->count, 1, "DelUniqueDebarment with wrong arguments does not delete the debarment" );
162 $delUniqueDebarment = Koha::Patron::Debarments::DelUniqueDebarment({
163 borrowernumber => $borrowernumber,
166 is( $delUniqueDebarment, 1, "DelUniqueDebarment returns 1" );
168 $restrictions = $patron->restrictions->search(
173 is( $restrictions->count, 0, "DelUniqueDebarment functions correctly" );
175 $restrictions = $patron->restrictions;
176 while ( my $restriction = $restrictions->next ) {
177 Koha::Patron::Debarments::DelDebarment( $restriction->borrower_debarment_id );
180 $restrictions = $patron->restrictions;
181 is( $restrictions->count, 0, "DelDebarment functions correctly" );
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' );
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' );
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' );
193 my $borrowernumber2 = Koha::Patron->new(
195 firstname => 'my firstname bis',
196 surname => 'my surname bis',
197 categorycode => $patron_category->{categorycode},
198 branchcode => $library->{branchcode},
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(
205 borrowernumber => $borrowernumber2,
206 expiration => $debarreddate2,
208 comment => $debarredcomment2,
211 my $borrowernumber3 = Koha::Patron->new(
213 firstname => 'my firstname ter',
214 surname => 'my surname ter',
215 categorycode => $patron_category->{categorycode},
216 branchcode => $library->{branchcode},
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' );