9 use t::lib::TestBuilder;
11 use Test::More tests => 31;
13 use_ok('Koha::Borrower::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 $borrowernumber = AddMember(
25 firstname => 'my firstname',
26 surname => 'my surname',
28 branchcode => $library->{branchcode},
31 my $success = AddDebarment({
32 borrowernumber => $borrowernumber,
33 expiration => '9999-06-10',
37 is( $success, 1, "AddDebarment returned true" );
40 my $debarments = GetDebarments({ borrowernumber => $borrowernumber });
41 is( @$debarments, 1, "GetDebarments returns 1 debarment" );
42 is( $debarments->[0]->{'type'}, 'MANUAL', "Correctly stored 'type'" );
43 is( $debarments->[0]->{'expiration'}, '9999-06-10', "Correctly stored 'expiration'" );
44 is( $debarments->[0]->{'comment'}, 'Test 1', "Correctly stored 'comment'" );
47 $success = AddDebarment({
48 borrowernumber => $borrowernumber,
52 $debarments = GetDebarments({ borrowernumber => $borrowernumber });
53 is( @$debarments, 2, "GetDebarments returns 2 debarments" );
54 is( $debarments->[1]->{'type'}, 'MANUAL', "Correctly stored 'type'" );
55 is( $debarments->[1]->{'expiration'}, undef, "Correctly stored debarrment with no expiration" );
56 is( $debarments->[1]->{'comment'}, 'Test 2', "Correctly stored 'comment'" );
60 borrower_debarment_id => $debarments->[1]->{'borrower_debarment_id'},
62 expiration => '9998-06-10',
64 $debarments = GetDebarments({ borrowernumber => $borrowernumber });
65 is( $debarments->[1]->{'comment'}, 'Test 3', "ModDebarment functions correctly" );
68 my $borrower = GetMember( borrowernumber => $borrowernumber );
69 is( $borrower->{'debarred'}, '9999-06-10', "Field borrowers.debarred set correctly" );
70 is( $borrower->{'debarredcomment'}, "Test 1\nTest 3", "Field borrowers.debarredcomment set correctly" );
74 borrowernumber => $borrowernumber,
77 $debarments = GetDebarments({
78 borrowernumber => $borrowernumber,
81 is( @$debarments, 1, "GetDebarments returns 1 OVERDUES debarment" );
82 is( $debarments->[0]->{'type'}, 'OVERDUES', "AddOverduesDebarment created new debarment correctly" );
85 borrowernumber => $borrowernumber,
86 expiration => '9999-11-09',
89 $debarments = GetDebarments({
90 borrowernumber => $borrowernumber,
93 is( @$debarments, 1, "GetDebarments returns 1 OVERDUES debarment after running AddOverduesDebarment twice" );
94 is( $debarments->[0]->{'expiration'}, '9999-11-09', "AddOverduesDebarment updated OVERDUES debarment correctly" );
97 my $delUniqueDebarment = DelUniqueDebarment({
99 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the arguments 'borrowernumber' and 'type' returns undef" );
100 $debarments = GetDebarments({
101 borrowernumber => $borrowernumber,
104 is( @$debarments, 1, "DelUniqueDebarment without the arguments 'borrowernumber' and 'type' does not delete the debarment" );
106 $delUniqueDebarment = DelUniqueDebarment({
107 borrowernumber => $borrowernumber,
109 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the argument 'type' returns undef" );
110 $debarments = GetDebarments({
111 borrowernumber => $borrowernumber,
114 is( @$debarments, 1, "DelUniqueDebarment without the argument 'type' does not delete the debarment" );
116 $delUniqueDebarment = DelUniqueDebarment({
119 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the argument 'borrowernumber' returns undef" );
120 $debarments = GetDebarments({
121 borrowernumber => $borrowernumber,
124 is( @$debarments, 1, "DelUniqueDebarment without the argument 'borrowerumber' does not delete the debarment" );
126 $delUniqueDebarment = DelUniqueDebarment({
127 borrowernumber => $borrowernumber,
128 type => 'SUSPENSION',
130 is( $delUniqueDebarment, undef, "DelUniqueDebarment with wrong arguments returns undef" );
131 $debarments = GetDebarments({
132 borrowernumber => $borrowernumber,
135 is( @$debarments, 1, "DelUniqueDebarment with wrong arguments does not delete the debarment" );
137 $delUniqueDebarment = DelUniqueDebarment({
138 borrowernumber => $borrowernumber,
141 is( $delUniqueDebarment, 1, "DelUniqueDebarment returns 1" );
142 $debarments = GetDebarments({
143 borrowernumber => $borrowernumber,
146 is( @$debarments, 0, "DelUniqueDebarment functions correctly" );
149 $debarments = GetDebarments({ borrowernumber => $borrowernumber });
150 foreach my $d ( @$debarments ) {
151 DelDebarment( $d->{'borrower_debarment_id'} );
153 $debarments = GetDebarments({ borrowernumber => $borrowernumber });
154 is( @$debarments, 0, "DelDebarment functions correctly" );
156 $dbh->do(q|UPDATE borrowers SET debarred = '1970-01-01'|);
157 is( IsDebarred( $borrowernumber ), undef, 'A patron with a debarred date in the past is not debarred' );
159 $dbh->do(q|UPDATE borrowers SET debarred = NULL|);
160 is( IsDebarred( $borrowernumber ), undef, 'A patron without a debarred date is not debarred' );
162 $dbh->do(q|UPDATE borrowers SET debarred = '9999-12-31'|); # Note: Change this test before the first of January 10000!
163 is( IsDebarred( $borrowernumber ), '9999-12-31', 'A patron with a debarred date in the future is debarred' );