View | Details | Raw Unified | Return to bug 29157
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Object.t (-64 / +88 lines)
Lines 520-600 subtest 'new_from_api() tests' => sub { Link Here
520
520
521
subtest 'attributes_from_api() tests' => sub {
521
subtest 'attributes_from_api() tests' => sub {
522
522
523
    plan tests => 12;
523
    plan tests => 2;
524
524
525
    my $patron = Koha::Patron->new();
525
    subtest 'date and date-time handling tests' => sub {
526
526
527
    my $attrs = $patron->attributes_from_api(
527
        plan tests => 12;
528
        {
529
            updated_on => '2019-12-27T14:53:00',
530
        }
531
    );
532
528
533
    ok( exists $attrs->{updated_on},
529
        my $patron = Koha::Patron->new();
534
        'No translation takes place if no mapping' );
535
    is(
536
        ref( $attrs->{updated_on} ),
537
        'DateTime',
538
        'Given a string, a timestamp field is converted into a DateTime object'
539
    );
540
530
541
    $attrs = $patron->attributes_from_api(
531
        my $attrs = $patron->attributes_from_api(
542
        {
532
            {
543
            last_seen  => '2019-12-27T14:53:00'
533
                updated_on     => '2019-12-27T14:53:00',
544
        }
534
                last_seen      => '2019-12-27T14:53:00',
545
    );
535
                date_of_birth  => '2019-12-27',
536
            }
537
        );
546
538
547
    ok( exists $attrs->{lastseen},
539
        ok( exists $attrs->{updated_on},
548
        'Translation takes place because of the defined mapping' );
540
            'No translation takes place if no mapping' );
549
    is(
541
        is(
550
        ref( $attrs->{lastseen} ),
542
            ref( $attrs->{updated_on} ),
551
        'DateTime',
543
            'DateTime',
552
        'Given a string, a datetime field is converted into a DateTime object'
544
            'Given a string, a timestamp field is converted into a DateTime object'
553
    );
545
        );
554
546
555
    $attrs = $patron->attributes_from_api(
547
        ok( exists $attrs->{lastseen},
556
        {
548
            'Translation takes place because of the defined mapping' );
557
            date_of_birth  => '2019-12-27'
549
        is(
558
        }
550
            ref( $attrs->{lastseen} ),
559
    );
551
            'DateTime',
552
            'Given a string, a datetime field is converted into a DateTime object'
553
        );
560
554
561
    ok( exists $attrs->{dateofbirth},
555
        ok( exists $attrs->{dateofbirth},
562
        'Translation takes place because of the defined mapping' );
556
            'Translation takes place because of the defined mapping' );
563
    is(
557
        is(
564
        ref( $attrs->{dateofbirth} ),
558
            ref( $attrs->{dateofbirth} ),
565
        'DateTime',
559
            'DateTime',
566
        'Given a string, a date field is converted into a DateTime object'
560
            'Given a string, a date field is converted into a DateTime object'
567
    );
561
        );
568
562
569
    throws_ok
563
        $attrs = $patron->attributes_from_api(
570
        {
564
            {
571
            $attrs = $patron->attributes_from_api(
565
                last_seen      => undef,
572
                {
566
                date_of_birth  => undef,
573
                    date_of_birth => '20141205',
567
            }
574
                }
568
        );
575
            );
576
        }
577
        'Koha::Exceptions::BadParameter',
578
        'Bad date throws an exception';
579
569
580
    is(
570
        ok( exists $attrs->{lastseen},
581
        $@->parameter,
571
            'undef parameter is not skipped (Bug 29157)' );
582
        'date_of_birth',
572
        is(
583
        'Exception parameter is the API field name, not the DB one'
573
            $attrs->{lastseen},
584
    );
574
            undef,
575
            'Given undef, a datetime field is set to undef (Bug 29157)'
576
        );
585
577
586
    # Booleans
578
        ok( exists $attrs->{dateofbirth},
587
    $attrs = $patron->attributes_from_api(
579
            'undef parameter is not skipped (Bug 29157)' );
588
        {
580
        is(
589
            incorrect_address => Mojo::JSON->true,
581
            $attrs->{dateofbirth},
590
            patron_card_lost  => Mojo::JSON->false,
582
            undef,
591
        }
583
            'Given undef, a date field is set to undef (Bug 29157)'
592
    );
584
        );
585
586
        throws_ok
587
            {
588
                $attrs = $patron->attributes_from_api(
589
                    {
590
                        date_of_birth => '20141205',
591
                    }
592
                );
593
            }
594
            'Koha::Exceptions::BadParameter',
595
            'Bad date throws an exception';
596
597
        is(
598
            $@->parameter,
599
            'date_of_birth',
600
            'Exception parameter is the API field name, not the DB one'
601
        );
602
    };
603
604
    subtest 'booleans handling tests' => sub {
593
605
594
    ok( exists $attrs->{gonenoaddress}, 'Attribute gets translated' );
606
        plan tests => 4;
595
    is( $attrs->{gonenoaddress}, 1, 'Boolean correctly translated to integer (true => 1)' );
607
596
    ok( exists $attrs->{lost}, 'Attribute gets translated' );
608
        my $patron = Koha::Patron->new;
597
    is( $attrs->{lost}, 0, 'Boolean correctly translated to integer (false => 0)' );
609
610
        my $attrs = $patron->attributes_from_api(
611
            {
612
                incorrect_address => Mojo::JSON->true,
613
                patron_card_lost  => Mojo::JSON->false,
614
            }
615
        );
616
617
        ok( exists $attrs->{gonenoaddress}, 'Attribute gets translated' );
618
        is( $attrs->{gonenoaddress}, 1, 'Boolean correctly translated to integer (true => 1)' );
619
        ok( exists $attrs->{lost}, 'Attribute gets translated' );
620
        is( $attrs->{lost}, 0, 'Boolean correctly translated to integer (false => 0)' );
621
    };
598
};
622
};
599
623
600
subtest "Test update method" => sub {
624
subtest "Test update method" => sub {
(-)a/t/db_dependent/api/v1/patrons.t (-1 / +6 lines)
Lines 475-480 subtest 'update() tests' => sub { Link Here
475
        $newpatron->{ userid }     = "user" . $patron_1->id.$patron_2->id;
475
        $newpatron->{ userid }     = "user" . $patron_1->id.$patron_2->id;
476
        $newpatron->{ surname }    = "user" . $patron_1->id.$patron_2->id;
476
        $newpatron->{ surname }    = "user" . $patron_1->id.$patron_2->id;
477
477
478
        ## Trying to set to null on specially handled cases
479
        # Special case: a date
480
        $newpatron->{ date_of_birth } = undef;
481
        # Special case: a date-time
482
        $newpatron->{ last_seen } = undef;
483
478
        my $result = $t->put_ok( "//$userid:$password@/api/v1/patrons/" . $patron_2->borrowernumber => json => $newpatron )
484
        my $result = $t->put_ok( "//$userid:$password@/api/v1/patrons/" . $patron_2->borrowernumber => json => $newpatron )
479
          ->status_is(200, 'Patron updated successfully');
485
          ->status_is(200, 'Patron updated successfully');
480
486
481
- 

Return to bug 29157