Bug 14577 - Allow restriction of checkouts based on fines of guarantor's guarantees
[koha.git] / t / db_dependent / Patron.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 15;
21 use Test::Warn;
22
23 use C4::Context;
24 use Koha::Database;
25
26 BEGIN {
27     use_ok('Koha::Object');
28     use_ok('Koha::Patron');
29 }
30
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 my $categorycode = $schema->resultset('Category')->first()->categorycode();
35 my $branchcode = $schema->resultset('Branch')->first()->branchcode();
36
37 my $object = Koha::Patron->new();
38
39 is( $object->in_storage, 0, "Object is not in storage" );
40
41 $object->categorycode( $categorycode );
42 $object->branchcode( $branchcode );
43 $object->surname("Test Surname");
44 $object->store();
45
46 is( $object->in_storage, 1, "Object is now stored" );
47 my $guarantee1 = Koha::Patron->new(
48     {
49         categorycode => $categorycode,
50         branchcode   => $branchcode,
51         guarantorid  => $object->id
52     }
53 )->store();
54 my $guarantee2 = Koha::Patron->new(
55     {
56         categorycode => $categorycode,
57         branchcode   => $branchcode,
58         guarantorid  => $object->id
59     }
60 )->store();
61 my @guarantees = $object->guarantees();
62 is( $guarantees[0]->id, $guarantee1->id, "First guarantee matchs" );
63 is( $guarantees[1]->id, $guarantee2->id, "Second guarantee matchs" );
64
65 my $borrowernumber = $object->borrowernumber;
66
67 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
68 is( $patron->surname(), "Test Surname", "Object found in database" );
69
70 is( $object->is_changed(), 0, "Object is unchanged" );
71 $object->surname("Test Surname");
72 is( $object->is_changed(), 0, "Object is still unchanged" );
73 $object->surname("Test Surname 2");
74 is( $object->is_changed(), 1, "Object is changed" );
75
76 $object->store();
77 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
78
79 $object->set({ firstname => 'Test Firstname' });
80 is( $object->is_changed(), 1, "Object is changed after Set" );
81 $object->store();
82 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
83
84 $object->delete();
85 $patron = $schema->resultset('Borrower')->find( $borrowernumber );
86 ok( ! $patron, "Object no longer found in database" );
87 is( $object->in_storage, 0, "Object is not in storage" );
88
89 1;