From 3756ee47b01d0736250f678824c222c03fe20a34 Mon Sep 17 00:00:00 2001 From: Emmi Takkinen Date: Tue, 7 Apr 2020 16:17:19 +0300 Subject: [PATCH 3/3] Bug 16371: Rework tests Use TestBuilder and 'mock_preferences' in tests. To test, prove t/db_dependent/Koha/GetDailyQuoteForInterface.t Sponsored-by: Koha-Suomi Oy --- .../Koha/GetDailyQuoteForInterface.t | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/t/db_dependent/Koha/GetDailyQuoteForInterface.t b/t/db_dependent/Koha/GetDailyQuoteForInterface.t index 65d886df91..da4507df55 100644 --- a/t/db_dependent/Koha/GetDailyQuoteForInterface.t +++ b/t/db_dependent/Koha/GetDailyQuoteForInterface.t @@ -2,51 +2,61 @@ use Modern::Perl; use Test::More; -use Test::More tests => 1; +use Test::More tests => 3; use Try::Tiny; use C4::Koha; +use Koha::Database; -my $dbh = C4::Context->dbh; +use t::lib::TestBuilder; +use t::lib::Mocks; -# Start transaction -$dbh->{RaiseError} = 1; -# Setup stage -$dbh->do("DELETE FROM quotes"); -##populate test context -$dbh->do("INSERT INTO `quotes` VALUES - (10,'Dusk And Her Embrace','Unfurl thy limbs breathless succubus
How the full embosomed fog
Imparts the night to us....','0000-00-00 00:00:00'); -"); +BEGIN { + use_ok('C4::Koha'); +} + +can_ok('C4::Koha', qw( GetDailyQuoteForInterface )); + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new; +my $test_quote = $builder->build({ + source => 'Quote', + value => { + source => 'Dusk And Her Embrace', + text => 'Unfurl thy limbs breathless succubus
How the full embosomed fog
Imparts the night to us....', + } +}); my $expected_quote = { - id => 10, source => 'Dusk And Her Embrace', text => 'Unfurl thy limbs breathless succubus
How the full embosomed fog
Imparts the night to us....', - timestamp => '0000-00-00 00:00:00', }; +t::lib::Mocks::mock_preference('QuoteOfTheDay', ''); + +subtest "GetDailyQuoteForInterface" => sub { + + plan tests => 3; -subtest "GetDailyQuoteForInterface", \&GetDailyQuoteForInterfaceTest; -sub GetDailyQuoteForInterfaceTest { my ($quote); ##Set interface and get nothing because syspref is not set. C4::Context->interface('opac'); - $quote = C4::Koha::GetDailyQuoteForInterface(id => 10); + $quote = C4::Koha::GetDailyQuoteForInterface(id => $test_quote->{id}); ok(not($quote), "'QuoteOfTheDay'-syspref not set so nothing returned"); ##Set 'QuoteOfTheDay'-syspref to not include current interface 'opac' - C4::Context->set_preference('QuoteOfTheDay', 'intra commandline sip2 api yo-mama'); - $quote = C4::Koha::GetDailyQuoteForInterface(id => 10); + t::lib::Mocks::mock_preference('QuoteOfTheDay', 'intra commandline sip2 api yo-mama'); + $quote = C4::Koha::GetDailyQuoteForInterface(id => $test_quote->{id}); ok(not($quote), "'QuoteOfTheDay'-syspref doesn't include 'opac'"); ##Set 'QuoteOfTheDay'-syspref to include current interface 'opac' - C4::Context->set_preference('QuoteOfTheDay', 'intraopaccommandline'); - $quote = C4::Koha::GetDailyQuoteForInterface(id => 10); - is_deeply($quote, $expected_quote, "Got the expected quote"); + t::lib::Mocks::mock_preference('QuoteOfTheDay', 'intraopaccommandline'); + $quote = C4::Koha::GetDailyQuoteForInterface(id => $test_quote->{id}); + is($quote->{text}, $expected_quote->{text}, "Got the expected quote"); -} +}; -# teardown -C4::Context->set_preference('QuoteOfTheDay', undef); -$dbh->do("DELETE FROM quotes"); \ No newline at end of file +$schema->storage->txn_rollback; \ No newline at end of file -- 2.17.1