From 59d3dcb508a4f3be750e79797254f43bf5e1fae9 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 19 Dec 2013 11:05:54 +0100 Subject: [PATCH] Bug 11279: (follow-up) remove unnecessary check on number of quotes It is not necessary to process the case where the number of quotes is just one, as int(rand(1)) will always produce 0, which is a valid offset. Signed-off-by: Galen Charlton (cherry picked from commit 8ee6e4290e2d1ca4fea53363b7ca3275cb0e6f58) (cherry picked from commit bff4fac7b78fe8573527c8733f8364af351d23a9) Signed-off-by: Tomas Cohen Arazi --- C4/Koha.pm | 25 +++++++++---------------- t/db_dependent/Koha/GetDailyQuote.t | 12 +++++++++++- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/C4/Koha.pm b/C4/Koha.pm index 7966072ce1..9dc7f110ad 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -1454,22 +1454,15 @@ sub GetDailyQuote { $sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;'); $sth->execute; my $range = ($sth->fetchrow_array)[0]; - if ($range > 1) { - # chose a random id within that range if there is more than one quote - my $offset = int(rand($range)); - # grab it - $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?'; - $sth = C4::Context->dbh->prepare($query); - # see http://www.perlmonks.org/?node_id=837422 for why - # we're being verbose and using bind_param - $sth->bind_param(1, $offset, SQL_INTEGER); - $sth->execute(); - } - else { - $query = 'SELECT * FROM quotes;'; - $sth = C4::Context->dbh->prepare($query); - $sth->execute(); - } + # chose a random id within that range if there is more than one quote + my $offset = int(rand($range)); + # grab it + $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?'; + $sth = C4::Context->dbh->prepare($query); + # see http://www.perlmonks.org/?node_id=837422 for why + # we're being verbose and using bind_param + $sth->bind_param(1, $offset, SQL_INTEGER); + $sth->execute(); $quote = $sth->fetchrow_hashref(); # update the timestamp for that quote $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?'; diff --git a/t/db_dependent/Koha/GetDailyQuote.t b/t/db_dependent/Koha/GetDailyQuote.t index 2785b220ec..d003218c17 100644 --- a/t/db_dependent/Koha/GetDailyQuote.t +++ b/t/db_dependent/Koha/GetDailyQuote.t @@ -2,7 +2,7 @@ use Modern::Perl; -use Test::More tests => 9; +use Test::More tests => 12; use C4::Koha qw( GetDailyQuote ); use DateTime::Format::MySQL; use Koha::DateUtils qw(dt_from_string); @@ -63,5 +63,15 @@ cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct"); is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct"); is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct"); +$dbh->do(q|DELETE FROM quotes|); +$quote = eval {GetDailyQuote();}; +is( $@, '', 'GetDailyQuote does not die if no quote exist' ); +is_deeply( $quote, {}, 'GetDailyQuote return an empty hashref is no quote exist'); # Is it what we expect? +$dbh->do(q|INSERT INTO `quotes` VALUES + (6,'George Washington','To be prepared for war is one of the most effectual means of preserving peace.','0000-00-00 00:00:00') +|); + +$quote = GetDailyQuote(); +is( $quote->{id}, 6, ' GetDailyQuote returns the only existing quote' ); $dbh->rollback; -- 2.39.5