Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Circulation / ReturnClaims.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 => 3;
21 use Test::MockModule;
22 use Test::Warn;
23
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Circulation qw( LostItem AddIssue );
28
29 # Mock userenv, used by AddIssue
30 my $branch;
31 my $manager_id;
32 my $context = Test::MockModule->new('C4::Context');
33 $context->mock(
34     'userenv',
35     sub {
36         return {
37             branch    => $branch,
38             number    => $manager_id,
39             firstname => "Adam",
40             surname   => "Smaith"
41         };
42     }
43 );
44
45 my $schema = Koha::Database->schema;
46 $schema->storage->txn_begin;
47
48 my $builder = t::lib::TestBuilder->new();
49 Koha::CirculationRules->set_rule(
50     {
51         branchcode   => undef,
52         categorycode => undef,
53         itemtype     => undef,
54         rule_name    => 'issuelength',
55         rule_value   => 1
56     }
57 );
58
59 $branch = $builder->build( { source => 'Branch' } )->{branchcode};
60
61 subtest 'Test Koha::Checkout::claim_returned, do not mark as returned' => sub {
62     plan tests => 7;
63
64     t::lib::Mocks::mock_preference( 'ClaimReturnedLostValue', 1 );
65     t::lib::Mocks::mock_preference( 'MarkLostItemsAsReturned', q{} );
66     my $item   = $builder->build_sample_item;
67     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
68     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
69
70     my $claim = $checkout->claim_returned(
71         {
72             created_by => $patron->id,
73             notes      => "Test note",
74         }
75     );
76
77     is( $claim->issue_id, $checkout->id, "Claim issue id matches" );
78     is( $claim->itemnumber, $item->id, "Claim itemnumber matches" );
79     is( $claim->borrowernumber, $patron->id, "Claim borrowernumber matches" );
80     is( $claim->notes, "Test note", "Claim notes match" );
81     is( $claim->created_by, $patron->id, "Claim created_by matches" );
82     ok( $claim->created_on, "Claim created_on is set" );
83
84     my $checkout2 = Koha::Checkouts->find( $checkout->id );
85     is( $checkout2->id, $checkout->id, "Item is still checked out to patron")
86 };
87
88 subtest 'Test Koha::Patronn::return_claims' => sub {
89     plan tests => 7;
90
91     t::lib::Mocks::mock_preference( 'ClaimReturnedLostValue', 1 );
92     my $item   = $builder->build_sample_item;
93     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
94     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
95
96     $checkout->claim_returned(
97         {
98             created_by => $patron->id,
99             notes      => "Test note",
100         }
101     );
102
103     my $claims = $patron->return_claims;
104
105     is( $claims->count, 1, "Got back correct number of claims" );
106
107     my $claim = $claims->next;
108
109     is( $claim->issue_id, $checkout->id, "Claim issue id matches" );
110     is( $claim->itemnumber, $item->id, "Claim itemnumber matches" );
111     is( $claim->borrowernumber, $patron->id, "Claim borrowernumber matches" );
112     is( $claim->notes, "Test note", "Claim notes match" );
113     is( $claim->created_by, $patron->id, "Claim created_by matches" );
114     ok( $claim->created_on, "Claim created_on is set" );
115 };
116
117 subtest 'Test Koha::Checkout::claim_returned, mark as returned' => sub {
118     plan tests => 8;
119
120     t::lib::Mocks::mock_preference( 'ClaimReturnedLostValue', 1 );
121     t::lib::Mocks::mock_preference( 'MarkLostItemsAsReturned', q{claim_returned} );
122     my $item     = $builder->build_sample_item;
123     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
124     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
125
126     my $claim = $checkout->claim_returned(
127         {
128             created_by => $patron->id,
129             notes      => "Test note",
130         }
131     );
132
133     is( $claim->issue_id, $checkout->id, "Claim issue id matches" );
134     is( $claim->itemnumber, $item->id, "Claim itemnumber matches" );
135     is( $claim->borrowernumber, $patron->id, "Claim borrowernumber matches" );
136     is( $claim->notes, "Test note", "Claim notes match" );
137     is( $claim->created_by, $patron->id, "Claim created_by matches" );
138     ok( $claim->created_on, "Claim created_on is set" );
139
140     my $checkout2 = Koha::Checkouts->find( $checkout->id );
141     is( $checkout2, undef, "Checkout is not longer in the issues table");
142     $checkout2 = Koha::Old::Checkouts->find( $checkout->id );
143     is( $checkout2->id, $checkout->id, "Checkout was found in the old_issues table");
144 };
145
146 $schema->storage->txn_rollback;