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>.
20 use Test::More tests => 1;
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 {
39 t::lib::Mocks::mock_preference( 'StoreLastBorrower', '1' );
41 my $patron = $builder->build(
44 value => { privacy => 1, }
48 my $item = $builder->build_sample_item;
50 my $issue = $builder->build(
54 borrowernumber => $patron->{borrowernumber},
55 itemnumber => $item->itemnumber,
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' );
64 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, $patron->{branchcode}, undef, dt_from_string('2010-10-10') );
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' );
70 $patron = $builder->build(
73 value => { privacy => 1, }
77 $issue = $builder->build(
81 borrowernumber => $patron->{borrowernumber},
82 itemnumber => $item->itemnumber,
87 ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, $patron->{branchcode}, undef, dt_from_string('2010-10-10') );
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' );
93 $patron = $builder->build(
96 value => { privacy => 1, }
99 $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
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' );
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' );
111 t::lib::Mocks::mock_preference( 'StoreLastBorrower', '0' );
112 $patron = $builder->build(
114 source => 'Borrower',
115 value => { privacy => 1, }
119 $issue = $builder->build(
123 borrowernumber => $patron->{borrowernumber},
124 itemnumber => $item->itemnumber,
128 ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, $patron->{branchcode}, undef, dt_from_string('2010-10-10') );
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' );
134 $schema->storage->txn_rollback;