Debian Codegraph @

t/integration/template/rendering.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Fatal qw<exception>;
6 use Plack::Test;
7 use HTTP::Request::Common;
8 use Path::Tiny ();
9
10 use Dancer2::Template::TemplateToolkit;
11
12 # Template rendering: whether the layout is applied, which layout, and what
13 # tokens a view can count on.
14 #
15 # The layout decision is made in Role::Template::apply_layout
16 # (Dancer2/Core/Role/Template.pm:169-172) and distinguishes three cases that are
17 # easy to collapse into two by accident:
18 #
19 # layout key absent from options -> use the configured layout
20 # layout key present and true -> use *that* layout
21 # layout key present and false -> no layout at all
22 #
23 # Template::Toolkit is used rather than Template::Tiny because it is stricter
24 # about undefined tokens and is what most applications actually run.
25
26 my $VIEWS;
27 BEGIN {
28 $VIEWS = Path::Tiny->tempdir;
29 $VIEWS->child('layouts')->mkpath;
30
31 $VIEWS->child('body.tt')->spew_utf8('BODY');
32 $VIEWS->child('empty.tt')->spew_utf8('');
33 $VIEWS->child( 'layouts', 'main.tt' )->spew_utf8('MAIN{[% content %]}');
34 $VIEWS->child( 'layouts', 'alt.tt' )->spew_utf8('ALT{[% content %]}');
35
36 # Every default token, reported so a missing one is visible rather than
37 # silently rendering as empty.
38 $VIEWS->child('tokens.tt')->spew_utf8(
39 join ' ',
40 'settings=[% settings.some_setting %]',
41 'param=[% params.q %]',
42 'var=[% vars.myvar %]',
43 'session=[% session.who %]',
44 'request=[% request.path %]',
45 'perl=[% perl_version %]',
46 'dancer=[% dancer_version %]',
47 );
48 }
49
50 {
51 package TemplateApp;
52 use Dancer2;
53 set logger => 'null';
54 set views => $VIEWS->stringify;
55 set template => 'template_toolkit';
56 set layout => 'main';
57 set session => 'Simple';
58 set some_setting => 'SETTING-VALUE';
59
60 get '/configured' => sub { template 'body' };
61 get '/suppressed' => sub { template 'body', {}, { layout => 0 } };
62 get '/undef' => sub { template 'body', {}, { layout => undef } };
63 get '/named' => sub { template 'body', {}, { layout => 'alt' } };
64 get '/empty' => sub { template 'empty', {}, { layout => 0 } };
65
66 get '/tokens' => sub {
67 var myvar => 'VAR-VALUE';
68 session who => 'ovid';
69 template 'tokens', {}, { layout => 0 };
70 };
71 }
72
73 my $test = Plack::Test->create( TemplateApp->to_app );
74
75 subtest 'the configured layout is applied by default' => sub {
76 my $response = $test->request( GET '/configured' );
77
78 is( $response->code, 200, 'the page renders' );
79 is( $response->content, 'MAIN{BODY}',
80 'the view is wrapped in the configured layout' );
81 };
82
83 subtest 'a false layout option suppresses the layout entirely' => sub {
84 # 0 and undef are the two ways of saying "no layout", and they go through
85 # different halves of the ternary at Role/Template.pm:169-172.
86 is( $test->request( GET '/suppressed' )->content, 'BODY',
87 'layout => 0 renders the view with no layout' );
88 is( $test->request( GET '/undef' )->content, 'BODY',
89 'layout => undef does the same' );
90
91 # The control: the same view under the same app does get a layout when the
92 # option is absent, so the two above are suppressing something real.
93 is( $test->request( GET '/configured' )->content, 'MAIN{BODY}',
94 'while omitting the option still uses the configured layout' );
95 };
96
97 subtest 'a named layout option beats the configured layout' => sub {
98 my $response = $test->request( GET '/named' );
99
100 is( $response->content, 'ALT{BODY}',
101 'the layout named in the options is used' );
102 unlike( $response->content, qr/MAIN/,
103 'and the configured layout is not applied as well' );
104 };
105
106 subtest 'a view is given the default tokens' => sub {
107 my $response = $test->request( GET '/tokens?q=PARAM-VALUE' );
108 my $body = $response->content;
109
110 is( $response->code, 200, 'the page renders' );
111
112 # Each of these is a separate assertion so a failure names the token that
113 # went missing rather than just reporting the whole line differs.
114 like( $body, qr/\bsettings=SETTING-VALUE\b/, 'settings token is present' );
115 like( $body, qr/\bparam=PARAM-VALUE\b/, 'params token is present' );
116 like( $body, qr/\bvar=VAR-VALUE\b/, 'vars token is present' );
117 like( $body, qr/\bsession=ovid\b/, 'session token is present' );
118 like( $body, qr{\brequest=/tokens\b}, 'request token is present' );
119 like( $body, qr/\bperl=v\d+\.\d+/, 'perl_version token is present' );
120 like( $body, qr/\bdancer=\d/, 'dancer_version token is present' );
121 };
122
123 subtest 'an empty view renders as empty, and does not raise' => sub {
124 # Pinned as current behavior. An empty view file produces an empty string,
125 # which is defined, so it passes straight through apply_layout and process
126 # without tripping the "did not produce any content" guard.
127 my $response = $test->request( GET '/empty' );
128
129 is( $response->code, 200, 'an empty view is not an error' );
130 is( $response->content, '', 'and renders as nothing' );
131 };
132
133 subtest 'having no content source at all does raise' => sub {
134 # The guard at Role/Template.pm:239 fires when there is nothing to render:
135 # no view, and no 'content' passed in the options. Checked against the
136 # engine directly, because reaching it through a request would only show
137 # the 500 it turns into.
138 my $engine = Dancer2::Template::TemplateToolkit->new(
139 views => $VIEWS->stringify,
140 layout_dir => 'layouts',
141 config => {},
142 );
143
144 my $err = exception { $engine->process( undef, {}, {} ) };
145 ok( defined $err, 'processing with no view and no content throws' );
146 like( $err, qr/Template did not produce any content/,
147 'with the message that names the problem' );
148
149 # The contrast that makes the above meaningful: an empty view is *not*
150 # this case, and must not throw.
151 is( exception { $engine->process( 'empty', {}, {} ) }, undef,
152 'an empty view is a different case and does not throw' );
153 is( $engine->process( 'empty', {}, {} ), '',
154 'it returns the empty string' );
155 };
156
157 done_testing();