https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43395

--- Comment #5 from David Nind <[email protected]> ---
Created attachment 211175
  -->
https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211175&action=edit
Bug 43395: Validate template toolkit variables when saving notice

A notice using a variable it does not receive prints nothing for it, and
a field or method that does not exist on an object (e.g.
[% branch.brunchname %]) makes the whole notice or slip fail to generate.

This adds to the checks done when saving a notice (bug 39716):
- errors, that prevent saving:
  - fields and methods that do not exist on the objects the notice
    receives, following the links between objects: for
    [% checkout.item.biblio.titl %] it checks item on Koha::Checkout,
    biblio on Koha::Item, then reports titl as missing on Koha::Biblio
  - blocks processed but not defined in the template
  - plugins or variables used as a filter (| $Price) but not available
- warnings:
  - variables the notice does not receive, with a suggestion when
    another one holds the same kind of object (e.g. 'borrower' for
    'patron'), or else the list of the variables it receives, collapsed
    after the first 5
  - plugins used without [% USE ... %], which then print nothing
Each problem lists all the lines it is on.

Koha::Notice::Template::Validator finds the variables by compiling the
template with Template::Parser and reading the variable lookups from the
generated code, so it sees variables in method arguments, filters and
interpolated strings, and the variables the template defines itself
(SET, FOREACH, USE, MACRO, BLOCK). Loop variables get the type of the
elements of their list.

The variables each notice receives come from
Koha::Notice::Template::Variables (bug 36588). Codes chosen by the
library (e.g. in the overdue notice triggers) accept the variables of
every notice of their module. Steps the validator cannot follow (method
results that are not relationships, hashes, plain values) are trusted,
so linking through available objects is never reported.

The tests check every sample notice: the only problems found are real
ones in the sample notices themselves (ACCOUNT_DEBIT uses credit,
HOLD_SLIP email uses reserve, RECEIPT email uses payment and debit, and
several receipts use LibraryName), to be fixed separately.

Test plan:
1. Apply the patches, run yarn build and restart services
2. Go to Tools > Notices and slips, create a new notice in the
   "Patron (custom slip)" module with code TESTSLIP
3. Paste this in the Print template body:
   [% Branches.GetName( patron.branchcode ) %]
   [% IF patron %]Dear [% patron.firstname %][% END %]
   [% total | $Price %] [% borrower.firstnme %]
   [% PROCESS account_type_description %]
4. Click "Check template":
   => Errors, with the lines they are on:
      - Price is used without being loaded (line 3)
      - borrower.firstnme does not exist: Koha::Patron has no field or
        method named firstnme (line 3)
      - the block account_type_description is not defined (line 4)
   => Warnings:
      - Branches is used without being loaded (line 1)
      - patron is not available, did you mean borrower? (lines 1, 2)
      - total is not available, this notice receives: borrower, branch
   => Variable and plugin names are in bold
5. Click Save:
   => The notice cannot be saved
6. Replace the body with:
   [% USE Branches %][% Branches.GetName( borrower.branchcode ) %]
   [% borrower.firstname %] [% borrower.account.balance %]
   [% FOREACH c IN borrower.checkouts %][% c.item.biblio.title %][% END %]
   and check again:
   => No problems found
7. Add [% nosuchthing %] and check:
   => A warning lists the 2 variables the notice receives
8. Edit ODUE, add [% nosuchthing %] to the Email template and check:
   => The list of variables shows the first 5 and "and 22 more", which
      shows the others when clicked
9. Add [% FOREACH o IN overdues %][% o.item.barcod %][% END %]:
   => o.item.barcod is an error: Koha::Item has no field named barcod
10. Create a new notice in the "Holds" module with code TESTHOLD, and
    add to its Email template:
    [% Branches.GetName( 'CPL' ) %] [% 5 | $Price %]
    and check:
   => Branches is reported as used without being loaded (warning), and
      Price as used as a filter without being loaded (error), although
      the variables of this notice are not known
11. prove t/db_dependent/Koha/Notice/Template/Validator.t
t/db_dependent/Koha/Notice/Template.t t/db_dependent/api/v1/notice_templates.t

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: David Nind <[email protected]>

-- 
You are receiving this mail because:
You are watching all bug changes.
_______________________________________________
Koha-bugs mailing list -- [email protected]
To unsubscribe send an email to [email protected]
website : http://www.koha-community.org/
git : http://git.koha-community.org/
bugs : http://bugs.koha-community.org/

Reply via email to