Bug 23151: (follow-up) tweaked tests
[koha.git] / t / db_dependent / Illcomments.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 File::Basename qw/basename/;
21 use Koha::Database;
22 use Koha::Illrequests;
23 use Koha::Illrequestattributes;
24 use Koha::Illrequest::Config;
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 => 9;
32
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35 use_ok('Koha::Illcomment');
36 use_ok('Koha::Illcomments');
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 an ILL request
46 my $illrq = $builder->build({
47     source => 'Illrequest',
48     value => { borrowernumber => $patron->{borrowernumber} }
49 });
50 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
51 isa_ok( $illrq_obj, 'Koha::Illrequest' );
52
53 # Create a librarian
54 my $librarian = $builder->build({ source => 'Borrower' });
55
56 # Create a comment and tie it to the request and the librarian
57 my $comment_text = 'xyz';
58 my $illcomment = $builder->build({
59     source => 'Illcomment',
60     value => {
61         illrequest_id  => $illrq_obj->illrequest_id,
62         borrowernumber => $librarian->{borrowernumber},
63         comment        => $comment_text,
64     }
65 });
66
67 # Get all the comments
68 my $comments = $illrq_obj->illcomments;
69 isa_ok( $comments, 'Koha::Illcomments', "Illcomments" );
70 my @comments_list = $comments->as_list();
71 is( scalar @comments_list, 1, "We have 1 comment" );
72
73 # Get the first (and only) comment
74 my $comment = $comments->next();
75 isa_ok( $comment, 'Koha::Illcomment', "Illcomment" );
76
77 # Check the different data in the comment
78 is( $comment->illrequest_id,  $illrq_obj->illrequest_id,    'illrequest_id getter works' );
79 is( $comment->borrowernumber, $librarian->{borrowernumber}, 'borrowernumber getter works');
80 is( $comment->comment,        $comment_text,                'comment getter works');
81
82 $illrq_obj->delete;
83
84 $schema->storage->txn_rollback;