View | Details | Raw Unified | Return to bug 38441
Collapse All | Expand All

(-)a/t/db_dependent/Koha/ILL/Request/Workflow/HistoryCheck.t (-1 / +209 lines)
Line 0 Link Here
0
- 
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
22
use Test::MockModule;
23
use Test::MockObject;
24
25
use t::lib::TestBuilder;
26
use t::lib::Mocks;
27
28
use Koha::ILL::Request::Workflow::HistoryCheck;
29
use Koha::Database;
30
31
my $schema = Koha::Database->new->schema;
32
33
my $issn = '321321321';
34
35
subtest 'show_history_check' => sub {
36
37
    plan tests => 6;
38
39
    $schema->storage->txn_begin;
40
41
    my $builder = t::lib::TestBuilder->new;
42
43
    use_ok('Koha::ILL::Request::Workflow::HistoryCheck');
44
45
    my $metadata = {
46
        title  => 'This is a title',
47
        author => 'This is an author',
48
        issn   => $issn
49
    };
50
51
    # Because hashes can reorder themselves, we need to make sure ours is in a
52
    # predictable order
53
    my $sorted = {};
54
    foreach my $key ( keys %{$metadata} ) {
55
        $sorted->{$key} = $metadata->{$key};
56
    }
57
58
    my $history_check = Koha::ILL::Request::Workflow::HistoryCheck->new( $sorted, 'staff' );
59
60
    isa_ok( $history_check, 'Koha::ILL::Request::Workflow::HistoryCheck' );
61
62
    is(
63
        $history_check->prep_metadata($sorted),
64
        'eyJhdXRob3IiOiJUaGlzIGlzIGFuIGF1dGhvciIsImlzc24iOiIzMjEzMjEzMjEiLCJ0aXRsZSI6%0AIlRoaXMgaXMgYSB0aXRsZSJ9%0A',
65
        'prep_metadata works'
66
    );
67
68
    # Mock capabilities and load_backend
69
    my $backend = Test::MockObject->new;
70
    $backend->set_always( 'capabilities', sub { return can_create_request => 1 } );
71
    my $illreqmodule = Test::MockModule->new('Koha::ILL::Request');
72
    $illreqmodule->mock(
73
        'load_backend',
74
        sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
75
    );
76
77
    # Mock ILLHistoryCheck enabled
78
    t::lib::Mocks::mock_preference( 'ILLHistoryCheck', 1 );
79
80
    my $ill_request = $builder->build_sample_ill_request();
81
    is(
82
        $history_check->show_history_check($ill_request),
83
        0, 'Request with ISSN ' . $issn . ' does not exist even though syspref is on. Not showing history check screen'
84
    );
85
86
    $builder->build(
87
        {
88
            source => 'Illrequestattribute',
89
            value  => { illrequest_id => $ill_request->illrequest_id, type => 'issn', value => $issn }
90
        }
91
    );
92
93
    is(
94
        $history_check->show_history_check($ill_request),
95
        1, 'Request with ISSN ' . $issn . ' exists and syspref is on. Able to show history check screen'
96
    );
97
98
    # Mock ILLHistoryCheck disabled
99
    t::lib::Mocks::mock_preference( 'ILLHistoryCheck', 0 );
100
101
    is(
102
        $history_check->show_history_check($ill_request),
103
        0, 'not able to show history check screen'
104
    );
105
106
    $schema->storage->txn_rollback;
107
108
};
109
110
subtest 'after_request_created' => sub {
111
112
    plan tests => 4;
113
114
    $schema->storage->txn_begin;
115
116
    my $builder = t::lib::TestBuilder->new;
117
118
    my $metadata = {
119
        title  => 'This is a title',
120
        author => 'This is an author',
121
        issn   => $issn
122
    };
123
124
    my $authenticated_patron = $builder->build_object(
125
        {
126
            class => 'Koha::Patrons',
127
        }
128
    );
129
130
    my $other_patron = $builder->build_object(
131
        {
132
            class => 'Koha::Patrons',
133
        }
134
    );
135
136
    t::lib::Mocks::mock_userenv( { patron => $authenticated_patron } );
137
138
    # Create an old request
139
    my $existing_ill_request =
140
        $builder->build_sample_ill_request( { borrowernumber => $other_patron->borrowernumber } );
141
    $builder->build(
142
        {
143
            source => 'Illrequestattribute',
144
            value  => { illrequest_id => $existing_ill_request->illrequest_id, type => 'issn', value => $issn }
145
        }
146
    );
147
148
    # Create a new request with new issn
149
    my $new_ill_request =
150
        $builder->build_sample_ill_request( { borrowernumber => $authenticated_patron->borrowernumber } );
151
    my $original_staff_notes = $new_ill_request->notesstaff;
152
    $metadata->{issn} = 'nonexistentissn';
153
    $builder->build(
154
        {
155
            source => 'Illrequestattribute',
156
            value  => { illrequest_id => $new_ill_request->illrequest_id, type => 'issn', value => $metadata->{issn} }
157
        }
158
    );
159
    my $opac_history_check = Koha::ILL::Request::Workflow::HistoryCheck->new( $metadata, 'opac' );
160
    $opac_history_check->after_request_created( $metadata, $new_ill_request );
161
162
    is(
163
        $new_ill_request->notesstaff,
164
        $original_staff_notes,
165
        'History check didnt find any matching requests. Staff notes have not been updated.'
166
    );
167
168
    # Create a third request with preexisting issn by other patron
169
    my $third_ill_request =
170
        $builder->build_sample_ill_request( { borrowernumber => $authenticated_patron->borrowernumber } );
171
    $metadata->{issn} = $issn;
172
    $builder->build(
173
        {
174
            source => 'Illrequestattribute',
175
            value  => { illrequest_id => $third_ill_request->illrequest_id, type => 'issn', value => $issn }
176
        }
177
    );
178
    $opac_history_check->after_request_created( $metadata, $third_ill_request );
179
180
    like(
181
        $third_ill_request->notesstaff, qr/Request has been submitted by other patrons in the past/,
182
        'Contains staffnotes related submissions by other patrons'
183
    );
184
185
    # Create a fourth request with preexisting issn by self patron and others
186
    my $fourth_ill_request =
187
        $builder->build_sample_ill_request( { borrowernumber => $authenticated_patron->borrowernumber } );
188
    $metadata->{issn} = $issn;
189
    $builder->build(
190
        {
191
            source => 'Illrequestattribute',
192
            value  => { illrequest_id => $fourth_ill_request->illrequest_id, type => 'issn', value => $issn }
193
        }
194
    );
195
    $opac_history_check->after_request_created( $metadata, $fourth_ill_request );
196
197
    like(
198
        $fourth_ill_request->notesstaff, qr/Request has been submitted by other patrons in the past/,
199
        'Contains staffnotes related submissions by other patrons'
200
    );
201
202
    like(
203
        $fourth_ill_request->notesstaff, qr/Request has been submitted by this patron in the past/,
204
        'Contains staffnotes related submissions by self patron'
205
    );
206
207
};
208
209
1;

Return to bug 38441