Bug 14697: Introduce Koha::Checkouts::ReturnClaim(s)
[koha.git] / t / db_dependent / Koha / Checkouts / ReturnClaim.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 use Test::Exception;
22
23 use Koha::Database;
24 use Koha::Checkouts::ReturnClaims;
25
26 use t::lib::TestBuilder;
27
28 my $schema  = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30
31 subtest "store() tests" => sub {
32
33     plan tests => 6;
34
35     $schema->storage->txn_begin;
36
37     my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
38     my $patron    = $builder->build_object({ class => 'Koha::Patrons' });
39     my $item      = $builder->build_sample_item;
40
41     my $checkout = $builder->build_object(
42         {
43             class => 'Koha::Checkouts',
44             value => {
45                 borrowernumber => $patron->borrowernumber,
46                 itemnumber     => $item->itemnumber,
47                 branchcode     => $patron->branchcode
48             }
49         }
50     );
51
52     throws_ok
53         { Koha::Checkouts::ReturnClaim->new(
54             {
55                 issue_id       => $checkout->id,
56                 itemnumber     => $checkout->itemnumber,
57                 borrowernumber => $checkout->borrowernumber,
58                 notes          => 'Some notes'
59             }
60           )->store }
61         'Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy',
62         'Exception thrown correctly';
63
64     is( Koha::Checkouts::ReturnClaims->search({ issue_id => $checkout->id })->count, 0, 'No claims stored' );
65
66     my $claim = Koha::Checkouts::ReturnClaim->new(
67         {
68             issue_id       => $checkout->id,
69             itemnumber     => $checkout->itemnumber,
70             borrowernumber => $checkout->borrowernumber,
71             notes          => 'Some notes',
72             created_by     => $librarian->borrowernumber
73         }
74     )->store;
75
76     is( ref($claim), 'Koha::Checkouts::ReturnClaim', 'Object type is correct' );
77     is( Koha::Checkouts::ReturnClaims->search( { issue_id => $checkout->id } )->count, 1, 'Claim stored on the DB');
78
79     {   # hide useless warnings
80         local *STDERR;
81         open STDERR, '>', '/dev/null';
82         throws_ok {
83             Koha::Checkouts::ReturnClaim->new(
84                 {
85                     issue_id       => $checkout->id + 1000,
86                     itemnumber     => $checkout->itemnumber,
87                     borrowernumber => $checkout->borrowernumber,
88                     notes          => 'Some notes',
89                     created_by     => $librarian->borrowernumber
90                 }
91             )->store;
92         }
93         'Koha::Exceptions::Object::FKConstraint',
94             'An exception is thrown on invalid issue_id';
95         close STDERR;
96
97         is(
98             $@->broken_fk,
99             'issue_id',
100             'Exception field is correct'
101         );
102     }
103
104     $schema->storage->txn_rollback;
105 };