Bug 23382: (follow-up) Ensure closed day is inside charge window
[koha.git] / t / db_dependent / Circulation / StoreLastBorrower.t
blob37f2c7e7364a97642bbf7615a612ecaff9e2b729
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 1;
22 use C4::Circulation;
23 use C4::Context;
24 use Koha::Database;
25 use Koha::DateUtils;
26 use Koha::Items;
28 use t::lib::Mocks;
29 use t::lib::TestBuilder;
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'Test StoreLastBorrower' => sub {
37 plan tests => 6;
39 t::lib::Mocks::mock_preference( 'StoreLastBorrower', '1' );
41 my $patron = $builder->build(
43 source => 'Borrower',
44 value => { privacy => 1, }
48 my $item = $builder->build(
50 source => 'Item',
51 value => {
52 itemlost => 0,
53 withdrawn => 0,
58 my $issue = $builder->build(
60 source => 'Issue',
61 value => {
62 borrowernumber => $patron->{borrowernumber},
63 itemnumber => $item->{itemnumber},
68 my $item_object = Koha::Items->find( $item->{itemnumber} );
69 my $patron_object = $item_object->last_returned_by();
70 is( $patron_object, undef, 'Koha::Item::last_returned_by returned undef' );
72 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, $patron->{branchcode}, undef, dt_from_string('2010-10-10') );
74 $item_object = Koha::Items->find( $item->{itemnumber} );
75 $patron_object = $item_object->last_returned_by();
76 is( ref($patron_object), 'Koha::Patron', 'Koha::Item::last_returned_by returned Koha::Patron' );
78 $patron = $builder->build(
80 source => 'Borrower',
81 value => { privacy => 1, }
85 $issue = $builder->build(
87 source => 'Issue',
88 value => {
89 borrowernumber => $patron->{borrowernumber},
90 itemnumber => $item->{itemnumber},
95 ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, $patron->{branchcode}, undef, dt_from_string('2010-10-10') );
97 $item_object = Koha::Items->find( $item->{itemnumber} );
98 $patron_object = $item_object->last_returned_by();
99 is( $patron_object->id, $patron->{borrowernumber}, 'Second patron to return item replaces the first' );
101 $patron = $builder->build(
103 source => 'Borrower',
104 value => { privacy => 1, }
107 $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
109 $item_object->last_returned_by($patron_object);
110 $item_object = Koha::Items->find( $item->{itemnumber} );
111 my $patron_object2 = $item_object->last_returned_by();
112 is( $patron_object->id, $patron_object2->id,
113 'Calling last_returned_by with Borrower object sets last_returned_by to that borrower' );
115 $patron_object->delete;
116 $item_object = Koha::Items->find( $item->{itemnumber} );
117 is( $item_object->last_returned_by, undef, 'last_returned_by should return undef if the last patron to return the item has been deleted' );
119 t::lib::Mocks::mock_preference( 'StoreLastBorrower', '0' );
120 $patron = $builder->build(
122 source => 'Borrower',
123 value => { privacy => 1, }
127 $issue = $builder->build(
129 source => 'Issue',
130 value => {
131 borrowernumber => $patron->{borrowernumber},
132 itemnumber => $item->{itemnumber},
136 ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, $patron->{branchcode}, undef, dt_from_string('2010-10-10') );
138 $item_object = Koha::Items->find( $item->{itemnumber} );
139 is( $item_object->last_returned_by, undef, 'Last patron to return item should not be stored if StoreLastBorrower if off' );
142 $schema->storage->txn_rollback;