|
Lines 17-32
Link Here
|
| 17 |
|
17 |
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 11; |
20 |
use Test::More tests => 6; |
| 21 |
use Test::Exception; |
21 |
use Test::Exception; |
| 22 |
|
22 |
|
| 23 |
use t::lib::TestBuilder; |
23 |
use t::lib::TestBuilder; |
| 24 |
|
24 |
|
| 25 |
use String::Random qw( random_string ); |
25 |
use Digest::MD5 qw( md5_base64 md5_hex ); |
| 26 |
use Try::Tiny; |
26 |
use Try::Tiny; |
| 27 |
|
27 |
|
| 28 |
use C4::Context; |
28 |
use C4::Context; |
| 29 |
use C4::Members; |
29 |
use C4::Members; |
|
|
30 |
use C4::Members::Attributes qw( GetBorrowerAttributes ); |
| 31 |
use Koha::Patrons; |
| 30 |
|
32 |
|
| 31 |
BEGIN { |
33 |
BEGIN { |
| 32 |
use_ok('Koha::Patron::Modification'); |
34 |
use_ok('Koha::Patron::Modification'); |
|
Lines 36-41
BEGIN {
Link Here
|
| 36 |
my $schema = Koha::Database->new->schema; |
38 |
my $schema = Koha::Database->new->schema; |
| 37 |
my $builder = t::lib::TestBuilder->new; |
39 |
my $builder = t::lib::TestBuilder->new; |
| 38 |
|
40 |
|
|
|
41 |
subtest 'new() tests' => sub { |
| 42 |
|
| 43 |
plan tests => 3; |
| 44 |
|
| 45 |
$schema->storage->txn_begin; |
| 46 |
|
| 47 |
Koha::Patron::Modifications->search->delete; |
| 48 |
|
| 49 |
# Create new pending modification |
| 50 |
Koha::Patron::Modification->new( |
| 51 |
{ verification_token => '1234567890', |
| 52 |
surname => 'Hall', |
| 53 |
firstname => 'Kyle' |
| 54 |
} |
| 55 |
)->store(); |
| 56 |
|
| 57 |
## Get the new pending modification |
| 58 |
my $borrower = Koha::Patron::Modifications->find( |
| 59 |
{ verification_token => '1234567890' } ); |
| 60 |
|
| 61 |
## Verify we get the same data |
| 62 |
is( $borrower->surname, 'Hall', |
| 63 |
'Found modification has matching surname' ); |
| 64 |
|
| 65 |
throws_ok { |
| 66 |
Koha::Patron::Modification->new( |
| 67 |
{ verification_token => '1234567890', |
| 68 |
surname => 'Hall', |
| 69 |
firstname => 'Daria' |
| 70 |
} |
| 71 |
)->store(); |
| 72 |
} |
| 73 |
'Koha::Exceptions::Patron::Modification::DuplicateVerificationToken', |
| 74 |
'Attempting to add a duplicate verification raises the correct exception'; |
| 75 |
is( $@, |
| 76 |
'Duplicate verification token 1234567890', |
| 77 |
'Exception carries the right message' |
| 78 |
); |
| 79 |
|
| 80 |
$schema->storage->txn_rollback; |
| 81 |
}; |
| 39 |
|
82 |
|
| 40 |
subtest 'store( extended_attributes ) tests' => sub { |
83 |
subtest 'store( extended_attributes ) tests' => sub { |
| 41 |
|
84 |
|
|
Lines 47-55
subtest 'store( extended_attributes ) tests' => sub {
Link Here
|
| 47 |
|
90 |
|
| 48 |
my $patron |
91 |
my $patron |
| 49 |
= $builder->build( { source => 'Borrower' } )->{borrowernumber}; |
92 |
= $builder->build( { source => 'Borrower' } )->{borrowernumber}; |
| 50 |
my $verification_token = random_string(".........."); |
93 |
my $verification_token = md5_hex( time().{}.rand().{}.$$ ); |
| 51 |
my $valid_json_text = '[{"code":"CODE","value":"VALUE"}]'; |
94 |
my $valid_json_text = '[{"code":"CODE","value":"VALUE"}]'; |
| 52 |
my $invalid_json_text = '[{'; |
95 |
my $invalid_json_text = '[{"code":"CODE";"value":"VALUE"}]'; |
| 53 |
|
96 |
|
| 54 |
Koha::Patron::Modification->new( |
97 |
Koha::Patron::Modification->new( |
| 55 |
{ verification_token => $verification_token, |
98 |
{ verification_token => $verification_token, |
|
Lines 69-75
subtest 'store( extended_attributes ) tests' => sub {
Link Here
|
| 69 |
$valid_json_text, |
112 |
$valid_json_text, |
| 70 |
'Patron modification correctly stored with valid JSON data' ); |
113 |
'Patron modification correctly stored with valid JSON data' ); |
| 71 |
|
114 |
|
| 72 |
$verification_token = random_string(".........."); |
115 |
$verification_token = md5_hex( time().{}.rand().{}.$$ ); |
| 73 |
throws_ok { |
116 |
throws_ok { |
| 74 |
Koha::Patron::Modification->new( |
117 |
Koha::Patron::Modification->new( |
| 75 |
{ verification_token => $verification_token, |
118 |
{ verification_token => $verification_token, |
|
Lines 87-183
subtest 'store( extended_attributes ) tests' => sub {
Link Here
|
| 87 |
$schema->storage->txn_rollback; |
130 |
$schema->storage->txn_rollback; |
| 88 |
}; |
131 |
}; |
| 89 |
|
132 |
|
|
|
133 |
subtest 'approve tests' => sub { |
| 90 |
|
134 |
|
| 91 |
$schema->storage->txn_begin; |
135 |
plan tests => 7; |
| 92 |
|
136 |
|
| 93 |
my $dbh = C4::Context->dbh; |
137 |
$schema->storage->txn_begin; |
| 94 |
$dbh->do("DELETE FROM borrower_modifications"); |
|
|
| 95 |
|
138 |
|
| 96 |
## Create new pending modification |
139 |
Koha::Patron::Modifications->search->delete; |
| 97 |
Koha::Patron::Modification->new( |
|
|
| 98 |
{ |
| 99 |
verification_token => '1234567890', |
| 100 |
surname => 'Hall', |
| 101 |
firstname => 'Kyle' |
| 102 |
} |
| 103 |
)->store(); |
| 104 |
|
140 |
|
| 105 |
## Ensure duplicate verification tokens cannot be added to the database |
141 |
my $patron_hashref = $builder->build( { source => 'Borrower' } ); |
| 106 |
try { |
142 |
$builder->build( |
| 107 |
Koha::Patron::Modification->new( |
143 |
{ source => 'BorrowerAttributeType', value => { code => 'CODE_1' } } |
| 108 |
{ |
144 |
); |
| 109 |
verification_token => '1234567890', |
145 |
$builder->build( |
| 110 |
surname => 'Hall', |
146 |
{ source => 'BorrowerAttributeType', value => { code => 'CODE_2' } } |
| 111 |
firstname => 'Daria' |
147 |
); |
|
|
148 |
my $verification_token = md5_hex( time().{}.rand().{}.$$ ); |
| 149 |
my $valid_json_text |
| 150 |
= '[{"code":"CODE_1","value":"VALUE_1"},{"code":"CODE_2","value":"VALUE_2"}]'; |
| 151 |
my $patron_modification = Koha::Patron::Modification->new( |
| 152 |
{ borrowernumber => $patron_hashref->{borrowernumber}, |
| 153 |
firstname => 'Kyle', |
| 154 |
verification_token => $verification_token, |
| 155 |
extended_attributes => $valid_json_text |
| 112 |
} |
156 |
} |
| 113 |
)->store(); |
157 |
)->store(); |
| 114 |
} catch { |
158 |
|
| 115 |
ok( $_->isa('Koha::Exceptions::Patron::Modification::DuplicateVerificationToken'), |
159 |
ok( $patron_modification->approve, |
| 116 |
'Attempting to add a duplicate verification token to the database should raise a Koha::Exceptions::Koha::Patron::Modification::DuplicateVerificationToken exception' ); |
160 |
'Patron modification correctly approved' ); |
| 117 |
is( $_->message, "Duplicate verification token 1234567890", 'Exception carries the right message' ); |
161 |
my $patron = Koha::Patrons->find( $patron_hashref->{borrowernumber} ); |
|
|
162 |
isnt( |
| 163 |
$patron->firstname, |
| 164 |
$patron_hashref->{firstname}, |
| 165 |
'Patron modification changed firstname' |
| 166 |
); |
| 167 |
is( $patron->firstname, 'Kyle', |
| 168 |
'Patron modification set the right firstname' ); |
| 169 |
my @patron_attributes = GetBorrowerAttributes( $patron->borrowernumber ); |
| 170 |
is( $patron_attributes[0][0]->{code}, |
| 171 |
'CODE_1', 'Patron modification correctly saved attribute code' ); |
| 172 |
is( $patron_attributes[0][0]->{value}, |
| 173 |
'VALUE_1', 'Patron modification correctly saved attribute value' ); |
| 174 |
|
| 175 |
# Create a new Koha::Patron::Modification, skip extended_attributes to |
| 176 |
# bypass checks |
| 177 |
$patron_modification = Koha::Patron::Modification->new( |
| 178 |
{ borrowernumber => $patron_hashref->{borrowernumber}, |
| 179 |
firstname => 'Kylie', |
| 180 |
verification_token => $verification_token |
| 181 |
} |
| 182 |
)->store(); |
| 183 |
|
| 184 |
# Add invalid JSON to extended attributes |
| 185 |
$patron_modification->extended_attributes( |
| 186 |
'[{"code":"CODE";"values:VALUES"}]'); |
| 187 |
throws_ok { $patron_modification->approve } |
| 188 |
'Koha::Exceptions::Patron::Modification::InvalidData', |
| 189 |
'The right exception is thrown if invalid data is on extended_attributes'; |
| 190 |
|
| 191 |
$patron = Koha::Patrons->find( $patron_hashref->{borrowernumber} ); |
| 192 |
isnt( $patron->firstname, 'Kylie', 'Patron modification didn\'t apply' ); |
| 193 |
|
| 194 |
$schema->storage->txn_rollback; |
| 118 |
}; |
195 |
}; |
| 119 |
|
196 |
|
| 120 |
## Get the new pending modification |
197 |
subtest 'pending_count() and pending() tests' => sub { |
| 121 |
my $borrower = |
|
|
| 122 |
Koha::Patron::Modifications->find( { verification_token => '1234567890' } ); |
| 123 |
|
198 |
|
| 124 |
## Verify we get the same data |
199 |
plan tests => 7; |
| 125 |
is( $borrower->surname, 'Hall', 'Found modification has matching surname' ); |
|
|
| 126 |
|
200 |
|
| 127 |
## Create new pending modification for a patron |
201 |
$schema->storage->txn_begin; |
| 128 |
my $borr1 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; |
|
|
| 129 |
|
202 |
|
| 130 |
my $m1 = Koha::Patron::Modification->new( |
203 |
Koha::Patron::Modifications->search->delete; |
| 131 |
{ |
204 |
my $library_1 = $builder->build( { source => 'Branch' } )->{branchcode}; |
| 132 |
borrowernumber => $borr1, |
205 |
my $library_2 = $builder->build( { source => 'Branch' } )->{branchcode}; |
| 133 |
surname => 'Hall', |
206 |
my $patron_1 |
| 134 |
firstname => 'Kyle' |
207 |
= $builder->build( |
| 135 |
} |
208 |
{ source => 'Borrower', value => { branchcode => $library_1 } } ) |
| 136 |
)->store(); |
209 |
->{borrowernumber}; |
| 137 |
|
210 |
my $patron_2 |
| 138 |
## Test the counter |
211 |
= $builder->build( |
| 139 |
is( Koha::Patron::Modifications->pending_count, |
212 |
{ source => 'Borrower', value => { branchcode => $library_2 } } ) |
| 140 |
1, 'Test pending_count()' ); |
213 |
->{borrowernumber}; |
| 141 |
|
214 |
my $patron_3 |
| 142 |
## Create new pending modification for another patron |
215 |
= $builder->build( |
| 143 |
my $borr2 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; |
216 |
{ source => 'Borrower', value => { branchcode => $library_2 } } ) |
| 144 |
my $m2 = Koha::Patron::Modification->new( |
217 |
->{borrowernumber}; |
| 145 |
{ |
218 |
my $verification_token_1 = md5_hex( time().{}.rand().{}.$$ ); |
| 146 |
borrowernumber => $borr2, |
219 |
my $verification_token_2 = md5_hex( time().{}.rand().{}.$$ ); |
| 147 |
surname => 'Smith', |
220 |
my $verification_token_3 = md5_hex( time().{}.rand().{}.$$ ); |
| 148 |
firstname => 'Sandy' |
221 |
|
| 149 |
} |
222 |
|
| 150 |
)->store(); |
223 |
my $modification_1 = Koha::Patron::Modification->new( |
|
|
224 |
{ borrowernumber => $patron_1, |
| 225 |
surname => 'Hall', |
| 226 |
firstname => 'Kyle', |
| 227 |
verification_token => $verification_token_1 |
| 228 |
} |
| 229 |
)->store(); |
| 151 |
|
230 |
|
| 152 |
## Test the counter |
231 |
is( Koha::Patron::Modifications->pending_count, |
| 153 |
is( |
232 |
1, 'pending_count() correctly returns 1' ); |
| 154 |
Koha::Patron::Modifications->pending_count(), 2, |
|
|
| 155 |
'Add a new pending modification and test pending_count() again' |
| 156 |
); |
| 157 |
|
233 |
|
| 158 |
## Check GetPendingModifications |
234 |
my $modification_2 = Koha::Patron::Modification->new( |
| 159 |
my $pendings = Koha::Patron::Modifications->pending; |
235 |
{ borrowernumber => $patron_2, |
| 160 |
my @firstnames_mod = |
236 |
surname => 'Smith', |
| 161 |
sort ( $pendings->[0]->{firstname}, $pendings->[1]->{firstname} ); |
237 |
firstname => 'Sandy', |
| 162 |
ok( $firstnames_mod[0] eq 'Kyle', 'Test pending()' ); |
238 |
verification_token => $verification_token_2 |
| 163 |
ok( $firstnames_mod[1] eq 'Sandy', 'Test pending() again' ); |
239 |
} |
|
|
240 |
)->store(); |
| 241 |
|
| 242 |
my $modification_3 = Koha::Patron::Modification->new( |
| 243 |
{ borrowernumber => $patron_3, |
| 244 |
surname => 'Smith', |
| 245 |
firstname => 'Sandy', |
| 246 |
verification_token => $verification_token_3 |
| 247 |
} |
| 248 |
)->store(); |
| 164 |
|
249 |
|
| 165 |
## This should delete the row from the table |
250 |
is( Koha::Patron::Modifications->pending_count, |
| 166 |
$m2->delete(); |
251 |
3, 'pending_count() correctly returns 3' ); |
| 167 |
|
252 |
|
| 168 |
## Save a copy of the borrowers original data |
253 |
is( Koha::Patron::Modifications->pending_count($library_1), |
| 169 |
my $old_borrower = GetMember( borrowernumber => $borr1 ); |
254 |
1, 'pending_count() correctly returns 1 if filtered by library' ); |
| 170 |
|
255 |
|
| 171 |
## Apply the modifications |
256 |
is( Koha::Patron::Modifications->pending_count($library_2), |
| 172 |
$m1->approve(); |
257 |
2, 'pending_count() correctly returns 2 if filtered by library' ); |
| 173 |
|
258 |
|
| 174 |
## Get a copy of the borrowers current data |
259 |
$modification_1->approve; |
| 175 |
my $new_borrower = GetMember( borrowernumber => $borr1 ); |
|
|
| 176 |
|
260 |
|
| 177 |
## Check to see that the approved modifications were saved |
261 |
is( Koha::Patron::Modifications->pending_count, |
| 178 |
ok( $new_borrower->{'surname'} eq 'Hall', |
262 |
2, 'pending_count() correctly returns 2' ); |
| 179 |
'Test approve() applies modification to borrower' ); |
|
|
| 180 |
|
263 |
|
| 181 |
$schema->storage->txn_rollback; |
264 |
$modification_2->approve; |
|
|
265 |
|
| 266 |
is( Koha::Patron::Modifications->pending_count, |
| 267 |
1, 'pending_count() correctly returns 1' ); |
| 268 |
|
| 269 |
$modification_3->approve; |
| 270 |
|
| 271 |
is( Koha::Patron::Modifications->pending_count, |
| 272 |
0, 'pending_count() correctly returns 0' ); |
| 273 |
|
| 274 |
$schema->storage->txn_rollback; |
| 275 |
}; |
| 182 |
|
276 |
|
| 183 |
1; |
277 |
1; |
| 184 |
- |
|
|