From ec3bce4fa1de3a080db2d38904c0bfc0f71d1b98 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 Signed-off-by: Jonathan Druart --- 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 2575a6732e..0eed3de4ff 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 6b087ff77d..af8c35cd34 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.39.5