Bug 14878: Tests - Create the branchcodes when needed
[koha.git] / t / db_dependent / Borrower_Debarments.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Context;
6 use C4::Members;
7 use Koha::Database;
8
9 use t::lib::TestBuilder;
10
11 use Test::More tests => 31;
12
13 use_ok('Koha::Borrower::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 $borrowernumber = AddMember(
25     firstname =>  'my firstname',
26     surname => 'my surname',
27     categorycode => 'S',
28     branchcode => $library->{branchcode},
29 );
30
31 my $success = AddDebarment({
32     borrowernumber => $borrowernumber,
33     expiration => '9999-06-10',
34     type => 'MANUAL',
35     comment => 'Test 1',
36 });
37 is( $success, 1, "AddDebarment returned true" );
38
39
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'" );
45
46
47 $success = AddDebarment({
48     borrowernumber => $borrowernumber,
49     comment => 'Test 2',
50 });
51
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'" );
57
58
59 ModDebarment({
60     borrower_debarment_id => $debarments->[1]->{'borrower_debarment_id'},
61     comment => 'Test 3',
62     expiration => '9998-06-10',
63 });
64 $debarments = GetDebarments({ borrowernumber => $borrowernumber });
65 is( $debarments->[1]->{'comment'}, 'Test 3', "ModDebarment functions correctly" );
66
67
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" );
71
72
73 AddUniqueDebarment({
74     borrowernumber => $borrowernumber,
75     type           => 'OVERDUES'
76 });
77 $debarments = GetDebarments({
78     borrowernumber => $borrowernumber,
79     type => 'OVERDUES',
80 });
81 is( @$debarments, 1, "GetDebarments returns 1 OVERDUES debarment" );
82 is( $debarments->[0]->{'type'}, 'OVERDUES', "AddOverduesDebarment created new debarment correctly" );
83
84 AddUniqueDebarment({
85     borrowernumber => $borrowernumber,
86     expiration => '9999-11-09',
87     type => 'OVERDUES'
88 });
89 $debarments = GetDebarments({
90     borrowernumber => $borrowernumber,
91     type => 'OVERDUES',
92 });
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" );
95
96
97 my $delUniqueDebarment = DelUniqueDebarment({
98 });
99 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the arguments 'borrowernumber' and 'type' returns undef" );
100 $debarments = GetDebarments({
101     borrowernumber => $borrowernumber,
102     type => 'OVERDUES',
103 });
104 is( @$debarments, 1, "DelUniqueDebarment without the arguments 'borrowernumber' and 'type' does not delete the debarment" );
105
106 $delUniqueDebarment = DelUniqueDebarment({
107     borrowernumber => $borrowernumber,
108 });
109 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the argument 'type' returns undef" );
110 $debarments = GetDebarments({
111     borrowernumber => $borrowernumber,
112     type => 'OVERDUES',
113 });
114 is( @$debarments, 1, "DelUniqueDebarment without the argument 'type' does not delete the debarment" );
115
116 $delUniqueDebarment = DelUniqueDebarment({
117     type => 'OVERDUES'
118 });
119 is( $delUniqueDebarment, undef, "DelUniqueDebarment without the argument 'borrowernumber' returns undef" );
120 $debarments = GetDebarments({
121     borrowernumber => $borrowernumber,
122     type => 'OVERDUES',
123 });
124 is( @$debarments, 1, "DelUniqueDebarment without the argument 'borrowerumber' does not delete the debarment" );
125
126 $delUniqueDebarment = DelUniqueDebarment({
127     borrowernumber => $borrowernumber,
128     type => 'SUSPENSION',
129 });
130 is( $delUniqueDebarment, undef, "DelUniqueDebarment with wrong arguments returns undef" );
131 $debarments = GetDebarments({
132     borrowernumber => $borrowernumber,
133     type => 'OVERDUES',
134 });
135 is( @$debarments, 1, "DelUniqueDebarment with wrong arguments does not delete the debarment" );
136
137 $delUniqueDebarment = DelUniqueDebarment({
138     borrowernumber => $borrowernumber,
139     type => 'OVERDUES',
140 });
141 is( $delUniqueDebarment, 1, "DelUniqueDebarment returns 1" );
142 $debarments = GetDebarments({
143     borrowernumber => $borrowernumber,
144     type => 'OVERDUES',
145 });
146 is( @$debarments, 0, "DelUniqueDebarment functions correctly" );
147
148
149 $debarments = GetDebarments({ borrowernumber => $borrowernumber });
150 foreach my $d ( @$debarments ) {
151     DelDebarment( $d->{'borrower_debarment_id'} );
152 }
153 $debarments = GetDebarments({ borrowernumber => $borrowernumber });
154 is( @$debarments, 0, "DelDebarment functions correctly" );
155
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' );
158
159 $dbh->do(q|UPDATE borrowers SET debarred = NULL|);
160 is( IsDebarred( $borrowernumber ), undef, 'A patron without a debarred date is not debarred' );
161
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' );