From 9e7903dfd6714b22c670acdadc4a030f3e6c2ceb Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 4 Mar 2021 09:34:12 -0300 Subject: [PATCH] Bug 27857: Regression tests This patch adds regression tests for the changed behavior. Koha::Patron->extended_attributes will now throw exceptions and rollback on invalid attributes. The existing tests are amended so they don't try to deal with skipping bad attributes. Specific subtests are added for error situations. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Patron.t => FAIL: Tests fail because the new behavior is not implemented. Signed-off-by: David Nind Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart --- t/db_dependent/Koha/Patron.t | 179 +++++++++++++++++++++++++---------- 1 file changed, 128 insertions(+), 51 deletions(-) diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 7dc56d5b8f..f5f33e0d8a 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -368,7 +368,9 @@ subtest 'is_superlibrarian() tests' => sub { }; subtest 'extended_attributes' => sub { + plan tests => 14; + my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -395,10 +397,6 @@ subtest 'extended_attributes' => sub { my $attribute_type3 = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' }); - my $deleted_attribute_type = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' }); - my $deleted_attribute_type_code = $deleted_attribute_type->code; - $deleted_attribute_type->delete; - my $new_library = $builder->build( { source => 'Branch' } ); my $attribute_type_limited = Koha::Patron::Attribute::Type->new( { code => 'my code3', description => 'my description3' } )->store; @@ -427,10 +425,6 @@ subtest 'extended_attributes' => sub { { attribute => 'my attribute limited 2', code => $attribute_type_limited->code(), - }, - { - attribute => 'my nonexistent attribute 2', - code => $deleted_attribute_type_code, } ]; @@ -441,10 +435,7 @@ subtest 'extended_attributes' => sub { $patron_1->extended_attributes->filter_by_branch_limitations->delete; $patron_2->extended_attributes->filter_by_branch_limitations->delete; $patron_1->extended_attributes($attributes_for_1); - - warning_like { - $patron_2->extended_attributes($attributes_for_2); - } [ qr/a foreign key constraint fails/ ], 'nonexistent attribute should have not exploded but print a warning'; + $patron_2->extended_attributes($attributes_for_2); my $extended_attributes_for_1 = $patron_1->extended_attributes; is( $extended_attributes_for_1->count, 3, 'There should be 3 attributes now for patron 1'); @@ -452,65 +443,36 @@ subtest 'extended_attributes' => sub { my $extended_attributes_for_2 = $patron_2->extended_attributes; is( $extended_attributes_for_2->count, 2, 'There should be 2 attributes now for patron 2'); - my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code }); - is( $attribute_12->next->attribute, 'my attribute12', 'search by code should return the correct attribute' ); + my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code })->next; + is( $attribute_12->attribute, 'my attribute12', 'search by code should return the correct attribute' ); $attribute_12 = $patron_2->get_extended_attribute( $attribute_type1->code ); is( $attribute_12->attribute, 'my attribute12', 'Koha::Patron->get_extended_attribute should return the correct attribute value' ); - warning_is { - $extended_attributes_for_2 = $patron_2->extended_attributes->merge_with( - [ - { - attribute => 'my attribute12 XXX', - code => $attribute_type1->code(), - }, - { - attribute => 'my nonexistent attribute 2', - code => $deleted_attribute_type_code, - }, - { - attribute => 'my attribute 3', # Adding a new attribute using merge_with - code => $attribute_type3->code, - }, - ] - ); - } - "Cannot merge element: unrecognized code = '$deleted_attribute_type_code'", - "Trying to merge_with using a nonexistent attribute code should display a warning"; - - is( @$extended_attributes_for_2, 3, 'There should be 3 attributes now for patron 3'); my $expected_attributes_for_2 = [ { code => $attribute_type1->code(), - attribute => 'my attribute12 XXX', + attribute => 'my attribute12', }, { code => $attribute_type_limited->code(), attribute => 'my attribute limited 2', - }, - { - attribute => 'my attribute 3', - code => $attribute_type3->code, - }, + } ]; # Sorting them by code $expected_attributes_for_2 = [ sort { $a->{code} cmp $b->{code} } @$expected_attributes_for_2 ]; + my @extended_attributes_for_2 = $extended_attributes_for_2->as_list; is_deeply( [ { - code => $extended_attributes_for_2->[0]->{code}, - attribute => $extended_attributes_for_2->[0]->{attribute} + code => $extended_attributes_for_2[0]->code, + attribute => $extended_attributes_for_2[0]->attribute }, { - code => $extended_attributes_for_2->[1]->{code}, - attribute => $extended_attributes_for_2->[1]->{attribute} - }, - { - code => $extended_attributes_for_2->[2]->{code}, - attribute => $extended_attributes_for_2->[2]->{attribute} - }, + code => $extended_attributes_for_2[1]->code, + attribute => $extended_attributes_for_2[1]->attribute + } ], $expected_attributes_for_2 ); @@ -537,5 +499,120 @@ subtest 'extended_attributes' => sub { #$limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code ); #is( $limited_value, undef, ); + subtest 'non-repeatable attributes tests' => sub { + + plan tests => 3; + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + my $attribute_type = $builder->build_object( + { + class => 'Koha::Patron::Attribute::Types', + value => { repeatable => 0 } + } + ); + + is( $patron->extended_attributes->count, 0, 'Patron has no extended attributes' ); + + throws_ok + { + $patron->extended_attributes( + [ + { code => $attribute_type->code, attribute => 'a' }, + { code => $attribute_type->code, attribute => 'b' } + ] + ); + } + 'Koha::Exceptions::Patron::Attribute::NonRepeatable', + 'Exception thrown on non-repeatable attribute'; + + is( $patron->extended_attributes->count, 0, 'Extended attributes storing rolled back' ); + }; + + subtest 'unique attributes tests' => sub { + + plan tests => 5; + + my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); + + my $attribute_type_1 = $builder->build_object( + { + class => 'Koha::Patron::Attribute::Types', + value => { unique => 1 } + } + ); + + my $attribute_type_2 = $builder->build_object( + { + class => 'Koha::Patron::Attribute::Types', + value => { unique => 0 } + } + ); + + is( $patron_1->extended_attributes->count, 0, 'patron_1 has no extended attributes' ); + is( $patron_2->extended_attributes->count, 0, 'patron_2 has no extended attributes' ); + + $patron_1->extended_attributes( + [ + { code => $attribute_type_1->code, attribute => 'a' }, + { code => $attribute_type_2->code, attribute => 'a' } + ] + ); + + throws_ok + { + $patron_2->extended_attributes( + [ + { code => $attribute_type_1->code, attribute => 'a' }, + { code => $attribute_type_2->code, attribute => 'a' } + ] + ); + } + 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint', + 'Exception thrown on unique attribute'; + + is( $patron_1->extended_attributes->count, 2, 'Extended attributes stored' ); + is( $patron_2->extended_attributes->count, 0, 'Extended attributes storing rolled back' ); + }; + + subtest 'invalid type attributes tests' => sub { + + plan tests => 3; + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + + my $attribute_type_1 = $builder->build_object( + { + class => 'Koha::Patron::Attribute::Types', + value => { repeatable => 0 } + } + ); + + my $attribute_type_2 = $builder->build_object( + { + class => 'Koha::Patron::Attribute::Types' + } + ); + + my $type_2 = $attribute_type_2->code; + $attribute_type_2->delete; + + is( $patron->extended_attributes->count, 0, 'Patron has no extended attributes' ); + + throws_ok + { + $patron->extended_attributes( + [ + { code => $attribute_type_1->code, attribute => 'a' }, + { code => $attribute_type_2->code, attribute => 'b' } + ] + ); + } + 'Koha::Exceptions::Patron::Attribute::InvalidType', + 'Exception thrown on invalid attribute type'; + + is( $patron->extended_attributes->count, 0, 'Extended attributes storing rolled back' ); + }; + $schema->storage->txn_rollback; };