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 <gmc@esilibrary.com>
This commit is contained in:
parent
5a3db23362
commit
8ee6e4290e
25
C4/Koha.pm
25
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 = ?';
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue