Bug 27526: Add tests for columns_to_str and host_items
[koha-ffzg.git] / t / db_dependent / Koha / ArticleRequest.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 => 5;
21 use Test::MockModule;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 my $schema  = Koha::Database->new->schema;
27 my $builder = t::lib::TestBuilder->new;
28
29 subtest 'request() tests' => sub {
30
31     plan tests => 2;
32
33     $schema->storage->txn_begin;
34
35     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
36     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
37
38     my $ar = $builder->build_object(
39         {   class => 'Koha::ArticleRequests',
40             value => { status => Koha::ArticleRequest::Status::Pending }
41         }
42     );
43
44     $ar->request()->discard_changes;
45
46     is( $ar->status, Koha::ArticleRequest::Status::Requested );
47
48     $schema->storage->txn_rollback;
49 };
50
51 subtest 'set_pending() tests' => sub {
52
53     plan tests => 2;
54
55     $schema->storage->txn_begin;
56
57     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
58     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
59
60     my $ar = $builder->build_object(
61         {   class => 'Koha::ArticleRequests',
62             value => { status => Koha::ArticleRequest::Status::Requested }
63         }
64     );
65
66     $ar->set_pending()->discard_changes;
67
68     is( $ar->status, Koha::ArticleRequest::Status::Pending );
69
70     $schema->storage->txn_rollback;
71 };
72
73 subtest 'process() tests' => sub {
74
75     plan tests => 2;
76
77     $schema->storage->txn_begin;
78
79     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
80     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
81
82     my $ar = $builder->build_object(
83         {   class => 'Koha::ArticleRequests',
84             value => { status => Koha::ArticleRequest::Status::Requested }
85         }
86     );
87
88     $ar->process()->discard_changes;
89
90     is( $ar->status, Koha::ArticleRequest::Status::Processing );
91
92     $schema->storage->txn_rollback;
93 };
94
95 subtest 'complete() tests' => sub {
96
97     plan tests => 2;
98
99     $schema->storage->txn_begin;
100
101     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
102     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
103
104     my $ar = $builder->build_object(
105         {   class => 'Koha::ArticleRequests',
106             value => { status => Koha::ArticleRequest::Status::Requested }
107         }
108     );
109
110     $ar->complete()->discard_changes;
111
112     is( $ar->status, Koha::ArticleRequest::Status::Completed );
113
114     $schema->storage->txn_rollback;
115 };
116
117 subtest 'cancel() tests' => sub {
118
119     plan tests => 4;
120
121     $schema->storage->txn_begin;
122
123     my $ar_mock = Test::MockModule->new('Koha::ArticleRequest');
124     $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } );
125
126     my $ar = $builder->build_object(
127         {   class => 'Koha::ArticleRequests',
128             value => { status => Koha::ArticleRequest::Status::Requested }
129         }
130     );
131
132     my $reason = "Hey, ho";
133     my $notes  = "Let's go!";
134
135     $ar->cancel({ cancellation_reason => $reason, notes => $notes })->discard_changes;
136
137     is( $ar->status, Koha::ArticleRequest::Status::Canceled );
138     is( $ar->cancellation_reason, $reason, 'Cancellation reason stored correctly' );
139     is( $ar->notes, $notes, 'Notes stored correctly' );
140
141     $schema->storage->txn_rollback;
142 };