Lines 16-23
Link Here
|
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
use DateTime::Format::MySQL; |
19 |
use Test::More tests => 15; |
20 |
use Test::More tests => 13; |
|
|
21 |
|
20 |
|
22 |
use Koha::Database; |
21 |
use Koha::Database; |
23 |
use Koha::DateUtils qw(dt_from_string); |
22 |
use Koha::DateUtils qw(dt_from_string); |
Lines 29-34
use t::lib::Mocks;
Link Here
|
29 |
|
28 |
|
30 |
BEGIN { |
29 |
BEGIN { |
31 |
use_ok('Koha::Quote'); |
30 |
use_ok('Koha::Quote'); |
|
|
31 |
use_ok('Koha::Quotes'); |
32 |
} |
32 |
} |
33 |
|
33 |
|
34 |
my $quote = Koha::Quote->new(); |
34 |
my $quote = Koha::Quote->new(); |
Lines 39-45
$schema->storage->txn_begin;
Link Here
|
39 |
my $dbh = C4::Context->dbh; |
39 |
my $dbh = C4::Context->dbh; |
40 |
|
40 |
|
41 |
# Ids not starting with 1 to reflect possible deletes, this acts as a regression test for bug 11297 |
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()); |
42 |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; |
|
|
43 |
my $timestamp = $dtf->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_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_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_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; |
Lines 53-119
my $expected_quote = {
Link Here
|
53 |
timestamp => $timestamp, |
54 |
timestamp => $timestamp, |
54 |
}; |
55 |
}; |
55 |
|
56 |
|
56 |
$quote = Koha::Quote->get_daily_quote('id'=>$quote_3->id); |
57 |
#First test with QuoteOfTheDay disabled |
|
|
58 |
t::lib::Mocks::mock_preference('QuoteOfTheDay', 0); |
59 |
|
60 |
##Set interface and get nothing because syspref is not set. |
61 |
C4::Context->interface('opac'); |
62 |
$quote = Koha::Quotes->get_daily_quote(id => $quote_1->id); |
63 |
ok(not($quote), "'QuoteOfTheDay'-syspref not set so nothing returned"); |
64 |
|
65 |
##Set 'QuoteOfTheDay'-syspref to not include current interface 'opac' |
66 |
t::lib::Mocks::mock_preference('QuoteOfTheDay', 'intranet'); |
67 |
$quote = Koha::Quotes->get_daily_quote(id => $quote_1->id); |
68 |
ok(not($quote), "'QuoteOfTheDay'-syspref doesn't include 'opac'"); |
69 |
|
70 |
##Set 'QuoteOfTheDay'-syspref to include current interface 'opac' |
71 |
t::lib::Mocks::mock_preference('QuoteOfTheDay', 'opac,intranet'); |
72 |
|
73 |
$quote = Koha::Quotes->get_daily_quote('id'=>$quote_3->id); |
57 |
cmp_ok($quote->id, '==', $expected_quote->{'id'}, "Correctly got quote by ID"); |
74 |
cmp_ok($quote->id, '==', $expected_quote->{'id'}, "Correctly got quote by ID"); |
58 |
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct"); |
75 |
is($quote->{'quote'}, $expected_quote->{'quote'}, "Quote is correct"); |
59 |
|
76 |
|
60 |
$quote = Koha::Quote->get_daily_quote('random'=>1); |
77 |
$quote = Koha::Quotes->get_daily_quote('random'=>1); |
61 |
ok($quote, "Got a random quote."); |
78 |
ok($quote, "Got a random quote."); |
62 |
cmp_ok($quote->id, '>', 0, 'Id is greater than 0'); |
79 |
cmp_ok($quote->id, '>', 0, 'Id is greater than 0'); |
63 |
|
80 |
|
64 |
$timestamp = DateTime::Format::MySQL->format_datetime(dt_from_string->add( seconds => 1 )); # To make it the last one |
81 |
$timestamp = $dtf->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 }); |
82 |
Koha::Quotes->search({ id => $expected_quote->{'id'} })->update({ timestamp => $timestamp }); |
66 |
$expected_quote->{'timestamp'} = $timestamp; |
83 |
$expected_quote->{'timestamp'} = $timestamp; |
67 |
|
84 |
|
68 |
$quote = Koha::Quote->get_daily_quote()->unblessed; # this is the "default" mode of selection |
85 |
$quote = Koha::Quotes->get_daily_quote()->unblessed; # this is the "default" mode of selection |
69 |
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct"); |
86 |
cmp_ok($quote->{'id'}, '==', $expected_quote->{'id'}, "Id is correct"); |
70 |
is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct"); |
87 |
is($quote->{'source'}, $expected_quote->{'source'}, "Source is correct"); |
71 |
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct"); |
88 |
is($quote->{'timestamp'}, $expected_quote->{'timestamp'}, "Timestamp $timestamp is correct"); |
72 |
|
89 |
|
73 |
Koha::Quotes->search()->delete(); |
90 |
Koha::Quotes->search()->delete(); |
74 |
$quote = eval {Koha::Quote->get_daily_quote();}; |
91 |
$quote = eval {Koha::Quotes->get_daily_quote();}; |
75 |
is( $@, '', 'get_daily_quote does not die if no quote exist' ); |
92 |
is( $@, '', 'get_daily_quote does not die if no quote exist' ); |
76 |
is_deeply( $quote, undef, 'return undef if quotes do not exists'); # Is it what we expect? |
93 |
is_deeply( $quote, undef, 'return undef if quotes do not exists'); # Is it what we expect? |
77 |
|
94 |
|
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; |
95 |
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; |
79 |
|
96 |
|
80 |
$quote = Koha::Quote->get_daily_quote(); |
97 |
$quote = Koha::Quotes->get_daily_quote(); |
81 |
is( $quote->id, $quote_6->id, ' get_daily_quote returns the only existing quote' ); |
98 |
is( $quote->id, $quote_6->id, ' get_daily_quote returns the only existing quote' ); |
82 |
|
99 |
|
83 |
$schema->storage->txn_rollback; |
100 |
$schema->storage->txn_rollback; |
84 |
|
|
|
85 |
subtest "get_daily_quote_for_interface" => sub { |
86 |
|
87 |
plan tests => 3; |
88 |
|
89 |
$schema->storage->txn_begin; |
90 |
|
91 |
my ($quote); |
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; |
93 |
|
94 |
my $expected_quote = { |
95 |
id => $quote_1->id, |
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), |
99 |
}; |
100 |
|
101 |
t::lib::Mocks::mock_preference('QuoteOfTheDay', 0); |
102 |
|
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"); |
107 |
|
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'"); |
112 |
|
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)->unblessed; |
116 |
is_deeply($quote, $expected_quote, "Got the expected quote"); |
117 |
|
118 |
$schema->storage->txn_rollback; |
119 |
}; |
120 |
- |