3 # This is to test C4/Koha
4 # It requires a working Koha database with the sample data
7 use DateTime
::Format
::MySQL
;
8 use Test
::More tests
=> 6;
10 use t
::lib
::TestBuilder
;
14 use Koha
::DateUtils
qw(dt_from_string);
15 use Koha
::AuthorisedValue
;
16 use Koha
::AuthorisedValueCategories
;
19 use_ok
('C4::Koha', qw( :DEFAULT GetDailyQuote GetItemTypesCategorized));
20 use_ok
('C4::Members');
23 my $schema = Koha
::Database
->new->schema;
24 $schema->storage->txn_begin;
25 my $builder = t
::lib
::TestBuilder
->new;
26 my $dbh = C4
::Context
->dbh;
28 our $itype_1 = $builder->build({ source
=> 'Itemtype' });
30 subtest
'Authorized Values Tests' => sub {
34 category
=> 'CATEGORY',
35 authorised_value
=> 'AUTHORISED_VALUE',
37 lib_opac
=> 'LIBOPAC',
38 imageurl
=> 'IMAGEURL'
41 my $avc = Koha
::AuthorisedValueCategories
->find($data->{category
});
42 Koha
::AuthorisedValueCategory
->new({ category_name
=> $data->{category
} })->store unless $avc;
43 # Insert an entry into authorised_value table
44 my $insert_success = Koha
::AuthorisedValue
->new(
45 { category
=> $data->{category
},
46 authorised_value
=> $data->{authorised_value
},
48 lib_opac
=> $data->{lib_opac
},
49 imageurl
=> $data->{imageurl
}
52 ok
( $insert_success, "Insert data in database" );
57 my $query = "DELETE FROM authorised_values WHERE category=? AND authorised_value=? AND lib=? AND lib_opac=? AND imageurl=?;";
58 my $sth = $dbh->prepare($query);
59 $sth->execute($data->{category
}, $data->{authorised_value
}, $data->{lib
}, $data->{lib_opac
}, $data->{imageurl
});
63 eval { require Test
::Deep
; import Test
::Deep
; };
64 skip
"Test::Deep required to run the GetAuthorisedValues() tests.", 2 if $@
;
65 Koha
::AuthorisedValueCategory
->new({ category_name
=> 'BUG10656' })->store;
66 Koha
::AuthorisedValue
->new(
67 { category
=> 'BUG10656',
68 authorised_value
=> 'ZZZ',
70 lib_opac
=> 'A_PUBLIC',
74 Koha
::AuthorisedValue
->new(
75 { category
=> 'BUG10656',
76 authorised_value
=> 'AAA',
78 lib_opac
=> 'Z_PUBLIC',
83 # the next one sets lib_opac to NULL; in that case, the staff
84 # display value is meant to be used.
85 Koha
::AuthorisedValue
->new(
86 { category
=> 'BUG10656',
87 authorised_value
=> 'DDD',
94 my $authvals = GetAuthorisedValues
('BUG10656');
100 category
=> 'BUG10656',
101 authorised_value
=> 'AAA',
103 lib_opac
=> 'Z_PUBLIC',
108 category
=> 'BUG10656',
109 authorised_value
=> 'DDD',
116 category
=> 'BUG10656',
117 authorised_value
=> 'ZZZ',
119 lib_opac
=> 'A_PUBLIC',
123 'list of authorised values in staff mode sorted by staff label (bug 10656)'
125 $authvals = GetAuthorisedValues
('BUG10656', 1);
131 category
=> 'BUG10656',
132 authorised_value
=> 'ZZZ',
134 lib_opac
=> 'A_PUBLIC',
139 category
=> 'BUG10656',
140 authorised_value
=> 'DDD',
147 category
=> 'BUG10656',
148 authorised_value
=> 'AAA',
150 lib_opac
=> 'Z_PUBLIC',
154 'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
160 ### test for C4::Koha->GetDailyQuote()
163 eval { require Test
::Deep
; import Test
::Deep
; };
164 skip
"Test::Deep required to run the GetDailyQuote tests.", 1 if $@
;
166 subtest
'Daily Quotes Test' => sub {
171 skip
"C4::Koha can't \'GetDailyQuote\'!", 3 unless can_ok
('C4::Koha','GetDailyQuote');
173 # Fill the quote table with the default needed and a spare
174 $dbh->do("DELETE FROM quotes WHERE id=3 OR id=25;");
175 my $sql = "INSERT INTO quotes (id,source,text,timestamp) VALUES
176 (25,'Richard Nixon','When the President does it, that means that it is not illegal.',NOW()),
177 (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.',NOW());";
180 my $expected_quote = {
182 source
=> 'Abraham Lincoln',
183 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.',
184 timestamp
=> re
('\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}'), #'YYYY-MM-DD HH:MM:SS'
187 # test quote retrieval based on id
189 my $quote = GetDailyQuote
('id'=>3);
190 cmp_deeply
($quote, $expected_quote, "Got a quote based on id.") or
191 diag
('Be sure to run this test on a clean install of sample data.');
193 # test quote retrieval based on today's date
195 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
196 my $sth = C4
::Context
->dbh->prepare($query);
197 $sth->execute(DateTime
::Format
::MySQL
->format_datetime( dt_from_string
() ), $expected_quote->{'id'});
199 DateTime
::Format
::MySQL
->format_datetime( dt_from_string
() ) =~ m/(\d{4}-\d{2}-\d{2})/;
200 $expected_quote->{'timestamp'} = re
("^$1");
202 # $expected_quote->{'timestamp'} = DateTime::Format::MySQL->format_datetime( dt_from_string() ); # update the timestamp of expected quote data
204 $quote = GetDailyQuote
(); # this is the "default" mode of selection
205 cmp_deeply
($quote, $expected_quote, "Got a quote based on today's date.") or
206 diag
('Be sure to run this test on a clean install of sample data.');
208 # test random quote retrieval
210 $quote = GetDailyQuote
('random'=>1);
211 ok
($quote, "Got a random quote.");
217 subtest
'ISBN tests' => sub {
220 my $isbn13 = "9780330356473";
221 my $isbn13D = "978-0-330-35647-3";
222 my $isbn10 = "033035647X";
223 my $isbn10D = "0-330-35647-X";
224 is
( xml_escape
(undef), '',
225 'xml_escape() returns empty string on undef input' );
229 ''"&<>'',
230 'xml_escape() works as expected'
232 is
( $str, q{'"&<>'}, '... and does not change input in place' );
233 is
( C4
::Koha
::_isbn_cleanup
('0-590-35340-3'),
234 '0590353403', '_isbn_cleanup removes hyphens' );
235 is
( C4
::Koha
::_isbn_cleanup
('0590353403 (pbk.)'),
236 '0590353403', '_isbn_cleanup removes parenthetical' );
237 is
( C4
::Koha
::_isbn_cleanup
('978-0-321-49694-2'),
238 '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
242 subtest
'GetItemTypesCategorized test' => sub{
245 my $avc = Koha
::AuthorisedValueCategories
->find('ITEMTYPECAT');
246 Koha
::AuthorisedValueCategory
->new({ category_name
=> 'ITEMTYPECAT' })->store unless $avc;
247 my $insertGroup = Koha
::AuthorisedValue
->new(
248 { category
=> 'ITEMTYPECAT',
249 authorised_value
=> 'Qwertyware',
250 lib
=> 'Keyboard software',
251 lib_opac
=> 'Computer stuff',
255 ok
($insertGroup, "Create group Qwertyware");
257 my $query = "INSERT into itemtypes (itemtype, description, searchcategory, hideinopac) values (?,?,?,?)";
258 my $insertSth = C4
::Context
->dbh->prepare($query);
259 $insertSth->execute('BKghjklo1', 'One type of book', '', 0);
260 $insertSth->execute('BKghjklo2', 'Another type of book', 'Qwertyware', 0);
261 $insertSth->execute('BKghjklo3', 'Yet another type of book', 'Qwertyware', 0);
263 # Azertyware should not exist.
264 my @itemtypes = Koha
::ItemTypes
->search({ searchcategory
=> 'Azertyware' });
265 is
( @itemtypes, 0, 'Search item types by searchcategory: Invalid category returns nothing');
267 @itemtypes = Koha
::ItemTypes
->search({ searchcategory
=> 'Qwertyware' });
268 my @got = map { $_->itemtype } @itemtypes;
269 my @expected = ( 'BKghjklo2', 'BKghjklo3' );
270 is_deeply
(\
@got,\
@expected,'Search item types by searchcategory: valid category returns itemtypes');
272 # add more data since GetItemTypesCategorized's search is more subtle
273 $insertGroup = Koha
::AuthorisedValue
->new(
274 { category
=> 'ITEMTYPECAT',
275 authorised_value
=> 'Veryheavybook',
276 lib
=> 'Weighty literature',
280 $insertSth->execute('BKghjklo4', 'Another hidden book', 'Veryheavybook', 1);
282 my $hrCat = GetItemTypesCategorized
();
283 ok
(exists $hrCat->{Qwertyware
}, 'GetItemTypesCategorized: fully visible category exists');
284 ok
($hrCat->{Veryheavybook
} &&
285 $hrCat->{Veryheavybook
}->{hideinopac
}==1, 'GetItemTypesCategorized: non-visible category hidden' );
287 is
( $hrCat->{Veryheavybook
}->{description
}, 'Weighty literature', 'A category with only lib description passes through');
288 is
( $hrCat->{Qwertyware
}->{description
}, 'Computer stuff', 'A category with lib_opac description uses that');
290 $insertSth->execute('BKghjklo5', 'An hidden book', 'Qwertyware', 1);
291 $hrCat = GetItemTypesCategorized
();
292 ok
(exists $hrCat->{Qwertyware
}, 'GetItemTypesCategorized: partially visible category exists');
294 my @only = ( 'BKghjklo1', 'BKghjklo2', 'BKghjklo3', 'BKghjklo4', 'BKghjklo5', 'Qwertyware', 'Veryheavybook' );
296 foreach my $key (@only) {
297 push @results, $key if exists $hrCat->{$key};
299 @expected = ( 'BKghjklo1', 'Qwertyware', 'Veryheavybook' );
300 is_deeply
(\
@results,\
@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
303 $schema->storage->txn_rollback;