From 32df8cade92251cdf09bb06271e3966b2200d79e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 16 Jan 2017 13:25:31 +0100 Subject: [PATCH] [SIGNED-OFF] Bug 17962: TT syntax for notices - Prove that ACQ_NOTIF_ON_RECEIV is compatible To make ACQ_NOTIF_ON_RECEIV TT compatible, we need to expose data from the aqorders table. We already have a package for it in the Koha namespace but it is based on Koha::Object[s]. The other path creates dummy Koha::Tmp::Order[s] packages to make it usable. Of course we should use a valid Koha::Acquisition::Order[s] based on Koha::Object, but it's outside the scope of this bug report. This notice template is quite simple, and it's a good one to start. From C4::Acq::NotifyOrderUsers, GetPreparedLetter is called with 4 elements: the library, the patron to notify, the biblio and the order information. Note that prior to this patch aqorders was filled from GetOrder, which retrieved a lot of information from the acquisition table (aqbasket, aqbookseller). The idea with the TT syntax is to access the data from where it really exists. So if a user wants to display the basket name, [% order.basket.basketname %] should be used instead. Note that this will not work at the moment, the basket method is not defined in the order package. However the basic template should work as before. The test added to TemplateToolkit proves that. Test plan: Use the default ACQ_NOTIF_ON_RECEIV to notify a patron that an order has been received. That generated template should be exactly the same as prior to this patch. Signed-off-by: Josef Moravec --- C4/Letters.pm | 6 +++ t/db_dependent/Letters/TemplateToolkit.t | 88 +++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 3ff39b1..29e54c8 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1498,6 +1498,12 @@ sub _get_tt_params { plural => 'news', pk => 'idnew', }, + aqorders => { + module => 'Koha::Tmp::Orders', # Should Koha::Acquisition::Orders when will be based on Koha::Objects + singular => 'order', + plural => 'orders', + pk => 'ordernumber', + }, reserves => { module => 'Koha::Holds', singular => 'hold', diff --git a/t/db_dependent/Letters/TemplateToolkit.t b/t/db_dependent/Letters/TemplateToolkit.t index bdcfb96..9bf8033 100644 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/t/db_dependent/Letters/TemplateToolkit.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 15; +use Test::More tests => 16; use Test::Warn; use MARC::Record; @@ -39,6 +39,7 @@ use Koha::Serial; use Koha::Subscription; use Koha::Suggestion; use Koha::Checkout; +use Koha::Notice::Templates; use Koha::Patron::Modification; my $schema = Koha::Database->schema; @@ -279,3 +280,88 @@ $prepared_letter = GetPreparedLetter( ) ); is( $prepared_letter->{content}, $modification->id(), 'Patron modification object used correctly' ); + +subtest 'regression tests' => sub { + plan tests => 1; + + my $library = $builder->build( { source => 'Branch' } ); + my $patron = $builder->build( { source => 'Borrower' } ); + my $biblio = Koha::Biblio->new({title => 'Test Biblio'})->store->unblessed; + my $biblioitem = Koha::Biblioitem->new({biblionumber => $biblio->{biblionumber}})->store()->unblessed; + my $item1 = Koha::Item->new( + { + biblionumber => $biblio->{biblionumber}, + biblioitemnumber => $biblioitem->{biblioitemnumber}, + } + )->store->unblessed; + my $item2 = Koha::Item->new( + { + biblionumber => $biblio->{biblionumber}, + biblioitemnumber => $biblioitem->{biblioitemnumber}, + } + )->store->unblessed; + + subtest 'ACQ_NOTIF_ON_RECEIV ' => sub { + plan tests => 1; + my $code = 'ACQ_NOTIF_ON_RECEIV'; + my $branchcode = $library->{branchcode}; + my $order = $builder->build({ source => 'Aqorder' }); + + my $template = q| + Dear <> <>, + The order <> (<>) has been received. + Your library. + |; + my $params = { code => $code, branchcode => $branchcode, tables => { branches => $library, borrowers => $patron, biblio => $biblio, aqorders => $order } }; + my $letter = process_letter( { template => $template, %$params }); + my $tt_template = q| + Dear [% borrower.firstname %] [% borrower.surname %], + The order [% order.ordernumber %] ([% biblio.title %]) has been received. + Your library. + |; + my $tt_letter = process_letter( { template => $tt_template, %$params }); + + is( $tt_letter->{content}, $letter->{content}, ); + }; +}; + +sub reset_template { + my ( $params ) = @_; + my $template = $params->{template}; + my $code = $params->{code}; + my $module = $params->{module} || 'test_module'; + + Koha::Notice::Templates->search( { code => $code } )->delete; + Koha::Notice::Template->new( + { + module => $module, + code => $code, + branchcode => '', + name => $code, + title => $code, + message_transport_type => 'email', + content => $template + } + )->store; +} + +sub process_letter { + my ($params) = @_; + my $template = $params->{template}; + my $tables = $params->{tables}; + my $substitute = $params->{substitute}; + my $code = $params->{code}; + my $module = $params->{module} || 'test_module'; + my $branchcode = $params->{branchcode}; + + reset_template( $params ); + + my $letter = C4::Letters::GetPreparedLetter( + module => $module, + letter_code => $code, + branchcode => '', + tables => $tables, + substitute => $substitute, + ); + return $letter; +} -- 2.1.4