Bug 14862: Upgrade jQuery from 1.7 to 3.4.1 in OPAC
[koha.git] / t / db_dependent / TestBuilder.t
blob30ce6770770cde18d1ea29d111f8004be4e821c1
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright 2014 - Biblibre SARL
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Test::More tests => 13;
23 use Test::Warn;
24 use File::Basename qw(dirname);
26 use Koha::Database;
27 use Koha::Patrons;
29 BEGIN {
30 use_ok('t::lib::TestBuilder');
33 our $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35 our $builder;
37 subtest 'Start with some trivial tests' => sub {
38 plan tests => 7;
40 $builder = t::lib::TestBuilder->new;
41 isnt( $builder, undef, 'We got a builder' );
43 my $data;
44 warning_like { $data = $builder->build; } qr/.+/, 'Catch a warning';
45 is( $data, undef, 'build without arguments returns undef' );
46 is( ref( $builder->schema ), 'Koha::Schema', 'check schema' );
47 is( ref( $builder->can('delete') ), 'CODE', 'found delete method' );
49 # invalid argument
50 warning_like { $builder->build({
51 source => 'Borrower',
52 value => { surname => { invalid_hash => 1 } },
53 }) } qr/^Hash not allowed for surname/,
54 'Build should not accept a hash for this column';
56 # return undef if a record exists
57 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
58 my $param = { source => 'Branch', value => { branchcode => $branchcode } };
59 warning_like { $builder->build( $param ) }
60 qr/Violation of unique constraint/,
61 'Catch warn on adding existing record';
65 subtest 'Build all sources' => sub {
66 plan tests => 1;
68 my @sources = $builder->schema->sources;
69 my @source_in_failure;
70 for my $source ( @sources ) {
71 my $res;
72 # Skip the source if it is a view
73 next if $schema->source($source)->isa('DBIx::Class::ResultSource::View');
74 eval { $res = $builder->build( { source => $source } ); };
75 push @source_in_failure, $source if $@ || !defined( $res );
77 is( @source_in_failure, 0,
78 'TestBuilder should be able to create an object for every source' );
79 if ( @source_in_failure ) {
80 diag( "The following sources have not been generated correctly: " .
81 join ', ', @source_in_failure );
86 subtest 'Test length of some generated fields' => sub {
87 plan tests => 3;
89 # Test the length of a returned character field
90 my $bookseller = $builder->build({ source => 'Aqbookseller' });
91 my $max = $schema->source('Aqbookseller')->column_info('phone')->{size};
92 is( length( $bookseller->{phone} ) > 0, 1,
93 'The length for a generated string (phone) should not be zero' );
94 is( length( $bookseller->{phone} ) <= $max, 1,
95 'Check maximum length for a generated string (phone)' );
97 my $item = $builder->build({ source => 'Item' });
98 is( $item->{replacementprice}, sprintf("%.2f", $item->{replacementprice}), "The number of decimals for floats should not be more than 2" );
102 subtest 'Test FKs in overduerules_transport_type' => sub {
103 plan tests => 5;
105 my $my_overduerules_transport_type = {
106 message_transport_type => {
107 message_transport_type => 'my msg_t_t',
109 overduerules_id => {
110 branchcode => 'codeB',
111 categorycode => 'codeC',
115 my $overduerules_transport_type = $builder->build({
116 source => 'OverduerulesTransportType',
117 value => $my_overduerules_transport_type,
120 $overduerules_transport_type->{message_transport_type},
121 $my_overduerules_transport_type->{message_transport_type}->{message_transport_type},
122 'build stores the message_transport_type correctly'
125 $schema->resultset('Overduerule')->find( $overduerules_transport_type->{overduerules_id} )->branchcode,
126 $my_overduerules_transport_type->{overduerules_id}->{branchcode},
127 'build stores the branchcode correctly'
130 $schema->resultset('Overduerule')->find( $overduerules_transport_type->{overduerules_id} )->categorycode,
131 $my_overduerules_transport_type->{overduerules_id}->{categorycode},
132 'build stores the categorycode correctly'
135 $schema->resultset('MessageTransportType')->find( $overduerules_transport_type->{message_transport_type} )->message_transport_type,
136 $overduerules_transport_type->{message_transport_type},
137 'build stores the foreign key message_transport_type correctly'
139 isnt(
140 $schema->resultset('Overduerule')->find( $my_overduerules_transport_type->{overduerules_id} )->letter2,
141 undef,
142 'build generates values if they are not given'
147 subtest 'Tests with composite FK in userpermission' => sub {
148 plan tests => 9;
150 my $my_user_permission = default_userpermission();
151 my $user_permission = $builder->build({
152 source => 'UserPermission',
153 value => $my_user_permission,
156 # Checks on top level of userpermission
157 isnt(
158 $user_permission->{borrowernumber},
159 undef,
160 'build generates a borrowernumber correctly'
163 $user_permission->{code},
164 $my_user_permission->{code}->{code},
165 'build stores code correctly'
168 # Checks one level deeper userpermission -> borrower
169 my $patron = $schema->resultset('Borrower')->find({ borrowernumber => $user_permission->{borrowernumber} });
171 $patron->surname,
172 $my_user_permission->{borrowernumber}->{surname},
173 'build stores surname correctly'
175 isnt(
176 $patron->cardnumber,
177 undef,
178 'build generated cardnumber'
181 # Checks two levels deeper userpermission -> borrower -> branch
182 my $branch = $schema->resultset('Branch')->find({ branchcode => $patron->branchcode->branchcode });
184 $branch->branchname,
185 $my_user_permission->{borrowernumber}->{branchcode}->{branchname},
186 'build stores branchname correctly'
188 isnt(
189 $branch->branchaddress1,
190 undef,
191 'build generated branch address'
194 # Checks with composite FK: userpermission -> permission
195 my $perm = $schema->resultset('Permission')->find({ module_bit => $user_permission->{module_bit}, code => $my_user_permission->{code}->{code} });
196 isnt( $perm, undef, 'build generated record for composite FK' );
198 $perm->code,
199 $my_user_permission->{code}->{code},
200 'build stored code correctly'
203 $perm->description,
204 $my_user_permission->{code}->{description},
205 'build stored description correctly'
209 sub default_userpermission {
210 return {
211 borrowernumber => {
212 surname => 'my surname',
213 address => 'my adress',
214 city => 'my city',
215 branchcode => {
216 branchname => 'my branchname',
218 categorycode => {
219 hidelostitems => 0,
220 category_type => 'A',
221 default_privacy => 'default',
223 privacy => 1,
225 module_bit => {
226 flag => 'my flag',
228 code => {
229 code => 'my code',
230 description => 'my desc',
236 subtest 'Test build with NULL values' => sub {
237 plan tests => 3;
239 # PK should not be null
240 my $params = { source => 'Branch', value => { branchcode => undef }};
241 warning_like { $builder->build( $params ) }
242 qr/Null value for branchcode/,
243 'Catch warn on adding branch with a null branchcode';
244 # Nullable column
245 my $info = $schema->source( 'Item' )->column_info( 'barcode' );
246 $params = { source => 'Item', value => { barcode => undef }};
247 my $item = $builder->build( $params );
248 is( $info->{is_nullable} && $item && !defined( $item->{barcode} ), 1,
249 'Barcode can be NULL' );
250 # Nullable FK
251 $params = { source => 'Reserve', value => { itemnumber => undef }};
252 my $reserve = $builder->build( $params );
253 $info = $schema->source( 'Reserve' )->column_info( 'itemnumber' );
254 is( $reserve && $info->{is_nullable} && $info->{is_foreign_key} &&
255 !defined( $reserve->{itemnumber} ), 1, 'Nullable FK' );
259 subtest 'Tests for delete method' => sub {
260 plan tests => 12;
262 # Test delete with single and multiple records
263 my $basket1 = $builder->build({ source => 'Aqbasket' });
264 my $basket2 = $builder->build({ source => 'Aqbasket' });
265 my $basket3 = $builder->build({ source => 'Aqbasket' });
266 my ( $id1, $id2 ) = ( $basket1->{basketno}, $basket2->{basketno} );
267 $builder->delete({ source => 'Aqbasket', records => $basket1 });
268 isnt( exists $basket1->{basketno}, 1, 'Delete cleared PK hash value' );
270 is( $builder->schema->resultset('Aqbasket')->search({ basketno => $id1 })->count, 0, 'Basket1 is no longer found' );
271 is( $builder->schema->resultset('Aqbasket')->search({ basketno => $id2 })->count, 1, 'Basket2 is still found' );
272 is( $builder->delete({ source => 'Aqbasket', records => [ $basket2, $basket3 ] }), 2, "Returned two delete attempts" );
273 is( $builder->schema->resultset('Aqbasket')->search({ basketno => $id2 })->count, 0, 'Basket2 is no longer found' );
276 # Test delete in table without primary key (..)
277 is( $schema->source('TmpHoldsqueue')->primary_columns, 0,
278 'Table without primary key detected' );
279 my $bibno = 123; # just a number
280 my $cnt1 = $schema->resultset('TmpHoldsqueue')->count;
281 # Insert a new record in TmpHoldsqueue with that biblionumber
282 my $val = { biblionumber => $bibno };
283 my $rec = $builder->build({ source => 'TmpHoldsqueue', value => $val });
284 my $cnt2 = $schema->resultset('TmpHoldsqueue')->count;
285 is( defined($rec) && $cnt2 == $cnt1 + 1 , 1, 'Created a record' );
286 is( $builder->delete({ source => 'TmpHoldsqueue', records => $rec }),
287 undef, 'delete returns undef' );
288 is( $rec->{biblionumber}, $bibno, 'Hash value untouched' );
289 is( $schema->resultset('TmpHoldsqueue')->count, $cnt2,
290 "Method did not delete record in table without PK" );
292 # Test delete with NULL values
293 $val = { branchcode => undef };
294 is( $builder->delete({ source => 'Branch', records => $val }), 0,
295 'delete returns zero for an undef search with one key' );
296 $val = { module_bit => 1, #catalogue
297 code => undef };
298 is( $builder->delete({ source => 'Permission', records => $val }), 0,
299 'delete returns zero for an undef search with a composite PK' );
303 subtest 'Auto-increment values tests' => sub {
304 plan tests => 3;
306 # Pick a table with AI PK
307 my $source = 'Biblio'; # table
308 my $column = 'biblionumber'; # ai column
310 my $col_info = $schema->source( $source )->column_info( $column );
311 is( $col_info->{is_auto_increment}, 1, "biblio.biblionumber is detected as autoincrement");
313 # Create a biblio
314 my $biblio_1 = $builder->build({ source => $source });
315 # Get the AI value
316 my $ai_value = $biblio_1->{ biblionumber };
317 # Create a biblio
318 my $biblio_2 = $builder->build({ source => $source });
319 # Get the next AI value
320 my $next_ai_value = $biblio_2->{ biblionumber };
321 is( $ai_value + 1, $next_ai_value, "AI values are consecutive");
323 # respect autoincr column
324 warning_like { $builder->build({
325 source => $source,
326 value => { biblionumber => 123 },
327 }) } qr/^Value not allowed for auto_incr/,
328 'Build should not overwrite an auto_incr column';
331 subtest 'Date handling' => sub {
332 plan tests => 2;
334 $builder = t::lib::TestBuilder->new;
336 my $patron = $builder->build( { source => 'Borrower' } );
337 is( length( $patron->{updated_on} ), 19, 'A timestamp column value should be YYYY-MM-DD HH:MM:SS' );
338 is( length( $patron->{dateofbirth} ), 10, 'A date column value should be YYYY-MM-DD' );
341 subtest 'Default values' => sub {
342 plan tests => 2;
343 $builder = t::lib::TestBuilder->new;
344 my $item = $builder->build( { source => 'Item' } );
345 is( $item->{more_subfields_xml}, undef, 'This xml field should be undef' );
346 $item = $builder->build( { source => 'Item', value => { more_subfields_xml => 'some xml' } } );
347 is( $item->{more_subfields_xml}, 'some xml', 'Default should not overwrite assigned value' );
350 subtest 'build_object() tests' => sub {
352 plan tests => 5;
354 $builder = t::lib::TestBuilder->new();
356 my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
357 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
359 my $issuing_rule = $builder->build_object(
360 { class => 'Koha::IssuingRules',
361 value => {
362 categorycode => $categorycode,
363 itemtype => $itemtype
368 is( ref($issuing_rule), 'Koha::IssuingRule', 'Type is correct' );
369 is( $issuing_rule->categorycode,
370 $categorycode, 'Category code correctly set' );
371 is( $issuing_rule->itemtype, $itemtype, 'Item type correctly set' );
373 subtest 'Test all classes' => sub {
374 my $Koha_modules_dir = dirname(__FILE__) . '/../../Koha';
375 my @koha_object_based_modules = `/bin/grep -rl -e '^sub object_class' $Koha_modules_dir`;
376 my @source_in_failure;
377 for my $module_filepath ( @koha_object_based_modules ) {
378 chomp $module_filepath;
379 next unless $module_filepath =~ m|\.pm$|;
380 my $module = $module_filepath;
381 $module =~ s|^.*/(Koha.*)\.pm$|$1|;
382 $module =~ s|/|::|g;
383 next if $module eq 'Koha::Objects';
384 eval "require $module";;
385 my $object = $builder->build_object( { class => $module } );
386 is( ref($object), $module->object_class, "Testing $module" );
387 eval {$object->get_from_storage};
388 is( $@, '', "Module $module should have koha_object[s]_class method if needed" );
392 subtest 'test parameters' => sub {
393 plan tests => 3;
395 warning_is { $issuing_rule = $builder->build_object( {} ); }
396 { carped => 'Missing class param' },
397 'The class parameter is mandatory, raises a warning if absent';
398 is( $issuing_rule, undef,
399 'If the class parameter is missing, undef is returned' );
401 warnings_like {
402 $builder->build_object(
403 { class => 'Koha::Patrons', categorycode => 'foobar' } );
404 } qr{Unknown parameter\(s\): categorycode}, "Unknown parameter detected";
408 subtest '->build parameter' => sub {
409 plan tests => 4;
411 # Test to make sure build() warns user of unknown parameters.
412 warnings_are {
413 $builder->build({
414 source => 'Branch',
415 value => {
416 branchcode => 'BRANCH_1'
419 } [], "No warnings on correct use";
421 warnings_like {
422 $builder->build({
423 source => 'Branch',
424 branchcode => 'BRANCH_2' # This is wrong!
426 } qr/unknown param/i, "Carp unknown parameters";
428 warnings_like {
429 $builder->build({
430 zource => 'Branch', # Intentional spelling error
432 } qr/Source parameter not specified/, "Catch warning on missing source";
434 warnings_like {
435 $builder->build(
436 { source => 'Borrower', categorycode => 'foobar' } );
437 } qr{Unknown parameter\(s\): categorycode}, "Unkown parameter detected";
440 $schema->storage->txn_rollback;