Bug 10666: (follow-up) really fix the daily quotes test
[koha.git] / t / db_dependent / Koha.t
1 #!/usr/bin/perl
2 #
3 # This is to test C4/Koha
4 # It requires a working Koha database with the sample data
5
6 use strict;
7 use warnings;
8 use C4::Context;
9 use Koha::DateUtils qw(dt_from_string);
10
11 use Test::More tests => 6;
12 use DateTime::Format::MySQL;
13
14 eval {use Test::Deep;};
15
16 BEGIN {
17     use_ok('C4::Koha', qw( :DEFAULT GetDailyQuote ));
18     use_ok('C4::Members');
19 }
20
21 my $dbh = C4::Context->dbh;
22
23 subtest 'Authorized Values Tests' => sub {
24     plan tests => 6;
25
26     my $data = {
27         category            => 'CATEGORY',
28         authorised_value    => 'AUTHORISED_VALUE',
29         lib                 => 'LIB',
30         lib_opac            => 'LIBOPAC',
31         imageurl            => 'IMAGEURL'
32     };
33
34
35 # Insert an entry into authorised_value table
36     my $insert_success = AddAuthorisedValue($data->{category}, $data->{authorised_value}, $data->{lib}, $data->{lib_opac}, $data->{imageurl});
37     ok($insert_success, "Insert data in database");
38
39
40 # Tests
41     SKIP: {
42         skip "INSERT failed", 5 unless $insert_success;
43
44         is ( GetAuthorisedValueByCode($data->{category}, $data->{authorised_value}), $data->{lib}, "GetAuthorisedValueByCode" );
45         is ( GetKohaImageurlFromAuthorisedValues($data->{category}, $data->{lib}), $data->{imageurl}, "GetKohaImageurlFromAuthorisedValues" );
46
47         my $sortdet=C4::Members::GetSortDetails("lost", "3");
48         is ($sortdet, "Lost and Paid For", "lost and paid works");
49
50         my $sortdet2=C4::Members::GetSortDetails("loc", "child");
51         is ($sortdet2, "Children's Area", "Child area works");
52
53         my $sortdet3=C4::Members::GetSortDetails("withdrawn", "1");
54         is ($sortdet3, "Withdrawn", "Withdrawn works");
55     }
56
57 # Clean up
58     if($insert_success){
59         my $query = "DELETE FROM authorised_values WHERE category=? AND authorised_value=? AND lib=? AND lib_opac=? AND imageurl=?;";
60         my $sth = $dbh->prepare($query);
61         $sth->execute($data->{category}, $data->{authorised_value}, $data->{lib}, $data->{lib_opac}, $data->{imageurl});
62     }
63 };
64
65 subtest 'Itemtype info Tests' => sub {
66     like ( getitemtypeinfo('BK')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on unspecified interface returns intranet imageurl (legacy behavior)' );
67     like ( getitemtypeinfo('BK', 'intranet')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on "intranet" interface returns intranet imageurl' );
68     like ( getitemtypeinfo('BK', 'opac')->{'imageurl'}, qr/opac-tmpl/, 'getitemtypeinfo on "opac" interface returns opac imageurl' );
69 };
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 ### test for C4::Koha->GetDailyQuote()
86 SKIP:
87     {
88         skip "Test::Deep required to run the GetDailyQuote tests.", 1 if $@;
89
90         subtest 'Daily Quotes Test' => sub {
91             plan tests => 4;
92
93             SKIP: {
94
95                 skip "C4::Koha can't \'GetDailyQuote\'!", 3 unless can_ok('C4::Koha','GetDailyQuote');
96
97                 my $expected_quote = {
98                     id          => 3,
99                     source      => 'Abraham Lincoln',
100                     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.',
101                     timestamp   => re('\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}'),   #'0000-00-00 00:00:00',
102                 };
103
104 # test quote retrieval based on id
105
106                 my $quote = GetDailyQuote('id'=>3);
107                 cmp_deeply ($quote, $expected_quote, "Got a quote based on id.") or
108                     diag('Be sure to run this test on a clean install of sample data.');
109
110 # test quote retrieval based on today's date
111
112                 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
113                 my $sth = C4::Context->dbh->prepare($query);
114                 $sth->execute(DateTime::Format::MySQL->format_datetime( dt_from_string() ), $expected_quote->{'id'});
115
116                 DateTime::Format::MySQL->format_datetime( dt_from_string() ) =~ m/(\d{4}-\d{2}-\d{2})/;
117                 $expected_quote->{'timestamp'} = re("^$1");
118
119 #        $expected_quote->{'timestamp'} = DateTime::Format::MySQL->format_datetime( dt_from_string() );   # update the timestamp of expected quote data
120
121                 $quote = GetDailyQuote(); # this is the "default" mode of selection
122                 cmp_deeply ($quote, $expected_quote, "Got a quote based on today's date.") or
123                     diag('Be sure to run this test on a clean install of sample data.');
124
125 # test random quote retrieval
126
127                 $quote = GetDailyQuote('random'=>1);
128                 ok ($quote, "Got a random quote.");
129             }
130         };
131 }
132
133
134 #
135 # test that &slashifyDate returns correct (non-US) date
136 #
137 subtest 'Date and ISBN tests' => sub {
138     plan tests => 7;
139
140     my $date    = "01/01/2002";
141     my $newdate = &slashifyDate("2002-01-01");
142     my $isbn13  = "9780330356473";
143     my $isbn13D = "978-0-330-35647-3";
144     my $isbn10  = "033035647X";
145     my $isbn10D = "0-330-35647-X";
146     ok( $date eq $newdate, 'slashifyDate' );
147     my $undef = undef;
148     is( xml_escape($undef), '',
149         'xml_escape() returns empty string on undef input' );
150     my $str = q{'"&<>'};
151     is(
152         xml_escape($str),
153         '&apos;&quot;&amp;&lt;&gt;&apos;',
154         'xml_escape() works as expected'
155     );
156     is( $str, q{'"&<>'}, '... and does not change input in place' );
157     is( C4::Koha::_isbn_cleanup('0-590-35340-3'),
158         '0590353403', '_isbn_cleanup removes hyphens' );
159     is( C4::Koha::_isbn_cleanup('0590353403 (pbk.)'),
160         '0590353403', '_isbn_cleanup removes parenthetical' );
161     is( C4::Koha::_isbn_cleanup('978-0-321-49694-2'),
162         '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
163
164 };