From 8b130c2eed52ba108752269470741c1321c67e96 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 --- 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 4725f2362f..5f28c35808 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -545,7 +545,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 bd466b984c..bcee55ca75 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -455,15 +455,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', } ); @@ -519,6 +517,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