From 58b81110dcd871bb0eb5131f4af2b3179dbcc2d5 Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Tue, 2 Oct 2018 14:56:53 +0100 Subject: [PATCH] Bug 20750: Add unit tests Signed-off-by: Josef Moravec Bug 20750: (follow-up) Remove status_alias test This test is now redundant. We can't test the return of $req->status_alias against the value of $req->{status_alias} any more since we've overloaded the status_alias method and it won't always return the value of the object's property. We test the functionality of the status_alias method elsewhere so we're still covered. Signed-off-by: Josef Moravec Signed-off-by: Kyle M Hall Signed-off-by: Nick Clemens --- t/Log.t | 1 - t/db_dependent/Illrequest/Logger.t | 118 +++++++++++++++++++++++++++++++++++++ t/db_dependent/Illrequests.t | 4 +- 3 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 t/db_dependent/Illrequest/Logger.t diff --git a/t/Log.t b/t/Log.t index d53386bc33..bfcd75a3af 100755 --- a/t/Log.t +++ b/t/Log.t @@ -10,4 +10,3 @@ use Test::More tests => 1; BEGIN { use_ok('C4::Log'); } - diff --git a/t/db_dependent/Illrequest/Logger.t b/t/db_dependent/Illrequest/Logger.t new file mode 100644 index 0000000000..e3b5670221 --- /dev/null +++ b/t/db_dependent/Illrequest/Logger.t @@ -0,0 +1,118 @@ + +#!/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::Database; + +use Test::More tests => 2; +use Test::MockModule; +use Test::MockObject; +use t::lib::Mocks; + +my $schema = Koha::Database->new->schema; + +# A mock response from C4::Log::GetLogs() +my $logs = [ + { + info => '{"log_origin": "core"}', + action => 'STATUS_CHANGE', + timestamp => '2018-10-02 11:12:22' + }, + { + info => '{"log_origin": "core"}', + action => 'STATUS_CHANGE', + timestamp => '2018-10-02 11:12:12' + }, + { + info => '{"log_origin": "core"}', + action => 'STATUS_CHANGE', + timestamp => '2018-10-02 11:12:32' + } +]; +# Mock the modules we use +my $c4_log = Test::MockModule->new('C4::Log'); +$c4_log->mock('logaction', sub { 1 }); +$c4_log->mock('GetLogs', sub { return $logs; }); +my $c4_tpl = Test::MockModule->new('C4::Templates'); +$c4_tpl->mock('_get_template_file', + sub { return ('htdocs', 'theme', 'lang', 'base/'); }); + +use_ok('Koha::Illrequest::Logger'); + +subtest 'Basics' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + my $logger = Koha::Illrequest::Logger->new; + + # new() + # + ok( defined($logger), 'new() returned something' ); + ok( $logger->isa('Koha::Illrequest::Logger'), + 'new() returns the correct object' ); + + # This is an incomplete data hashref, we use it to + # test validation of the data before logging + my $log_obj = { + modulename => 'modulename', + actionname => 'actionname', + infos => 'infos' + }; + + # log_something() + # + # Do we only log when the pref is set (currently unset) + is($logger->log_something(), '', + 'logaction() not being called without pref being set'); + + # Set the pref + t::lib::Mocks::mock_preference( 'IllLog', 1 ); + # We should not log without all the required data, we are still + # using the incomplete hashref + is($logger->log_something(), '', + 'logaction() being called when data is incomplete'); + + # Fix the data hashref, then test that logging occurs + $log_obj->{objectnumber} = 'objectnumber'; + is($logger->log_something($log_obj), 1, + 'logaction() being called when pref is set and data is complete'); + + # log_maybe() + # + is($logger->log_maybe({}, {}), '', + 'log_maybe() does not log with incomplete data'); + + # get_log_template() + # + is( + $logger->get_log_template({ + request => {}, + origin => 'core', + action => 'STATUS_CHANGE' + }), + 'base/status_change.tt', + 'get_log_template() fetches correct core template' + ); + + $schema->storage->txn_rollback; +}; + +1; diff --git a/t/db_dependent/Illrequests.t b/t/db_dependent/Illrequests.t index ffbc98dc4f..9046852b45 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 => 25; + plan tests => 24; $schema->storage->txn_begin; @@ -62,8 +62,6 @@ subtest 'Basic object tests' => sub { "Branchcode getter works."); is($illrq_obj->status, $illrq->{status}, "Status getter works."); - is($illrq_obj->status_alias, $illrq->{status_alias}, - "Status_alias getter works."); is($illrq_obj->placed, $illrq->{placed}, "Placed getter works."); is($illrq_obj->replied, $illrq->{replied}, -- 2.11.4.GIT