3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 5;
28 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
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',
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',
54 is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
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' );
59 subtest 'guarantees' => sub {
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' );
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 }});
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;
80 subtest 'siblings' => sub {
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;
101 $retrieved_patron_1->delete;
102 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
104 $schema->storage->txn_rollback;