3 # This is to test C4/Koha
4 # It requires a working Koha database with the sample data
7 use Test::More tests => 5;
11 use t::lib::TestBuilder;
15 use Koha::AuthorisedValue;
16 use Koha::AuthorisedValueCategories;
19 use_ok('C4::Koha', qw( GetAuthorisedValues GetItemTypesCategorized xml_escape ));
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});
62 Koha::AuthorisedValueCategory->new({ category_name => 'BUG10656' })->store;
63 Koha::AuthorisedValue->new(
64 { category => 'BUG10656',
65 authorised_value => 'ZZZ',
67 lib_opac => 'A_PUBLIC',
71 Koha::AuthorisedValue->new(
72 { category => 'BUG10656',
73 authorised_value => 'AAA',
75 lib_opac => 'Z_PUBLIC',
80 # the next one sets lib_opac to NULL; in that case, the staff
81 # display value is meant to be used.
82 Koha::AuthorisedValue->new(
83 { category => 'BUG10656',
84 authorised_value => 'DDD',
91 my $authvals = GetAuthorisedValues('BUG10656');
97 category => 'BUG10656',
98 authorised_value => 'AAA',
100 lib_opac => 'Z_PUBLIC',
105 category => 'BUG10656',
106 authorised_value => 'DDD',
113 category => 'BUG10656',
114 authorised_value => 'ZZZ',
116 lib_opac => 'A_PUBLIC',
120 'list of authorised values in staff mode sorted by staff label (bug 10656)'
122 $authvals = GetAuthorisedValues('BUG10656', 1);
128 category => 'BUG10656',
129 authorised_value => 'ZZZ',
131 lib_opac => 'A_PUBLIC',
136 category => 'BUG10656',
137 authorised_value => 'DDD',
144 category => 'BUG10656',
145 authorised_value => 'AAA',
147 lib_opac => 'Z_PUBLIC',
151 'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
154 warning_is { GetAuthorisedValues() } [], 'No warning when no parameter passed to GetAuthorisedValues';
158 subtest 'ISBN tests' => sub {
161 my $isbn13 = "9780330356473";
162 my $isbn13D = "978-0-330-35647-3";
163 my $isbn10 = "033035647X";
164 my $isbn10D = "0-330-35647-X";
165 is( xml_escape(undef), '',
166 'xml_escape() returns empty string on undef input' );
170 ''"&<>'',
171 'xml_escape() works as expected'
173 is( $str, q{'"&<>'}, '... and does not change input in place' );
174 is( C4::Koha::_isbn_cleanup('0-590-35340-3'),
175 '0590353403', '_isbn_cleanup removes hyphens' );
176 is( C4::Koha::_isbn_cleanup('0590353403 (pbk.)'),
177 '0590353403', '_isbn_cleanup removes parenthetical' );
178 is( C4::Koha::_isbn_cleanup('978-0-321-49694-2'),
179 '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
183 subtest 'GetItemTypesCategorized test' => sub{
186 my $avc = Koha::AuthorisedValueCategories->find('ITEMTYPECAT');
187 Koha::AuthorisedValueCategory->new({ category_name => 'ITEMTYPECAT' })->store unless $avc;
188 my $insertGroup = Koha::AuthorisedValue->new(
189 { category => 'ITEMTYPECAT',
190 authorised_value => 'Qwertyware',
191 lib => 'Keyboard software',
192 lib_opac => 'Computer stuff',
196 ok($insertGroup, "Create group Qwertyware");
198 my $query = "INSERT into itemtypes (itemtype, description, searchcategory, hideinopac) values (?,?,?,?)";
199 my $insertSth = C4::Context->dbh->prepare($query);
200 $insertSth->execute('BKghjklo1', 'One type of book', '', 0);
201 $insertSth->execute('BKghjklo2', 'Another type of book', 'Qwertyware', 0);
202 $insertSth->execute('BKghjklo3', 'Yet another type of book', 'Qwertyware', 0);
204 # Azertyware should not exist.
205 my @itemtypes = Koha::ItemTypes->search({ searchcategory => 'Azertyware' })->as_list;
206 is( @itemtypes, 0, 'Search item types by searchcategory: Invalid category returns nothing');
208 @itemtypes = Koha::ItemTypes->search({ searchcategory => 'Qwertyware' })->as_list;
209 my @got = map { $_->itemtype } @itemtypes;
210 my @expected = ( 'BKghjklo2', 'BKghjklo3' );
211 is_deeply(\@got,\@expected,'Search item types by searchcategory: valid category returns itemtypes');
213 # add more data since GetItemTypesCategorized's search is more subtle
214 $insertGroup = Koha::AuthorisedValue->new(
215 { category => 'ITEMTYPECAT',
216 authorised_value => 'Veryheavybook',
217 lib => 'Weighty literature',
221 $insertSth->execute('BKghjklo4', 'Another hidden book', 'Veryheavybook', 1);
223 my $hrCat = GetItemTypesCategorized();
224 ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: fully visible category exists');
225 ok($hrCat->{Veryheavybook} &&
226 $hrCat->{Veryheavybook}->{hideinopac}==1, 'GetItemTypesCategorized: non-visible category hidden' );
228 is( $hrCat->{Veryheavybook}->{description}, 'Weighty literature', 'A category with only lib description passes through');
229 is( $hrCat->{Qwertyware}->{description}, 'Computer stuff', 'A category with lib_opac description uses that');
231 $insertSth->execute('BKghjklo5', 'An hidden book', 'Qwertyware', 1);
232 $hrCat = GetItemTypesCategorized();
233 ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: partially visible category exists');
235 my @only = ( 'BKghjklo1', 'BKghjklo2', 'BKghjklo3', 'BKghjklo4', 'BKghjklo5', 'Qwertyware', 'Veryheavybook' );
237 foreach my $key (@only) {
238 push @results, $key if exists $hrCat->{$key};
240 @expected = ( 'BKghjklo1', 'Qwertyware', 'Veryheavybook' );
241 is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
244 $schema->storage->txn_rollback;