@@ -, +, @@ --- C4/Koha.pm | 25 +++++++++---------------- t/db_dependent/Koha/GetDailyQuote.t | 12 +++++++++++- 2 files changed, 20 insertions(+), 17 deletions(-) --- a/C4/Koha.pm +++ a/C4/Koha.pm @@ -1442,22 +1442,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 = ?'; --- a/t/db_dependent/Koha/GetDailyQuote.t +++ a/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; --