use Modern::Perl;
-use Test::More tests => 10;
+use Test::More tests => 13;
use Test::Warn;
-use Data::Dumper qw(Dumper);
+use File::Basename qw(dirname);
use Koha::Database;
+use Koha::Patrons;
BEGIN {
use_ok('t::lib::TestBuilder');
}
-my $schema = Koha::Database->new->schema;
+our $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
-my $builder;
-
+our $builder;
subtest 'Start with some trivial tests' => sub {
- plan tests => 6;
+ plan tests => 7;
$builder = t::lib::TestBuilder->new;
isnt( $builder, undef, 'We got a builder' );
- is( $builder->build, undef, 'build without arguments returns undef' );
+ my $data;
+ warning_like { $data = $builder->build; } qr/.+/, 'Catch a warning';
+ is( $data, undef, 'build without arguments returns undef' );
is( ref( $builder->schema ), 'Koha::Schema', 'check schema' );
is( ref( $builder->can('delete') ), 'CODE', 'found delete method' );
my @source_in_failure;
for my $source ( @sources ) {
my $res;
+ # Skip the source if it is a view
+ next if $schema->source($source)->isa('DBIx::Class::ResultSource::View');
eval { $res = $builder->build( { source => $source } ); };
push @source_in_failure, $source if $@ || !defined( $res );
}
subtest 'Test length of some generated fields' => sub {
- plan tests => 2;
+ plan tests => 3;
# Test the length of a returned character field
my $bookseller = $builder->build({ source => 'Aqbookseller' });
'The length for a generated string (phone) should not be zero' );
is( length( $bookseller->{phone} ) <= $max, 1,
'Check maximum length for a generated string (phone)' );
+
+ my $item = $builder->build({ source => 'Item' });
+ is( $item->{replacementprice}, sprintf("%.2f", $item->{replacementprice}), "The number of decimals for floats should not be more than 2" );
};
my $patron = $builder->build( { source => 'Borrower' } );
is( length( $patron->{updated_on} ), 19, 'A timestamp column value should be YYYY-MM-DD HH:MM:SS' );
is( length( $patron->{dateofbirth} ), 10, 'A date column value should be YYYY-MM-DD' );
+};
+
+subtest 'Default values' => sub {
+ plan tests => 3;
+ $builder = t::lib::TestBuilder->new;
+ my $item = $builder->build( { source => 'Item' } );
+ is( $item->{more_subfields_xml}, undef, 'This xml field should be undef' );
+ $item = $builder->build( { source => 'Item', value => { more_subfields_xml => 'some xml' } } );
+ is( $item->{more_subfields_xml}, 'some xml', 'Default should not overwrite assigned value' );
+
+ subtest 'generated dynamically (coderef)' => sub {
+ plan tests => 2;
+ my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+ like( $patron->category->category_type, qr{^(A|C|S|I|P|)$}, );
+
+ my $patron_category_X = $builder->build_object({ class => 'Koha::Patron::Categories', value => { category_type => 'X' } });
+ $patron = $builder->build_object({ class => 'Koha::Patrons', value => {categorycode => $patron_category_X->categorycode} });
+ is( $patron->category->category_type, 'X', );
+ };
};
-$schema->storage->txn_rollback;
+subtest 'build_object() tests' => sub {
+
+ plan tests => 5;
+
+ $builder = t::lib::TestBuilder->new();
-1;
+ my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
+ my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
+ my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
+
+ my $issuing_rule = $builder->build_object(
+ { class => 'Koha::CirculationRules',
+ value => {
+ branchcode => $branchcode,
+ categorycode => $categorycode,
+ itemtype => $itemtype
+ }
+ }
+ );
+
+ is( ref($issuing_rule), 'Koha::CirculationRule', 'Type is correct' );
+ is( $issuing_rule->categorycode,
+ $categorycode, 'Category code correctly set' );
+ is( $issuing_rule->itemtype, $itemtype, 'Item type correctly set' );
+
+ subtest 'Test all classes' => sub {
+ my $Koha_modules_dir = dirname(__FILE__) . '/../../Koha';
+ my @koha_object_based_modules = `/bin/grep -rl -e '^sub object_class' $Koha_modules_dir`;
+ my @source_in_failure;
+ for my $module_filepath ( @koha_object_based_modules ) {
+ chomp $module_filepath;
+ next unless $module_filepath =~ m|\.pm$|;
+ my $module = $module_filepath;
+ $module =~ s|^.*/(Koha.*)\.pm$|$1|;
+ $module =~ s|/|::|g;
+ next if $module eq 'Koha::Objects';
+ eval "require $module";
+ my $object = $builder->build_object( { class => $module } );
+ is( ref($object), $module->object_class, "Testing $module" );
+ if ( ! grep {$module eq $_ } qw( Koha::Old::Patrons Koha::Statistics ) ) { # FIXME deletedborrowers and statistics do not have a PK
+ eval {$object->get_from_storage};
+ is( $@, '', "Module $module should have koha_object[s]_class method if needed" );
+ }
+
+ # Testing koha_object_class and koha_objects_class
+ my $object_class = Koha::Object::_get_object_class($object->_result->result_class);
+ eval "require $object_class";
+ is( $@, '', "Module $object_class should be defined");
+ my $objects_class = Koha::Objects::_get_objects_class($object->_result->result_class);
+ eval "require $objects_class";
+ is( $@, '', "Module $objects_class should be defined");
+ }
+ };
+
+ subtest 'test parameters' => sub {
+ plan tests => 3;
+
+ warning_is { $issuing_rule = $builder->build_object( {} ); }
+ { carped => 'Missing class param' },
+ 'The class parameter is mandatory, raises a warning if absent';
+ is( $issuing_rule, undef,
+ 'If the class parameter is missing, undef is returned' );
+
+ warnings_like {
+ $builder->build_object(
+ { class => 'Koha::Patrons', categorycode => 'foobar' } );
+ } qr{Unknown parameter\(s\): categorycode}, "Unknown parameter detected";
+ };
+};
+
+subtest '->build parameter' => sub {
+ plan tests => 4;
+
+ # Test to make sure build() warns user of unknown parameters.
+ warnings_are {
+ $builder->build({
+ source => 'Branch',
+ value => {
+ branchcode => 'BRANCH_1'
+ }
+ })
+ } [], "No warnings on correct use";
+
+ warnings_like {
+ $builder->build({
+ source => 'Branch',
+ branchcode => 'BRANCH_2' # This is wrong!
+ })
+ } qr/unknown param/i, "Carp unknown parameters";
+
+ warnings_like {
+ $builder->build({
+ zource => 'Branch', # Intentional spelling error
+ })
+ } qr/Source parameter not specified/, "Catch warning on missing source";
+
+ warnings_like {
+ $builder->build(
+ { source => 'Borrower', categorycode => 'foobar' } );
+ } qr{Unknown parameter\(s\): categorycode}, "Unkown parameter detected";
+};
+
+$schema->storage->txn_rollback;