Bug 30484: Add unit tests

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

https://bugs.koha-community.org/show_bug.cgi?id=28909
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Andrew Isherwood 2022-04-13 11:30:40 +01:00 committed by Tomas Cohen Arazi
parent 65a2fc2fa3
commit 3b8a4f21be
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
3 changed files with 166 additions and 1 deletions

View File

@ -0,0 +1,64 @@
#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::MockObject;
use Koha::Illrequest;
use Koha::Illrequest::SupplierUpdate;
use Test::More tests => 4;
use_ok('Koha::Illrequest::SupplierUpdate');
my $update = Koha::Illrequest::SupplierUpdate->new(
'test_type',
'test_name',
'Arbitrary update text'
);
isa_ok( $update, 'Koha::Illrequest::SupplierUpdate' );
my $processor = Test::MockObject->new;
$processor->set_isa('Koha::Illrequest::Processor');
$processor->{name} = 'Test processor';
$processor->mock('run', sub {
my ( $self, $update, $options, $result ) = @_;
push @{$result->{success}}, 'Hello';
});
# attach_processor
$update->attach_processor($processor);
is(
scalar @{$update->{processors}},
1,
'attach_processors works'
);
# run_processors
is_deeply(
$update->run_processors({}),
[{
name => 'Test processor',
result => {
success => ['Hello'],
error => []
}
}],
'run_processors calls attached processors'
);

View File

@ -0,0 +1,37 @@
#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Koha::Illrequest::SupplierUpdateProcessor;
use Test::More tests => 3;
use Test::Warn;
my $processor = Koha::Illrequest::SupplierUpdateProcessor->new(
'test_type',
'test_name',
'Test processor name'
);
use_ok('Koha::Illrequest::SupplierUpdateProcessor');
isa_ok( $processor, 'Koha::Illrequest::SupplierUpdateProcessor' );
warning_like {
$processor->run()
} qr/run should only be invoked by a subclass/, 'Invoking base class "run" warns';

View File

@ -799,6 +799,18 @@ subtest 'Backend core methods' => sub {
},
"Backend confirm: arbitrary stage.");
# backend_get_update
$backend->mock(
'get_supplier_update',
sub {
my ( $self, $options ) = @_;
return $options;
}
);
$backend->mock('capabilities', sub { return sub { return 1; } });
is_deeply($illrq->backend_get_update({}), 1,
"Backend get_update method.");
$config->set_always('partner_code', "ILLTSTLIB");
$backend->set_always('metadata', { Test => "Foobar" });
my $illbrn = $builder->build({
@ -838,7 +850,7 @@ subtest 'Backend core methods' => sub {
subtest 'Helpers' => sub {
plan tests => 21;
plan tests => 25;
$schema->storage->txn_begin;
@ -882,6 +894,30 @@ subtest 'Helpers' => sub {
$illrq_obj->_config($config);
$illrq_obj->_backend($backend);
#attach_processors
my $type = 'test_type_1';
my $name = 'test_name_1';
my $update = Test::MockObject->new;
$update->set_isa('Koha::Illrequest::SupplierUpdate');
$update->{source_type} = $type;
$update->{source_name} = $name;
$update->{processors} = [];
$update->mock('attach_processor', sub {
my ( $self, $to_attach ) = @_;
push @{$self->{processors}}, $to_attach;
});
my $processor = Test::MockObject->new;
$processor->{target_source_type} = $type;
$processor->{target_source_name} = $name;
$illrq_obj->init_processors();
$illrq_obj->push_processor($processor);
$illrq_obj->attach_processors($update);
is_deeply(
scalar @{$update->{processors}},
1,
'attaching processors as appropriate works'
);
# getPrefix
$config->set_series('getPrefixes',
{ HDE => "TEST", TSL => "BAR", default => "DEFAULT" },
@ -933,6 +969,27 @@ subtest 'Helpers' => sub {
);
is($notice, 'ILL_PICKUP_READY' ,"Notice is correctly created");
# ill update notice, passes additional text parameter
my $attr_update = Koha::MessageAttributes->find({ message_name => 'Ill_update' });
C4::Members::Messaging::SetMessagingPreference({
borrowernumber => $patron->{borrowernumber},
message_attribute_id => $attr_update->message_attribute_id,
message_transport_types => ['email']
});
my $return_patron_update = $illrq_obj->send_patron_notice('ILL_REQUEST_UPDATE', 'Some additional text');
my $notice_update = $schema->resultset('MessageQueue')->search({
letter_code => 'ILL_REQUEST_UPDATE',
message_transport_type => 'email',
borrowernumber => $illrq_obj->borrowernumber
})->next()->letter_code;
is_deeply(
$return_patron_update,
{ result => { success => ['email'], fail => [] } },
"Correct return when notice created"
);
is($notice_update, 'ILL_REQUEST_UPDATE' ,"Notice is correctly created");
my $return_patron_fail = $illrq_obj->send_patron_notice();
is_deeply(
$return_patron_fail,
@ -1013,6 +1070,13 @@ subtest 'Helpers' => sub {
'Correct content returned from get_notice with metadata correctly ordered'
);
$illrq_obj->append_to_note('Some text');
like(
$illrq_obj->notesstaff,
qr/Some text$/,
'appending to a note works'
);
$schema->storage->txn_rollback;
};