Bug 28320: Add DB connection check to the SIP SC status message
[koha.git] / t / db_dependent / Circulation / StoreLastBorrower.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 => 1;
21
22 use C4::Circulation;
23 use C4::Context;
24 use Koha::Database;
25 use Koha::DateUtils;
26 use Koha::Items;
27
28 use t::lib::Mocks;
29 use t::lib::TestBuilder;
30
31 my $schema  = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'Test StoreLastBorrower' => sub {
37     plan tests => 6;
38
39     t::lib::Mocks::mock_preference( 'StoreLastBorrower', '1' );
40
41     my $patron = $builder->build(
42         {
43             source => 'Borrower',
44             value  => { privacy => 1, }
45         }
46     );
47
48     my $item = $builder->build_sample_item;
49
50     my $issue = $builder->build(
51         {
52             source => 'Issue',
53             value  => {
54                 borrowernumber => $patron->{borrowernumber},
55                 itemnumber     => $item->itemnumber,
56             },
57         }
58     );
59
60     $item = $item->get_from_storage;
61     my $patron_object = $item->last_returned_by();
62     is( $patron_object, undef, 'Koha::Item::last_returned_by returned undef' );
63
64     my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, $patron->{branchcode},  undef, dt_from_string('2010-10-10') );
65
66     $item = $item->get_from_storage;
67     $patron_object = $item->last_returned_by();
68     is( ref($patron_object), 'Koha::Patron', 'Koha::Item::last_returned_by returned Koha::Patron' );
69
70     $patron = $builder->build(
71         {
72             source => 'Borrower',
73             value  => { privacy => 1, }
74         }
75     );
76
77     $issue = $builder->build(
78         {
79             source => 'Issue',
80             value  => {
81                 borrowernumber => $patron->{borrowernumber},
82                 itemnumber     => $item->itemnumber,
83             },
84         }
85     );
86
87     ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, $patron->{branchcode}, undef, dt_from_string('2010-10-10') );
88
89     $item = $item->get_from_storage;
90     $patron_object = $item->last_returned_by();
91     is( $patron_object->id, $patron->{borrowernumber}, 'Second patron to return item replaces the first' );
92
93     $patron = $builder->build(
94         {
95             source => 'Borrower',
96             value  => { privacy => 1, }
97         }
98     );
99     $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
100
101     $item->last_returned_by($patron_object);
102     $item = $item->get_from_storage;
103     my $patron_object2 = $item->last_returned_by();
104     is( $patron_object->id, $patron_object2->id,
105         'Calling last_returned_by with Borrower object sets last_returned_by to that borrower' );
106
107     $patron_object->delete;
108     $item = $item->get_from_storage;
109     is( $item->last_returned_by, undef, 'last_returned_by should return undef if the last patron to return the item has been deleted' );
110
111     t::lib::Mocks::mock_preference( 'StoreLastBorrower', '0' );
112     $patron = $builder->build(
113         {
114             source => 'Borrower',
115             value  => { privacy => 1, }
116         }
117     );
118
119     $issue = $builder->build(
120         {
121             source => 'Issue',
122             value  => {
123                 borrowernumber => $patron->{borrowernumber},
124                 itemnumber     => $item->itemnumber,
125             },
126         }
127     );
128     ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, $patron->{branchcode}, undef, dt_from_string('2010-10-10') );
129
130     $item = $item->get_from_storage;
131     is( $item->last_returned_by, undef, 'Last patron to return item should not be stored if StoreLastBorrower if off' );
132 };
133
134 $schema->storage->txn_rollback;
135