View | Details | Raw Unified | Return to bug 11279
Collapse All | Expand All

(-)a/C4/Koha.pm (-16 / +9 lines)
Lines 1442-1463 sub GetDailyQuote { Link Here
1442
        $sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;');
1442
        $sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;');
1443
        $sth->execute;
1443
        $sth->execute;
1444
        my $range = ($sth->fetchrow_array)[0];
1444
        my $range = ($sth->fetchrow_array)[0];
1445
        if ($range > 1) {
1445
        # chose a random id within that range if there is more than one quote
1446
            # chose a random id within that range if there is more than one quote
1446
        my $offset = int(rand($range));
1447
            my $offset = int(rand($range));
1447
        # grab it
1448
            # grab it
1448
        $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?';
1449
            $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?';
1449
        $sth = C4::Context->dbh->prepare($query);
1450
            $sth = C4::Context->dbh->prepare($query);
1450
        # see http://www.perlmonks.org/?node_id=837422 for why
1451
            # see http://www.perlmonks.org/?node_id=837422 for why
1451
        # we're being verbose and using bind_param
1452
            # we're being verbose and using bind_param
1452
        $sth->bind_param(1, $offset, SQL_INTEGER);
1453
            $sth->bind_param(1, $offset, SQL_INTEGER);
1453
        $sth->execute();
1454
            $sth->execute();
1455
        }
1456
        else {
1457
            $query = 'SELECT * FROM quotes;';
1458
            $sth = C4::Context->dbh->prepare($query);
1459
            $sth->execute();
1460
        }
1461
        $quote = $sth->fetchrow_hashref();
1454
        $quote = $sth->fetchrow_hashref();
1462
        # update the timestamp for that quote
1455
        # update the timestamp for that quote
1463
        $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
1456
        $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
(-)a/t/db_dependent/Koha/GetDailyQuote.t (-2 / +11 lines)
Lines 2-8 Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
4
5
use Test::More tests => 9;
5
use Test::More tests => 12;
6
use C4::Koha qw( GetDailyQuote );
6
use C4::Koha qw( GetDailyQuote );
7
use DateTime::Format::MySQL;
7
use DateTime::Format::MySQL;
8
use Koha::DateUtils qw(dt_from_string);
8
use Koha::DateUtils qw(dt_from_string);
Lines 63-67 cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct"); Link Here
63
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
63
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
64
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
64
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
65
65
66
$dbh->do(q|DELETE FROM quotes|);
67
$quote = eval {GetDailyQuote();};
68
is( $@, '', 'GetDailyQuote does not die if no quote exist' );
69
is_deeply( $quote, {}, 'GetDailyQuote return an empty hashref is no quote exist'); # Is it what we expect?
70
$dbh->do(q|INSERT INTO `quotes` VALUES
71
    (6,'George Washington','To be prepared for war is one of the most effectual means of preserving peace.','0000-00-00 00:00:00')
72
|);
73
74
$quote = GetDailyQuote();
75
is( $quote->{id}, 6, ' GetDailyQuote returns the only existing quote' );
66
76
67
$dbh->rollback;
77
$dbh->rollback;
68
- 

Return to bug 11279