3 # This is to test C4/Koha
4 # It requires a working Koha database with the sample data
8 use Koha::DateUtils qw(dt_from_string);
9 use Koha::AuthorisedValue;
11 use Test::More tests => 9;
12 use DateTime::Format::MySQL;
15 use_ok('C4::Koha', qw( :DEFAULT GetDailyQuote GetItemTypesByCategory GetItemTypesCategorized));
16 use_ok('C4::Members');
19 my $dbh = C4::Context->dbh;
20 $dbh->{AutoCommit} = 0;
21 $dbh->{RaiseError} = 1;
23 subtest 'Authorized Values Tests' => sub {
27 category => 'CATEGORY',
28 authorised_value => 'AUTHORISED_VALUE',
30 lib_opac => 'LIBOPAC',
31 imageurl => 'IMAGEURL'
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},
40 lib_opac => $data->{lib_opac},
41 imageurl => $data->{imageurl}
44 ok( $insert_success, "Insert data in database" );
49 skip "INSERT failed", 4 unless $insert_success;
51 is ( GetAuthorisedValueByCode($data->{category}, $data->{authorised_value}), $data->{lib}, "GetAuthorisedValueByCode" );
53 my $sortdet=C4::Members::GetSortDetails("lost", "3");
54 is ($sortdet, "Lost and Paid For", "lost and paid works");
56 my $sortdet2=C4::Members::GetSortDetails("loc", "child");
57 is ($sortdet2, "Children's Area", "Child area works");
59 my $sortdet3=C4::Members::GetSortDetails("withdrawn", "1");
60 is ($sortdet3, "Withdrawn", "Withdrawn works");
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});
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',
77 lib_opac => 'A_PUBLIC',
81 Koha::AuthorisedValue->new(
82 { category => 'BUG10656',
83 authorised_value => 'AAA',
85 lib_opac => 'Z_PUBLIC',
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',
101 my $authvals = GetAuthorisedValues('BUG10656');
107 category => 'BUG10656',
108 authorised_value => 'AAA',
111 lib_opac => 'Z_PUBLIC',
116 category => 'BUG10656',
117 authorised_value => 'DDD',
125 category => 'BUG10656',
126 authorised_value => 'ZZZ',
129 lib_opac => 'A_PUBLIC',
133 'list of authorised values in staff mode sorted by staff label (bug 10656)'
135 $authvals = GetAuthorisedValues('BUG10656', '', 1);
141 category => 'BUG10656',
142 authorised_value => 'ZZZ',
145 lib_opac => 'A_PUBLIC',
150 category => 'BUG10656',
151 authorised_value => 'DDD',
159 category => 'BUG10656',
160 authorised_value => 'AAA',
163 lib_opac => 'Z_PUBLIC',
167 'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
173 subtest 'Itemtype info Tests' => sub {
174 like ( getitemtypeinfo('BK')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on unspecified interface returns intranet imageurl (legacy behavior)' );
175 like ( getitemtypeinfo('BK', 'intranet')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on "intranet" interface returns intranet imageurl' );
176 like ( getitemtypeinfo('BK', 'opac')->{'imageurl'}, qr/opac-tmpl/, 'getitemtypeinfo on "opac" interface returns opac imageurl' );
179 ### test for C4::Koha->GetDailyQuote()
182 eval { require Test::Deep; import Test::Deep; };
183 skip "Test::Deep required to run the GetDailyQuote tests.", 1 if $@;
185 subtest 'Daily Quotes Test' => sub {
190 skip "C4::Koha can't \'GetDailyQuote\'!", 3 unless can_ok('C4::Koha','GetDailyQuote');
192 # Fill the quote table with the default needed and a spare
193 $dbh->do("DELETE FROM quotes WHERE id=3 OR id=25;");
194 my $sql = "INSERT INTO quotes (id,source,text,timestamp) VALUES
195 (25,'Richard Nixon','When the President does it, that means that it is not illegal.','0000-00-00 00:00:00'),
196 (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');";
199 my $expected_quote = {
201 source => 'Abraham Lincoln',
202 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.',
203 timestamp => re('\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}'), #'0000-00-00 00:00:00',
206 # test quote retrieval based on id
208 my $quote = GetDailyQuote('id'=>3);
209 cmp_deeply ($quote, $expected_quote, "Got a quote based on id.") or
210 diag('Be sure to run this test on a clean install of sample data.');
212 # test quote retrieval based on today's date
214 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
215 my $sth = C4::Context->dbh->prepare($query);
216 $sth->execute(DateTime::Format::MySQL->format_datetime( dt_from_string() ), $expected_quote->{'id'});
218 DateTime::Format::MySQL->format_datetime( dt_from_string() ) =~ m/(\d{4}-\d{2}-\d{2})/;
219 $expected_quote->{'timestamp'} = re("^$1");
221 # $expected_quote->{'timestamp'} = DateTime::Format::MySQL->format_datetime( dt_from_string() ); # update the timestamp of expected quote data
223 $quote = GetDailyQuote(); # this is the "default" mode of selection
224 cmp_deeply ($quote, $expected_quote, "Got a quote based on today's date.") or
225 diag('Be sure to run this test on a clean install of sample data.');
227 # test random quote retrieval
229 $quote = GetDailyQuote('random'=>1);
230 ok ($quote, "Got a random quote.");
236 subtest 'ISBN tests' => sub {
239 my $isbn13 = "9780330356473";
240 my $isbn13D = "978-0-330-35647-3";
241 my $isbn10 = "033035647X";
242 my $isbn10D = "0-330-35647-X";
243 is( xml_escape(undef), '',
244 'xml_escape() returns empty string on undef input' );
248 ''"&<>'',
249 'xml_escape() works as expected'
251 is( $str, q{'"&<>'}, '... and does not change input in place' );
252 is( C4::Koha::_isbn_cleanup('0-590-35340-3'),
253 '0590353403', '_isbn_cleanup removes hyphens' );
254 is( C4::Koha::_isbn_cleanup('0590353403 (pbk.)'),
255 '0590353403', '_isbn_cleanup removes parenthetical' );
256 is( C4::Koha::_isbn_cleanup('978-0-321-49694-2'),
257 '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
261 subtest 'GetFrameworksLoop() tests' => sub {
264 $dbh->do("DELETE FROM biblio_framework");
266 my $frameworksloop = GetFrameworksLoop();
267 is ( scalar(@$frameworksloop), 0, 'No frameworks' );
269 $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'A', 'Third framework' )");
270 $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'B', 'Second framework' )");
271 $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'C', 'First framework' )");
273 $frameworksloop = GetFrameworksLoop();
274 is ( scalar(@$frameworksloop), 3, 'All frameworks' );
275 is ( scalar ( grep { defined $_->{'selected'} } @$frameworksloop ), 0, 'None selected' );
277 $frameworksloop = GetFrameworksLoop( 'B' );
278 is ( scalar ( grep { defined $_->{'selected'} } @$frameworksloop ), 1, 'One selected' );
279 my @descriptions = map { $_->{'description'} } @$frameworksloop;
280 is ( $descriptions[0], 'First framework', 'Ordered result' );
286 'description' => 'First framework',
291 'description' => 'Second framework',
292 'selected' => 1, # selected
296 'description' => 'Third framework',
300 'Full check, sorted by description with selected val (Bug 12675)'
304 subtest 'GetItemTypesByCategory GetItemTypesCategorized test' => sub{
307 my $insertGroup = Koha::AuthorisedValue->new(
308 { category => 'ITEMTYPECAT',
309 authorised_value => 'Quertyware',
313 ok($insertGroup, "Create group Qwertyware");
315 my $query = "INSERT into itemtypes (itemtype, description, searchcategory, hideinopac) values (?,?,?,?)";
316 my $insertSth = C4::Context->dbh->prepare($query);
317 $insertSth->execute('BKghjklo1', 'One type of book', '', 0);
318 $insertSth->execute('BKghjklo2', 'Another type of book', 'Qwertyware', 0);
319 $insertSth->execute('BKghjklo3', 'Yet another type of book', 'Qwertyware', 0);
321 # Azertyware should not exist.
322 my @results = GetItemTypesByCategory('Azertyware');
323 is(scalar @results, 0, 'GetItemTypesByCategory: Invalid category returns nothing');
325 @results = GetItemTypesByCategory('Qwertyware');
326 my @expected = ( 'BKghjklo2', 'BKghjklo3' );
327 is_deeply(\@results,\@expected,'GetItemTypesByCategory: valid category returns itemtypes');
329 # add more data since GetItemTypesCategorized's search is more subtle
330 $insertGroup = Koha::AuthorisedValue->new(
331 { category => 'ITEMTYPECAT',
332 authorised_value => 'Varyheavybook',
336 $insertSth->execute('BKghjklo4', 'Another hidden book', 'Veryheavybook', 1);
338 my $hrCat = GetItemTypesCategorized();
339 ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: fully visible category exists');
340 ok($hrCat->{Veryheavybook} &&
341 $hrCat->{Veryheavybook}->{hideinopac}==1, 'GetItemTypesCategorized: non-visible category hidden' );
343 $insertSth->execute('BKghjklo5', 'An hidden book', 'Qwertyware', 1);
344 $hrCat = GetItemTypesCategorized();
345 ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: partially visible category exists');
347 my @only = ( 'BKghjklo1', 'BKghjklo2', 'BKghjklo3', 'BKghjklo4', 'BKghjklo5', 'Qwertyware', 'Veryheavybook' );
349 foreach my $key (@only) {
350 push @results, $key if exists $hrCat->{$key};
352 @expected = ( 'BKghjklo1', 'Qwertyware', 'Veryheavybook' );
353 is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
356 subtest 'GetItemTypes test' => sub {
358 $dbh->do(q|DELETE FROM itemtypes|);
359 $dbh->do(q|INSERT INTO itemtypes(itemtype, description) VALUES ('a', 'aa desc'), ('b', 'zz desc'), ('d', 'dd desc'), ('c', 'yy desc')|);
360 my $itemtypes = C4::Koha::GetItemTypes( style => 'array' );
361 $itemtypes = [ map { $_->{itemtype} } @$itemtypes ];
362 is_deeply( $itemtypes, [ 'a', 'd', 'c', 'b' ], 'GetItemTypes(array) should return itemtypes ordered by description');