From d6d8c5928befed9e1fbfb2417749b3bd3e9c97d1 Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Tue, 19 Mar 2019 14:15:15 +0000 Subject: [PATCH] Bug 22531: Add unit tests Add tests for new and modified methods --- t/db_dependent/Illrequest/Logger.t | 45 +++++++++++++++++++++++- t/db_dependent/Illrequests.t | 71 +++++++++++++++++++++++++++++++++++--- 2 files changed, 111 insertions(+), 5 deletions(-) diff --git a/t/db_dependent/Illrequest/Logger.t b/t/db_dependent/Illrequest/Logger.t index e3b5670221..1970185f6b 100644 --- a/t/db_dependent/Illrequest/Logger.t +++ b/t/db_dependent/Illrequest/Logger.t @@ -24,8 +24,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; # A mock response from C4::Log::GetLogs() my $logs = [ @@ -43,6 +45,11 @@ my $logs = [ info => '{"log_origin": "core"}', action => 'STATUS_CHANGE', timestamp => '2018-10-02 11:12:32' + }, + { + info => '{"status_before":"GENREQ","log_origin":"core","additional":{"partner_email_previous":"me@nowhere.com","partner_email_now":"you@nowhere.com"},"status_after":"GENREQ"}', + action => 'STATUS_CHANGE', + timestamp => '2018-10-02 11:12:42' } ]; # Mock the modules we use @@ -57,7 +64,7 @@ use_ok('Koha::Illrequest::Logger'); subtest 'Basics' => sub { - plan tests => 7; + plan tests => 9; $schema->storage->txn_begin; @@ -112,6 +119,42 @@ 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', + value => { borrowernumber => 123 } + }); + 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 d3a68084bc..96a0b86c4e 100644 --- a/t/db_dependent/Illrequests.t +++ b/t/db_dependent/Illrequests.t @@ -39,7 +39,7 @@ use_ok('Koha::Illrequests'); subtest 'Basic object tests' => sub { - plan tests => 24; + plan tests => 26; $schema->storage->txn_begin; @@ -111,6 +111,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; }; @@ -231,7 +239,7 @@ subtest 'Status Graph tests' => sub { subtest 'Backend testing (mocks)' => sub { - plan tests => 13; + plan tests => 16; $schema->storage->txn_begin; @@ -239,7 +247,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'); @@ -250,7 +260,12 @@ subtest 'Backend testing (mocks)' => sub { value => { borrowernumber => $patron->{borrowernumber} } }); + 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."); @@ -297,6 +312,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 @@ -842,7 +895,7 @@ subtest 'Checking Limits' => sub { subtest 'Custom statuses' => sub { - plan tests => 3; + plan tests => 5; $schema->storage->txn_begin; @@ -890,5 +943,15 @@ 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.11.0