Bug 18936: Fix some more tests
[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 => 2;
21 use Test::MockModule;
22 use Test::Warn;
23
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Circulation;
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' => sub {
62     plan tests => 6;
63
64     t::lib::Mocks::mock_preference( 'ClaimReturnedLostValue', 1 );
65     my $biblio = $builder->build_object( { class => 'Koha::Biblios' } );
66     my $item   = $builder->build_object(
67         {
68             class => 'Koha::Items',
69             value => {
70                 biblionumber => $biblio->biblionumber,
71                 notforloan   => 0,
72                 itemlost     => 0,
73                 withdrawn    => 0,
74             }
75         }
76     );
77     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
78     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
79
80     my $claim = $checkout->claim_returned(
81         {
82             created_by => $patron->id,
83             notes      => "Test note",
84         }
85     );
86
87     is( $claim->issue_id, $checkout->id, "Claim issue id matches" );
88     is( $claim->itemnumber, $item->id, "Claim itemnumber matches" );
89     is( $claim->borrowernumber, $patron->id, "Claim borrowernumber matches" );
90     is( $claim->notes, "Test note", "Claim notes match" );
91     is( $claim->created_by, $patron->id, "Claim created_by matches" );
92     ok( $claim->created_on, "Claim created_on is set" );
93 };
94
95 subtest 'Test Koha::Patronn::return_claims' => sub {
96     plan tests => 7;
97
98     t::lib::Mocks::mock_preference( 'ClaimReturnedLostValue', 1 );
99     my $biblio = $builder->build_object( { class => 'Koha::Biblios' } );
100     my $item   = $builder->build_object(
101         {
102             class => 'Koha::Items',
103             value => {
104                 biblionumber => $biblio->biblionumber,
105                 notforloan   => 0,
106                 itemlost     => 0,
107                 withdrawn    => 0,
108             }
109         }
110     );
111     my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
112     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
113
114     $checkout->claim_returned(
115         {
116             created_by => $patron->id,
117             notes      => "Test note",
118         }
119     );
120
121     my $claims = $patron->return_claims;
122
123     is( $claims->count, 1, "Got back correct number of claims" );
124
125     my $claim = $claims->next;
126
127     is( $claim->issue_id, $checkout->id, "Claim issue id matches" );
128     is( $claim->itemnumber, $item->id, "Claim itemnumber matches" );
129     is( $claim->borrowernumber, $patron->id, "Claim borrowernumber matches" );
130     is( $claim->notes, "Test note", "Claim notes match" );
131     is( $claim->created_by, $patron->id, "Claim created_by matches" );
132     ok( $claim->created_on, "Claim created_on is set" );
133 };
134
135 $schema->storage->txn_rollback;