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