Bug 27004: Regression tests
[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 => 11;
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     my $nullified_created_by = $builder->build_object(
65         {
66             class => 'Koha::Checkouts::ReturnClaims',
67             value => {
68                 created_by => undef
69             }
70         }
71     );
72
73     is( $nullified_created_by->created_by, undef, 'Is undef' );
74     ok( $nullified_created_by->in_storage, 'In storage' );
75     is(
76         ref($nullified_created_by->notes('Some other note')->store),
77         'Koha::Checkouts::ReturnClaim',
78         'No exception, store success'
79     );
80
81     is( Koha::Checkouts::ReturnClaims->search({ issue_id => $checkout->id })->count, 0, 'No claims stored' );
82
83     my $claim = Koha::Checkouts::ReturnClaim->new(
84         {
85             issue_id       => $checkout->id,
86             itemnumber     => $checkout->itemnumber,
87             borrowernumber => $checkout->borrowernumber,
88             notes          => 'Some notes',
89             created_by     => $librarian->borrowernumber
90         }
91     )->store;
92
93     is( ref($claim), 'Koha::Checkouts::ReturnClaim', 'Object type is correct' );
94     is( Koha::Checkouts::ReturnClaims->search( { issue_id => $checkout->id } )->count, 1, 'Claim stored on the DB');
95
96     {   # hide useless warnings
97         local *STDERR;
98         open STDERR, '>', '/dev/null';
99         throws_ok {
100             Koha::Checkouts::ReturnClaim->new(
101                 {
102                     issue_id       => $checkout->id,
103                     itemnumber     => $checkout->itemnumber,
104                     borrowernumber => $checkout->borrowernumber,
105                     notes          => 'Some notes',
106                     created_by     => $librarian->borrowernumber
107                 }
108             )->store;
109         }
110         'Koha::Exceptions::Object::DuplicateID',
111             'An exception is thrown on duplicate issue_id';
112         close STDERR;
113
114         like(
115             $@->duplicate_id,
116             qr/(return_claims\.)?issue_id/,
117             'Exception field is correct'
118         );
119     }
120
121     {    # hide useless warnings
122         local *STDERR;
123         open STDERR, '>', '/dev/null';
124
125         my $another_checkout = $builder->build_object({ class => 'Koha::Checkouts' });
126         my $checkout_id = $another_checkout->id;
127         $another_checkout->delete;
128
129         my $THE_claim;
130
131         throws_ok {
132             $THE_claim = Koha::Checkouts::ReturnClaim->new(
133                 {
134                     issue_id       => $checkout_id,
135                     itemnumber     => $checkout->itemnumber,
136                     borrowernumber => $checkout->borrowernumber,
137                     notes          => 'Some notes',
138                     created_by     => $librarian->borrowernumber
139                 }
140             )->store;
141         }
142         'Koha::Exceptions::Object::FKConstraint',
143           'An exception is thrown on invalid issue_id';
144         close STDERR;
145
146         is( $@->broken_fk, 'issue_id', 'Exception field is correct' );
147     }
148
149     $schema->storage->txn_rollback;
150 };