Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Koha.t
1 #!/usr/bin/perl
2 #
3 # This is to test C4/Koha
4 # It requires a working Koha database with the sample data
5
6 use Modern::Perl;
7 use Test::More tests => 5;
8
9 use t::lib::TestBuilder;
10
11 use C4::Context;
12 use Koha::Database;
13 use Koha::AuthorisedValue;
14 use Koha::AuthorisedValueCategories;
15
16 BEGIN {
17     use_ok('C4::Koha', qw( GetAuthorisedValues GetItemTypesCategorized xml_escape ));
18     use_ok('C4::Members');
19 }
20
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;
25
26 our $itype_1 = $builder->build({ source => 'Itemtype' });
27
28 subtest 'Authorized Values Tests' => sub {
29     plan tests => 3;
30
31     my $data = {
32         category            => 'CATEGORY',
33         authorised_value    => 'AUTHORISED_VALUE',
34         lib                 => 'LIB',
35         lib_opac            => 'LIBOPAC',
36         imageurl            => 'IMAGEURL'
37     };
38
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},
45             lib              => $data->{lib},
46             lib_opac         => $data->{lib_opac},
47             imageurl         => $data->{imageurl}
48         }
49     )->store;
50     ok( $insert_success, "Insert data in database" );
51
52
53 # Clean up
54     if($insert_success){
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});
58     }
59
60     SKIP: {
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',
67                 lib              => 'Z_STAFF',
68                 lib_opac         => 'A_PUBLIC',
69                 imageurl         => ''
70             }
71         )->store;
72         Koha::AuthorisedValue->new(
73             {   category         => 'BUG10656',
74                 authorised_value => 'AAA',
75                 lib              => 'A_STAFF',
76                 lib_opac         => 'Z_PUBLIC',
77                 imageurl         => ''
78             }
79         )->store;
80
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',
86                 lib              => 'D_STAFF',
87                 lib_opac         => undef,
88                 imageurl         => ''
89             }
90         )->store;
91
92         my $authvals = GetAuthorisedValues('BUG10656');
93         cmp_deeply(
94             $authvals,
95             [
96                 {
97                     id => ignore(),
98                     category => 'BUG10656',
99                     authorised_value => 'AAA',
100                     lib => 'A_STAFF',
101                     lib_opac => 'Z_PUBLIC',
102                     imageurl => '',
103                 },
104                 {
105                     id => ignore(),
106                     category => 'BUG10656',
107                     authorised_value => 'DDD',
108                     lib => 'D_STAFF',
109                     lib_opac => undef,
110                     imageurl => '',
111                 },
112                 {
113                     id => ignore(),
114                     category => 'BUG10656',
115                     authorised_value => 'ZZZ',
116                     lib => 'Z_STAFF',
117                     lib_opac => 'A_PUBLIC',
118                     imageurl => '',
119                 },
120             ],
121             'list of authorised values in staff mode sorted by staff label (bug 10656)'
122         );
123         $authvals = GetAuthorisedValues('BUG10656', 1);
124         cmp_deeply(
125             $authvals,
126             [
127                 {
128                     id => ignore(),
129                     category => 'BUG10656',
130                     authorised_value => 'ZZZ',
131                     lib => 'A_PUBLIC',
132                     lib_opac => 'A_PUBLIC',
133                     imageurl => '',
134                 },
135                 {
136                     id => ignore(),
137                     category => 'BUG10656',
138                     authorised_value => 'DDD',
139                     lib => 'D_STAFF',
140                     lib_opac => undef,
141                     imageurl => '',
142                 },
143                 {
144                     id => ignore(),
145                     category => 'BUG10656',
146                     authorised_value => 'AAA',
147                     lib => 'Z_PUBLIC',
148                     lib_opac => 'Z_PUBLIC',
149                     imageurl => '',
150                 },
151             ],
152             'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
153         );
154     }
155
156 };
157
158 subtest 'ISBN tests' => sub {
159     plan tests => 6;
160
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' );
167     my $str = q{'"&<>'};
168     is(
169         xml_escape($str),
170         '&apos;&quot;&amp;&lt;&gt;&apos;',
171         'xml_escape() works as expected'
172     );
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' );
180
181 };
182
183 subtest 'GetItemTypesCategorized test' => sub{
184     plan tests => 9;
185
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',
193         }
194     )->store;
195
196     ok($insertGroup, "Create group Qwertyware");
197
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);
203
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');
207
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');
212
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',
218         }
219     )->store;
220
221     $insertSth->execute('BKghjklo4', 'Another hidden book', 'Veryheavybook', 1);
222
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' );
227
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');
230
231     $insertSth->execute('BKghjklo5', 'An hidden book', 'Qwertyware', 1);
232     $hrCat = GetItemTypesCategorized();
233     ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: partially visible category exists');
234
235     my @only = ( 'BKghjklo1', 'BKghjklo2', 'BKghjklo3', 'BKghjklo4', 'BKghjklo5', 'Qwertyware', 'Veryheavybook' );
236     my @results = ();
237     foreach my $key (@only) {
238         push @results, $key if exists $hrCat->{$key};
239     }
240     @expected = ( 'BKghjklo1', 'Qwertyware', 'Veryheavybook' );
241     is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
242 };
243
244 $schema->storage->txn_rollback;