From 2bb03b7a3043b2e1c874263d71f9faf880c990e6 Mon Sep 17 00:00:00 2001 From: Petro Vashchuk Date: Mon, 28 Apr 2025 14:18:56 +0300 Subject: [PATCH] Bug 32767: Add tests for the trims attributes feature --- t/db_dependent/Koha/Patron/Attribute.t | 75 +++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Patron/Attribute.t b/t/db_dependent/Koha/Patron/Attribute.t index 97015d0c217..4fd3189972a 100755 --- a/t/db_dependent/Koha/Patron/Attribute.t +++ b/t/db_dependent/Koha/Patron/Attribute.t @@ -37,7 +37,7 @@ my $builder = t::lib::TestBuilder->new; subtest 'store() tests' => sub { - plan tests => 6; + plan tests => 7; subtest 'Update an attribute should update the patron "updated_on" field' => sub { @@ -282,6 +282,79 @@ subtest 'store() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'trim identifier tests (Bug 32767)' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + my $patron_1 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; + my $patron_2 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; + + my $attribute_type_1 = $builder->build( + { source => 'BorrowerAttributeType', + value => { unique_id => 1, trim_value => 0 } + } + ); + Koha::Patron::Attribute->new( + { borrowernumber => $patron_1, + code => $attribute_type_1->{code}, + attribute => '123' + } + )->store; + Koha::Patron::Attribute->new( + { borrowernumber => $patron_2, + code => $attribute_type_1->{code}, + attribute => ' 123' + } + )->store; + my $attr_count + = Koha::Patron::Attributes->search( + { code => $attribute_type_1->{code} } ) + ->count; + is( $attr_count, 2, + 'Two identical attributes stored and retrieved correctly because one of them had whitespace at the start' ); + + my $attribute_type_2 = $builder->build( + { source => 'BorrowerAttributeType', + value => { unique_id => 1, trim_value => 1 } + } + ); + + Koha::Patron::Attribute->new( + { borrowernumber => $patron_1, + code => $attribute_type_2->{code}, + attribute => '123' + } + )->store; + throws_ok { + Koha::Patron::Attribute->new( + { borrowernumber => $patron_2, + code => $attribute_type_2->{code}, + attribute => ' 123' + } + )->store; + } + 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint', + 'Exception thrown trying to store more than one unique attribute now that we enabled trim_value'; + + is( + "$@", + "Your action breaks a unique constraint on the attribute. type=" + . $attribute_type_2->{code} + . " value=123", + 'Exception stringified correctly, attribute passed correctly' + ); + + my $attributes = Koha::Patron::Attributes->search( + { borrowernumber => $patron_1, code => $attribute_type_2->{code} } ); + is( $attributes->count, 1, '1 unique attribute stored' ); + is( $attributes->next->attribute, + '123', 'our unique attribute remains unchanged' ); + + $schema->storage->txn_rollback; + }; + subtest 'invalid type tests' => sub { plan tests => 2; -- 2.49.0