Bug 30287: (follow-up) Unit test for html_content
[koha.git] / t / db_dependent / Koha / Illbackend.t
1 #!/usr/bin/perl
2
3 # Copyright 2023 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 1;
23
24 use Koha::Illbackend;
25
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
28
29 my $builder = t::lib::TestBuilder->new;
30 my $schema  = Koha::Database->new->schema;
31
32 subtest 'existing_statuses() tests' => sub {
33
34     plan tests => 2;
35
36     $schema->storage->txn_begin;
37     Koha::Illrequests->search->delete;
38
39     # Mock ILLBackend (as object)
40     my $backend = Test::MockObject->new;
41     $backend->set_isa('Koha::Illbackends::Mock');
42     $backend->set_always( 'name', 'Mock' );
43
44     $backend->mock(
45         'status_graph',
46         sub {
47             return {
48                 READY => {
49                     prev_actions   => [ 'NEW', 'REQREV', 'QUEUED', 'CANCREQ' ],
50                     id             => 'READY',
51                     name           => 'Ready',
52                     ui_method_name => 'Make request ready',
53                     method         => 'confirm',
54                     next_actions   => [ 'REQREV', 'COMP', 'CHK' ],
55                     ui_method_icon => 'fa-check',
56                 }
57             };
58         },
59     );
60
61     # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
62     my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
63     $illreqmodule->mock(
64         'load_backend',
65         sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
66     );
67
68     my $alias = $builder->build_object(
69         {
70             class => 'Koha::AuthorisedValues',
71             value => {
72                 category         => 'ILL_STATUS_ALIAS',
73                 authorised_value => 'BOB',
74                 lib              => "Bob is the best status"
75             }
76         }
77     );
78
79     my $backend_req_status = $builder->build_object(
80         {
81             class => 'Koha::Illrequests',
82             value => {
83                 status       => 'READY',
84                 status_alias => undef,
85                 biblio_id    => undef,
86                 backend      => 'Mock'
87             }
88         }
89     );
90
91     my $req = $builder->build_object(
92         {
93             class => 'Koha::Illrequests',
94             value => {
95                 status       => 'REQ',
96                 status_alias => undef,
97                 biblio_id    => undef,
98                 backend      => 'Mock'
99             }
100         }
101     );
102     my $chk = $builder->build_object(
103         {
104             class => 'Koha::Illrequests',
105             value => {
106                 status       => 'CHK',
107                 status_alias => undef,
108                 biblio_id    => undef,
109                 backend      => 'Mock'
110             }
111         }
112     );
113     my $bob = $builder->build_object(
114         {
115             class => 'Koha::Illrequests',
116             value => {
117                 status       => 'REQ',
118                 status_alias => 'BOB',
119                 biblio_id    => undef,
120                 backend      => 'Mock'
121             }
122         }
123     );
124     my $req2 = $builder->build_object(
125         {
126             class => 'Koha::Illrequests',
127             value => {
128                 status       => 'REQ',
129                 status_alias => undef,
130                 biblio_id    => undef,
131                 backend      => 'Mock'
132             }
133         }
134     );
135
136     my $backend_module = Koha::Illbackend->new;
137
138     my $existing_statuses = $backend_module->existing_statuses('Mock');
139
140     is( @{$existing_statuses}, 4, "Return 4 unique existing statuses" );
141
142     # FIXME: Add tests to check content and order of return
143     my $expected_statuses = [
144         { code => 'CHK',   str => 'Checked out' },
145         { code => 'READY', str => 'Ready' },
146         { code => 'REQ',   str => 'Requested' },
147         { code => 'BOB',   str => 'Bob is the best status' }
148     ];
149
150     is_deeply( $existing_statuses, $expected_statuses, 'Deep match on return' );
151
152     $schema->storage->txn_rollback;
153 };