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