Bug 31183: Unit tests
[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 use Test::Warn;
9 use Test::Deep;
10
11 use t::lib::TestBuilder;
12
13 use C4::Context;
14 use Koha::Database;
15 use Koha::AuthorisedValue;
16 use Koha::AuthorisedValueCategories;
17
18 BEGIN {
19     use_ok('C4::Koha', qw( GetAuthorisedValues GetItemTypesCategorized xml_escape ));
20     use_ok('C4::Members');
21 }
22
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;
27
28 our $itype_1 = $builder->build({ source => 'Itemtype' });
29
30 subtest 'Authorized Values Tests' => sub {
31     plan tests => 4;
32
33     my $data = {
34         category            => 'CATEGORY',
35         authorised_value    => 'AUTHORISED_VALUE',
36         lib                 => 'LIB',
37         lib_opac            => 'LIBOPAC',
38         imageurl            => 'IMAGEURL'
39     };
40
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}
50         }
51     )->store;
52     ok( $insert_success, "Insert data in database" );
53
54
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});
60     }
61
62     Koha::AuthorisedValueCategory->new({ category_name => 'BUG10656' })->store;
63     Koha::AuthorisedValue->new(
64         {   category         => 'BUG10656',
65             authorised_value => 'ZZZ',
66             lib              => 'Z_STAFF',
67             lib_opac         => 'A_PUBLIC',
68             imageurl         => ''
69         }
70     )->store;
71     Koha::AuthorisedValue->new(
72         {   category         => 'BUG10656',
73             authorised_value => 'AAA',
74             lib              => 'A_STAFF',
75             lib_opac         => 'Z_PUBLIC',
76             imageurl         => ''
77         }
78     )->store;
79
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',
85             lib              => 'D_STAFF',
86             lib_opac         => undef,
87             imageurl         => ''
88         }
89     )->store;
90
91     my $authvals = GetAuthorisedValues('BUG10656');
92     cmp_deeply(
93         $authvals,
94         [
95             {
96                 id => ignore(),
97                 category => 'BUG10656',
98                 authorised_value => 'AAA',
99                 lib => 'A_STAFF',
100                 lib_opac => 'Z_PUBLIC',
101                 imageurl => '',
102             },
103             {
104                 id => ignore(),
105                 category => 'BUG10656',
106                 authorised_value => 'DDD',
107                 lib => 'D_STAFF',
108                 lib_opac => undef,
109                 imageurl => '',
110             },
111             {
112                 id => ignore(),
113                 category => 'BUG10656',
114                 authorised_value => 'ZZZ',
115                 lib => 'Z_STAFF',
116                 lib_opac => 'A_PUBLIC',
117                 imageurl => '',
118             },
119         ],
120         'list of authorised values in staff mode sorted by staff label (bug 10656)'
121     );
122     $authvals = GetAuthorisedValues('BUG10656', 1);
123     cmp_deeply(
124         $authvals,
125         [
126             {
127                 id => ignore(),
128                 category => 'BUG10656',
129                 authorised_value => 'ZZZ',
130                 lib => 'A_PUBLIC',
131                 lib_opac => 'A_PUBLIC',
132                 imageurl => '',
133             },
134             {
135                 id => ignore(),
136                 category => 'BUG10656',
137                 authorised_value => 'DDD',
138                 lib => 'D_STAFF',
139                 lib_opac => undef,
140                 imageurl => '',
141             },
142             {
143                 id => ignore(),
144                 category => 'BUG10656',
145                 authorised_value => 'AAA',
146                 lib => 'Z_PUBLIC',
147                 lib_opac => 'Z_PUBLIC',
148                 imageurl => '',
149             },
150         ],
151         'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
152     );
153
154     warning_is { GetAuthorisedValues() } [], 'No warning when no parameter passed to GetAuthorisedValues';
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;