]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Koha.t
Bug 29883: Add test
[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
10 use t::lib::TestBuilder;
11
12 use C4::Context;
13 use Koha::Database;
14 use Koha::AuthorisedValue;
15 use Koha::AuthorisedValueCategories;
16
17 BEGIN {
18     use_ok('C4::Koha', qw( GetAuthorisedValues GetItemTypesCategorized xml_escape ));
19     use_ok('C4::Members');
20 }
21
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;
26
27 our $itype_1 = $builder->build({ source => 'Itemtype' });
28
29 subtest 'Authorized Values Tests' => sub {
30     plan tests => 4;
31
32     my $data = {
33         category            => 'CATEGORY',
34         authorised_value    => 'AUTHORISED_VALUE',
35         lib                 => 'LIB',
36         lib_opac            => 'LIBOPAC',
37         imageurl            => 'IMAGEURL'
38     };
39
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},
46             lib              => $data->{lib},
47             lib_opac         => $data->{lib_opac},
48             imageurl         => $data->{imageurl}
49         }
50     )->store;
51     ok( $insert_success, "Insert data in database" );
52
53
54 # Clean up
55     if($insert_success){
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});
59     }
60
61     SKIP: {
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',
68                 lib              => 'Z_STAFF',
69                 lib_opac         => 'A_PUBLIC',
70                 imageurl         => ''
71             }
72         )->store;
73         Koha::AuthorisedValue->new(
74             {   category         => 'BUG10656',
75                 authorised_value => 'AAA',
76                 lib              => 'A_STAFF',
77                 lib_opac         => 'Z_PUBLIC',
78                 imageurl         => ''
79             }
80         )->store;
81
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',
87                 lib              => 'D_STAFF',
88                 lib_opac         => undef,
89                 imageurl         => ''
90             }
91         )->store;
92
93         my $authvals = GetAuthorisedValues('BUG10656');
94         cmp_deeply(
95             $authvals,
96             [
97                 {
98                     id => ignore(),
99                     category => 'BUG10656',
100                     authorised_value => 'AAA',
101                     lib => 'A_STAFF',
102                     lib_opac => 'Z_PUBLIC',
103                     imageurl => '',
104                 },
105                 {
106                     id => ignore(),
107                     category => 'BUG10656',
108                     authorised_value => 'DDD',
109                     lib => 'D_STAFF',
110                     lib_opac => undef,
111                     imageurl => '',
112                 },
113                 {
114                     id => ignore(),
115                     category => 'BUG10656',
116                     authorised_value => 'ZZZ',
117                     lib => 'Z_STAFF',
118                     lib_opac => 'A_PUBLIC',
119                     imageurl => '',
120                 },
121             ],
122             'list of authorised values in staff mode sorted by staff label (bug 10656)'
123         );
124         $authvals = GetAuthorisedValues('BUG10656', 1);
125         cmp_deeply(
126             $authvals,
127             [
128                 {
129                     id => ignore(),
130                     category => 'BUG10656',
131                     authorised_value => 'ZZZ',
132                     lib => 'A_PUBLIC',
133                     lib_opac => 'A_PUBLIC',
134                     imageurl => '',
135                 },
136                 {
137                     id => ignore(),
138                     category => 'BUG10656',
139                     authorised_value => 'DDD',
140                     lib => 'D_STAFF',
141                     lib_opac => undef,
142                     imageurl => '',
143                 },
144                 {
145                     id => ignore(),
146                     category => 'BUG10656',
147                     authorised_value => 'AAA',
148                     lib => 'Z_PUBLIC',
149                     lib_opac => 'Z_PUBLIC',
150                     imageurl => '',
151                 },
152             ],
153             'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
154         );
155     }
156
157     warning_is { GetAuthorisedValues() } [], 'No warning when no parameter passed to GetAuthorisedValues';
158
159 };
160
161 subtest 'ISBN tests' => sub {
162     plan tests => 6;
163
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' );
170     my $str = q{'"&<>'};
171     is(
172         xml_escape($str),
173         '&apos;&quot;&amp;&lt;&gt;&apos;',
174         'xml_escape() works as expected'
175     );
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' );
183
184 };
185
186 subtest 'GetItemTypesCategorized test' => sub{
187     plan tests => 9;
188
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',
196         }
197     )->store;
198
199     ok($insertGroup, "Create group Qwertyware");
200
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);
206
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');
210
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');
215
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',
221         }
222     )->store;
223
224     $insertSth->execute('BKghjklo4', 'Another hidden book', 'Veryheavybook', 1);
225
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' );
230
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');
233
234     $insertSth->execute('BKghjklo5', 'An hidden book', 'Qwertyware', 1);
235     $hrCat = GetItemTypesCategorized();
236     ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: partially visible category exists');
237
238     my @only = ( 'BKghjklo1', 'BKghjklo2', 'BKghjklo3', 'BKghjklo4', 'BKghjklo5', 'Qwertyware', 'Veryheavybook' );
239     my @results = ();
240     foreach my $key (@only) {
241         push @results, $key if exists $hrCat->{$key};
242     }
243     @expected = ( 'BKghjklo1', 'Qwertyware', 'Veryheavybook' );
244     is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
245 };
246
247 $schema->storage->txn_rollback;