Bug 18174: Add update to Koha::Object

Since Koha::Objects has a update method, we should allow it too in
Koha::Object. Note that it is just redirecting to DBIx immediately.

Changed the exception when the method generates an error. The previous
code suggested that the method was not found, but this is not the case.

Test plan:
Run t/db_dependent/Koha/Object.t

Followed test plan, tests passes OK.
Signed-off-by: Marc Véron <veron@veron.ch>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Marcel de Rooy 2017-02-27 10:57:18 +01:00 committed by Kyle M Hall
parent 512f79b87c
commit cd812cb421
2 changed files with 29 additions and 5 deletions

View File

@ -301,13 +301,12 @@ sub AUTOLOAD {
}
}
my @known_methods = qw( is_changed id in_storage get_column discard_changes);
my @known_methods = qw( is_changed id in_storage get_column discard_changes update );
Koha::Exceptions::Object::MethodNotCoveredByTests->throw( "The method $method is not covered by tests!" ) unless grep {/^$method$/} @known_methods;
my $r = eval { $self->_result->$method(@_) };
if ( $@ ) {
Koha::Exceptions::Object::MethodNotFound->throw( "No method $method for " . ref($self) );
Koha::Exceptions::Object->throw( ref($self) . "::$method generated this error: " . $@ );
}
return $r;
}

View File

@ -17,14 +17,16 @@
use Modern::Perl;
use Test::More tests => 8;
use Test::More tests => 9;
use Test::Warn;
use C4::Context;
use Koha::Database;
use Koha::DateUtils qw( dt_from_string );
use Koha::Libraries;
use Scalar::Util qw( isvstring );
use Try::Tiny;
use t::lib::TestBuilder;
@ -168,6 +170,29 @@ subtest 'TO_JSON tests' => sub {
$schema->storage->txn_rollback;
};
subtest "Test update method" => sub {
plan tests => 6;
$schema->storage->txn_begin;
1;
my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
my $library = Koha::Libraries->find( $branchcode );
$library->update({ branchname => 'New_Name', branchcity => 'AMS' });
is( $library->branchname, 'New_Name', 'Changed name with update' );
is( $library->branchcity, 'AMS', 'Changed city too' );
is( $library->is_changed, 0, 'Change should be stored already' );
try {
$library->update({
branchcity => 'NYC', not_a_column => 53, branchname => 'Name3',
});
fail( 'It should not be possible to update an unexisting column without an error from Koha::Object/DBIx' );
} catch {
ok( $_->isa('Koha::Exceptions::Object'), 'Caught error when updating wrong column' );
$library->discard_changes; #requery after failing update
};
# Check if the columns are not updated
is( $library->branchcity, 'AMS', 'First column not updated' );
is( $library->branchname, 'New_Name', 'Third column not updated' );
$schema->storage->txn_rollback;
};