@@ -, +, @@ Koha::Object->new --- Koha/Object.pm | 13 +++++++++++-- t/db_dependent/Koha/Objects.t | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) --- a/Koha/Object.pm +++ a/Koha/Object.pm @@ -57,8 +57,17 @@ sub new { my $self = {}; if ($attributes) { - $self->{_result} = - Koha::Database->new()->schema()->resultset( $class->_type() ) + my $schema = Koha::Database->new->schema; + + # Remove the arguments which exist, are not defined but NOT NULL to use the default value + my $columns_info = $schema->resultset( $class->_type )->result_source->columns_info; + for my $column_name ( keys %$attributes ) { + my $c_info = $columns_info->{$column_name}; + next if $c_info->{is_nullable}; + next if not exists $attributes->{$column_name} or defined $attributes->{$column_name}; + delete $attributes->{$column_name}; + } + $self->{_result} = $schema->resultset( $class->_type() ) ->new($attributes); } --- a/t/db_dependent/Koha/Objects.t +++ a/t/db_dependent/Koha/Objects.t @@ -19,11 +19,13 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 8; use Test::Warn; use Koha::Authority::Types; use Koha::Cities; +use Koha::Patron::Category; +use Koha::Patron::Categories; use Koha::Patrons; use Koha::Database; @@ -80,6 +82,17 @@ subtest 'not_covered_yet' => sub { plan tests => 1; warning_is { Koha::Patrons->search->not_covered_yet } { carped => 'The method not_covered_yet is not covered by tests' }, "If a method is not covered by tests, the AUTOLOAD method won't execute the method"; }; +subtest 'new' => sub { + plan tests => 2; + my $a_cat_code = 'A_CAT_CODE'; + my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store; + is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' ); + Koha::Patron::Categories->find($a_cat_code)->delete; + $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store; + is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value even if the argument exists but is not defined' ); + Koha::Patron::Categories->find($a_cat_code)->delete; +}; $schema->storage->txn_rollback; 1; + --