From 06f853f74a242745ee8472690a2a8a293a31ee47 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Thu, 7 Oct 2021 15:16:01 -0300
Subject: [PATCH] Bug 29157: Regression tests

This patch implements regression tests for the filed bug.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Eric Phetteplace <phette23@gmail.com>
---
 t/db_dependent/Koha/Object.t    | 152 ++++++++++++++++++--------------
 t/db_dependent/api/v1/patrons.t |   6 ++
 2 files changed, 94 insertions(+), 64 deletions(-)

diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t
index c3de0be3de..8004c26446 100755
--- a/t/db_dependent/Koha/Object.t
+++ b/t/db_dependent/Koha/Object.t
@@ -520,81 +520,105 @@ subtest 'new_from_api() tests' => sub {
 
 subtest 'attributes_from_api() tests' => sub {
 
-    plan tests => 12;
+    plan tests => 2;
 
-    my $patron = Koha::Patron->new();
+    subtest 'date and date-time handling tests' => sub {
 
-    my $attrs = $patron->attributes_from_api(
-        {
-            updated_on => '2019-12-27T14:53:00',
-        }
-    );
+        plan tests => 12;
 
-    ok( exists $attrs->{updated_on},
-        'No translation takes place if no mapping' );
-    is(
-        ref( $attrs->{updated_on} ),
-        'DateTime',
-        'Given a string, a timestamp field is converted into a DateTime object'
-    );
+        my $patron = Koha::Patron->new();
 
-    $attrs = $patron->attributes_from_api(
-        {
-            last_seen  => '2019-12-27T14:53:00'
-        }
-    );
+        my $attrs = $patron->attributes_from_api(
+            {
+                updated_on     => '2019-12-27T14:53:00',
+                last_seen      => '2019-12-27T14:53:00',
+                date_of_birth  => '2019-12-27',
+            }
+        );
 
-    ok( exists $attrs->{lastseen},
-        'Translation takes place because of the defined mapping' );
-    is(
-        ref( $attrs->{lastseen} ),
-        'DateTime',
-        'Given a string, a datetime field is converted into a DateTime object'
-    );
+        ok( exists $attrs->{updated_on},
+            'No translation takes place if no mapping' );
+        is(
+            ref( $attrs->{updated_on} ),
+            'DateTime',
+            'Given a string, a timestamp field is converted into a DateTime object'
+        );
 
-    $attrs = $patron->attributes_from_api(
-        {
-            date_of_birth  => '2019-12-27'
-        }
-    );
+        ok( exists $attrs->{lastseen},
+            'Translation takes place because of the defined mapping' );
+        is(
+            ref( $attrs->{lastseen} ),
+            'DateTime',
+            'Given a string, a datetime field is converted into a DateTime object'
+        );
 
-    ok( exists $attrs->{dateofbirth},
-        'Translation takes place because of the defined mapping' );
-    is(
-        ref( $attrs->{dateofbirth} ),
-        'DateTime',
-        'Given a string, a date field is converted into a DateTime object'
-    );
+        ok( exists $attrs->{dateofbirth},
+            'Translation takes place because of the defined mapping' );
+        is(
+            ref( $attrs->{dateofbirth} ),
+            'DateTime',
+            'Given a string, a date field is converted into a DateTime object'
+        );
 
-    throws_ok
-        {
-            $attrs = $patron->attributes_from_api(
-                {
-                    date_of_birth => '20141205',
-                }
-            );
-        }
-        'Koha::Exceptions::BadParameter',
-        'Bad date throws an exception';
+        $attrs = $patron->attributes_from_api(
+            {
+                last_seen      => undef,
+                date_of_birth  => undef,
+            }
+        );
 
-    is(
-        $@->parameter,
-        'date_of_birth',
-        'Exception parameter is the API field name, not the DB one'
-    );
+        ok( exists $attrs->{lastseen},
+            'undef parameter is not skipped (Bug 29157)' );
+        is(
+            $attrs->{lastseen},
+            undef,
+            'Given undef, a datetime field is set to undef (Bug 29157)'
+        );
 
-    # Booleans
-    $attrs = $patron->attributes_from_api(
-        {
-            incorrect_address => Mojo::JSON->true,
-            patron_card_lost  => Mojo::JSON->false,
-        }
-    );
+        ok( exists $attrs->{dateofbirth},
+            'undef parameter is not skipped (Bug 29157)' );
+        is(
+            $attrs->{dateofbirth},
+            undef,
+            'Given undef, a date field is set to undef (Bug 29157)'
+        );
+
+        throws_ok
+            {
+                $attrs = $patron->attributes_from_api(
+                    {
+                        date_of_birth => '20141205',
+                    }
+                );
+            }
+            'Koha::Exceptions::BadParameter',
+            'Bad date throws an exception';
+
+        is(
+            $@->parameter,
+            'date_of_birth',
+            'Exception parameter is the API field name, not the DB one'
+        );
+    };
+
+    subtest 'booleans handling tests' => sub {
 
-    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)' );
+        plan tests => 4;
+
+        my $patron = Koha::Patron->new;
+
+        my $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 {
diff --git a/t/db_dependent/api/v1/patrons.t b/t/db_dependent/api/v1/patrons.t
index dbe6c1bbc5..6f38a92b59 100755
--- a/t/db_dependent/api/v1/patrons.t
+++ b/t/db_dependent/api/v1/patrons.t
@@ -475,6 +475,12 @@ subtest 'update() tests' => sub {
         $newpatron->{ userid }     = "user" . $patron_1->id.$patron_2->id;
         $newpatron->{ surname }    = "user" . $patron_1->id.$patron_2->id;
 
+        ## Trying to set to null on specially handled cases
+        # Special case: a date
+        $newpatron->{ date_of_birth } = undef;
+        # Special case: a date-time
+        $newpatron->{ last_seen } = undef;
+
         my $result = $t->put_ok( "//$userid:$password@/api/v1/patrons/" . $patron_2->borrowernumber => json => $newpatron )
           ->status_is(200, 'Patron updated successfully');
 
-- 
2.33.0