From 502c8d046aee82bd82fd894615a6d32ec5e635bc Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 15 Jan 2020 09:45:24 -0300 Subject: [PATCH] Bug 23893: Special care for booleans This patch acknowledges the fact that in D8 the Mojo::JSON->true and Mojo::JSON->false values don't translate into integers when passed to DBIC. It works correctly on D9 onwards, but we haven't formally deprecated Jessie. This is adding back this translation, in the right place now that all mappings code has been integrated into Koha::Object(s) directly. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Signed-off-by: Joy Nelson --- Koha/Object.pm | 7 ++++++- t/db_dependent/Koha/Object.t | 19 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index af4f203e37..7b242120d0 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -487,7 +487,12 @@ sub attributes_from_api { ? $from_api_mapping->{$key} : $key; - if ( _date_or_datetime_column_type( $columns_info->{$koha_field_name}->{data_type} ) ) { + if ( $columns_info->{$koha_field_name}->{is_boolean} ) { + # TODO: Remove when D8 is formally deprecated + # Handle booleans gracefully + $value = ( $value ) ? 1 : 0; + } + elsif ( _date_or_datetime_column_type( $columns_info->{$koha_field_name}->{data_type} ) ) { try { $value = dt_from_string($value, 'rfc3339'); } diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index b3c57bdac9..856eefcae9 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -387,15 +387,13 @@ subtest 'new_from_api() tests' => sub { subtest 'attributes_from_api() tests' => sub { - plan tests => 8; + plan tests => 12; my $patron = Koha::Patron->new(); - use Data::Printer colored => 1; - my $attrs = $patron->attributes_from_api( { - updated_on => '2019-12-27T14:53:00' + updated_on => '2019-12-27T14:53:00', } ); @@ -451,6 +449,19 @@ subtest 'attributes_from_api() tests' => sub { 'date_of_birth', 'Exception parameter is the API field name, not the DB one' ); + + # Booleans + $attrs = $patron->attributes_from_api( + { + incorrect_address => Mojo::JSON->true, + patron_card_lost => Mojo::JSON->false, + } + ); + + ok( exists $attrs->{gonenoaddress}, 'Attribute gets translated' ); + is( $attrs->{gonenoaddress}, 1, 'Boolean correctly translated to integer (true => 1)' ); + ok( exists $attrs->{lost}, 'Attribute gets translated' ); + is( $attrs->{lost}, 0, 'Boolean correctly translated to integer (false => 0)' ); }; subtest "Test update method" => sub { -- 2.39.5