@@ -, +, @@ --- 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(-) --- a/Koha/Patron.pm +++ a/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]+$'} --- a/t/db_dependent/Koha/Plugins/Barcode_transform_hooks.t +++ a/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( { --- a/t/lib/Koha/Plugin/Test.pm +++ a/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"); } --