From 346eaf34f12c351f055c86a0f30d60eb18e32a9e Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Fri, 17 Sep 2021 12:29:14 +0100 Subject: [PATCH] Bug 22531: Add unit tests Add tests for new and modified methods Signed-off-by: Barry Cannon Signed-off-by: Martin Renvoize --- Koha/Illrequest/Logger.pm | 4 +- t/db_dependent/Illrequest/Logger.t | 42 ++++++++++++++++-- t/db_dependent/Illrequests.t | 69 ++++++++++++++++++++++++++++-- 3 files changed, 107 insertions(+), 8 deletions(-) diff --git a/Koha/Illrequest/Logger.pm b/Koha/Illrequest/Logger.pm index cf7d2b6384..83520030fd 100644 --- a/Koha/Illrequest/Logger.pm +++ b/Koha/Illrequest/Logger.pm @@ -23,7 +23,7 @@ use JSON qw( from_json to_json ); use C4::Koha qw( GetAuthorisedValues ); use C4::Context; use C4::Templates; -use C4::Log qw( logaction ); +use C4::Log; use Koha::ActionLogs; use Koha::Notice::Template; use Koha::Patron; @@ -235,7 +235,7 @@ sub log_something { defined $to_log->{infos} && C4::Context->preference("IllLog") ) { - logaction( + C4::Log::logaction( $to_log->{modulename}, $to_log->{actionname}, $to_log->{objectnumber}, diff --git a/t/db_dependent/Illrequest/Logger.t b/t/db_dependent/Illrequest/Logger.t index 07d4097573..b058d3d465 100755 --- a/t/db_dependent/Illrequest/Logger.t +++ b/t/db_dependent/Illrequest/Logger.t @@ -1,4 +1,3 @@ - #!/usr/bin/perl # This file is part of Koha. @@ -24,8 +23,10 @@ use Test::More tests => 2; use Test::MockModule; use Test::MockObject; use t::lib::Mocks; +use t::lib::TestBuilder; my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; # Mock the modules we use my $c4_log = Test::MockModule->new('C4::Log'); @@ -38,7 +39,7 @@ use_ok('Koha::Illrequest::Logger'); subtest 'Basics' => sub { - plan tests => 7; + plan tests => 9; $schema->storage->txn_begin; @@ -72,7 +73,7 @@ subtest 'Basics' => sub { 'logaction() being called when data is incomplete'); # Fix the data hashref, then test that logging occurs - $log_obj->{objectnumber} = 'objectnumber'; + $log_obj->{objectnumber} = 123; is($logger->log_something($log_obj), 1, 'logaction() being called when pref is set and data is complete'); @@ -93,6 +94,41 @@ subtest 'Basics' => sub { 'get_log_template() fetches correct core template' ); + # logged_requested_partners + my $categorycode = $builder->build({ source => 'Category' })->{categorycode}; + my $branchcode = $builder->build({ source => 'Branch' })->{branchcode}; + my $illrq = $builder->build_object({ + class => 'Koha::Illrequests' + }); + my $config = Test::MockObject->new; + $config->set_always('partner_code', $categorycode); + $illrq->_config($config); + Koha::Patron->new( + { + surname => 'Test 1', + email => 'me@nowhere.com', + categorycode => $categorycode, + branchcode => $branchcode + } + )->store(); + my $returned = Koha::Illrequest::Logger::_logged_requested_partners({ + request => $illrq, + info => { + log_origin => "core", + status_before => "GENREQ", + status_after => "GENREQ", + additional => { + partner_email_previous => 'me@nowhere.com', + partner_email_now => 'you@nowhere.com' + } + } + }); + isa_ok($returned, 'HASH', + 'logged_requested_partners returns a hasref' + ); + is($returned->{'me@nowhere.com'}->{surname}, 'Test 1', + 'logged_requested_partners returns full patron objects'); + $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/Illrequests.t b/t/db_dependent/Illrequests.t index 5f4f25cdcc..137cdfa37b 100755 --- a/t/db_dependent/Illrequests.t +++ b/t/db_dependent/Illrequests.t @@ -50,7 +50,7 @@ use_ok('Koha::Illrequests'); subtest 'Basic object tests' => sub { - plan tests => 24; + plan tests => 26; $schema->storage->txn_begin; @@ -122,6 +122,14 @@ subtest 'Basic object tests' => sub { is(Koha::Illrequests->search->count, 0, "No illrequest found after delete."); + $illrq_obj->status('REQ'); + is($illrq_obj->status, 'REQ', + "status correctly handles strings"); + + $illrq_obj->status({ status => 'NEW', additional => 'add'}); + is($illrq_obj->status, 'NEW', + "status correctly handles hashrefs"); + $schema->storage->txn_rollback; }; @@ -492,7 +500,7 @@ subtest 'Status Graph tests' => sub { subtest 'Backend testing (mocks)' => sub { - plan tests => 13; + plan tests => 16; $schema->storage->txn_begin; @@ -500,7 +508,9 @@ subtest 'Backend testing (mocks)' => sub { # the Dummy plugin installed. load_backend & available_backends don't # currently have tests as a result. - t::lib::Mocks->mock_config('interlibrary_loans', { backend_dir => 'a_dir' } ); + my $categorycode = $builder->build({ source => 'Category' })->{categorycode}; + my $branchcode = $builder->build({ source => 'Branch' })->{branchcode}; + my $backend = Test::MockObject->new; $backend->set_isa('Koha::Illbackends::Mock'); $backend->set_always('name', 'Mock'); @@ -510,7 +520,12 @@ subtest 'Backend testing (mocks)' => sub { class => 'Koha::Illrequests', }); + my $config = Test::MockObject->new; + $config->set_always('partner_code', $categorycode); + $config->set_always('backend_dir', 'a_dir'); + $illrq->_backend($backend); + $illrq->_config($config); isa_ok($illrq->_backend, 'Koha::Illbackends::Mock', "OK accessing mocked backend."); @@ -557,6 +572,44 @@ subtest 'Backend testing (mocks)' => sub { "Test metadata." ); + $backend->mock( + 'capabilities', + sub { + my ($self, $name) = @_; + if ($name eq 'get_requested_partners') { + return sub { + return 'me@nowhere.com; you@nowhere.com'; + } + } + } + ); + is($illrq->requested_partners, 'me@nowhere.com; you@nowhere.com', + "requested_partners returns string by default"); + + Koha::Patron->new( + { + surname => 'Test 1', + email => 'me@nowhere.com', + categorycode => $categorycode, + branchcode => $branchcode + } + )->store(); + + Koha::Patron->new( + { + surname => 'Test 2', + email => 'you@nowhere.com', + categorycode => $categorycode, + branchcode => $branchcode + } + )->store(); + + my $part = $illrq->requested_partners(1); + isa_ok($part, 'ARRAY', + "requested_partners returns array when requested"); + isa_ok(@{$part}[0], 'HASH', + "requested_partners return array contains unblessed Koha patrons"); + # capabilities: # No backend graph extension @@ -1395,6 +1448,16 @@ subtest 'Custom statuses' => sub { is($ill_req->statusalias, undef, "Koha::Illrequest->status overloading resetting status_alias"); + $ill_req->status_alias($av->authorised_value); + is($ill_req->status_alias, $av->authorised_value, + "Koha::Illrequest->status_alias correctly handling string"); + + $ill_req->status_alias( + { status => $av->authorised_value, additional => 'xyz' } + ); + is($ill_req->status_alias, $av->authorised_value, + "Koha::Illrequest->status_alias correctly handling hashref"); + $schema->storage->txn_rollback; }; -- 2.20.1