Bug 42018: Add unit test

Test plan:
1) Apply just the unit test patch
2) Run t/db_dependent/Koha/Patrons.t
3) Test should fail
4) Apply remaining patch
5) Repeat step 3
6) Test should pass

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Andrew Fuerste Henry <andrew@bywatersolutions.com>
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Matt Blenkinsop 2026-08-06 16:10:26 +01:00 committed by Pedro Amorim
parent 37ef59a9c8
commit dbd680caec
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
1 changed files with 56 additions and 1 deletions

View File

@ -3888,7 +3888,7 @@ subtest 'find_by_identifier() tests' => sub {
};
subtest 'check_for_existing_matches' => sub {
plan tests => 7;
plan tests => 14;
$schema->storage->txn_begin;
@ -3929,6 +3929,61 @@ subtest 'check_for_existing_matches' => sub {
{ firstname => 'John', surname => 'Smith', dateofbirth => '1980-01-01', city => 'Sandwich, Kent' } );
is( $match_result->{duplicate_found}, 0, 'No duplicate found' );
# An empty value is ignored on both sides of the comparison
t::lib::Mocks::mock_preference( 'PatronDuplicateMatchingAddFields', 'firstname|surname|email' );
$patron->email(undef)->store;
$match_result = Koha::Patrons->check_for_existing_matches(
{ firstname => 'John', surname => 'Smith', email => 'john.smith@example.com' } );
is(
$match_result->{duplicate_found}, 1,
'Duplicate found when the email is only populated in the patron being added'
);
is(
$match_result->{matching_patrons}->next->borrowernumber, $patron->borrowernumber,
'The existing patron with no email is returned as the match'
);
$patron->email(q{})->store;
$match_result = Koha::Patrons->check_for_existing_matches(
{ firstname => 'John', surname => 'Smith', email => 'john.smith@example.com' } );
is(
$match_result->{duplicate_found}, 1,
'Duplicate found when the email of the existing patron is an empty string'
);
$patron->email('john.smith@example.com')->store;
$match_result = Koha::Patrons->check_for_existing_matches(
{ firstname => 'John', surname => 'Smith', email => 'someone.else@example.com' } );
is(
$match_result->{duplicate_found}, 0,
'No duplicate found when both emails are populated but do not match'
);
# Date columns are not compared against the empty string
t::lib::Mocks::mock_preference( 'PatronDuplicateMatchingAddFields', 'firstname|surname|dateofbirth' );
$patron->dateofbirth(undef)->store;
$match_result = Koha::Patrons->check_for_existing_matches(
{ firstname => 'John', surname => 'Smith', dateofbirth => '1980-01-01' } );
is(
$match_result->{duplicate_found}, 1,
'Duplicate found when a date column is only populated in the patron being added'
);
# A value of '0' is a value, not an empty field
t::lib::Mocks::mock_preference( 'PatronDuplicateMatchingAddFields', 'firstname|surname|sort1' );
$patron->sort1('A')->store;
$match_result =
Koha::Patrons->check_for_existing_matches( { firstname => 'John', surname => 'Smith', sort1 => '0' } );
is( $match_result->{duplicate_found}, 0, "A value of '0' is compared rather than ignored" );
$patron->sort1('0')->store;
$match_result =
Koha::Patrons->check_for_existing_matches( { firstname => 'John', surname => 'Smith', sort1 => '0' } );
is( $match_result->{duplicate_found}, 1, "Duplicate found when both records have a sort1 of '0'" );
$schema->storage->txn_rollback;
};