Bug 30719: (QA follow-up) Rename illbatches endpoint to ill/batches
[koha.git] / t / db_dependent / Illbatches.t
1 #s!/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 File::Basename qw/basename/;
21 use Koha::Database;
22 use Koha::Illbatch;
23 use Koha::Illbatches;
24 use Koha::Illrequests;
25 use Koha::Patrons;
26 use t::lib::Mocks;
27 use t::lib::TestBuilder;
28 use Test::MockObject;
29 use Test::MockModule;
30
31 use Test::More tests => 8;
32
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35 use_ok('Koha::Illbatch');
36 use_ok('Koha::Illbatches');
37
38 $schema->storage->txn_begin;
39
40 Koha::Illrequests->search->delete;
41
42 # Create a patron
43 my $patron = $builder->build( { source => 'Borrower' } );
44
45 # Create a librarian
46 my $librarian = $builder->build(
47     {
48         source => 'Borrower',
49         value  => { firstname => "Grogu" }
50     }
51 );
52
53 # Create a branch
54 my $branch = $builder->build( { source => 'Branch' } );
55
56 # Create a batch
57 my $illbatch = $builder->build(
58     {
59         source => 'Illbatch',
60         value  => {
61             name           => "My test batch",
62             backend        => "Mock",
63             borrowernumber => $librarian->{borrowernumber},
64             branchcode     => $branch->{branchcode}
65         }
66     }
67 );
68 my $batch_obj = Koha::Illbatches->find( $illbatch->{id} );
69 isa_ok( $batch_obj, 'Koha::Illbatch' );
70
71 # Create an ILL request in the batch
72 my $illrq = $builder->build(
73     {
74         source => 'Illrequest',
75         value  => {
76             borrowernumber => $patron->{borrowernumber},
77             batch_id       => $illbatch->{id}
78         }
79     }
80 );
81 my $illrq_obj = Koha::Illrequests->find( $illrq->{illrequest_id} );
82
83 # Check requests_count
84 my $requests_count = $batch_obj->requests_count;
85 is( $requests_count, 1, 'requests_count returns correctly' );
86
87 # Check patron
88 my $batch_patron = $batch_obj->patron;
89 isa_ok( $batch_patron, 'Koha::Patron' );
90 is( $batch_patron->firstname, "Grogu", "patron returns correctly" );
91
92 # Check branch
93 my $batch_branch = $batch_obj->branch;
94 isa_ok( $batch_branch, 'Koha::Library' );
95 is( $batch_branch->branchcode, $branch->{branchcode}, "branch returns correctly" );
96
97 $illrq_obj->delete;
98 $schema->storage->txn_rollback;