Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[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 => 2;
21 use Test::MockModule;
22
23 use Koha::ArticleRequest::Status;
24 use Koha::ArticleRequests;
25 use Koha::Database;
26
27 use t::lib::Mocks;
28 use t::lib::TestBuilder;
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 subtest 'requested() tests' => sub {
34
35     plan tests => 5;
36
37     $schema->storage->txn_begin;
38
39     my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
40     my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
41
42     my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library_1->id, flags => 1 } } );
43     t::lib::Mocks::mock_userenv( { branchcode => $library_1 } );
44
45     # FIXME: we moved past this pattern. This method should be refactored
46     #        as ->filter_by_requested
47     Koha::ArticleRequests->delete;
48
49     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
50     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
51
52     my $ar_1 = $builder->build_object(
53         {   class => 'Koha::ArticleRequests',
54             value => { status => Koha::ArticleRequest::Status::Requested, branchcode => $library_1->id }
55         }
56     );
57
58     my $ar_2 = $builder->build_object(
59         {   class => 'Koha::ArticleRequests',
60             value => { status => Koha::ArticleRequest::Status::Requested, branchcode => $library_2->id }
61         }
62     );
63
64     my $ar_3 = $builder->build_object(
65         {   class => 'Koha::ArticleRequests',
66             value => { status => Koha::ArticleRequest::Status::Pending, branchcode => $library_2->id }
67         }
68     );
69
70     my $requested = Koha::ArticleRequests->requested;
71     is( $requested->count,        2,                                       'Two article requests with the REQUESTED status' );
72     is( $requested->next->status, Koha::ArticleRequest::Status::Requested, 'Status is correct' );
73     is( $requested->next->status, Koha::ArticleRequest::Status::Requested, 'Status is correct' );
74
75     my $requested_branch = Koha::ArticleRequests->requested( $library_1->id );
76     is( $requested_branch->count, 1, 'One article request with the REQUESTED status, for the selected branchcode' );
77     is( $requested_branch->next->status, Koha::ArticleRequest::Status::Requested, 'Status is correct' );
78
79     $schema->storage->txn_rollback;
80 };
81
82 subtest 'filter_by_current / filter_by_finished tests' => sub {
83
84     plan tests => 2;
85
86     $schema->storage->txn_begin;
87
88     my $ar_requested = $builder->build_object(
89         {
90             class => 'Koha::ArticleRequests',
91             value => { status => Koha::ArticleRequest::Status::Requested }
92         }
93     );
94     my $ar_pending = $builder->build_object(
95         {
96             class => 'Koha::ArticleRequests',
97             value => { status => Koha::ArticleRequest::Status::Pending }
98         }
99     );
100     my $ar_processing = $builder->build_object(
101         {
102             class => 'Koha::ArticleRequests',
103             value => { status => Koha::ArticleRequest::Status::Processing }
104         }
105     );
106     my $ar_completed = $builder->build_object(
107         {
108             class => 'Koha::ArticleRequests',
109             value => { status => Koha::ArticleRequest::Status::Completed }
110         }
111     );
112     my $ar_cancelled = $builder->build_object(
113         {
114             class => 'Koha::ArticleRequests',
115             value => { status => Koha::ArticleRequest::Status::Canceled }
116         }
117     );
118
119     my $article_requests = Koha::ArticleRequests->search(
120         {
121             id => [
122                 $ar_requested->id, $ar_pending->id, $ar_processing->id,
123                 $ar_completed->id, $ar_cancelled->id
124             ]
125         }
126     );
127
128     my $current_article_requests = $article_requests->filter_by_current;
129
130     is( $current_article_requests->count, 3, 'Count is correct' );
131
132     my $finished_article_requests = $article_requests->filter_by_finished;
133
134     is( $finished_article_requests->count, 2, 'Count is correct' );
135
136     $schema->storage->txn_rollback;
137 };