From ecb75563bb417007dc56282e9d69eb7cc4a05489 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 20 Nov 2020 14:13:39 +0000 Subject: [PATCH] Bug 26352: (QA follow-up) Add additional tests This patch adds additional unit tests for autoMemberNum handling and corrects the implimentation there. Signed-off-by: Martin Renvoize --- Koha/Patron.pm | 2 +- .../Koha/Plugins/Barcode_transform_hooks.t | 25 +++++++++++++++---- t/lib/Koha/Plugin/Test.pm | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 5fd9bcf832..cc41d1ef1a 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -112,7 +112,7 @@ Autogenerate next cardnumber from highest value found in database sub fixup_cardnumber { my ( $self ) = @_; - my ( $max ) = Koha::Plugins->call( 'patron_barcode_transform', $self ) || undef; + my ( $max ) = Koha::Plugins->call( 'patron_barcode_transform', $self->cardnumber ) || undef; $max ||= Koha::Patrons->search({ cardnumber => {-regexp => '^-?[0-9]+$'} diff --git a/t/db_dependent/Koha/Plugins/Barcode_transform_hooks.t b/t/db_dependent/Koha/Plugins/Barcode_transform_hooks.t index 684c7d8dfa..7f3682b993 100755 --- a/t/db_dependent/Koha/Plugins/Barcode_transform_hooks.t +++ b/t/db_dependent/Koha/Plugins/Barcode_transform_hooks.t @@ -42,7 +42,7 @@ t::lib::Mocks::mock_config( 'enable_plugins', 1 ); subtest '() hook tests' => sub { - plan tests => 2; + plan tests => 4; $schema->storage->txn_begin; @@ -51,12 +51,27 @@ subtest '() hook tests' => sub { my $plugin = Koha::Plugin::Test->new->enable; - my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); - my $cardnumber = $patron->cardnumber; + my $patron = $builder->build_object( + { class => 'Koha::Patrons', value => { cardnumber => undef } } ); + t::lib::Mocks::mock_preference( 'autoMemberNum', 1 ); + warnings_like { $patron->store(); } + [ + qr/patron_barcode_transform called with parameter: /, + qr/patron_barcode_transform called with parameter: / + ], + 'Koha::Patron::store calls the patron_barcode_transform hook twice when autoMemberNum is enabled and cardnumber is undefined'; + + $patron->cardnumber('TEST'); + warning_like { $patron->store(); } + qr/patron_barcode_transform called with parameter: TEST/, + 'Koha::Patron::store calls the patron_barcode_transform hook once when autoMemberNum is enabled and cardnumber is set'; + + t::lib::Mocks::mock_preference( 'autoMemberNum', 0 ); + $patron->cardnumber(undef); warning_like { $patron->store(); } - qr/patron_barcode_transform called with parameter: $cardnumber/, - 'Koha::Patron::store calls the patron_barcode_transform hook'; + qr/patron_barcode_transform called with parameter: /, + 'Koha::Patron::store calls the patron_barcode_transform hook once when autoMemberNum is disabled and cardnumber is undefined'; t::lib::Mocks::mock_userenv( { diff --git a/t/lib/Koha/Plugin/Test.pm b/t/lib/Koha/Plugin/Test.pm index 137d2355a8..26c1d36109 100644 --- a/t/lib/Koha/Plugin/Test.pm +++ b/t/lib/Koha/Plugin/Test.pm @@ -100,6 +100,7 @@ sub item_barcode_transform { sub patron_barcode_transform { my ( $self, $barcode ) = @_; + $barcode //= ''; Koha::Exceptions::Exception->throw("patron_barcode_transform called with parameter: $barcode"); } -- 2.20.1