Bug 22930: Make TestBuilder->build_object explodes if parameters are wrong
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 16 May 2019 17:56:07 +0000 (12:56 -0500)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 30 May 2019 19:30:55 +0000 (20:30 +0100)
This patch implements bug 15339 for build_object.
We want it to warn if the call is wrong.

Test plan:
Make sure the tests are still returning green

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
(cherry picked from commit 52325a771a02b603e807c29d586fe44fabebb792)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

t/db_dependent/TestBuilder.t
t/lib/TestBuilder.pm

index e69c194..bf23e0b 100644 (file)
@@ -351,7 +351,7 @@ subtest 'Default values' => sub {
 
 subtest 'build_object() tests' => sub {
 
-    plan tests => 6;
+    plan tests => 5;
 
     $builder = t::lib::TestBuilder->new();
 
@@ -372,12 +372,6 @@ subtest 'build_object() tests' => sub {
         $categorycode, 'Category code correctly set' );
     is( $issuing_rule->itemtype, $itemtype, 'Item type correctly set' );
 
-    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' );
-
     subtest 'Test all classes' => sub {
         my $Koha_modules_dir = dirname(__FILE__) . '/../../Koha';
         my @koha_object_based_modules = `/bin/grep -rl 'sub object_class' $Koha_modules_dir`;
@@ -394,10 +388,25 @@ subtest 'build_object() tests' => sub {
             is( ref($object), $module->object_class, "Testing $module" );
         }
     };
+
+    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}, "";
+    };
 };
 
 subtest '->build parameter' => sub {
-    plan tests => 3;
+    plan tests => 4;
 
     # Test to make sure build() warns user of unknown parameters.
     warnings_are {
@@ -421,6 +430,11 @@ subtest '->build parameter' => sub {
             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}, "";
 };
 
 $schema->storage->txn_rollback;
index b0eb922..493b264 100644 (file)
@@ -76,6 +76,9 @@ sub build_object {
         return;
     }
 
+    my @unknowns = grep( !/^(class|value)$/, keys %{ $params });
+    carp "Unknown parameter(s): ", join( ', ', @unknowns ) if scalar @unknowns;
+
     load $class;
     my $source = $class->_type;
     my @pks = $self->schema->source( $class->_type )->primary_columns;