Bug 11543: (followup) add one more test
[koha.git] / t / db_dependent / Koha / GetDailyQuote.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 12;
6 use C4::Koha qw( GetDailyQuote );
7 use DateTime::Format::MySQL;
8 use Koha::DateUtils qw(dt_from_string);
9
10 BEGIN {
11     use_ok('C4::Koha');
12 }
13
14 can_ok('C4::Koha', qw( GetDailyQuote ));
15
16 my $dbh = C4::Context->dbh;
17
18 # Start transaction
19 $dbh->{AutoCommit} = 0;
20 $dbh->{RaiseError} = 1;
21
22 # Setup stage
23 $dbh->do("DELETE FROM quotes");
24
25 # Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297
26 $dbh->do("INSERT INTO `quotes` VALUES
27 (6,'George Washington','To be prepared for war is one of the most effectual means of preserving peace.','0000-00-00 00:00:00'),
28 (7,'Thomas Jefferson','When angry, count ten, before you speak; if very angry, an hundred.','0000-00-00 00:00:00'),
29 (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'),
30 (9,'Abraham Lincoln','I have always found that mercy bears richer fruits than strict justice.','0000-00-00 00:00:00'),
31 (10,'Andrew Johnson','I feel incompetent to perform duties...which have been so unexpectedly thrown upon me.','0000-00-00 00:00:00');");
32
33 my $expected_quote = {
34     id          => 8,
35     source      => 'Abraham Lincoln',
36     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.',
37     timestamp   => '0000-00-00 00:00:00',
38 };
39
40 diag("Get a quote based on id");
41 my $quote = GetDailyQuote('id'=>8);
42 cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
43 is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
44
45
46 diag("Get a random quote");
47 $quote = GetDailyQuote('random'=>1);
48 ok($quote, "Got a random quote.");
49 cmp_ok($quote->{'id'}, '>', 0, 'Id is greater than 0');
50
51
52 diag("Get a quote based on today's date");
53 $dbh->do("UPDATE quotes SET timestamp = '0000-00-00 00:00:00';");
54 my $timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string());
55 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
56 my $sth = C4::Context->dbh->prepare($query);
57 $sth->execute( $timestamp , $expected_quote->{'id'});
58
59 $expected_quote->{'timestamp'} = $timestamp;
60
61 $quote = GetDailyQuote(); # this is the "default" mode of selection
62 cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
63 is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
64 is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
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' );
76
77 $dbh->rollback;