diff options
-rwxr-xr-x | LedgerSMB/Template/PDF.pm | 23 | ||||
-rwxr-xr-x | LedgerSMB/Template/TXT.pm | 9 | ||||
-rw-r--r-- | t/01-load.t | 3 | ||||
-rw-r--r-- | t/04-template-handling.t | 120 | ||||
-rw-r--r-- | t/99-versioning.t | 39 | ||||
-rw-r--r-- | t/data/04-template.txt | 2 |
6 files changed, 137 insertions, 59 deletions
diff --git a/LedgerSMB/Template/PDF.pm b/LedgerSMB/Template/PDF.pm index 7286b917..f201b11d 100755 --- a/LedgerSMB/Template/PDF.pm +++ b/LedgerSMB/Template/PDF.pm @@ -45,12 +45,27 @@ sub get_template { my $name = shift; return "${name}.tex"; } + sub preprocess { my $rawvars = shift; my $vars; my $type = ref $rawvars; - #XXX Fix escaping - return $rawvars; + + if ($type eq 'ARRAY') { + for (@{$rawvars}) { + push @{$vars}, preprocess($_); + } + } elsif ($type eq 'HASH') { + for ( keys %{$rawvars} ) { + $vars->{$_} = preprocess($rawvars->{$_}); + } + } else { + #XXX Fix escaping + $rawvars =~ s/([&\$\\_<>~^#\%\{\}])/\\$1/g; + $rawvars =~ s/"(.*)"/``$1''/gs; + return $rawvars; + } + return $vars; } sub process { @@ -63,12 +78,12 @@ sub process { INCLUDE_PATH => $parent->{include_path}, START_TAG => quotemeta('<?lsmb'), END_TAG => quotemeta('?>'), - DELIMITER => ';', + DELIMITER => ';' }) || throw Error::Simple Template::Latex->error(); if (not $template->process( get_template($parent->{template}), - $cleanvars, "$parent->{outputfile}.pdf", binmode => ':utf8')) { + $cleanvars, "$parent->{outputfile}.pdf", binmode => 1)) { throw Error::Simple $template->error(); } $parent->{mimetype} = 'application/pdf'; diff --git a/LedgerSMB/Template/TXT.pm b/LedgerSMB/Template/TXT.pm index d7a76430..40d6edb5 100755 --- a/LedgerSMB/Template/TXT.pm +++ b/LedgerSMB/Template/TXT.pm @@ -13,7 +13,7 @@ Returns the appropriate template filename for this format. =item preprocess ($vars) -Currently does nothing. +Returns $vars. =item process ($parent, $cleanvars) @@ -21,7 +21,7 @@ Processes the template for text. =item postprocess ($parent) -Currently does nothing. +Returns the output filename. =back @@ -48,10 +48,7 @@ sub get_template { sub preprocess { my $rawvars = shift; - my $vars; - my $type = ref $rawvars; - - return $vars; + return $rawvars; } sub process { diff --git a/t/01-load.t b/t/01-load.t index ac7c970d..7a0aaf62 100644 --- a/t/01-load.t +++ b/t/01-load.t @@ -2,7 +2,7 @@ use strict; use warnings; -use Test::More tests => 34; +use Test::More tests => 35; use_ok('LedgerSMB'); use_ok('LedgerSMB::AA'); @@ -36,6 +36,7 @@ use_ok('LedgerSMB::Template'); use_ok('LedgerSMB::Template::HTML'); use_ok('LedgerSMB::Template::PDF'); use_ok('LedgerSMB::Template::PS'); +use_ok('LedgerSMB::Template::TXT'); use_ok('LedgerSMB::User'); SKIP: { diff --git a/t/04-template-handling.t b/t/04-template-handling.t index 1f05a266..a8b20c27 100644 --- a/t/04-template-handling.t +++ b/t/04-template-handling.t @@ -18,6 +18,9 @@ use LedgerSMB::Sysconfig; use LedgerSMB::Locale; use LedgerSMB::Template; use LedgerSMB::Template::HTML; +use LedgerSMB::Template::PS; +use LedgerSMB::Template::PDF; +use LedgerSMB::Template::TXT; $LedgerSMB::Sysconfig::tempdir = 't/var'; @@ -31,6 +34,10 @@ my $locale; $locale = LedgerSMB::Locale->get_handle('fr'); +############## +## AM tests ## +############## + # AM->check_template_name checks # check_template operates by calling $form->error if the checks fail $form = new Form; @@ -116,7 +123,10 @@ is(join("\n", @r), $form->{body}, 'AM, save_template: Good save'); is(unlink($form->{file}), 1, 'AM, save_template: removing testfile'); ok(!-e $form->{file}, 'AM, save_template: testfile removed'); -# LedgerSMB::Template::HTML checks +###################################### +## LedgerSMB::Template::HTML checks ## +###################################### + is(LedgerSMB::Template::HTML::get_template('04-template'), '04-template.html', 'HTML, get_template: Returned correct template file name'); is(LedgerSMB::Template::HTML::preprocess('04-template'), '04-template', @@ -137,6 +147,10 @@ is_deeply(LedgerSMB::Template::HTML::preprocess({'fruit' => '&veggies', is(LedgerSMB::Template::HTML::postprocess({outputfile => '04-template'}), '04-template.html', 'HTML, postprocess: Return output filename'); +#################### +## Template tests ## +#################### + # Template->new $myconfig = {'templates' => 't/data'}; throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => 'x/0')} @@ -159,36 +173,15 @@ is($template->{include_path}, 't/data/de;t/data', 'Template, new: Object creation with valid language has good include_path'); $template = undef; $template = new LedgerSMB::Template('user' => $myconfig, 'language' => 'de', - 'path' => 't/data'); + 'path' => 't/data', 'outputfile' => 'test'); ok(defined $template, 'Template, new: Object creation with valid language and path'); isa_ok($template, 'LedgerSMB::Template', 'Template, new: Object creation with valid language and path'); is($template->{include_path}, 't/data', 'Template, new: Object creation with valid path overrides language'); - -$template = undef; -$template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML', - 'template' => '04-template'); -ok(defined $template, - 'Template, new: Object creation with format and template'); -isa_ok($template, 'LedgerSMB::Template', - 'Template, new: Object creation with format and template'); -is($template->{include_path}, 't/data', - 'Template, new: Object creation with format and template'); -is($template->render({'login' => 'foo&bar'}), 't/var/04-template-output.html', - 'Template, render: Simple HTML template, default filename'); -ok(-e 't/var/04-template-output.html', 'Template, render (HTML): File created'); -open($FH, '<', 't/var/04-template-output.html'); -@r = <$FH>; -close($FH); -chomp(@r); -is(join("\n", @r), "I am a template.\nLook at me foo&bar.", - 'Template, render (HTML): Simple HTML template, correct output'); -is(unlink('t/var/04-template-output.html'), 1, - 'Template, render: removing testfile'); -ok(!-e 't/var/04-template-output.html', - 'Template, render (HTML): testfile removed'); +is($template->{outputfile}, 't/var/test', + 'Template, new: Object creation with filename is correct'); $template = undef; $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML', @@ -214,17 +207,21 @@ ok(defined $template, throws_ok{$template->render({'login' => 'foo'})} qr/Can't locate/, 'Template, render: Invalid format caught'; +##################### +## Rendering tests ## +##################### + $template = undef; $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'PDF', 'template' => '04-template'); ok(defined $template, - 'Template, new: Object creation with format and template'); + 'Template, new (PDF): Object creation with format and template'); isa_ok($template, 'LedgerSMB::Template', - 'Template, new: Object creation with format and template'); + 'Template, new (PDF): Object creation with format and template'); is($template->{include_path}, 't/data', - 'Template, new: Object creation with format and template'); -is($template->render({'login' => 'foo\&bar'}), 't/var/04-template-output.pdf', - 'Template, render: Simple PDF template, default filename'); + 'Template, new (PDF): Object creation with format and template'); +is($template->render({'login' => 'foo&bar'}), 't/var/04-template-output.pdf', + 'Template, render (PDF): Simple PDF template, default filename'); ok(-e 't/var/04-template-output.pdf', 'Template, render (PDF): File created'); is(unlink('t/var/04-template-output.pdf'), 1, 'Template, render (PDF): removing testfile'); @@ -235,26 +232,61 @@ $template = undef; $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'PS', 'template' => '04-template'); ok(defined $template, - 'Template, new: Object creation with format and template'); + 'Template, new (PS): Object creation with format and template'); isa_ok($template, 'LedgerSMB::Template', - 'Template, new: Object creation with format and template'); + 'Template, new (PS): Object creation with format and template'); is($template->{include_path}, 't/data', - 'Template, new: Object creation with format and template'); + 'Template, new (PS): Object creation with format and template'); is($template->render({'login' => 'foo\&bar'}), 't/var/04-template-output.ps', - 'Template, render: Simple Postscript template, default filename'); + 'Template, render (PS): Simple Postscript template, default filename'); ok(-e 't/var/04-template-output.ps', 'Template, render (PS): File created'); is(unlink('t/var/04-template-output.ps'), 1, 'Template, render (PS): removing testfile'); ok(!-e 't/var/04-template-output.ps', 'Template, render (PS): testfile removed'); -##open($FH, '<', 't/var/04-template-output.html'); -##@r = <$FH>; -##close($FH); -##chomp(@r); -##is(join("\n", @r), "I am a template.\nLook at me foo&bar.", -## 'Template, render (HTML): Simple HTML template, correct output'); -##is(unlink('t/var/04-template-output.html'), 1, -## 'Template, render: removing testfile'); -##ok(!-e 't/var/04-template-output.html', -## 'Template, render (HTML): testfile removed'); +$template = undef; +$template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'TXT', + 'template' => '04-template'); +ok(defined $template, + 'Template, new (TXT): Object creation with format and template'); +isa_ok($template, 'LedgerSMB::Template', + 'Template, new (TXT): Object creation with format and template'); +is($template->{include_path}, 't/data', + 'Template, new (TXT): Object creation with format and template'); +is($template->render({'login' => 'foo&bar'}), 't/var/04-template-output.txt', + 'Template, render: Simple text template, default filename'); +ok(-e 't/var/04-template-output.txt', 'Template, render (TXT): File created'); +open($FH, '<', 't/var/04-template-output.txt'); +@r = <$FH>; +close($FH); +chomp(@r); +is(join("\n", @r), "I am a template.\nLook at me foo&bar.", + 'Template, render (TXT): Simple TXT template, correct output'); +is(unlink('t/var/04-template-output.txt'), 1, + 'Template, render (TXT): removing testfile'); +ok(!-e 't/var/04-template-output.html', + 'Template, render (TXT): testfile removed'); + +$template = undef; +$template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML', + 'template' => '04-template'); +ok(defined $template, + 'Template, new (HTML): Object creation with format and template'); +isa_ok($template, 'LedgerSMB::Template', + 'Template, new (HTML): Object creation with format and template'); +is($template->{include_path}, 't/data', + 'Template, new (HTML): Object creation with format and template'); +is($template->render({'login' => 'foo&bar'}), 't/var/04-template-output.html', + 'Template, render (HTML): Simple HTML template, default filename'); +ok(-e 't/var/04-template-output.html', 'Template, render (HTML): File created'); +open($FH, '<', 't/var/04-template-output.html'); +@r = <$FH>; +close($FH); +chomp(@r); +is(join("\n", @r), "I am a template.\nLook at me foo&bar.", + 'Template, render (HTML): Simple HTML template, correct output'); +is(unlink('t/var/04-template-output.html'), 1, + 'Template, render (HTML): removing testfile'); +ok(!-e 't/var/04-template-output.html', + 'Template, render (HTML): testfile removed'); diff --git a/t/99-versioning.t b/t/99-versioning.t index d1b833c5..3259e07d 100644 --- a/t/99-versioning.t +++ b/t/99-versioning.t @@ -33,13 +33,44 @@ chomp $ver; is($lsmb->{version}, $ver, 'LedgerSMB version matches VERSION'); is($form->{version}, $ver, 'Form version matches VERSION'); +my @dparts; +my @lparts; +my $age; SKIP: { skip 'LedgerSMB is trunk', 1 if $lsmb->{version} =~ /trunk$/i; - cmp_ok($lsmb->{version}, 'ge', $lsmb->{dbversion}, - 'lsmb: version >= dbversion'); + @dparts = split /\./, $lsmb->{dbversion}; + @lparts = split /\./, $lsmb->{version}; + $age = 0; + foreach my $dpart (@dparts) { + my $lpart = shift @lparts; + if (!defined $lpart) { + $age = 1; + last; + } elsif ($lpart > $dpart) { + last; + } elsif ($dpart > $lpart) { + $age = 1; + last; + } + } + ok($age == 0, 'lsmb: version >= dbversion'); } SKIP: { skip 'Form is trunk', 1 if $form->{version} =~ /trunk$/i; - cmp_ok($form->{version}, 'ge', $form->{dbversion}, - 'form: version >= dbversion'); + @dparts = split /\./, $form->{dbversion}; + @lparts = split /\./, $form->{version}; + $age = 0; + foreach my $dpart (@dparts) { + my $lpart = shift @lparts; + if (!defined $lpart) { + $age = 1; + last; + } elsif ($lpart > $dpart) { + last; + } elsif ($dpart > $lpart) { + $age = 1; + last; + } + } + ok($age == 0, 'form: version >= dbversion'); } diff --git a/t/data/04-template.txt b/t/data/04-template.txt new file mode 100644 index 00000000..3b236040 --- /dev/null +++ b/t/data/04-template.txt @@ -0,0 +1,2 @@ +I am a template. +Look at me <?lsmb login ?>. |