From b6d09fa0a2cee194121d434a68f9f88ce70d576d Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Wed, 8 Mar 2017 17:59:37 +0200 Subject: [PATCH] Bug 18230: Generate verification_token in Koha::Patron::Modification->new To test: 1. prove t/db_dependent/Koha/Patron/Modifications.t --- Koha/Patron/Modification.pm | 24 ++++++++++++++++++++++++ t/db_dependent/Koha/Patron/Modifications.t | 14 +++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Koha/Patron/Modification.pm b/Koha/Patron/Modification.pm index 696f7e8..36a81cf 100644 --- a/Koha/Patron/Modification.pm +++ b/Koha/Patron/Modification.pm @@ -27,6 +27,7 @@ use Koha::Patron::Modifications; # TODO: Remove once Koha::Patron::Attribute(s) is implemented use C4::Members::Attributes qw( SetBorrowerAttributes ); +use Digest::MD5 qw( md5_hex ); use JSON; use Try::Tiny; @@ -40,6 +41,29 @@ Koha::Patron::Modification - Class represents a request to modify or create a pa =cut +=head2 new + +=cut + +sub new { + my $class = shift; + my ($attributes) = @_; + + # Generate a verification_token unless provided + if ( ref $attributes && !$attributes->{verification_token} ) { + my $verification_token = md5_hex( time().{}.rand().{}.$$ ); + while ( Koha::Patron::Modifications->search( { + verification_token => $verification_token + } )->count() + ) { + $verification_token = md5_hex( time().{}.rand().{}.$$ ); + } + $attributes->{verification_token} = $verification_token; + } + + return $class->SUPER::new(@_); +} + =head2 store =cut diff --git a/t/db_dependent/Koha/Patron/Modifications.t b/t/db_dependent/Koha/Patron/Modifications.t index 0755858..ed2590d 100755 --- a/t/db_dependent/Koha/Patron/Modifications.t +++ b/t/db_dependent/Koha/Patron/Modifications.t @@ -40,7 +40,7 @@ my $builder = t::lib::TestBuilder->new; subtest 'new() tests' => sub { - plan tests => 3; + plan tests => 4; $schema->storage->txn_begin; @@ -77,6 +77,18 @@ subtest 'new() tests' => sub { 'Exception carries the right message' ); + # Create new pending modification, let verification_token be generated + # automatically + Koha::Patron::Modification->new( + { + surname => 'Koha-Suomi', + firstname => 'Suha-Koomi', + } + )->store(); + $borrower = Koha::Patron::Modifications->find( + { surname => 'Koha-Suomi' } ); + ok(length($borrower->verification_token) > 0, 'Verification token generated.'); + $schema->storage->txn_rollback; }; -- 1.9.1