Bug 27944: Add missing tests
[koha.git] / t / db_dependent / Koha / ArticleRequests.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::MockModule;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Koha::ArticleRequests;
27 use Koha::Database;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'requested() tests' => sub {
33
34     plan tests => 5;
35
36     $schema->storage->txn_begin;
37
38     my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
39     my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
40
41     my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library_1->id, flags => 1 } } );
42     t::lib::Mocks::mock_userenv( { branchcode => $library_1 } );
43
44     # FIXME: we moved past this pattern. This method should be refactored
45     #        as ->filter_by_requested
46     Koha::ArticleRequests->delete;
47
48     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
49     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
50
51     my $ar_1 = $builder->build_object(
52         {   class => 'Koha::ArticleRequests',
53             value => { status => Koha::ArticleRequest::Status::Requested, branchcode => $library_1->id }
54         }
55     );
56
57     my $ar_2 = $builder->build_object(
58         {   class => 'Koha::ArticleRequests',
59             value => { status => Koha::ArticleRequest::Status::Requested, branchcode => $library_2->id }
60         }
61     );
62
63     my $ar_3 = $builder->build_object(
64         {   class => 'Koha::ArticleRequests',
65             value => { status => Koha::ArticleRequest::Status::Pending, branchcode => $library_2->id }
66         }
67     );
68
69     my $requested = Koha::ArticleRequests->requested;
70     is( $requested->count,        2,                                       'Two article requests with the REQUESTED status' );
71     is( $requested->next->status, Koha::ArticleRequest::Status::Requested, 'Status is correct' );
72     is( $requested->next->status, Koha::ArticleRequest::Status::Requested, 'Status is correct' );
73
74     my $requested_branch = Koha::ArticleRequests->requested( $library_1->id );
75     is( $requested_branch->count, 1, 'One article request with the REQUESTED status, for the selected branchcode' );
76     is( $requested_branch->next->status, Koha::ArticleRequest::Status::Requested, 'Status is correct' );
77
78     $schema->storage->txn_rollback;
79 };