PageRenderTime 110ms CodeModel.GetById 65ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 1ms

/xt/locale.t

http://github.com/gshank/html-formhandler
Unknown | 131 lines | 102 code | 29 blank | 0 comment | 0 complexity | c03209b68e91f549f43c2a11020f8d64 MD5 | raw file
  1use strict;
  2use warnings;
  3use Test::More;
  4use Test::Exception;
  5use HTML::FormHandler::Field::Text;
  6
  7use lib ('t/lib');
  8
  9# ensure $ENV is properly set up
 10my @LH_VARS = ('LANGUAGE_HANDLE', 'HTTP_ACCEPT_LANGUAGE', 'LANG', 'LANGUAGE' );
 11my %LOC_ENV;
 12$LOC_ENV{$_} = delete $ENV{$_} for @LH_VARS;
 13
 14# a primitive translation package
 15{
 16    package HTML::FormHandler::I18N::xx_xx;
 17    use base 'HTML::FormHandler::I18N';
 18
 19    # Auto define lexicon
 20    our %Lexicon = (
 21        '_AUTO' => 1,
 22        'You lost, insert coin' => 'Not won, coin needed',
 23        'Test field' => 'Grfg svryq',
 24    );
 25}
 26
 27# a simple demo form
 28{
 29   package Test::Form;
 30   use HTML::FormHandler::Moose;
 31   extends 'HTML::FormHandler';
 32
 33   has_field 'test_field';
 34}
 35
 36my $form;
 37
 38
 39################ Locale -none-
 40
 41$ENV{LANGUAGE} = 'en-US';
 42
 43# create form w/o locale must work
 44lives_ok { $form = Test::Form->new } 'create form w/o locale lives';
 45ok($form, 'create form w/o locale');
 46is(ref($form->language_handle), 'HTML::FormHandler::I18N::en_us', 'locale en_us');
 47
 48# ensure we know / don't know the translations
 49$HTML::FormHandler::I18N::en_us::Lexicon{'You lost, insert coin'} = 'XX Dummy 42';
 50$HTML::FormHandler::I18N::en_us::Lexicon{'Must insert a [_1] coin'} = 'Want a [_1] coin';
 51delete $HTML::FormHandler::I18N::en_us::Lexicon{'Test field'};
 52delete $HTML::FormHandler::I18N::en_us::Lexicon{'You won'};
 53
 54# translating a known error works
 55$form->field('test_field')->clear_errors;
 56$form->field('test_field')->add_error('You lost, insert coin');
 57is_deeply($form->field('test_field')->errors, ['XX Dummy 42'], 'error is translated into en_us');
 58
 59# translating a known label
 60is($form->field('test_field')->label, 'Test field', 'Label w/o translation = ok');
 61
 62# translating a known error with a positional parameter
 63$form->field('test_field')->clear_errors;
 64$form->field('test_field')->add_error('Must insert a [_1] coin', 'cleaned');
 65is_deeply($form->field('test_field')->errors, ['Want a cleaned coin'], 'error w/parameter is translated into en_us');
 66
 67# translating an unknown error also works
 68$form->field('test_field')->clear_errors;
 69$form->field('test_field')->add_error('You won');
 70is_deeply($form->field('test_field')->errors, ['You won'], 'error is translated into en_us');
 71
 72# translating an error with bracket issues
 73$form->field('test_field')->clear_errors;
 74dies_ok( sub { $form->field('test_field')->add_error('You are not authorized for this archive. See: [<a href="/help/auth">more information</a>],  [<a href="/need_auth">request authorization</a>]') }, 'dies on maketext error' );
 75lives_ok( sub { $form->field('test_field')->add_error('An error with ~[escaped brackets~]'); },
 76   'does not die when brackets are escaped with tilde' );
 77
 78################ Locale xx_xx set via ENV{LANGUAGE_HANDLE}
 79$ENV{LANGUAGE_HANDLE} = 'xx_xx';
 80
 81# create form w/ locale must work
 82undef $form;
 83lives_ok { $form = Test::Form->new } 'create form w/ locale lives';
 84ok($form, 'create form w/ locale');
 85is(ref($form->language_handle), 'HTML::FormHandler::I18N::xx_xx', 'locale xx_xx');
 86
 87# translating a known error works
 88$form->field('test_field')->clear_errors;
 89$form->field('test_field')->add_error('You lost, insert coin');
 90is_deeply($form->field('test_field')->errors, ['Not won, coin needed'], 'error is translated into xx_xx');
 91
 92# translating an unknown error also works
 93$form->field('test_field')->clear_errors;
 94$form->field('test_field')->add_error('You won');
 95is_deeply($form->field('test_field')->errors, ['You won'], 'error is translated into xx_xx');
 96
 97# translating a known label
 98is($form->field('test_field')->loc_label, 'Grfg svryq', 'label rot13 to xx_xx');
 99
100# remove from environment variable, so we can use builder
101delete $ENV{LANGUAGE_HANDLE};
102{
103    package MyApp::Test::Form;
104    use HTML::FormHandler::Moose;
105    extends 'HTML::FormHandler';
106    use MyApp::I18N::abc_de;
107
108    sub _build_language_handle { MyApp::I18N::abc_de->new }
109    has_field 'foo';
110    has_field 'bar';
111    sub validate_foo {
112        my ( $self, $field ) = @_;
113        $field->add_error('You lost, insert coin');
114    }
115}
116
117$form = MyApp::Test::Form->new;
118
119ok( $form, 'form built' );
120$form->process( params => { foo => 'test' } );
121is( $form->field('foo')->errors->[0], 'Loser! coin needed', 'right message' );
122is( ref $form->language_handle, 'MyApp::I18N::abc_de', 'using right lh');
123is( ref $form->field('foo')->language_handle, 'MyApp::I18N::abc_de', 'field uses form language handle' );
124
125$form->process( language_handle => HTML::FormHandler::I18N->get_handle('en_us'), params => { foo => 'byebye' } );
126my $field_lh = $form->field('bar')->language_handle;
127is( ref $field_lh, 'HTML::FormHandler::I18N::en_us', 'field uses form language handle' );
128
129$ENV{$_} = 'en-US' for @LH_VARS;
130
131done_testing;