From aea705505c794a5828e39f3bc038671fa90e7872 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 13 Oct 2021 14:46:08 +0200 Subject: [PATCH] Bug 28739: Execute the letter processing inside a transaction --- C4/Letters.pm | 3 +++ Koha/Object.pm | 17 +++++++++++++++++ t/db_dependent/Letters/TemplateToolkit.t | 23 ++++++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index f3cabc28565..5b53d96c326 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1639,7 +1639,10 @@ sub _process_tt { $content = qq|[% USE KohaDates %][% USE Remove_MARC_punctuation %]$content|; my $output; + my $schema = Koha::Database->new->schema; + $schema->txn_begin; $template->process( \$content, $tt_params, \$output ) || croak "ERROR PROCESSING TEMPLATE: " . $template->error(); + $schema->txn_rollback; return $output; } diff --git a/Koha/Object.pm b/Koha/Object.pm index 6f55c3b4861..e4b537bb5c5 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -374,6 +374,18 @@ sub add_message { my ( $self, $params ) = @_; push @{ $self->{_messages} }, Koha::Object::Message->new($params); +} + +=head3 $object->read_only(); + +Set the object as read_only, only getter methods will be allowed + +=cut + +sub read_only { + my ($self) = @_; + + $self->{_read_only} = 1; return $self; } @@ -811,6 +823,8 @@ sub AUTOLOAD { # Using direct setter/getter like $item->barcode() or $item->barcode($barcode); if ( grep { $_ eq $method } @columns ) { if ( @_ ) { + die "Setter method $method called on read-only object" + if $self->{_read_only}; $self->_result()->set_column( $method, @_ ); return $self; } else { @@ -819,6 +833,9 @@ sub AUTOLOAD { } } + die "Cannot call method $method on a read-only object" + if $self->{_read_only}; + my @known_methods = qw( is_changed id in_storage get_column discard_changes make_column_dirty ); Koha::Exceptions::Object::MethodNotCoveredByTests->throw( diff --git a/t/db_dependent/Letters/TemplateToolkit.t b/t/db_dependent/Letters/TemplateToolkit.t index 127a98bd117..e5905d53426 100755 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/t/db_dependent/Letters/TemplateToolkit.t @@ -19,7 +19,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 28; +use Test::More tests => 29; use Test::MockModule; use Test::Warn; @@ -1171,6 +1171,27 @@ EOF is( $letter->{content}, $expected_content ); }; +subtest 'Execute TT process in a DB transaction' => sub { + plan tests => 2; + my $code = 'TEST_TXN'; + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $template = < $template, code => $code, module => 'test' }); + my $letter = GetPreparedLetter( + module => 'test', + letter_code => $code, + tables => { + branches => $library->branchcode, + } + ); + my $branchcode = $library->branchcode; + like($letter->{content}, qr{=$branchcode=}, 'content generated with the library'); + is( ref($library->get_from_storage), 'Koha::Library', 'calling ->delete on the object has not been comitted'); +}; + sub reset_template { my ( $params ) = @_; my $template = $params->{template}; -- 2.25.1