From 244a93fbeba9dabb6bd761fb8c28baa8dac9d8a9 Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Wed, 13 Apr 2022 11:30:40 +0100 Subject: [PATCH] Bug 30484: Add unit tests --- t/db_dependent/Illrequest/SupplierUpdate.t | 64 +++++++++++++++++ .../Illrequest/SupplierUpdateProcessor.t | 37 ++++++++++ t/db_dependent/Illrequests.t | 72 ++++++++++++++++++- 3 files changed, 170 insertions(+), 3 deletions(-) create mode 100755 t/db_dependent/Illrequest/SupplierUpdate.t create mode 100755 t/db_dependent/Illrequest/SupplierUpdateProcessor.t diff --git a/t/db_dependent/Illrequest/SupplierUpdate.t b/t/db_dependent/Illrequest/SupplierUpdate.t new file mode 100755 index 0000000000..fc6de90332 --- /dev/null +++ b/t/db_dependent/Illrequest/SupplierUpdate.t @@ -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 . + +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, $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' +); diff --git a/t/db_dependent/Illrequest/SupplierUpdateProcessor.t b/t/db_dependent/Illrequest/SupplierUpdateProcessor.t new file mode 100755 index 0000000000..5577fb417c --- /dev/null +++ b/t/db_dependent/Illrequest/SupplierUpdateProcessor.t @@ -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 . + +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'; diff --git a/t/db_dependent/Illrequests.t b/t/db_dependent/Illrequests.t index df8ebbdecb..9267599ebc 100755 --- a/t/db_dependent/Illrequests.t +++ b/t/db_dependent/Illrequests.t @@ -636,7 +636,7 @@ subtest 'Backend testing (mocks)' => sub { subtest 'Backend core methods' => sub { - plan tests => 19; + plan tests => 20; $schema->storage->txn_begin; @@ -799,6 +799,20 @@ subtest 'Backend core methods' => sub { }, "Backend confirm: arbitrary stage."); + # backend_get_update + $backend->mock( + 'get_supplier_update', + sub { + my ( $self, $delay ) = @_; + return { + delay => $delay + } + } + ); + $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 +852,7 @@ subtest 'Backend core methods' => sub { subtest 'Helpers' => sub { - plan tests => 21; + plan tests => 25; $schema->storage->txn_begin; @@ -882,6 +896,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 +971,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 +1072,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; }; @@ -1460,4 +1526,4 @@ subtest 'Checking in hook' => sub { is( $illrq->status, 'RET' ); $schema->storage->txn_rollback; -}; +}; \ No newline at end of file -- 2.20.1