Lines 1-133
Link Here
|
1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
2 |
|
2 |
|
3 |
use Modern::Perl; |
3 |
use Modern::Perl; |
4 |
use Test::More tests => 14; |
4 |
use Test::More tests => 8; |
5 |
|
5 |
|
6 |
use C4::Context; |
6 |
use C4::Context; |
7 |
use t::lib::TestBuilder; |
7 |
use t::lib::TestBuilder; |
8 |
use C4::Members; |
8 |
use C4::Members; |
9 |
|
9 |
|
10 |
use Koha::Patron::Modifications; |
10 |
BEGIN { |
|
|
11 |
use_ok('Koha::Patron::Modification'); |
12 |
use_ok('Koha::Patron::Modifications'); |
13 |
} |
11 |
|
14 |
|
12 |
my $dbh = C4::Context->dbh; |
15 |
my $schema = Koha::Database->new->schema; |
13 |
$dbh->{RaiseError} = 1; |
16 |
$schema->storage->txn_begin; |
14 |
$dbh->{AutoCommit} = 0; |
|
|
15 |
|
17 |
|
|
|
18 |
my $dbh = C4::Context->dbh; |
16 |
$dbh->do("DELETE FROM borrower_modifications"); |
19 |
$dbh->do("DELETE FROM borrower_modifications"); |
17 |
|
20 |
|
18 |
## Create new pending modification |
21 |
## Create new pending modification |
19 |
Koha::Patron::Modifications->new( verification_token => '1234567890' ) |
22 |
Koha::Patron::Modification->new( |
20 |
->AddModifications( { surname => 'Hall', firstname => 'Kyle' } ); |
23 |
{ |
|
|
24 |
verification_token => '1234567890', |
25 |
surname => 'Hall', |
26 |
firstname => 'Kyle' |
27 |
} |
28 |
)->store(); |
21 |
|
29 |
|
22 |
## Get the new pending modification |
30 |
## Get the new pending modification |
23 |
my $borrower = Koha::Patron::Modifications->GetModifications( |
31 |
my $borrower = |
24 |
{ verification_token => '1234567890' } ); |
32 |
Koha::Patron::Modifications->find( { verification_token => '1234567890' } ); |
25 |
|
33 |
|
26 |
## Verify we get the same data |
34 |
## Verify we get the same data |
27 |
ok( $borrower->{'surname'} = 'Hall', |
35 |
is( $borrower->surname, 'Hall', 'Found modification has matching surname' ); |
28 |
'Test AddModifications() and GetModifications()' ); |
|
|
29 |
|
30 |
## Check the Verify method |
31 |
ok( |
32 |
Koha::Patron::Modifications->Verify('1234567890'), |
33 |
'Test that Verify() succeeds with a valid token' |
34 |
); |
35 |
|
36 |
## Delete the pending modification |
37 |
$borrower = Koha::Patron::Modifications->DelModifications( |
38 |
{ verification_token => '1234567890' } ); |
39 |
|
40 |
## Verify it's no longer in the database |
41 |
$borrower = Koha::Patron::Modifications->GetModifications( |
42 |
{ verification_token => '1234567890' } ); |
43 |
ok( !defined( $borrower->{'surname'} ), 'Test DelModifications()' ); |
44 |
|
45 |
## Check the Verify method |
46 |
ok( |
47 |
!Koha::Patron::Modifications->Verify('1234567890'), |
48 |
'Test that Verify() method fails for a bad token' |
49 |
); |
50 |
|
36 |
|
51 |
## Create new pending modification for a patron |
37 |
## Create new pending modification for a patron |
52 |
my $builder = t::lib::TestBuilder->new; |
38 |
my $builder = t::lib::TestBuilder->new; |
53 |
my $borr1 = $builder->build({ source => 'Borrower' })->{borrowernumber}; |
39 |
my $borr1 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; |
54 |
Koha::Patron::Modifications->new( borrowernumber => $borr1 ) |
40 |
|
55 |
->AddModifications( { surname => 'Hall', firstname => 'Kyle' } ); |
41 |
my $m1 = Koha::Patron::Modification->new( |
|
|
42 |
{ |
43 |
borrowernumber => $borr1, |
44 |
surname => 'Hall', |
45 |
firstname => 'Kyle' |
46 |
} |
47 |
)->store(); |
56 |
|
48 |
|
57 |
## Test the counter |
49 |
## Test the counter |
58 |
ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1, |
50 |
is( Koha::Patron::Modifications->pending_count, |
59 |
'Test GetPendingModificationsCount()' ); |
51 |
1, 'Test pending_count()' ); |
60 |
|
52 |
|
61 |
## Create new pending modification for another patron |
53 |
## Create new pending modification for another patron |
62 |
my $borr2 = $builder->build({ source => 'Borrower' })->{borrowernumber}; |
54 |
my $borr2 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; |
63 |
Koha::Patron::Modifications->new( borrowernumber => $borr2 ) |
55 |
my $m2 = Koha::Patron::Modification->new( |
64 |
->AddModifications( { surname => 'Smith', firstname => 'Sandy' } ); |
56 |
{ |
|
|
57 |
borrowernumber => $borr2, |
58 |
surname => 'Smith', |
59 |
firstname => 'Sandy' |
60 |
} |
61 |
)->store(); |
65 |
|
62 |
|
66 |
## Test the counter |
63 |
## Test the counter |
67 |
ok( |
64 |
is( |
68 |
Koha::Patron::Modifications->GetPendingModificationsCount() == 2, |
65 |
Koha::Patron::Modifications->pending_count(), 2, |
69 |
'Add a new pending modification and test GetPendingModificationsCount() again' |
66 |
'Add a new pending modification and test pending_count() again' |
70 |
); |
67 |
); |
71 |
|
68 |
|
72 |
## Check GetPendingModifications |
69 |
## Check GetPendingModifications |
73 |
my $pendings = Koha::Patron::Modifications->GetPendingModifications(); |
70 |
my $pendings = Koha::Patron::Modifications->pending; |
74 |
my @firstnames_mod = sort ( $pendings->[0]->{firstname}, $pendings->[1]->{firstname} ); |
71 |
my @firstnames_mod = |
75 |
ok( $firstnames_mod[0] eq 'Kyle', 'Test GetPendingModifications()' ); |
72 |
sort ( $pendings->[0]->{firstname}, $pendings->[1]->{firstname} ); |
76 |
ok( $firstnames_mod[1] eq 'Sandy', 'Test GetPendingModifications() again' ); |
73 |
ok( $firstnames_mod[0] eq 'Kyle', 'Test pending()' ); |
|
|
74 |
ok( $firstnames_mod[1] eq 'Sandy', 'Test pending() again' ); |
77 |
|
75 |
|
78 |
## This should delete the row from the table |
76 |
## This should delete the row from the table |
79 |
Koha::Patron::Modifications->DenyModifications( $borr2 ); |
77 |
$m2->delete(); |
80 |
|
|
|
81 |
## Test the counter |
82 |
ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1, |
83 |
'Test DenyModifications()' ); |
84 |
|
78 |
|
85 |
## Save a copy of the borrowers original data |
79 |
## Save a copy of the borrowers original data |
86 |
my $old_borrower = GetMember( borrowernumber => $borr1 ); |
80 |
my $old_borrower = GetMember( borrowernumber => $borr1 ); |
87 |
|
81 |
|
88 |
## Apply the modifications |
82 |
## Apply the modifications |
89 |
Koha::Patron::Modifications->ApproveModifications( $borr1 ); |
83 |
$m1->approve(); |
90 |
|
|
|
91 |
## Test the counter |
92 |
ok( |
93 |
Koha::Patron::Modifications->GetPendingModificationsCount() == 0, |
94 |
'Test ApproveModifications() removes pending modification from db' |
95 |
); |
96 |
|
84 |
|
97 |
## Get a copy of the borrowers current data |
85 |
## Get a copy of the borrowers current data |
98 |
my $new_borrower = GetMember( borrowernumber => $borr1 ); |
86 |
my $new_borrower = GetMember( borrowernumber => $borr1 ); |
99 |
|
87 |
|
100 |
## Check to see that the approved modifications were saved |
88 |
## Check to see that the approved modifications were saved |
101 |
ok( $new_borrower->{'surname'} eq 'Hall', |
89 |
ok( $new_borrower->{'surname'} eq 'Hall', |
102 |
'Test ApproveModifications() applys modification to borrower' ); |
90 |
'Test approve() applys modification to borrower' ); |
103 |
|
|
|
104 |
## Now let's put it back the way it was |
105 |
Koha::Patron::Modifications->new( borrowernumber => $borr1 )->AddModifications( |
106 |
{ |
107 |
surname => $old_borrower->{'surname'}, |
108 |
firstname => $old_borrower->{'firstname'} |
109 |
} |
110 |
); |
111 |
|
112 |
## Test the counter |
113 |
ok( Koha::Patron::Modifications->GetPendingModificationsCount() == 1, |
114 |
'Test GetPendingModificationsCount()' ); |
115 |
|
116 |
## Apply the modifications |
117 |
Koha::Patron::Modifications->ApproveModifications( $borr1 ); |
118 |
|
119 |
## Test the counter |
120 |
ok( |
121 |
Koha::Patron::Modifications->GetPendingModificationsCount() == 0, |
122 |
'Test ApproveModifications() removes pending modification from db, again' |
123 |
); |
124 |
|
125 |
$new_borrower = GetMember( borrowernumber => $borr1 ); |
126 |
|
127 |
## Test to verify the borrower has been updated with the original values |
128 |
ok( |
129 |
$new_borrower->{'surname'} eq $old_borrower->{'surname'}, |
130 |
'Test ApproveModifications() applys modification to borrower, again' |
131 |
); |
132 |
|
91 |
|
133 |
$dbh->rollback(); |
92 |
$dbh->rollback(); |
134 |
- |
|
|