Bug 17278: Fix test compilation errors
[koha.git] / t / db_dependent / Koha_borrower_modifications.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 10;
5 use Try::Tiny;
6
7 use t::lib::TestBuilder;
8
9 use C4::Context;
10 use C4::Members;
11
12 BEGIN {
13     use_ok('Koha::Patron::Modification');
14     use_ok('Koha::Patron::Modifications');
15 }
16
17 my $schema = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
19
20 my $dbh = C4::Context->dbh;
21 $dbh->do("DELETE FROM borrower_modifications");
22
23 ## Create new pending modification
24 Koha::Patron::Modification->new(
25     {
26         verification_token => '1234567890',
27         surname            => 'Hall',
28         firstname          => 'Kyle'
29     }
30 )->store();
31
32 ## Ensure duplicate verification tokens cannot be added to the database
33 try {
34     Koha::Patron::Modification->new(
35         {
36             verification_token => '1234567890',
37             surname            => 'Hall',
38             firstname          => 'Daria'
39         }
40     )->store();
41 } catch {
42     ok( $_->isa('Koha::Exceptions::Patron::Modification::DuplicateVerificationToken'),
43         'Attempting to add a duplicate verification token to the database should raise a Koha::Exceptions::Koha::Patron::Modification::DuplicateVerificationToken exception' );
44     is( $_->message, "Duplicate verification token 1234567890", 'Exception carries the right message' );
45 };
46
47 ## Get the new pending modification
48 my $borrower =
49   Koha::Patron::Modifications->find( { verification_token => '1234567890' } );
50
51 ## Verify we get the same data
52 is( $borrower->surname, 'Hall', 'Found modification has matching surname' );
53
54 ## Create new pending modification for a patron
55 my $builder = t::lib::TestBuilder->new;
56 my $borr1 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
57
58 my $m1 = Koha::Patron::Modification->new(
59     {
60         borrowernumber => $borr1,
61         surname        => 'Hall',
62         firstname      => 'Kyle'
63     }
64 )->store();
65
66 ## Test the counter
67 is( Koha::Patron::Modifications->pending_count,
68     1, 'Test pending_count()' );
69
70 ## Create new pending modification for another patron
71 my $borr2 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
72 my $m2 = Koha::Patron::Modification->new(
73     {
74         borrowernumber => $borr2,
75         surname        => 'Smith',
76         firstname      => 'Sandy'
77     }
78 )->store();
79
80 ## Test the counter
81 is(
82     Koha::Patron::Modifications->pending_count(), 2,
83 'Add a new pending modification and test pending_count() again'
84 );
85
86 ## Check GetPendingModifications
87 my $pendings = Koha::Patron::Modifications->pending;
88 my @firstnames_mod =
89   sort ( $pendings->[0]->{firstname}, $pendings->[1]->{firstname} );
90 ok( $firstnames_mod[0] eq 'Kyle',  'Test pending()' );
91 ok( $firstnames_mod[1] eq 'Sandy', 'Test pending() again' );
92
93 ## This should delete the row from the table
94 $m2->delete();
95
96 ## Save a copy of the borrowers original data
97 my $old_borrower = GetMember( borrowernumber => $borr1 );
98
99 ## Apply the modifications
100 $m1->approve();
101
102 ## Get a copy of the borrowers current data
103 my $new_borrower = GetMember( borrowernumber => $borr1 );
104
105 ## Check to see that the approved modifications were saved
106 ok( $new_borrower->{'surname'} eq 'Hall',
107     'Test approve() applies modification to borrower' );
108
109 $dbh->rollback();