Bug 30719: Tests
[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     source => 'Borrower',
48     value => {
49         firstname => "Grogu"
50     }
51 });
52
53 # Create a branch
54 my $branch = $builder->build({
55     source => 'Branch'
56 });
57
58 # Create a batch
59 my $illbatch = $builder->build({
60     source => 'Illbatch',
61     value => {
62         name  => "My test batch",
63         backend  => "Mock",
64         borrowernumber => $librarian->{borrowernumber},
65         branchcode => $branch->{branchcode}
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     source => 'Illrequest',
74     value => {
75         borrowernumber => $patron->{borrowernumber},
76         batch_id       => $illbatch->{id}
77     }
78 });
79 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
80
81 # Check requests_count
82 my $requests_count = $batch_obj->requests_count;
83 is( $requests_count, 1, 'requests_count returns correctly' );
84
85 # Check patron
86 my $batch_patron = $batch_obj->patron;
87 isa_ok( $batch_patron, 'Koha::Patron' );
88 is( $batch_patron->firstname, "Grogu", "patron returns correctly" );
89
90 # Check branch
91 my $batch_branch = $batch_obj->branch;
92 isa_ok( $batch_branch, 'Koha::Library' );
93 is( $batch_branch->branchcode, $branch->{branchcode}, "branch returns correctly" );
94
95 $illrq_obj->delete;
96 $schema->storage->txn_rollback;