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

(-)a/t/db_dependent/Koha/Patrons.t (-1 / +33 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::NoWarnings;
22
use Test::NoWarnings;
23
use Test::More tests => 47;
23
use Test::More tests => 48;
24
use Test::Warn;
24
use Test::Warn;
25
use Test::Exception;
25
use Test::Exception;
26
use Test::MockModule;
26
use Test::MockModule;
Lines 3623-3626 subtest 'find_by_identifier() tests' => sub { Link Here
3623
    $schema->storage->txn_rollback;
3623
    $schema->storage->txn_rollback;
3624
};
3624
};
3625
3625
3626
subtest 'check_for_existing_matches' => sub {
3627
    plan tests => 5;
3628
3629
    $schema->storage->txn_begin;
3630
3631
    my $patron = $builder->build_object(
3632
        { class => 'Koha::Patrons', value => { firstname => 'John', surname => 'Smith', dateofbirth => '1980-01-01' } }
3633
    );
3634
3635
    t::lib::Mocks::mock_preference( 'PatronDuplicateMatchingAddFields', 'dateofbirth|firstname|surname' );
3636
3637
    my $match_result = Koha::Patrons->check_for_existing_matches(
3638
        { firstname => 'John', surname => 'Smith', dateofbirth => '1980-01-01' } );
3639
    is( $match_result->{duplicate_found},                        1,                       'Duplicate found' );
3640
    is( $match_result->{matching_patrons}->next->borrowernumber, $patron->borrowernumber, 'Matching patron' );
3641
3642
    t::lib::Mocks::mock_preference( 'PatronDuplicateMatchingAddFields', 'dateofbirth|firstname|surname|city' );
3643
    $match_result = Koha::Patrons->check_for_existing_matches(
3644
        { firstname => 'John', surname => 'Smith', dateofbirth => '1980-01-01', city => '' } );
3645
    is( $match_result->{duplicate_found}, 1, 'Duplicate found despite missing data for the city field' );
3646
    is(
3647
        $match_result->{matching_patrons}->next->borrowernumber, $patron->borrowernumber,
3648
        'Patron match ignores undefined field and still identifies the match'
3649
    );
3650
3651
    $match_result = Koha::Patrons->check_for_existing_matches(
3652
        { firstname => 'John', surname => 'Smith', dateofbirth => '1980-01-01', city => 'Sandwich, Kent' } );
3653
    is( $match_result->{duplicate_found}, 0, 'No duplicate found' );
3654
3655
    $schema->storage->txn_rollback;
3656
};
3657
3626
$schema->storage->txn_rollback;
3658
$schema->storage->txn_rollback;
(-)a/t/db_dependent/api/v1/patrons.t (-17 / +21 lines)
Lines 428-434 subtest 'add() tests' => sub { Link Here
428
    $schema->storage->txn_rollback;
428
    $schema->storage->txn_rollback;
429
429
430
    subtest 'librarian access tests' => sub {
430
    subtest 'librarian access tests' => sub {
431
        plan tests => 37;
431
        plan tests => 40;
432
432
433
        $schema->storage->txn_begin;
433
        $schema->storage->txn_begin;
434
434
Lines 459-464 subtest 'add() tests' => sub { Link Here
459
        delete $newpatron->{expired};
459
        delete $newpatron->{expired};
460
        delete $newpatron->{anonymized};
460
        delete $newpatron->{anonymized};
461
461
462
        my $password = 'thePassword123';
463
        $librarian->set_password( { password => $password, skip_validation => 1 } );
464
        my $userid = $librarian->userid;
465
        t::lib::Mocks::mock_preference( 'PatronDuplicateMatchingAddFields', 'firstname|surname|dateofbirth' );
466
467
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => json => $newpatron )->status_is(409)
468
            ->json_is( '/error' => "A patron record matching these details already exists" );
469
462
        # Create a library just to make sure its ID doesn't exist on the DB
470
        # Create a library just to make sure its ID doesn't exist on the DB
463
        my $library_to_delete  = $builder->build_object( { class => 'Koha::Libraries' } );
471
        my $library_to_delete  = $builder->build_object( { class => 'Koha::Libraries' } );
464
        my $deleted_library_id = $library_to_delete->id;
472
        my $deleted_library_id = $library_to_delete->id;
Lines 466-488 subtest 'add() tests' => sub { Link Here
466
        # Delete library
474
        # Delete library
467
        $library_to_delete->delete;
475
        $library_to_delete->delete;
468
476
469
        my $password = 'thePassword123';
470
        $librarian->set_password( { password => $password, skip_validation => 1 } );
471
        my $userid = $librarian->userid;
472
473
        $newpatron->{library_id} = $deleted_library_id;
477
        $newpatron->{library_id} = $deleted_library_id;
474
478
475
        # Test duplicate userid constraint
479
        # Test duplicate userid constraint
476
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => json => $newpatron )->status_is(400)
480
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => { 'x-confirm-not-duplicate' => 1 } => json => $newpatron )
477
            ->json_is( '/error' => "Problem with " . $newpatron->{userid} );
481
            ->status_is(400)->json_is( '/error' => "Problem with " . $newpatron->{userid} );
478
482
479
        $newpatron->{library_id} = $patron->branchcode;
483
        $newpatron->{library_id} = $patron->branchcode;
480
484
481
        # Test duplicate cardnumber constraint
485
        # Test duplicate cardnumber constraint
482
        $newpatron->{userid} = undef;    # force regeneration
486
        $newpatron->{userid} = undef;    # force regeneration
483
        warning_like {
487
        warning_like {
484
            $t->post_ok( "//$userid:$password@/api/v1/patrons" => json => $newpatron )->status_is(409)
488
            $t->post_ok(
485
                ->json_has( '/error', 'Fails when trying to POST duplicate cardnumber' )
489
                "//$userid:$password@/api/v1/patrons" => { 'x-confirm-not-duplicate' => 1 } => json => $newpatron )
490
                ->status_is(409)->json_has( '/error', 'Fails when trying to POST duplicate cardnumber' )
486
                ->json_like( '/conflict' => qr/(borrowers\.)?cardnumber/ );
491
                ->json_like( '/conflict' => qr/(borrowers\.)?cardnumber/ );
487
        }
492
        }
488
        qr/DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key '(borrowers\.)?cardnumber'/;
493
        qr/DBD::mysql::st execute failed: Duplicate entry '(.*?)' for key '(borrowers\.)?cardnumber'/;
Lines 496-503 subtest 'add() tests' => sub { Link Here
496
501
497
        $newpatron->{category_id} = $deleted_category_id;    # Test invalid patron category
502
        $newpatron->{category_id} = $deleted_category_id;    # Test invalid patron category
498
503
499
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => json => $newpatron )->status_is(400)
504
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => { 'x-confirm-not-duplicate' => 1 } => json => $newpatron )
500
            ->json_is( '/error' => "Given category_id does not exist" );
505
            ->status_is(400)->json_is( '/error' => "Given category_id does not exist" );
501
        $newpatron->{category_id} = $patron->categorycode;
506
        $newpatron->{category_id} = $patron->categorycode;
502
507
503
        $newpatron->{falseproperty} = "Non existent property";
508
        $newpatron->{falseproperty} = "Non existent property";
Lines 543-550 subtest 'add() tests' => sub { Link Here
543
        $newpatron->{userid}     = "newuserid";
548
        $newpatron->{userid}     = "newuserid";
544
        $letter_enqueued         = 0;
549
        $letter_enqueued         = 0;
545
        t::lib::Mocks::mock_preference( 'AutoEmailNewUser', 1 );
550
        t::lib::Mocks::mock_preference( 'AutoEmailNewUser', 1 );
546
        $t->post_ok(
551
        $t->post_ok( "//$userid:$password@/api/v1/patrons" =>
547
            "//$userid:$password@/api/v1/patrons" => { 'x-koha-override' => 'welcome_no' } => json => $newpatron )
552
                { 'x-koha-override' => 'welcome_no', 'x-confirm-not-duplicate' => 1 } => json => $newpatron )
548
            ->status_is( 201, 'Patron created successfully with welcome_no override' );
553
            ->status_is( 201, 'Patron created successfully with welcome_no override' );
549
        is( $letter_enqueued, 0, "No welcome notice sent due to welcome_no override" );
554
        is( $letter_enqueued, 0, "No welcome notice sent due to welcome_no override" );
550
555
Lines 553-559 subtest 'add() tests' => sub { Link Here
553
        $newpatron->{userid}     = "newuserid2";
558
        $newpatron->{userid}     = "newuserid2";
554
        $letter_enqueued         = 0;
559
        $letter_enqueued         = 0;
555
        t::lib::Mocks::mock_preference( 'AutoEmailNewUser', 1 );
560
        t::lib::Mocks::mock_preference( 'AutoEmailNewUser', 1 );
556
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => json => $newpatron )
561
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => { 'x-confirm-not-duplicate' => 1 } => json => $newpatron )
557
            ->status_is( 201, 'Patron created successfully with AutoEmailNewUser = 1' );
562
            ->status_is( 201, 'Patron created successfully with AutoEmailNewUser = 1' );
558
        is( $letter_enqueued, 1, "Welcome notice sent due to AutoEmailNewUser = 1" );
563
        is( $letter_enqueued, 1, "Welcome notice sent due to AutoEmailNewUser = 1" );
559
564
Lines 563-569 subtest 'add() tests' => sub { Link Here
563
        $newpatron->{userid}     = "newuserid3";
568
        $newpatron->{userid}     = "newuserid3";
564
        $letter_enqueued         = 0;
569
        $letter_enqueued         = 0;
565
        t::lib::Mocks::mock_preference( 'AutoEmailNewUser', 1 );
570
        t::lib::Mocks::mock_preference( 'AutoEmailNewUser', 1 );
566
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => json => $newpatron )
571
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => { 'x-confirm-not-duplicate' => 1 } => json => $newpatron )
567
            ->status_is( 201, 'Patron created successfully but email will not be sent' );
