3 # This is to test C4/Koha
4 # It requires a working Koha database with the sample data
7 use Test::More tests => 5;
10 use t::lib::TestBuilder;
14 use Koha::AuthorisedValue;
15 use Koha::AuthorisedValueCategories;
18 use_ok('C4::Koha', qw( GetAuthorisedValues GetItemTypesCategorized xml_escape ));
19 use_ok('C4::Members');
22 my $schema = Koha::Database->new->schema;
23 $schema->storage->txn_begin;
24 my $builder = t::lib::TestBuilder->new;
25 my $dbh = C4::Context->dbh;
27 our $itype_1 = $builder->build({ source => 'Itemtype' });
29 subtest 'Authorized Values Tests' => sub {
33 category => 'CATEGORY',
34 authorised_value => 'AUTHORISED_VALUE',
36 lib_opac => 'LIBOPAC',
37 imageurl => 'IMAGEURL'
40 my $avc = Koha::AuthorisedValueCategories->find($data->{category});
41 Koha::AuthorisedValueCategory->new({ category_name => $data->{category} })->store unless $avc;
42 # Insert an entry into authorised_value table
43 my $insert_success = Koha::AuthorisedValue->new(
44 { category => $data->{category},
45 authorised_value => $data->{authorised_value},
47 lib_opac => $data->{lib_opac},
48 imageurl => $data->{imageurl}
51 ok( $insert_success, "Insert data in database" );
56 my $query = "DELETE FROM authorised_values WHERE category=? AND authorised_value=? AND lib=? AND lib_opac=? AND imageurl=?;";
57 my $sth = $dbh->prepare($query);
58 $sth->execute($data->{category}, $data->{authorised_value}, $data->{lib}, $data->{lib_opac}, $data->{imageurl});
62 eval { require Test::Deep; import Test::Deep; };
63 skip "Test::Deep required to run the GetAuthorisedValues() tests.", 2 if $@;
64 Koha::AuthorisedValueCategory->new({ category_name => 'BUG10656' })->store;
65 Koha::AuthorisedValue->new(
66 { category => 'BUG10656',
67 authorised_value => 'ZZZ',
69 lib_opac => 'A_PUBLIC',
73 Koha::AuthorisedValue->new(
74 { category => 'BUG10656',
75 authorised_value => 'AAA',
77 lib_opac => 'Z_PUBLIC',
82 # the next one sets lib_opac to NULL; in that case, the staff
83 # display value is meant to be used.
84 Koha::AuthorisedValue->new(
85 { category => 'BUG10656',
86 authorised_value => 'DDD',
93 my $authvals = GetAuthorisedValues('BUG10656');
99 category => 'BUG10656',
100 authorised_value => 'AAA',
102 lib_opac => 'Z_PUBLIC',
107 category => 'BUG10656',
108 authorised_value => 'DDD',
115 category => 'BUG10656',
116 authorised_value => 'ZZZ',
118 lib_opac => 'A_PUBLIC',
122 'list of authorised values in staff mode sorted by staff label (bug 10656)'
124 $authvals = GetAuthorisedValues('BUG10656', 1);
130 category => 'BUG10656',
131 authorised_value => 'ZZZ',
133 lib_opac => 'A_PUBLIC',
138 category => 'BUG10656',
139 authorised_value => 'DDD',
146 category => 'BUG10656',
147 authorised_value => 'AAA',
149 lib_opac => 'Z_PUBLIC',
153 'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
157 warning_is { GetAuthorisedValues() } [], 'No warning when no parameter passed to GetAuthorisedValues';
161 subtest 'ISBN tests' => sub {
164 my $isbn13 = "9780330356473";
165 my $isbn13D = "978-0-330-35647-3";
166 my $isbn10 = "033035647X";
167 my $isbn10D = "0-330-35647-X";
168 is( xml_escape(undef), '',
169 'xml_escape() returns empty string on undef input' );
173 ''"&<>'',
174 'xml_escape() works as expected'
176 is( $str, q{'"&<>'}, '... and does not change input in place' );
177 is( C4::Koha::_isbn_cleanup('0-590-35340-3'),
178 '0590353403', '_isbn_cleanup removes hyphens' );
179 is( C4::Koha::_isbn_cleanup('0590353403 (pbk.)'),
180 '0590353403', '_isbn_cleanup removes parenthetical' );
181 is( C4::Koha::_isbn_cleanup('978-0-321-49694-2'),
182 '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
186 subtest 'GetItemTypesCategorized test' => sub{
189 my $avc = Koha::AuthorisedValueCategories->find('ITEMTYPECAT');
190 Koha::AuthorisedValueCategory->new({ category_name => 'ITEMTYPECAT' })->store unless $avc;
191 my $insertGroup = Koha::AuthorisedValue->new(
192 { category => 'ITEMTYPECAT',
193 authorised_value => 'Qwertyware',
194 lib => 'Keyboard software',
195 lib_opac => 'Computer stuff',
199 ok($insertGroup, "Create group Qwertyware");
201 my $query = "INSERT into itemtypes (itemtype, description, searchcategory, hideinopac) values (?,?,?,?)";
202 my $insertSth = C4::Context->dbh->prepare($query);
203 $insertSth->execute('BKghjklo1', 'One type of book', '', 0);
204 $insertSth->execute('BKghjklo2', 'Another type of book', 'Qwertyware', 0);
205 $insertSth->execute('BKghjklo3', 'Yet another type of book', 'Qwertyware', 0);
207 # Azertyware should not exist.
208 my @itemtypes = Koha::ItemTypes->search({ searchcategory => 'Azertyware' })->as_list;
209 is( @itemtypes, 0, 'Search item types by searchcategory: Invalid category returns nothing');
211 @itemtypes = Koha::ItemTypes->search({ searchcategory => 'Qwertyware' })->as_list;
212 my @got = map { $_->itemtype } @itemtypes;
213 my @expected = ( 'BKghjklo2', 'BKghjklo3' );
214 is_deeply(\@got,\@expected,'Search item types by searchcategory: valid category returns itemtypes');
216 # add more data since GetItemTypesCategorized's search is more subtle
217 $insertGroup = Koha::AuthorisedValue->new(
218 { category => 'ITEMTYPECAT',
219 authorised_value => 'Veryheavybook',
220 lib => 'Weighty literature',
224 $insertSth->execute('BKghjklo4', 'Another hidden book', 'Veryheavybook', 1);
226 my $hrCat = GetItemTypesCategorized();
227 ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: fully visible category exists');
228 ok($hrCat->{Veryheavybook} &&
229 $hrCat->{Veryheavybook}->{hideinopac}==1, 'GetItemTypesCategorized: non-visible category hidden' );
231 is( $hrCat->{Veryheavybook}->{description}, 'Weighty literature', 'A category with only lib description passes through');
232 is( $hrCat->{Qwertyware}->{description}, 'Computer stuff', 'A category with lib_opac description uses that');
234 $insertSth->execute('BKghjklo5', 'An hidden book', 'Qwertyware', 1);
235 $hrCat = GetItemTypesCategorized();
236 ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: partially visible category exists');
238 my @only = ( 'BKghjklo1', 'BKghjklo2', 'BKghjklo3', 'BKghjklo4', 'BKghjklo5', 'Qwertyware', 'Veryheavybook' );
240 foreach my $key (@only) {
241 push @results, $key if exists $hrCat->{$key};
243 @expected = ( 'BKghjklo1', 'Qwertyware', 'Veryheavybook' );
244 is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
247 $schema->storage->txn_rollback;