3 # This file is part of Koha.
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.
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.
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>.
19 use DateTime::Format::MySQL;
20 use Test::More tests => 13;
23 use Koha::DateUtils qw(dt_from_string);
27 use t::lib::TestBuilder;
31 use_ok('Koha::Quote');
34 my $quote = Koha::Quote->new();
35 isa_ok( $quote, 'Koha::Quote', 'Quote class returned' );
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39 my $dbh = C4::Context->dbh;
41 # Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297
42 my $timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string());
43 my $quote_1 = Koha::Quote->new({ source => 'George Washington', text => 'To be prepared for war is one of the most effectual means of preserving peace.', timestamp => $timestamp })->store;
44 my $quote_2 = Koha::Quote->new({ source => 'Thomas Jefferson', text => 'When angry, count ten, before you speak; if very angry, an hundred.', timestamp => $timestamp })->store;
45 my $quote_3 = Koha::Quote->new({ source => 'Abraham Lincoln', 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', timestamp => $timestamp })->store;
46 my $quote_4 = Koha::Quote->new({ source => 'Abraham Lincoln', text => 'I have always found that mercy bears richer fruits than strict justice.', timestamp => $timestamp })->store;
47 my $quote_5 = Koha::Quote->new({ source => 'Andrew Johnson', text => 'I feel incompetent to perform duties...which have been so unexpectedly thrown upon me.', timestamp => $timestamp })->store;
49 my $expected_quote = {
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,
56 $quote = Koha::Quote->get_daily_quote('id'=>$quote_3->id);
57 cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Correctly got quote by ID");
58 is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct");
60 $quote = Koha::Quote->get_daily_quote('random'=>1);
61 ok($quote, "Got a random quote.");
62 cmp_ok($quote->{'id'}, '>', 0, 'Id is greater than 0');
64 $timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string->add( seconds => 1 )); # To make it the last one
65 Koha::Quotes->search({ id => $expected_quote->{'id'} })->update({ timestamp => $timestamp });
66 $expected_quote->{'timestamp'} = $timestamp;
68 $quote = Koha::Quote->get_daily_quote(); # this is the "default" mode of selection
69 cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct");
70 is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct");
71 is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct");
73 $dbh->do(q|DELETE FROM quotes|);
74 $quote = eval {Koha::Quote->get_daily_quote();};
75 is( $@, '', 'get_daily_quote does not die if no quote exist' );
76 is_deeply( $quote, {}, 'get_daily_quote return an empty hashref is no quote exist'); # Is it what we expect?
78 my $quote_6 = Koha::Quote->new({ source => 'George Washington', text => 'To be prepared for war is one of the most effectual means of preserving peace.', timestamp => dt_from_string() })->store;
80 $quote = Koha::Quote->get_daily_quote();
81 is( $quote->{id}, $quote_6->id, ' get_daily_quote returns the only existing quote' );
83 $schema->storage->txn_rollback;
85 subtest "get_daily_quote_for_interface" => sub {
89 $schema->storage->txn_begin;
92 my $quote_1 = Koha::Quote->new({ source => 'Dusk And Her Embrace', text => 'Unfurl thy limbs breathless succubus<br/>How the full embosomed fog<br/>Imparts the night to us....', timestamp => dt_from_string })->store;
94 my $expected_quote = {
96 source => 'Dusk And Her Embrace',
97 text => 'Unfurl thy limbs breathless succubus<br/>How the full embosomed fog<br/>Imparts the night to us....',
98 timestamp => DateTime::Format::MySQL->format_datetime(dt_from_string),
101 t::lib::Mocks::mock_preference('QuoteOfTheDay', 0);
103 ##Set interface and get nothing because syspref is not set.
104 C4::Context->interface('opac');
105 $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id);
106 ok(not($quote), "'QuoteOfTheDay'-syspref not set so nothing returned");
108 ##Set 'QuoteOfTheDay'-syspref to not include current interface 'opac'
109 t::lib::Mocks::mock_preference('QuoteOfTheDay', 'intranet');
110 $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id);
111 ok(not($quote), "'QuoteOfTheDay'-syspref doesn't include 'opac'");
113 ##Set 'QuoteOfTheDay'-syspref to include current interface 'opac'
114 t::lib::Mocks::mock_preference('QuoteOfTheDay', 'opac,intranet');
115 $quote = Koha::Quote->get_daily_quote_for_interface(id => $quote_1->id);
116 is_deeply($quote, $expected_quote, "Got the expected quote");
118 $schema->storage->txn_rollback;