572
            ->status_is( 201, 'Patron created successfully but email will not be sent' );
568
        is( $letter_enqueued, 0, "No welcome notice sent due to missing email address" );
573
        is( $letter_enqueued, 0, "No welcome notice sent due to missing email address" );
569
        $mocked_patron->unmock('notice_email_address');
574
        $mocked_patron->unmock('notice_email_address');
Lines 574-580 subtest 'add() tests' => sub { Link Here
574
        $letter_enqueued         = 0;
579
        $letter_enqueued         = 0;
575
        $message_has_content     = 0;
580
        $message_has_content     = 0;
576
        t::lib::Mocks::mock_preference( 'AutoEmailNewUser', 1 );
581
        t::lib::Mocks::mock_preference( 'AutoEmailNewUser', 1 );
577
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => json => $newpatron )
582
        $t->post_ok( "//$userid:$password@/api/v1/patrons" => { 'x-confirm-not-duplicate' => 1 } => json => $newpatron )
578
            ->status_is( 201, 'Patron created successfully with AutoEmailNewUser = 1 and empty WELCOME notice' );
583
            ->status_is( 201, 'Patron created successfully with AutoEmailNewUser = 1 and empty WELCOME notice' );
579
        is( $letter_enqueued, 0, "Welcome not sent as it would be empty" );
584
        is( $letter_enqueued, 0, "Welcome not sent as it would be empty" );
580
        $message_has_content = 1;
585
        $message_has_content = 1;
581
- 

Return to bug 40082