Bug 20144: [sql_modes] Correct quotes.timestamp values in tests
[koha.git] / t / db_dependent / Koha / GetDailyQuote.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 12;
21
22 use C4::Koha qw( GetDailyQuote );
23 use DateTime::Format::MySQL;
24 use Koha::DateUtils qw(dt_from_string);
25
26 BEGIN {
27     use_ok('C4::Koha');
28 }
29
30 can_ok('C4::Koha', qw( GetDailyQuote ));
31
32 my $dbh = C4::Context->dbh;
33
34 # Start transaction
35 $dbh->{AutoCommit} = 0;
36 $dbh->{RaiseError} = 1;
37
38 # Setup stage
39 $dbh->do("DELETE FROM quotes");
40
41 # Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297
42 $dbh->do("INSERT INTO `quotes` VALUES
43 (6,'George Washington','To be prepared for war is one of the most effectual means of preserving peace.',NOW()),
44 (7,'Thomas Jefferson','When angry, count ten, before you speak; if very angry, an hundred.',NOW()),
45 (8,'Abraham Lincoln','Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.',NOW()),
46 (9,'Abraham Lincoln','I have always found that mercy bears richer fruits than strict justice.',NOW()),
47 (10,'Andrew Johnson','I feel incompetent to perform duties...which have been so unexpectedly thrown upon me.',NOW());");
48
49 my $expected_quote = {
50     id          => 8,
51     source      => 'Abraham Lincoln',
52     text        => 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.',
53     timestamp   => dt_from_string,
54 };
55
56 my $quote = GetDailyQuote('id'=>8);
57 cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Correctly got quote by ID");
58 is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
59
60 $quote = GetDailyQuote('random'=>1);
61 ok($quote, "Got a random quote.");
62 cmp_ok($quote->{'id'}, '>', 0, 'Id is greater than 0');
63
64 my $timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string->add( hours => 1 )); # To make it the last one
65 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
66 my $sth = C4::Context->dbh->prepare($query);
67 $sth->execute( $timestamp , $expected_quote->{'id'});
68
69 $expected_quote->{'timestamp'} = $timestamp;
70
71 $quote = GetDailyQuote(); # this is the "default" mode of selection
72 cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
73 is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
74 is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
75
76 $dbh->do(q|DELETE FROM quotes|);
77 $quote = eval {GetDailyQuote();};
78 is( $@, '', 'GetDailyQuote does not die if no quote exist' );
79 is_deeply( $quote, {}, 'GetDailyQuote return an empty hashref is no quote exist'); # Is it what we expect?
80 $dbh->do(q|INSERT INTO `quotes` VALUES
81     (6,'George Washington','To be prepared for war is one of the most effectual means of preserving peace.',NOW())
82 |);
83
84 $quote = GetDailyQuote();
85 is( $quote->{id}, 6, ' GetDailyQuote returns the only existing quote' );
86
87 $dbh->rollback;