Bug 18754: [QA Follow-up] Tiny corrections
[koha.git] / t / db_dependent / Koha.t
blob258289d3b861e30fce30d67483e4260b1cf6666d
1 #!/usr/bin/perl
3 # This is to test C4/Koha
4 # It requires a working Koha database with the sample data
6 use Modern::Perl;
7 use DateTime::Format::MySQL;
8 use Test::More tests => 7;
10 use t::lib::TestBuilder;
12 use C4::Context;
13 use Koha::Database;
14 use Koha::DateUtils qw(dt_from_string);
15 use Koha::AuthorisedValue;
16 use Koha::AuthorisedValueCategories;
18 BEGIN {
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 {
31 plan tests => 3;
33 my $data = {
34 category => 'CATEGORY',
35 authorised_value => 'AUTHORISED_VALUE',
36 lib => 'LIB',
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},
47 lib => $data->{lib},
48 lib_opac => $data->{lib_opac},
49 imageurl => $data->{imageurl}
51 )->store;
52 ok( $insert_success, "Insert data in database" );
55 # Clean up
56 if($insert_success){
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});
62 SKIP: {
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',
69 lib => 'Z_STAFF',
70 lib_opac => 'A_PUBLIC',
71 imageurl => ''
73 )->store;
74 Koha::AuthorisedValue->new(
75 { category => 'BUG10656',
76 authorised_value => 'AAA',
77 lib => 'A_STAFF',
78 lib_opac => 'Z_PUBLIC',
79 imageurl => ''
81 )->store;
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',
88 lib => 'D_STAFF',
89 lib_opac => undef,
90 imageurl => ''
92 )->store;
94 my $authvals = GetAuthorisedValues('BUG10656');
95 cmp_deeply(
96 $authvals,
99 id => ignore(),
100 category => 'BUG10656',
101 authorised_value => 'AAA',
102 lib => 'A_STAFF',
103 lib_opac => 'Z_PUBLIC',
104 imageurl => '',
107 id => ignore(),
108 category => 'BUG10656',
109 authorised_value => 'DDD',
110 lib => 'D_STAFF',
111 lib_opac => undef,
112 imageurl => '',
115 id => ignore(),
116 category => 'BUG10656',
117 authorised_value => 'ZZZ',
118 lib => 'Z_STAFF',
119 lib_opac => 'A_PUBLIC',
120 imageurl => '',
123 'list of authorised values in staff mode sorted by staff label (bug 10656)'
125 $authvals = GetAuthorisedValues('BUG10656', 1);
126 cmp_deeply(
127 $authvals,
130 id => ignore(),
131 category => 'BUG10656',
132 authorised_value => 'ZZZ',
133 lib => 'A_PUBLIC',
134 lib_opac => 'A_PUBLIC',
135 imageurl => '',
138 id => ignore(),
139 category => 'BUG10656',
140 authorised_value => 'DDD',
141 lib => 'D_STAFF',
142 lib_opac => undef,
143 imageurl => '',
146 id => ignore(),
147 category => 'BUG10656',
148 authorised_value => 'AAA',
149 lib => 'Z_PUBLIC',
150 lib_opac => 'Z_PUBLIC',
151 imageurl => '',
154 'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
160 subtest 'Itemtype info Tests' => sub {
161 like ( getitemtypeinfo( $itype_1->{itemtype} )->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on unspecified interface returns intranet imageurl (legacy behavior)' );
162 like ( getitemtypeinfo( $itype_1->{itemtype}, 'intranet')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on "intranet" interface returns intranet imageurl' );
163 like ( getitemtypeinfo( $itype_1->{itemtype}, 'opac')->{'imageurl'}, qr/opac-tmpl/, 'getitemtypeinfo on "opac" interface returns opac imageurl' );
166 ### test for C4::Koha->GetDailyQuote()
167 SKIP:
169 eval { require Test::Deep; import Test::Deep; };
170 skip "Test::Deep required to run the GetDailyQuote tests.", 1 if $@;
172 subtest 'Daily Quotes Test' => sub {
173 plan tests => 4;
175 SKIP: {
177 skip "C4::Koha can't \'GetDailyQuote\'!", 3 unless can_ok('C4::Koha','GetDailyQuote');
179 # Fill the quote table with the default needed and a spare
180 $dbh->do("DELETE FROM quotes WHERE id=3 OR id=25;");
181 my $sql = "INSERT INTO quotes (id,source,text,timestamp) VALUES
182 (25,'Richard Nixon','When the President does it, that means that it is not illegal.','0000-00-00 00:00:00'),
183 (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');";
184 $dbh->do($sql);
186 my $expected_quote = {
187 id => 3,
188 source => 'Abraham Lincoln',
189 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.',
190 timestamp => re('\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}'), #'0000-00-00 00:00:00',
193 # test quote retrieval based on id
195 my $quote = GetDailyQuote('id'=>3);
196 cmp_deeply ($quote, $expected_quote, "Got a quote based on id.") or
197 diag('Be sure to run this test on a clean install of sample data.');
199 # test quote retrieval based on today's date
201 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
202 my $sth = C4::Context->dbh->prepare($query);
203 $sth->execute(DateTime::Format::MySQL->format_datetime( dt_from_string() ), $expected_quote->{'id'});
205 DateTime::Format::MySQL->format_datetime( dt_from_string() ) =~ m/(\d{4}-\d{2}-\d{2})/;
206 $expected_quote->{'timestamp'} = re("^$1");
208 # $expected_quote->{'timestamp'} = DateTime::Format::MySQL->format_datetime( dt_from_string() ); # update the timestamp of expected quote data
210 $quote = GetDailyQuote(); # this is the "default" mode of selection
211 cmp_deeply ($quote, $expected_quote, "Got a quote based on today's date.") or
212 diag('Be sure to run this test on a clean install of sample data.');
214 # test random quote retrieval
216 $quote = GetDailyQuote('random'=>1);
217 ok ($quote, "Got a random quote.");
223 subtest 'ISBN tests' => sub {
224 plan tests => 6;
226 my $isbn13 = "9780330356473";
227 my $isbn13D = "978-0-330-35647-3";
228 my $isbn10 = "033035647X";
229 my $isbn10D = "0-330-35647-X";
230 is( xml_escape(undef), '',
231 'xml_escape() returns empty string on undef input' );
232 my $str = q{'"&<>'};
234 xml_escape($str),
235 '&apos;&quot;&amp;&lt;&gt;&apos;',
236 'xml_escape() works as expected'
238 is( $str, q{'"&<>'}, '... and does not change input in place' );
239 is( C4::Koha::_isbn_cleanup('0-590-35340-3'),
240 '0590353403', '_isbn_cleanup removes hyphens' );
241 is( C4::Koha::_isbn_cleanup('0590353403 (pbk.)'),
242 '0590353403', '_isbn_cleanup removes parenthetical' );
243 is( C4::Koha::_isbn_cleanup('978-0-321-49694-2'),
244 '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
248 subtest 'GetItemTypesCategorized test' => sub{
249 plan tests => 7;
251 my $avc = Koha::AuthorisedValueCategories->find('ITEMTYPECAT');
252 Koha::AuthorisedValueCategory->new({ category_name => 'ITEMTYPECAT' })->store unless $avc;
253 my $insertGroup = Koha::AuthorisedValue->new(
254 { category => 'ITEMTYPECAT',
255 authorised_value => 'Quertyware',
257 )->store;
259 ok($insertGroup, "Create group Qwertyware");
261 my $query = "INSERT into itemtypes (itemtype, description, searchcategory, hideinopac) values (?,?,?,?)";
262 my $insertSth = C4::Context->dbh->prepare($query);
263 $insertSth->execute('BKghjklo1', 'One type of book', '', 0);
264 $insertSth->execute('BKghjklo2', 'Another type of book', 'Qwertyware', 0);
265 $insertSth->execute('BKghjklo3', 'Yet another type of book', 'Qwertyware', 0);
267 # Azertyware should not exist.
268 my @itemtypes = Koha::ItemTypes->search({ searchcategory => 'Azertyware' });
269 is( @itemtypes, 0, 'Search item types by searchcategory: Invalid category returns nothing');
271 @itemtypes = Koha::ItemTypes->search({ searchcategory => 'Qwertyware' });
272 my @got = map { $_->itemtype } @itemtypes;
273 my @expected = ( 'BKghjklo2', 'BKghjklo3' );
274 is_deeply(\@got,\@expected,'Search item types by searchcategory: valid category returns itemtypes');
276 # add more data since GetItemTypesCategorized's search is more subtle
277 $insertGroup = Koha::AuthorisedValue->new(
278 { category => 'ITEMTYPECAT',
279 authorised_value => 'Varyheavybook',
281 )->store;
283 $insertSth->execute('BKghjklo4', 'Another hidden book', 'Veryheavybook', 1);
285 my $hrCat = GetItemTypesCategorized();
286 ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: fully visible category exists');
287 ok($hrCat->{Veryheavybook} &&
288 $hrCat->{Veryheavybook}->{hideinopac}==1, 'GetItemTypesCategorized: non-visible category hidden' );
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' );
295 my @results = ();
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;