Bug 22294: Check return of as_isbn10 before calling methods on it
[srvgit] / Koha / Objects.pm
index 5080e7d..da43003 100644 (file)
@@ -20,6 +20,7 @@ package Koha::Objects;
 use Modern::Perl;
 
 use Carp;
+use List::MoreUtils qw( none );
 
 use Koha::Database;
 
@@ -70,19 +71,25 @@ sub _new_from_dbic {
 
 =head3 Koha::Objects->find();
 
-my $object = Koha::Objects->find($id);
-my $object = Koha::Objects->find( { keypart1 => $keypart1, keypart2 => $keypart2 } );
+Similar to DBIx::Class::ResultSet->find this method accepts:
+    \%columns_values | @pk_values, { key => $unique_constraint, %attrs }?
+Strictly speaking, columns_values should only refer to columns under an
+unique constraint.
+
+my $object = Koha::Objects->find( { col1 => $val1, col2 => $val2 } );
+my $object = Koha::Objects->find( $id );
+my $object = Koha::Objects->find( $idpart1, $idpart2, $attrs ); # composite PK
 
 =cut
 
 sub find {
-    my ( $self, $id ) = @_;
+    my ( $self, @pars ) = @_;
 
     croak 'Cannot use "->find" in list context' if wantarray;
 
-    return unless defined($id);
+    return if !@pars || none { defined($_) } @pars;
 
-    my $result = $self->_resultset()->find($id);
+    my $result = $self->_resultset()->find( @pars );
 
     return unless $result;
 
@@ -372,11 +379,19 @@ Currently count, pager, update and delete are covered.
 sub AUTOLOAD {
     my ( $self, @params ) = @_;
 
-    my @known_methods = qw( count pager update delete result_class single slice );
+    my @known_methods = qw( count is_paged pager update delete result_class single slice );
     my $method = our $AUTOLOAD;
     $method =~ s/.*:://;
 
-    carp "The method $method is not covered by tests" and return unless grep {/^$method$/} @known_methods;
+
+    unless ( grep { /^$method$/ } @known_methods ) {
+        my $class = ref($self) ? ref($self) : $self;
+        Koha::Exceptions::Object::MethodNotCoveredByTests->throw(
+            error      => sprintf("The method %s->%s is not covered by tests!", $class, $method),
+            show_trace => 1
+        );
+    }
+
     my $r = eval { $self->_resultset->$method(@params) };
     if ( $@ ) {
         carp "No method $method found for " . ref($self) . " " . $@;