Bug 22368: Make sure the tests will always pass
[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::Context;
23 use C4::Circulation;
24
25 use Koha::Database;
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(
49         {
50             source => 'Item',
51             value  => {
52                 itemlost  => 0,
53                 withdrawn => 0,
54             },
55         }
56     );
57
58     my $issue = $builder->build(
59         {
60             source => 'Issue',
61             value  => {
62                 borrowernumber => $patron->{borrowernumber},
63                 itemnumber     => $item->{itemnumber},
64             },
65         }
66     );
67
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' );
71
72     my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, $patron->{branchcode}, undef, undef, '2010-10-10' );
73
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' );
77
78     $patron = $builder->build(
79         {
80             source => 'Borrower',
81             value  => { privacy => 1, }
82         }
83     );
84
85     $issue = $builder->build(
86         {
87             source => 'Issue',
88             value  => {
89                 borrowernumber => $patron->{borrowernumber},
90                 itemnumber     => $item->{itemnumber},
91             },
92         }
93     );
94
95     ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, $patron->{branchcode}, undef, undef, '2010-10-10' );
96
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' );
100
101     $patron = $builder->build(
102         {
103             source => 'Borrower',
104             value  => { privacy => 1, }
105         }
106     );
107     $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
108
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' );
114
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' );
118
119     t::lib::Mocks::mock_preference( 'StoreLastBorrower', '0' );
120     $patron = $builder->build(
121         {
122             source => 'Borrower',
123             value  => { privacy => 1, }
124         }
125     );
126
127     $issue = $builder->build(
128         {
129             source => 'Issue',
130             value  => {
131                 borrowernumber => $patron->{borrowernumber},
132                 itemnumber     => $item->{itemnumber},
133             },
134         }
135     );
136     ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, $patron->{branchcode}, undef, undef, '2010-10-10' );
137
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' );
140 };
141
142 $schema->storage->txn_rollback;
143