3 # This is to test C4/Koha
4 # It requires a working Koha database with the sample data
7 use Test
::More tests
=> 5;
9 use t
::lib
::TestBuilder
;
13 use Koha
::AuthorisedValue
;
14 use Koha
::AuthorisedValueCategories
;
17 use_ok
('C4::Koha', qw( :DEFAULT GetItemTypesCategorized));
18 use_ok
('C4::Members');
21 my $schema = Koha
::Database
->new->schema;
22 $schema->storage->txn_begin;
23 my $builder = t
::lib
::TestBuilder
->new;
24 my $dbh = C4
::Context
->dbh;
26 our $itype_1 = $builder->build({ source
=> 'Itemtype' });
28 subtest
'Authorized Values Tests' => sub {
32 category
=> 'CATEGORY',
33 authorised_value
=> 'AUTHORISED_VALUE',
35 lib_opac
=> 'LIBOPAC',
36 imageurl
=> 'IMAGEURL'
39 my $avc = Koha
::AuthorisedValueCategories
->find($data->{category
});
40 Koha
::AuthorisedValueCategory
->new({ category_name
=> $data->{category
} })->store unless $avc;
41 # Insert an entry into authorised_value table
42 my $insert_success = Koha
::AuthorisedValue
->new(
43 { category
=> $data->{category
},
44 authorised_value
=> $data->{authorised_value
},
46 lib_opac
=> $data->{lib_opac
},
47 imageurl
=> $data->{imageurl
}
50 ok
( $insert_success, "Insert data in database" );
55 my $query = "DELETE FROM authorised_values WHERE category=? AND authorised_value=? AND lib=? AND lib_opac=? AND imageurl=?;";
56 my $sth = $dbh->prepare($query);
57 $sth->execute($data->{category
}, $data->{authorised_value
}, $data->{lib
}, $data->{lib_opac
}, $data->{imageurl
});
61 eval { require Test
::Deep
; import Test
::Deep
; };
62 skip
"Test::Deep required to run the GetAuthorisedValues() tests.", 2 if $@
;
63 Koha
::AuthorisedValueCategory
->new({ category_name
=> 'BUG10656' })->store;
64 Koha
::AuthorisedValue
->new(
65 { category
=> 'BUG10656',
66 authorised_value
=> 'ZZZ',
68 lib_opac
=> 'A_PUBLIC',
72 Koha
::AuthorisedValue
->new(
73 { category
=> 'BUG10656',
74 authorised_value
=> 'AAA',
76 lib_opac
=> 'Z_PUBLIC',
81 # the next one sets lib_opac to NULL; in that case, the staff
82 # display value is meant to be used.
83 Koha
::AuthorisedValue
->new(
84 { category
=> 'BUG10656',
85 authorised_value
=> 'DDD',
92 my $authvals = GetAuthorisedValues
('BUG10656');
98 category
=> 'BUG10656',
99 authorised_value
=> 'AAA',
101 lib_opac
=> 'Z_PUBLIC',
106 category
=> 'BUG10656',
107 authorised_value
=> 'DDD',
114 category
=> 'BUG10656',
115 authorised_value
=> 'ZZZ',
117 lib_opac
=> 'A_PUBLIC',
121 'list of authorised values in staff mode sorted by staff label (bug 10656)'
123 $authvals = GetAuthorisedValues
('BUG10656', 1);
129 category
=> 'BUG10656',
130 authorised_value
=> 'ZZZ',
132 lib_opac
=> 'A_PUBLIC',
137 category
=> 'BUG10656',
138 authorised_value
=> 'DDD',
145 category
=> 'BUG10656',
146 authorised_value
=> 'AAA',
148 lib_opac
=> 'Z_PUBLIC',
152 'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
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' });
206 is
( @itemtypes, 0, 'Search item types by searchcategory: Invalid category returns nothing');
208 @itemtypes = Koha
::ItemTypes
->search({ searchcategory
=> 'Qwertyware' });
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;