| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Test::Exception; |
| 5 |
|
| 6 |
local $ENV{LANGUAGE_HANDLE} = 'en_en'; |
| 7 |
|
| 8 |
# Text this distribution did not author must not be used as the Locale::Maketext |
| 9 |
# FORMAT. Maketext compiles a '[...]' group in the format into a method call, so |
| 10 |
# any such text carrying request data lets a submitted value reach a method on |
| 11 |
# the language handle, with the submitter choosing the method and its arguments. |
| 12 |
# |
| 13 |
# The first five blocks below FAIL without the fix: the payload is dispatched |
| 14 |
# instead of shown, or form processing dies outright. The remaining tests assert |
| 15 |
# the behaviour the fix must not break -- ordinary messages, bracket notation in |
| 16 |
# templates this distribution or the application authored, and lexicon lookup of |
| 17 |
# a message that merely happens to be translatable. Those pass either way, by |
| 18 |
# design; they are the regression guard. |
| 19 |
|
| 20 |
{ |
| 21 |
package Test::Inert::Warn; |
| 22 |
use HTML::FormHandler::Moose; |
| 23 |
extends 'HTML::FormHandler'; |
| 24 |
# An ordinary numeric transform. It succeeds, but it warns, and Perl quotes |
| 25 |
# the offending value into the warning verbatim -- so the submitted value |
| 26 |
# lands in text that _apply_actions traps and turns into a message. The |
| 27 |
# coderef must be compiled under 'use warnings' (as it is here) for the |
| 28 |
# warning to happen at all. |
| 29 |
has_field 'qty' => ( |
| 30 |
type => 'Text', |
| 31 |
apply => [ { transform => sub { $_[0] + 0 } } ], |
| 32 |
); |
| 33 |
no HTML::FormHandler::Moose; |
| 34 |
} |
| 35 |
|
| 36 |
{ |
| 37 |
package Test::Inert::ValueAsMessage; |
| 38 |
use HTML::FormHandler::Moose; |
| 39 |
extends 'HTML::FormHandler'; |
| 40 |
has_field 'echo' => ( type => 'Text' ); |
| 41 |
# add_error($value) where a duplicate request parameter made $value an |
| 42 |
# arrayref: the arrayref lands where a template and its arguments go. |
| 43 |
sub validate_echo { my ( $self, $field ) = @_; $field->add_error( $field->value ) } |
| 44 |
no HTML::FormHandler::Moose; |
| 45 |
} |
| 46 |
|
| 47 |
{ |
| 48 |
package Test::Inert::Typed; |
| 49 |
use HTML::FormHandler::Moose; |
| 50 |
extends 'HTML::FormHandler'; |
| 51 |
has_field 'nickname' => ( type => 'Text', apply => [ { type => 'Str' } ] ); |
| 52 |
no HTML::FormHandler::Moose; |
| 53 |
} |
| 54 |
|
| 55 |
{ |
| 56 |
package Test::Inert::MaxLen; |
| 57 |
use HTML::FormHandler::Moose; |
| 58 |
extends 'HTML::FormHandler'; |
| 59 |
has_field 'short' => ( type => 'Text', maxlength => 3 ); |
| 60 |
no HTML::FormHandler::Moose; |
| 61 |
} |
| 62 |
|
| 63 |
{ |
| 64 |
package Test::Inert::Template; |
| 65 |
use HTML::FormHandler::Moose; |
| 66 |
extends 'HTML::FormHandler'; |
| 67 |
has_field 'tags' => ( type => 'Text' ); |
| 68 |
# The documented spelling: the template is the application's, the value is |
| 69 |
# an argument. Bracket notation in it must still compile. |
| 70 |
sub validate_tags { |
| 71 |
my ( $self, $field ) = @_; |
| 72 |
$field->add_error( 'Too many [quant,_1,tag,tags] in [_2]', 3, $field->value ); |
| 73 |
} |
| 74 |
no HTML::FormHandler::Moose; |
| 75 |
} |
| 76 |
|
| 77 |
# A custom type whose message is a phrase the application has in its lexicon. |
| 78 |
# Text this distribution did not author is never used as the format, so it is |
| 79 |
# never a lexicon key either: the message is shown as the type gave it. That is |
| 80 |
# a deliberate consequence and is asserted here so it cannot change silently. |
| 81 |
{ |
| 82 |
package Test::Inert::L10N; |
| 83 |
our @ISA = ('HTML::FormHandler::I18N'); |
| 84 |
package Test::Inert::L10N::en; |
| 85 |
our @ISA = ('Test::Inert::L10N'); |
| 86 |
our %Lexicon = ( '_AUTO' => 1, 'Must be positive' => 'TRANSLATED OK' ); |
| 87 |
} |
| 88 |
{ |
| 89 |
package Test::Inert::Translated; |
| 90 |
use HTML::FormHandler::Moose; |
| 91 |
extends 'HTML::FormHandler'; |
| 92 |
use Moose::Util::TypeConstraints; |
| 93 |
subtype 'TestInertPositive', |
| 94 |
as 'Int', |
| 95 |
where { $_ > 0 }, |
| 96 |
message { 'Must be positive' }; |
| 97 |
has_field 'p' => ( type => 'Text', apply => [ { type => 'TestInertPositive' } ] ); |
| 98 |
no HTML::FormHandler::Moose; |
| 99 |
} |
| 100 |
|
| 101 |
# An exception object, or any other reference, reaching one of those sources. |
| 102 |
# Locale::Maketext stringifies its format argument -- honouring a '""' overload |
| 103 |
# -- so text arriving inside an object must be diverted just as a plain string |
| 104 |
# is, or the object becomes a way around the whole thing. |
| 105 |
{ |
| 106 |
package Test::Inert::Strung; |
| 107 |
use overload '""' => sub { $_[0]->{text} }, fallback => 1; |
| 108 |
sub new { my ( $class, %a ) = @_; return bless {%a}, $class } |
| 109 |
} |
| 110 |
|
| 111 |
{ |
| 112 |
package Test::Inert::Dies; |
| 113 |
use HTML::FormHandler::Moose; |
| 114 |
extends 'HTML::FormHandler'; |
| 115 |
has 'boom' => ( is => 'rw' ); |
| 116 |
has_field 'f' => ( |
| 117 |
type => 'Text', |
| 118 |
apply => [ { transform => sub { die $_[1]->form->boom } } ], |
| 119 |
); |
| 120 |
no HTML::FormHandler::Moose; |
| 121 |
} |
| 122 |
|
| 123 |
sub errors_of { |
| 124 |
my $form = shift; |
| 125 |
return join ' | ', map { $_->all_errors } $form->fields; |
| 126 |
} |
| 127 |
|
| 128 |
# -------------------------------------------------------------------------- |
| 129 |
# These five blocks fail without the fix. |
| 130 |
# -------------------------------------------------------------------------- |
| 131 |
|
| 132 |
# A method call in a trapped warning must be shown, not dispatched. sprintf is |
| 133 |
# used with a small width on purpose: the point is to detect that dispatch |
| 134 |
# happened at all, not to allocate anything. |
| 135 |
{ |
| 136 |
my $form = Test::Inert::Warn->new; |
| 137 |
lives_ok { $form->process( params => { qty => '[sprintf,%20d,7]' } ) } |
| 138 |
'a bracket group in a trapped warning does not kill form processing'; |
| 139 |
my $errors = errors_of($form); |
| 140 |
like $errors, qr/\Q[sprintf,%20d,7]\E/, |
| 141 |
'the bracket group is shown literally in the error message'; |
| 142 |
unlike $errors, qr/\s{15,}7/, |
| 143 |
'sprintf was not called (no padded output in the message)'; |
| 144 |
} |
| 145 |
|
| 146 |
# A bracket group that does not compile must not become an exception. Without |
| 147 |
# the fix this reaches Locale::Maketext's code generator and dies, which |
| 148 |
# add_error re-raises: an unhandled error out of process(). |
| 149 |
{ |
| 150 |
my $form = Test::Inert::Warn->new; |
| 151 |
lives_ok { $form->process( params => { qty => '[0]' } ) } |
| 152 |
'a malformed bracket group in a trapped warning does not die'; |
| 153 |
like errors_of($form), qr/\Q[0]\E/, 'it is shown literally too'; |
| 154 |
} |
| 155 |
|
| 156 |
# A duplicate request parameter must not supply a template and its arguments. |
| 157 |
{ |
| 158 |
my $form = Test::Inert::ValueAsMessage->new; |
| 159 |
lives_ok { $form->process( params => { echo => [ '[quant,_1,x]', '9' ] } ) } |
| 160 |
'an arrayref value passed to add_error does not die'; |
| 161 |
my $errors = errors_of($form); |
| 162 |
like $errors, qr/\Q[quant,_1,x]\E/, 'element 0 is shown literally'; |
| 163 |
unlike $errors, qr/\b9 xs\b/, |
| 164 |
'quant was not called with the submitter\'s argument'; |
| 165 |
} |
| 166 |
|
| 167 |
# A type constraint renders a rejected reference in bracket-and-comma form into |
| 168 |
# its own failure message, so a duplicate parameter alone is enough. Moose only |
| 169 |
# renders it that way when it can load a dumper, so skip when it cannot. |
| 170 |
SKIP: { |
| 171 |
skip 'Devel::PartialDump not available, so the type-constraint message does ' |
| 172 |
. 'not render the rejected value as a bracket group', 1 |
| 173 |
unless eval { require Devel::PartialDump; Devel::PartialDump->VERSION(0.14); 1 }; |
| 174 |
my $form = Test::Inert::Typed->new; |
| 175 |
lives_ok { $form->process( params => { nickname => [ 'a', 'b' ] } ) } |
| 176 |
'a duplicate parameter on a typed field does not die'; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
# A payload carried inside an exception object must be shown, not dispatched. |
| 181 |
{ |
| 182 |
my $form = Test::Inert::Dies->new( |
| 183 |
boom => Test::Inert::Strung->new( text => '[sprintf,%20d,7]' ) ); |
| 184 |
lives_ok { $form->process( params => { f => 'x' } ) } |
| 185 |
'an exception object stringifying to a bracket group does not kill ' |
| 186 |
. 'form processing'; |
| 187 |
my $errors = errors_of($form); |
| 188 |
like $errors, qr/\Q[sprintf,%20d,7]\E/, |
| 189 |
'the object\'s stringification is shown literally'; |
| 190 |
unlike $errors, qr/\s{15,}7/, |
| 191 |
'sprintf was not called through the object'; |
| 192 |
} |
| 193 |
|
| 194 |
# -------------------------------------------------------------------------- |
| 195 |
# These pass with and without the fix. They are the regression guard. |
| 196 |
# -------------------------------------------------------------------------- |
| 197 |
|
| 198 |
{ |
| 199 |
my $form = Test::Inert::MaxLen->new; |
| 200 |
$form->process( params => { short => 'abcdef' } ); |
| 201 |
like errors_of($form), qr/should not exceed/, |
| 202 |
'an ordinary built-in message is unchanged'; |
| 203 |
} |
| 204 |
|
| 205 |
{ |
| 206 |
my $form = Test::Inert::Template->new; |
| 207 |
$form->process( params => { tags => 'a,b,c' } ); |
| 208 |
my $errors = errors_of($form); |
| 209 |
like $errors, qr/Too many 3 tags/, |
| 210 |
'bracket notation in an application template still compiles'; |
| 211 |
like $errors, qr/\Qa,b,c\E/, 'and its argument is interpolated'; |
| 212 |
} |
| 213 |
|
| 214 |
{ |
| 215 |
my $form = Test::Inert::Warn->new; |
| 216 |
$form->process( params => { qty => 'abc' } ); |
| 217 |
like errors_of($form), qr/isn't numeric/, |
| 218 |
'a warning with no bracket metacharacters is still used as the message'; |
| 219 |
} |
| 220 |
|
| 221 |
{ |
| 222 |
my $lh = Test::Inert::L10N->get_handle('en'); |
| 223 |
my $form = Test::Inert::Translated->new( language_handle => $lh ); |
| 224 |
$form->process( params => { p => '-5' } ); |
| 225 |
is errors_of($form), 'Must be positive', |
| 226 |
'a type constraint message is shown as the type gave it -- foreign text ' |
| 227 |
. 'is never used as the format, so it is never a lexicon key either'; |
| 228 |
} |
| 229 |
|
| 230 |
done_testing; |