Bug 15656 [QA Followup] - Return without searching if patron has no guarantor
[koha.git] / t / db_dependent / Koha / Patrons.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 5;
23
24 use Koha::Patron;
25 use Koha::Patrons;
26 use Koha::Database;
27
28 use t::lib::TestBuilder;
29
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 my $builder       = t::lib::TestBuilder->new;
34 my $library = $builder->build({source => 'Branch' });
35 my $category = $builder->build({source => 'Category' });
36 my $nb_of_patrons = Koha::Patrons->search->count;
37 my $new_patron_1  = Koha::Patron->new(
38     {   cardnumber => 'test_cn_1',
39         branchcode => $library->{branchcode},
40         categorycode => $category->{categorycode},
41         surname => 'surname for patron1',
42         firstname => 'firstname for patron1',
43     }
44 )->store;
45 my $new_patron_2  = Koha::Patron->new(
46     {   cardnumber => 'test_cn_2',
47         branchcode => $library->{branchcode},
48         categorycode => $category->{categorycode},
49         surname => 'surname for patron2',
50         firstname => 'firstname for patron2',
51     }
52 )->store;
53
54 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
55
56 my $retrieved_patron_1 = Koha::Patrons->find( $new_patron_1->borrowernumber );
57 is( $retrieved_patron_1->cardnumber, $new_patron_1->cardnumber, 'Find a patron by borrowernumber should return the correct patron' );
58
59 subtest 'guarantees' => sub {
60     plan tests => 8;
61     my $guarantees = $new_patron_1->guarantees;
62     is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
63     is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee' );
64     my @guarantees = $new_patron_1->guarantees;
65     is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
66     is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
67
68     my $guarantee_1 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
69     my $guarantee_2 = $builder->build({ source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber }});
70
71     $guarantees = $new_patron_1->guarantees;
72     is( ref($guarantees), 'Koha::Patrons', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
73     is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
74     @guarantees = $new_patron_1->guarantees;
75     is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantees should return an array in a list context' );
76     is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
77     $_->delete for @guarantees;
78 };
79
80 subtest 'siblings' => sub {
81     plan tests => 7;
82     my $siblings = $new_patron_1->siblings;
83     is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
84     my $guarantee_1 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
85     my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
86     $siblings = $retrieved_guarantee_1->siblings;
87     is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
88     my @siblings = $retrieved_guarantee_1->siblings;
89     is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
90     is( $siblings->count,  0,       'guarantee_1 should not have siblings yet' );
91     my $guarantee_2 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
92     my $guarantee_3 = $builder->build( { source => 'Borrower', value => { guarantorid => $new_patron_1->borrowernumber } } );
93     $siblings = $retrieved_guarantee_1->siblings;
94     is( $siblings->count,               2,                               'guarantee_1 should have 2 siblings' );
95     is( $guarantee_2->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_2 should exist in the guarantees' );
96     is( $guarantee_3->{borrowernumber}, $siblings->next->borrowernumber, 'guarantee_3 should exist in the guarantees' );
97     $_->delete for $retrieved_guarantee_1->siblings;
98     $retrieved_guarantee_1->delete;
99 };
100
101 $retrieved_patron_1->delete;
102 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
103
104 $schema->storage->txn_rollback;
105
106 1;