Line 0
Link Here
|
0 |
- |
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 => 4; |
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 |
|
81 |
$retrieved_patron_1->delete; |
82 |
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' ); |
83 |
|
84 |
$schema->storage->txn_rollback; |
85 |
|
86 |
1; |