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>.
22 use Test::More tests => 11;
24 use Data::Dumper qw(Dumper);
29 use_ok('t::lib::TestBuilder');
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
37 subtest 'Start with some trivial tests' => sub {
40 $builder = t::lib::TestBuilder->new;
41 isnt( $builder, undef, 'We got a builder' );
43 is( $builder->build, undef, 'build without arguments returns undef' );
44 is( ref( $builder->schema ), 'Koha::Schema', 'check schema' );
45 is( ref( $builder->can('delete') ), 'CODE', 'found delete method' );
48 warning_like { $builder->build({
50 value => { surname => { invalid_hash => 1 } },
51 }) } qr/^Hash not allowed for surname/,
52 'Build should not accept a hash for this column';
54 # return undef if a record exists
55 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
56 my $param = { source => 'Branch', value => { branchcode => $branchcode } };
57 warning_like { $builder->build( $param ) }
58 qr/Violation of unique constraint/,
59 'Catch warn on adding existing record';
63 subtest 'Build all sources' => sub {
66 my @sources = $builder->schema->sources;
67 my @source_in_failure;
68 for my $source ( @sources ) {
70 # Skip the source if it is a view
71 next if $schema->source($source)->isa('DBIx::Class::ResultSource::View');
72 eval { $res = $builder->build( { source => $source } ); };
73 push @source_in_failure, $source if $@ || !defined( $res );
75 is( @source_in_failure, 0,
76 'TestBuilder should be able to create an object for every source' );
77 if ( @source_in_failure ) {
78 diag( "The following sources have not been generated correctly: " .
79 join ', ', @source_in_failure );
84 subtest 'Test length of some generated fields' => sub {
87 # Test the length of a returned character field
88 my $bookseller = $builder->build({ source => 'Aqbookseller' });
89 my $max = $schema->source('Aqbookseller')->column_info('phone')->{size};
90 is( length( $bookseller->{phone} ) > 0, 1,
91 'The length for a generated string (phone) should not be zero' );
92 is( length( $bookseller->{phone} ) <= $max, 1,
93 'Check maximum length for a generated string (phone)' );
97 subtest 'Test FKs in overduerules_transport_type' => sub {
100 my $my_overduerules_transport_type = {
101 message_transport_type => {
102 message_transport_type => 'my msg_t_t',
105 branchcode => 'codeB',
106 categorycode => 'codeC',
110 my $overduerules_transport_type = $builder->build({
111 source => 'OverduerulesTransportType',
112 value => $my_overduerules_transport_type,
115 $overduerules_transport_type->{message_transport_type},
116 $my_overduerules_transport_type->{message_transport_type}->{message_transport_type},
117 'build stores the message_transport_type correctly'
120 $schema->resultset('Overduerule')->find( $overduerules_transport_type->{overduerules_id} )->branchcode,
121 $my_overduerules_transport_type->{overduerules_id}->{branchcode},
122 'build stores the branchcode correctly'
125 $schema->resultset('Overduerule')->find( $overduerules_transport_type->{overduerules_id} )->categorycode,
126 $my_overduerules_transport_type->{overduerules_id}->{categorycode},
127 'build stores the categorycode correctly'
130 $schema->resultset('MessageTransportType')->find( $overduerules_transport_type->{message_transport_type} )->message_transport_type,
131 $overduerules_transport_type->{message_transport_type},
132 'build stores the foreign key message_transport_type correctly'
135 $schema->resultset('Overduerule')->find( $my_overduerules_transport_type->{overduerules_id} )->letter2,
137 'build generates values if they are not given'
142 subtest 'Tests with composite FK in userpermission' => sub {
145 my $my_user_permission = default_userpermission();
146 my $user_permission = $builder->build({
147 source => 'UserPermission',
148 value => $my_user_permission,
151 # Checks on top level of userpermission
153 $user_permission->{borrowernumber},
155 'build generates a borrowernumber correctly'
158 $user_permission->{code},
159 $my_user_permission->{code}->{code},
160 'build stores code correctly'
163 # Checks one level deeper userpermission -> borrower
164 my $patron = $schema->resultset('Borrower')->find({ borrowernumber => $user_permission->{borrowernumber} });
167 $my_user_permission->{borrowernumber}->{surname},
168 'build stores surname correctly'
173 'build generated cardnumber'
176 # Checks two levels deeper userpermission -> borrower -> branch
177 my $branch = $schema->resultset('Branch')->find({ branchcode => $patron->branchcode->branchcode });
180 $my_user_permission->{borrowernumber}->{branchcode}->{branchname},
181 'build stores branchname correctly'
184 $branch->branchaddress1,
186 'build generated branch address'
189 # Checks with composite FK: userpermission -> permission
190 my $perm = $schema->resultset('Permission')->find({ module_bit => $user_permission->{module_bit}, code => $my_user_permission->{code}->{code} });
191 isnt( $perm, undef, 'build generated record for composite FK' );
194 $my_user_permission->{code}->{code},
195 'build stored code correctly'
199 $my_user_permission->{code}->{description},
200 'build stored description correctly'
204 sub default_userpermission {
207 surname => 'my surname',
208 address => 'my adress',
211 branchname => 'my branchname',
215 category_type => 'A',
216 default_privacy => 'default',
225 description => 'my desc',
231 subtest 'Test build with NULL values' => sub {
234 # PK should not be null
235 my $params = { source => 'Branch', value => { branchcode => undef }};
236 warning_like { $builder->build( $params ) }
237 qr/Null value for branchcode/,
238 'Catch warn on adding branch with a null branchcode';
240 my $info = $schema->source( 'Item' )->column_info( 'barcode' );
241 $params = { source => 'Item', value => { barcode => undef }};
242 my $item = $builder->build( $params );
243 is( $info->{is_nullable} && $item && !defined( $item->{barcode} ), 1,
244 'Barcode can be NULL' );
246 $params = { source => 'Reserve', value => { itemnumber => undef }};
247 my $reserve = $builder->build( $params );
248 $info = $schema->source( 'Reserve' )->column_info( 'itemnumber' );
249 is( $reserve && $info->{is_nullable} && $info->{is_foreign_key} &&
250 !defined( $reserve->{itemnumber} ), 1, 'Nullable FK' );
254 subtest 'Tests for delete method' => sub {
257 # Test delete with single and multiple records
258 my $basket1 = $builder->build({ source => 'Aqbasket' });
259 my $basket2 = $builder->build({ source => 'Aqbasket' });
260 my $basket3 = $builder->build({ source => 'Aqbasket' });
261 my ( $id1, $id2 ) = ( $basket1->{basketno}, $basket2->{basketno} );
262 $builder->delete({ source => 'Aqbasket', records => $basket1 });
263 isnt( exists $basket1->{basketno}, 1, 'Delete cleared PK hash value' );
265 is( $builder->schema->resultset('Aqbasket')->search({ basketno => $id1 })->count, 0, 'Basket1 is no longer found' );
266 is( $builder->schema->resultset('Aqbasket')->search({ basketno => $id2 })->count, 1, 'Basket2 is still found' );
267 is( $builder->delete({ source => 'Aqbasket', records => [ $basket2, $basket3 ] }), 2, "Returned two delete attempts" );
268 is( $builder->schema->resultset('Aqbasket')->search({ basketno => $id2 })->count, 0, 'Basket2 is no longer found' );
271 # Test delete in table without primary key (..)
272 is( $schema->source('TmpHoldsqueue')->primary_columns, 0,
273 'Table without primary key detected' );
274 my $bibno = 123; # just a number
275 my $cnt1 = $schema->resultset('TmpHoldsqueue')->count;
276 # Insert a new record in TmpHoldsqueue with that biblionumber
277 my $val = { biblionumber => $bibno };
278 my $rec = $builder->build({ source => 'TmpHoldsqueue', value => $val });
279 my $cnt2 = $schema->resultset('TmpHoldsqueue')->count;
280 is( defined($rec) && $cnt2 == $cnt1 + 1 , 1, 'Created a record' );
281 is( $builder->delete({ source => 'TmpHoldsqueue', records => $rec }),
282 undef, 'delete returns undef' );
283 is( $rec->{biblionumber}, $bibno, 'Hash value untouched' );
284 is( $schema->resultset('TmpHoldsqueue')->count, $cnt2,
285 "Method did not delete record in table without PK" );
287 # Test delete with NULL values
288 $val = { branchcode => undef };
289 is( $builder->delete({ source => 'Branch', records => $val }), 0,
290 'delete returns zero for an undef search with one key' );
291 $val = { module_bit => 1, #catalogue
293 is( $builder->delete({ source => 'Permission', records => $val }), 0,
294 'delete returns zero for an undef search with a composite PK' );
298 subtest 'Auto-increment values tests' => sub {
301 # Pick a table with AI PK
302 my $source = 'Biblio'; # table
303 my $column = 'biblionumber'; # ai column
305 my $col_info = $schema->source( $source )->column_info( $column );
306 is( $col_info->{is_auto_increment}, 1, "biblio.biblionumber is detected as autoincrement");
309 my $biblio_1 = $builder->build({ source => $source });
311 my $ai_value = $biblio_1->{ biblionumber };
313 my $biblio_2 = $builder->build({ source => $source });
314 # Get the next AI value
315 my $next_ai_value = $biblio_2->{ biblionumber };
316 is( $ai_value + 1, $next_ai_value, "AI values are consecutive");
318 # respect autoincr column
319 warning_like { $builder->build({
321 value => { biblionumber => 123 },
322 }) } qr/^Value not allowed for auto_incr/,
323 'Build should not overwrite an auto_incr column';
326 subtest 'Date handling' => sub {
329 $builder = t::lib::TestBuilder->new;
331 my $patron = $builder->build( { source => 'Borrower' } );
332 is( length( $patron->{updated_on} ), 19, 'A timestamp column value should be YYYY-MM-DD HH:MM:SS' );
333 is( length( $patron->{dateofbirth} ), 10, 'A date column value should be YYYY-MM-DD' );
337 subtest 'Default values' => sub {
339 $builder = t::lib::TestBuilder->new;
340 my $item = $builder->build( { source => 'Item' } );
341 is( $item->{more_subfields_xml}, undef, 'This xml field should be undef' );
342 $item = $builder->build( { source => 'Item', value => { more_subfields_xml => 'some xml' } } );
343 is( $item->{more_subfields_xml}, 'some xml', 'Default should not overwrite assigned value' );
346 $schema->storage->txn_rollback;