Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Koha / Quotes.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 use Test::More tests => 14;
20
21 use Koha::Database;
22 use Koha::DateUtils qw(dt_from_string);
23 use Koha::Quote;
24 use Koha::Quotes;
25
26 use t::lib::TestBuilder;
27 use t::lib::Dates;
28 use t::lib::Mocks;
29
30 BEGIN {
31     use_ok('Koha::Quote');
32     use_ok('Koha::Quotes');
33 }
34
35 my $quote = Koha::Quote->new();
36 isa_ok( $quote, 'Koha::Quote', 'Quote class returned' );
37
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40 my $dbh = C4::Context->dbh;
41
42 # Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297
43 my $yesterday = dt_from_string()->subtract(days => 1);
44 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.' })->store;
45 my $quote_2 = Koha::Quote->new({ source => 'Thomas Jefferson', text => 'When angry, count ten, before you speak; if very angry, an hundred.' })->store;
46 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' })->store;
47 my $quote_4 = Koha::Quote->new({ source => 'Abraham Lincoln', text => 'I have always found that mercy bears richer fruits than strict justice.' })->store;
48 my $quote_5 = Koha::Quote->new({ source => 'Andrew Johnson', text => 'I feel incompetent to perform duties...which have been so unexpectedly thrown upon me.' })->store;
49
50 #First test with QuoteOfTheDay disabled
51 t::lib::Mocks::mock_preference('QuoteOfTheDay', 0);
52
53 ##Set interface and get nothing because syspref is not set.
54 C4::Context->interface('opac');
55 $quote = Koha::Quotes->get_daily_quote(id => $quote_1->id);
56 ok(not($quote), "'QuoteOfTheDay'-syspref not set so nothing returned");
57
58 ##Set 'QuoteOfTheDay'-syspref to not include current interface 'opac'
59 t::lib::Mocks::mock_preference('QuoteOfTheDay', 'intranet');
60 $quote = Koha::Quotes->get_daily_quote(id => $quote_1->id);
61 ok(not($quote), "'QuoteOfTheDay'-syspref doesn't include 'opac'");
62
63 ##Set 'QuoteOfTheDay'-syspref to include current interface 'opac'
64 t::lib::Mocks::mock_preference('QuoteOfTheDay', 'opac,intranet');
65
66 $quote = Koha::Quotes->get_daily_quote('id'=>$quote_3->id);
67 is($quote->id, $quote_3->id, "Correctly got quote by ID");
68 is($quote->text, $quote_3->text, "Quote is correct");
69 is(t::lib::Dates::compare($quote->timestamp, dt_from_string), 0, "get_daily_quote updated the timestamp/last seen");
70
71 $quote = Koha::Quotes->get_daily_quote('random'=>1);
72 ok($quote, "Got a random quote.");
73 cmp_ok($quote->id, '>', 0, 'Id is greater than 0');
74
75 subtest 'timestamp column is updated' => sub {
76     plan tests => 3;
77
78     Koha::Quotes->search->update( { timestamp => $yesterday } );
79
80     my $now = dt_from_string;
81
82     my $expected_quote = {
83         id          => $quote_3->id,
84         source      => 'Abraham Lincoln',
85         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.',
86         timestamp   => $yesterday,
87     };
88
89
90     Koha::Quotes->find( $expected_quote->{'id'} )
91                 ->update( { timestamp => $now->clone->subtract( seconds => 10 ) } ); ; # To make it the last one
92     $quote = Koha::Quotes->get_daily_quote(); # this is the "default" mode of selection
93     is($quote->id, $expected_quote->{'id'}, "Id is correct");
94     is($quote->source, $expected_quote->{'source'}, "Source is correct");
95     is(t::lib::Dates::compare($quote->timestamp, $now), 0, "get_daily_quote updated the timestamp/last seen");
96 };
97
98 Koha::Quotes->search()->delete();
99 $quote = eval {Koha::Quotes->get_daily_quote();};
100 is( $@, '', 'get_daily_quote does not die if no quote exist' );
101 is_deeply( $quote, undef, 'return undef if quotes do not exists'); # Is it what we expect?
102
103 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;
104
105 $quote = Koha::Quotes->get_daily_quote();
106 is( $quote->id, $quote_6->id, ' get_daily_quote returns the only existing quote' );
107
108 $schema->storage->txn_rollback;