Bug 16751: What is sitemaper?
[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 Modern::Perl;
7 use C4::Context;
8 use Koha::DateUtils qw(dt_from_string);
9 use Koha::AuthorisedValue;
10
11 use Test::More tests => 9;
12 use DateTime::Format::MySQL;
13
14 BEGIN {
15     use_ok('C4::Koha', qw( :DEFAULT GetDailyQuote GetItemTypesByCategory GetItemTypesCategorized));
16     use_ok('C4::Members');
17 }
18
19 my $dbh = C4::Context->dbh;
20 $dbh->{AutoCommit} = 0;
21 $dbh->{RaiseError} = 1;
22
23 subtest 'Authorized Values Tests' => sub {
24     plan tests => 7;
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 = Koha::AuthorisedValue->new(
37         {   category         => $data->{category},
38             authorised_value => $data->{authorised_value},
39             lib              => $data->{lib},
40             lib_opac         => $data->{lib_opac},
41             imageurl         => $data->{imageurl}
42         }
43     )->store;
44     ok( $insert_success, "Insert data in database" );
45
46
47 # Tests
48     SKIP: {
49         skip "INSERT failed", 4 unless $insert_success;
50
51         is ( GetAuthorisedValueByCode($data->{category}, $data->{authorised_value}), $data->{lib}, "GetAuthorisedValueByCode" );
52
53         my $sortdet=C4::Members::GetSortDetails("lost", "3");
54         is ($sortdet, "Lost and Paid For", "lost and paid works");
55
56         my $sortdet2=C4::Members::GetSortDetails("loc", "child");
57         is ($sortdet2, "Children's Area", "Child area works");
58
59         my $sortdet3=C4::Members::GetSortDetails("withdrawn", "1");
60         is ($sortdet3, "Withdrawn", "Withdrawn works");
61     }
62
63 # Clean up
64     if($insert_success){
65         my $query = "DELETE FROM authorised_values WHERE category=? AND authorised_value=? AND lib=? AND lib_opac=? AND imageurl=?;";
66         my $sth = $dbh->prepare($query);
67         $sth->execute($data->{category}, $data->{authorised_value}, $data->{lib}, $data->{lib_opac}, $data->{imageurl});
68     }
69
70     SKIP: {
71         eval { require Test::Deep; import Test::Deep; };
72         skip "Test::Deep required to run the GetAuthorisedValues() tests.", 2 if $@;
73         Koha::AuthorisedValue->new(
74             {   category         => 'BUG10656',
75                 authorised_value => 'ZZZ',
76                 lib              => 'Z_STAFF',
77                 lib_opac         => 'A_PUBLIC',
78                 imageurl         => ''
79             }
80         )->store;
81         Koha::AuthorisedValue->new(
82             {   category         => 'BUG10656',
83                 authorised_value => 'AAA',
84                 lib              => 'A_STAFF',
85                 lib_opac         => 'Z_PUBLIC',
86                 imageurl         => ''
87             }
88         )->store;
89
90         # the next one sets lib_opac to NULL; in that case, the staff
91         # display value is meant to be used.
92         Koha::AuthorisedValue->new(
93             {   category         => 'BUG10656',
94                 authorised_value => 'DDD',
95                 lib              => 'D_STAFF',
96                 lib_opac         => undef,
97                 imageurl         => ''
98             }
99         )->store;
100
101         my $authvals = GetAuthorisedValues('BUG10656');
102         cmp_deeply(
103             $authvals,
104             [
105                 {
106                     id => ignore(),
107                     category => 'BUG10656',
108                     authorised_value => 'AAA',
109                     lib => 'A_STAFF',
110                     lib_opac => 'Z_PUBLIC',
111                     imageurl => '',
112                 },
113                 {
114                     id => ignore(),
115                     category => 'BUG10656',
116                     authorised_value => 'DDD',
117                     lib => 'D_STAFF',
118                     lib_opac => undef,
119                     imageurl => '',
120                 },
121                 {
122                     id => ignore(),
123                     category => 'BUG10656',
124                     authorised_value => 'ZZZ',
125                     lib => 'Z_STAFF',
126                     lib_opac => 'A_PUBLIC',
127                     imageurl => '',
128                 },
129             ],
130             'list of authorised values in staff mode sorted by staff label (bug 10656)'
131         );
132         $authvals = GetAuthorisedValues('BUG10656', 1);
133         cmp_deeply(
134             $authvals,
135             [
136                 {
137                     id => ignore(),
138                     category => 'BUG10656',
139                     authorised_value => 'ZZZ',
140                     lib => 'A_PUBLIC',
141                     lib_opac => 'A_PUBLIC',
142                     imageurl => '',
143                 },
144                 {
145                     id => ignore(),
146                     category => 'BUG10656',
147                     authorised_value => 'DDD',
148                     lib => 'D_STAFF',
149                     lib_opac => undef,
150                     imageurl => '',
151                 },
152                 {
153                     id => ignore(),
154                     category => 'BUG10656',
155                     authorised_value => 'AAA',
156                     lib => 'Z_PUBLIC',
157                     lib_opac => 'Z_PUBLIC',
158                     imageurl => '',
159                 },
160             ],
161             'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
162         );
163     }
164
165 };
166
167 subtest 'Itemtype info Tests' => sub {
168     like ( getitemtypeinfo('BK')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on unspecified interface returns intranet imageurl (legacy behavior)' );
169     like ( getitemtypeinfo('BK', 'intranet')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on "intranet" interface returns intranet imageurl' );
170     like ( getitemtypeinfo('BK', 'opac')->{'imageurl'}, qr/opac-tmpl/, 'getitemtypeinfo on "opac" interface returns opac imageurl' );
171 };
172
173 ### test for C4::Koha->GetDailyQuote()
174 SKIP:
175     {
176         eval { require Test::Deep; import Test::Deep; };
177         skip "Test::Deep required to run the GetDailyQuote tests.", 1 if $@;
178
179         subtest 'Daily Quotes Test' => sub {
180             plan tests => 4;
181
182             SKIP: {
183
184                 skip "C4::Koha can't \'GetDailyQuote\'!", 3 unless can_ok('C4::Koha','GetDailyQuote');
185
186 # Fill the quote table with the default needed and a spare
187 $dbh->do("DELETE FROM quotes WHERE id=3 OR id=25;");
188 my $sql = "INSERT INTO quotes (id,source,text,timestamp) VALUES
189 (25,'Richard Nixon','When the President does it, that means that it is not illegal.','0000-00-00 00:00:00'),
190 (3,'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.','0000-00-00 00:00:00');";
191 $dbh->do($sql);
192
193                 my $expected_quote = {
194                     id          => 3,
195                     source      => 'Abraham Lincoln',
196                     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.',
197                     timestamp   => re('\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}'),   #'0000-00-00 00:00:00',
198                 };
199
200 # test quote retrieval based on id
201
202                 my $quote = GetDailyQuote('id'=>3);
203                 cmp_deeply ($quote, $expected_quote, "Got a quote based on id.") or
204                     diag('Be sure to run this test on a clean install of sample data.');
205
206 # test quote retrieval based on today's date
207
208                 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
209                 my $sth = C4::Context->dbh->prepare($query);
210                 $sth->execute(DateTime::Format::MySQL->format_datetime( dt_from_string() ), $expected_quote->{'id'});
211
212                 DateTime::Format::MySQL->format_datetime( dt_from_string() ) =~ m/(\d{4}-\d{2}-\d{2})/;
213                 $expected_quote->{'timestamp'} = re("^$1");
214
215 #        $expected_quote->{'timestamp'} = DateTime::Format::MySQL->format_datetime( dt_from_string() );   # update the timestamp of expected quote data
216
217                 $quote = GetDailyQuote(); # this is the "default" mode of selection
218                 cmp_deeply ($quote, $expected_quote, "Got a quote based on today's date.") or
219                     diag('Be sure to run this test on a clean install of sample data.');
220
221 # test random quote retrieval
222
223                 $quote = GetDailyQuote('random'=>1);
224                 ok ($quote, "Got a random quote.");
225             }
226         };
227 }
228
229
230 subtest 'ISBN tests' => sub {
231     plan tests => 6;
232
233     my $isbn13  = "9780330356473";
234     my $isbn13D = "978-0-330-35647-3";
235     my $isbn10  = "033035647X";
236     my $isbn10D = "0-330-35647-X";
237     is( xml_escape(undef), '',
238         'xml_escape() returns empty string on undef input' );
239     my $str = q{'"&<>'};
240     is(
241         xml_escape($str),
242         '&apos;&quot;&amp;&lt;&gt;&apos;',
243         'xml_escape() works as expected'
244     );
245     is( $str, q{'"&<>'}, '... and does not change input in place' );
246     is( C4::Koha::_isbn_cleanup('0-590-35340-3'),
247         '0590353403', '_isbn_cleanup removes hyphens' );
248     is( C4::Koha::_isbn_cleanup('0590353403 (pbk.)'),
249         '0590353403', '_isbn_cleanup removes parenthetical' );
250     is( C4::Koha::_isbn_cleanup('978-0-321-49694-2'),
251         '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
252
253 };
254
255 subtest 'GetFrameworksLoop() tests' => sub {
256     plan tests => 6;
257
258     $dbh->do("DELETE FROM biblio_framework");
259
260     my $frameworksloop = GetFrameworksLoop();
261     is ( scalar(@$frameworksloop), 0, 'No frameworks' );
262
263     $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'A', 'Third framework'  )");
264     $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'B', 'Second framework' )");
265     $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'C', 'First framework'  )");
266
267     $frameworksloop = GetFrameworksLoop();
268     is ( scalar(@$frameworksloop), 3, 'All frameworks' );
269     is ( scalar ( grep { defined $_->{'selected'} } @$frameworksloop ), 0, 'None selected' );
270
271     $frameworksloop = GetFrameworksLoop( 'B' );
272     is ( scalar ( grep { defined $_->{'selected'} } @$frameworksloop ), 1, 'One selected' );
273     my @descriptions = map { $_->{'description'} } @$frameworksloop;
274     is ( $descriptions[0], 'First framework', 'Ordered result' );
275     cmp_deeply(
276         $frameworksloop,
277         [
278             {
279                 'value' => 'C',
280                 'description' => 'First framework',
281                 'selected' => undef,
282             },
283             {
284                 'value' => 'B',
285                 'description' => 'Second framework',
286                 'selected' => 1,                # selected
287             },
288             {
289                 'value' => 'A',
290                 'description' => 'Third framework',
291                 'selected' => undef,
292             }
293         ],
294         'Full check, sorted by description with selected val (Bug 12675)'
295     );
296 };
297
298 subtest 'GetItemTypesByCategory GetItemTypesCategorized test' => sub{
299     plan tests => 7;
300
301     my $insertGroup = Koha::AuthorisedValue->new(
302         {   category         => 'ITEMTYPECAT',
303             authorised_value => 'Quertyware',
304         }
305     )->store;
306
307     ok($insertGroup, "Create group Qwertyware");
308
309     my $query = "INSERT into itemtypes (itemtype, description, searchcategory, hideinopac) values (?,?,?,?)";
310     my $insertSth = C4::Context->dbh->prepare($query);
311     $insertSth->execute('BKghjklo1', 'One type of book', '', 0);
312     $insertSth->execute('BKghjklo2', 'Another type of book', 'Qwertyware', 0);
313     $insertSth->execute('BKghjklo3', 'Yet another type of book', 'Qwertyware', 0);
314
315     # Azertyware should not exist.
316     my @results = GetItemTypesByCategory('Azertyware');
317     is(scalar @results, 0, 'GetItemTypesByCategory: Invalid category returns nothing');
318
319     @results = GetItemTypesByCategory('Qwertyware');
320     my @expected = ( 'BKghjklo2', 'BKghjklo3' );
321     is_deeply(\@results,\@expected,'GetItemTypesByCategory: valid category returns itemtypes');
322
323     # add more data since GetItemTypesCategorized's search is more subtle
324     $insertGroup = Koha::AuthorisedValue->new(
325         {   category         => 'ITEMTYPECAT',
326             authorised_value => 'Varyheavybook',
327         }
328     )->store;
329
330     $insertSth->execute('BKghjklo4', 'Another hidden book', 'Veryheavybook', 1);
331
332     my $hrCat = GetItemTypesCategorized();
333     ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: fully visible category exists');
334     ok($hrCat->{Veryheavybook} &&
335        $hrCat->{Veryheavybook}->{hideinopac}==1, 'GetItemTypesCategorized: non-visible category hidden' );
336
337     $insertSth->execute('BKghjklo5', 'An hidden book', 'Qwertyware', 1);
338     $hrCat = GetItemTypesCategorized();
339     ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: partially visible category exists');
340
341     my @only = ( 'BKghjklo1', 'BKghjklo2', 'BKghjklo3', 'BKghjklo4', 'BKghjklo5', 'Qwertyware', 'Veryheavybook' );
342     @results = ();
343     foreach my $key (@only) {
344         push @results, $key if exists $hrCat->{$key};
345     }
346     @expected = ( 'BKghjklo1', 'Qwertyware', 'Veryheavybook' );
347     is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
348 };
349
350 subtest 'GetItemTypes test' => sub {
351     plan tests => 1;
352     $dbh->do(q|DELETE FROM itemtypes|);
353     $dbh->do(q|INSERT INTO itemtypes(itemtype, description) VALUES ('a', 'aa desc'), ('b', 'zz desc'), ('d', 'dd desc'), ('c', 'yy desc')|);
354     my $itemtypes = C4::Koha::GetItemTypes( style => 'array' );
355     $itemtypes = [ map { $_->{itemtype} } @$itemtypes ];
356     is_deeply( $itemtypes, [ 'a', 'd', 'c', 'b' ], 'GetItemTypes(array) should return itemtypes ordered by description');
357 };
358
359 $dbh->rollback();