3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 2;
23 use Koha::ArticleRequest::Status;
24 use Koha::ArticleRequests;
28 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
33 subtest 'requested() tests' => sub {
37 $schema->storage->txn_begin;
39 my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
40 my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
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 } );
45 # FIXME: we moved past this pattern. This method should be refactored
46 # as ->filter_by_requested
47 Koha::ArticleRequests->delete;
49 my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
50 $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
52 my $ar_1 = $builder->build_object(
53 { class => 'Koha::ArticleRequests',
54 value => { status => Koha::ArticleRequest::Status::Requested, branchcode => $library_1->id }
58 my $ar_2 = $builder->build_object(
59 { class => 'Koha::ArticleRequests',
60 value => { status => Koha::ArticleRequest::Status::Requested, branchcode => $library_2->id }
64 my $ar_3 = $builder->build_object(
65 { class => 'Koha::ArticleRequests',
66 value => { status => Koha::ArticleRequest::Status::Pending, branchcode => $library_2->id }
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' );
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' );
79 $schema->storage->txn_rollback;
82 subtest 'filter_by_current / filter_by_finished tests' => sub {
86 $schema->storage->txn_begin;
88 my $ar_requested = $builder->build_object(
90 class => 'Koha::ArticleRequests',
91 value => { status => Koha::ArticleRequest::Status::Requested }
94 my $ar_pending = $builder->build_object(
96 class => 'Koha::ArticleRequests',
97 value => { status => Koha::ArticleRequest::Status::Pending }
100 my $ar_processing = $builder->build_object(
102 class => 'Koha::ArticleRequests',
103 value => { status => Koha::ArticleRequest::Status::Processing }
106 my $ar_completed = $builder->build_object(
108 class => 'Koha::ArticleRequests',
109 value => { status => Koha::ArticleRequest::Status::Completed }
112 my $ar_cancelled = $builder->build_object(
114 class => 'Koha::ArticleRequests',
115 value => { status => Koha::ArticleRequest::Status::Canceled }
119 my $article_requests = Koha::ArticleRequests->search(
122 $ar_requested->id, $ar_pending->id, $ar_processing->id,
123 $ar_completed->id, $ar_cancelled->id
128 my $current_article_requests = $article_requests->filter_by_current;
130 is( $current_article_requests->count, 3, 'Count is correct' );
132 my $finished_article_requests = $article_requests->filter_by_finished;
134 is( $finished_article_requests->count, 2, 'Count is correct' );
136 $schema->storage->txn_rollback;