Bug 18330: Handle date-time in Koha::Object->TO_JSON
authorLari Taskula <lari.taskula@jns.fi>
Fri, 24 Mar 2017 13:29:30 +0000 (15:29 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 14 Dec 2017 19:58:22 +0000 (16:58 -0300)
This patch formats timestamps and datetimes to meet date-time definition
in RFC3339, as required by Swagger documentation.

Signed-off-by: Benjamin Rokseth <benjamin.rokseth@kul.oslo.kommune.no>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

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

Koha/Object.pm
t/db_dependent/Koha/Object.t

index d305542..fc30adc 100644 (file)
@@ -25,6 +25,7 @@ use Mojo::JSON;
 
 use Koha::Database;
 use Koha::Exceptions::Object;
+use Koha::DateUtils;
 
 =head1 NAME
 
@@ -221,10 +222,30 @@ sub TO_JSON {
             # is ported to whatever distro we support by that time
             $unblessed->{$col} += 0;
         }
+        elsif ( _datetime_column_type( $columns_info->{$col}->{data_type} ) ) {
+            eval {
+                return undef unless $unblessed->{$col};
+                $unblessed->{$col} = output_pref({
+                    dateformat => 'rfc3339',
+                    dt         => dt_from_string($unblessed->{$col}, 'sql'),
+                });
+            };
+        }
     }
     return $unblessed;
 }
 
+sub _datetime_column_type {
+    my ($column_type) = @_;
+
+    my @dt_types = (
+        'timestamp',
+        'datetime'
+    );
+
+    return ( grep { $column_type eq $_ } @dt_types) ? 1 : 0;
+}
+
 sub _numeric_column_type {
     # TODO: Remove once the solution for
     # https://rt.cpan.org/Ticket/Display.html?id=119904
index a506b89..5f91415 100755 (executable)
@@ -146,18 +146,23 @@ subtest 'discard_changes' => sub {
 
 subtest 'TO_JSON tests' => sub {
 
-    plan tests => 5;
+    plan tests => 7;
 
     $schema->storage->txn_begin;
 
+    my $dt = dt_from_string();
     my $borrowernumber = $builder->build(
         { source => 'Borrower',
           value => { lost => 1,
-                     gonenoaddress => 0 } })->{borrowernumber};
+                     gonenoaddress => 0,
+                     updated_on => $dt,
+                     lastseen   => $dt, } })->{borrowernumber};
 
     my $patron = Koha::Patrons->find($borrowernumber);
     my $lost = $patron->TO_JSON()->{lost};
     my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
+    my $updated_on = $patron->TO_JSON->{updated_on};
+    my $lastseen = $patron->TO_JSON->{lastseen};
 
     ok( $lost->isa('JSON::PP::Boolean'), 'Boolean attribute type is correct' );
     is( $lost, 1, 'Boolean attribute value is correct (true)' );
@@ -167,6 +172,23 @@ subtest 'TO_JSON tests' => sub {
 
     ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
 
+    my $rfc3999_regex = qr/
+            (?<year>\d{4})
+            -
+            (?<month>\d{2})
+            -
+            (?<day>\d{2})
+            ([Tt\s])
+            (?<hour>\d{2})
+            :
+            (?<minute>\d{2})
+            :
+            (?<second>\d{2})
+            (([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))
+        /xms;
+    like( $updated_on, $rfc3999_regex, "Date-time $updated_on formatted correctly");
+    like( $lastseen, $rfc3999_regex, "Date-time $updated_on formatted correctly");
+
     $schema->storage->txn_rollback;
 };