Bug 14334: Remove AutoCommit from 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::Database;
25 use Koha::DateUtils qw(dt_from_string);
26
27 BEGIN {
28     use_ok('C4::Koha');
29 }
30
31 can_ok('C4::Koha', qw( GetDailyQuote ));
32
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35 my $dbh = C4::Context->dbh;
36
37 # Setup stage
38 $dbh->do("DELETE FROM quotes");
39
40 # Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297
41 $dbh->do("INSERT INTO `quotes` VALUES
42 (6,'George Washington','To be prepared for war is one of the most effectual means of preserving peace.',NOW()),
43 (7,'Thomas Jefferson','When angry, count ten, before you speak; if very angry, an hundred.',NOW()),
44 (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()),
45 (9,'Abraham Lincoln','I have always found that mercy bears richer fruits than strict justice.',NOW()),
46 (10,'Andrew Johnson','I feel incompetent to perform duties...which have been so unexpectedly thrown upon me.',NOW());");
47
48 my $expected_quote = {
49     id          => 8,
50     source      => 'Abraham Lincoln',
51     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.',
52     timestamp   => dt_from_string,
53 };
54
55 my $quote = GetDailyQuote('id'=>8);
56 cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Correctly got quote by ID");
57 is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
58
59 $quote = GetDailyQuote('random'=>1);
60 ok($quote, "Got a random quote.");
61 cmp_ok($quote->{'id'}, '>', 0, 'Id is greater than 0');
62
63 my $timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string->add( seconds => 1 )); # To make it the last one
64 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
65 my $sth = C4::Context->dbh->prepare($query);
66 $sth->execute( $timestamp , $expected_quote->{'id'});
67
68 $expected_quote->{'timestamp'} = $timestamp;
69
70 $quote = GetDailyQuote(); # this is the "default" mode of selection
71 cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
72 is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct");
73 is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
74
75 $dbh->do(q|DELETE FROM quotes|);
76 $quote = eval {GetDailyQuote();};
77 is( $@, '', 'GetDailyQuote does not die if no quote exist' );
78 is_deeply( $quote, {}, 'GetDailyQuote return an empty hashref is no quote exist'); # Is it what we expect?
79 $dbh->do(q|INSERT INTO `quotes` VALUES
80     (6,'George Washington','To be prepared for war is one of the most effectual means of preserving peace.',NOW())
81 |);
82
83 $quote = GetDailyQuote();
84 is( $quote->{id}, 6, ' GetDailyQuote returns the only existing quote' );