Bug 19120: Add tests to reproduce the problem
[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.','0000-00-00 00:00:00'),
44 (7,'Thomas Jefferson','When angry, count ten, before you speak; if very angry, an hundred.','0000-00-00 00:00:00'),
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.','0000-00-00 00:00:00'),
46 (9,'Abraham Lincoln','I have always found that mercy bears richer fruits than strict justice.','0000-00-00 00:00:00'),
47 (10,'Andrew Johnson','I feel incompetent to perform duties...which have been so unexpectedly thrown upon me.','0000-00-00 00:00:00');");
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   => '0000-00-00 00:00:00',
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 $dbh->do("UPDATE quotes SET timestamp = '0000-00-00 00:00:00';");
65 my $timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string());
66 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
67 my $sth = C4::Context->dbh->prepare($query);
68 $sth->execute( $timestamp , $expected_quote->{'id'});
69
70 $expected_quote->{'timestamp'} = $timestamp;
71
72 $quote = GetDailyQuote(); # this is the "default" mode of selection
73 cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
74 is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
75 is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
76
77 $dbh->do(q|DELETE FROM quotes|);
78 $quote = eval {GetDailyQuote();};
79 is( $@, '', 'GetDailyQuote does not die if no quote exist' );
80 is_deeply( $quote, {}, 'GetDailyQuote return an empty hashref is no quote exist'); # Is it what we expect?
81 $dbh->do(q|INSERT INTO `quotes` VALUES
82     (6,'George Washington','To be prepared for war is one of the most effectual means of preserving peace.','0000-00-00 00:00:00')
83 |);
84
85 $quote = GetDailyQuote();
86 is( $quote->{id}, 6, ' GetDailyQuote returns the only existing quote' );
87
88 $dbh->rollback;