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