Bug 17962: TT syntax for notices - Prove that ACQ_NOTIF_ON_RECEIV is compatible
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 16 Jan 2017 12:25:31 +0000 (13:25 +0100)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 17 Feb 2017 11:43:46 +0000 (11:43 +0000)
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 <josef.moravec@gmail.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
C4/Letters.pm
t/db_dependent/Letters/TemplateToolkit.t

index f6802ce..cf64147 100644 (file)
@@ -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',
index bdcfb96..9bf8033 100644 (file)
@@ -18,7 +18,7 @@
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 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 <<borrowers.firstname>> <<borrowers.surname>>,
+            The order <<aqorders.ordernumber>> (<<biblio.title>>) 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;
+}