mojomojo / t / formatter_all_textile.t
100644 448 lines (380 sloc) 11.938 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/perl -w
use Test::More tests => 27;
use HTTP::Request::Common;
use Test::Differences;
use FindBin '$Bin';
use lib "$Bin/../lib";
use Data::Dumper;
 
my $original_formatter
  ; # used to save/restore whatever formatter is set up in mojomojo.db
my $c; # the Catalyst object of this live server
my $test; # test description
 
BEGIN {
    $ENV{CATALYST_CONFIG} = 't/var/mojomojo.yml';
    use_ok('MojoMojo::Formatter::Textile')
      and note(
'Comprehensive/chained test of formatters, with the main formatter set to Textile'
      );
    use_ok( 'Catalyst::Test', 'MojoMojo' );
}
 
END {
    ok( $c->pref( main_formatter => $original_formatter ),
        'restore original formatter' );
}
 
( undef, $c ) = ctx_request('/');
#warn Dumper $c->config;
ok( $original_formatter = $c->pref('main_formatter'),
    'save original formatter' );
 
ok( $c->pref( main_formatter => 'MojoMojo::Formatter::Textile' ),
    'set preferred formatter to Textile' );
 
my $content = '';
my $body = get( POST '/.jsrpc/render', [ content => $content ] );
is( $body, 'Please type something', 'empty body' );
 
#----------------------------------------------------------------------------
$test = 'headings';
 
#----------------------------------------------------------------------------
$content = <<'TEXTILE';
h1. Welcome to MojoMojo!
 
This is your front page. Create
a [[New Page]] or edit this one
through the edit link at the bottom.
 
h2. Need some assistance?
 
Check out our [[Help]] section.
TEXTILE
$body = get( POST '/.jsrpc/render', [ content => $content ] );
is( $body, <<'HTML', $test );
<h1>Welcome to MojoMojo!</h1>
 
<p>This is your front page. Create<br />
a <span class="newWikiWord"><a title="Not found. Click to create this page." href="/New_Page.edit">New Page?</a></span> or edit this one <br />
through the edit link at the bottom.</p>
 
<h2>Need some assistance?</h2>
 
<p>Check out our <a class="existingWikiWord" href="/help">Help</a> section.</p>
HTML
 
{
    $test = 'say Perl';
 
    $content = <<Perl;
<pre lang="Perl">
say "Hola Cabrón";
</pre>
Perl
 
    $expected = <<Perl;
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;say&nbsp;<span class="kateOperator">"</span><span class="kateString">Hola&nbsp;Cabrón</span><span class="kateOperator">"</span>;
</pre>
Perl
 
    # Now run through all formatters.
    $test .= ' - run through all formatters';
    $got = get( POST '/.jsrpc/render', [ content => $content ] );
    is( $got, $expected, $test );
}
 
$test = 'Test > in <pre lang="Perl"> section.';
 
# The behavior of this test is different from what appears on the page
# when browsing. > is maintained in the test while it's encoded as &gt; in the page.
# I don't see the encoding as un-desirable here.
$content = <<'TEXTILE';
<pre lang="Perl">
if (1 > 2) {
print "test";
}
</pre>
TEXTILE
$body = get( POST '/.jsrpc/render', [ content => $content ] );
is( $body, <<'HTML', $test );
<pre>
<b>if</b>&nbsp;(<span class="kateFloat">1</span>&nbsp;>&nbsp;<span class="kateFloat">2</span>)&nbsp;{
&nbsp;&nbsp;<span class="kateFunction">print</span>&nbsp;<span class="kateOperator">"</span><span class="kateString">test</span><span class="kateOperator">"</span>;
}
</pre>
HTML
 
#----------------------------------------------------------------------------
$test = 'Only a <pre> section - no attribute';
$content = <<'TEXTILE';
<pre>
Hopen, Norway
</pre>
TEXTILE
$body = get( POST '/.jsrpc/render', [ content => $content ] );
is( $body, $content, $test );
 
TODO: {
    local $TODO = 'something before a pre adds defang_lang attribute to pre.';
    # We'd like this test to pass, but our current setup with Defang
    # adds
    # defang_lang=""
    # attribute to <pre> when there is some content before a pre.
    # (even a pre itself)
    $test = 'pre tag - no attribute and some text before pre';
    $content = <<'HTML';
Jeg har familie i
<pre>
Hopen, Norway
</pre>
HTML
    $expected = $content;
    $body = get( POST '/.jsrpc/render', [ content => $content ] );
    eq_or_diff( $body, $expected, $test );
}
 
$test = 'pre tag - no attribute and some text after pre';
$content = <<'HTML';
<pre>
Hopen, Norway
</pre>
er et sted langt mot nord.
HTML
$expected = '<pre>
Hopen, Norway
</pre>
 
 
<p>er et sted langt mot nord.</p>
';
$body = get( POST '/.jsrpc/render', [ content => $content ] );
eq_or_diff( $body, $expected, $test );
 
 
#----------------------------------------------------------------------------
$test = "Have <pre> sections behave like normal pre sections. Don't do entities
on < and > so one can use <span> and such";
 
# The behavior of this test is different from what appears on the page
# when browsing. > is maintained in the test while it's encoded in the page.
$content = <<'TEXTILE';
<pre>
<span>
if (1 < 2) {
print "pre section & no lang specified";
}
</span>
</pre>
TEXTILE
$body = get( POST '/.jsrpc/render', [ content => $content ] );
is( $body, $content, $test );
 
#----------------------------------------------------------------------------
$test = 'Is <br /> preserved?';
 
# NOTE: Textile turns \n in to <br /> so you don't need or want to do
# blab
# <br /> blah because you'll end up with:
# blab
# <br /><br />blah
$content = <<'TEXTILE';
Roses are red<br />Violets are blue
TEXTILE
$body = get( POST '/.jsrpc/render', [ content => $content ] );
eq_or_diff( $body, <<'HTML', $test );
<p>Roses are red<br />Violets are blue</p>
HTML
 
# This test is asking for a bit much perhaps. Use <pre lang="code"> </pre> instead.
#----------------------------------------------------------------------------
$test = '<code> behave like normal wrt to <span> - Use textile escape ==';
$content = <<'TEXTILE';
==<code><span style="font-size: 1.5em;">alguna cosa</span></code>
==
TEXTILE
$body = get( POST '/.jsrpc/render', [ content => $content ] );
eq_or_diff( $body, <<'HTML', $test );
<code><span style="font-size: 1.5em;">alguna cosa</span></code>
HTML
 
# Check that @ transforms to <code>
#----------------------------------------------------------------------------
$test = '@word@ behavior';
$content = <<'TEXTILE';
@mot@
TEXTILE
$body = get( POST '/.jsrpc/render', [ content => $content ] );
eq_or_diff( $body, <<'HTML', $test );
<p><code>mot</code></p>
HTML
 
#----------------------------------------------------------------------------
$test = 'blockquotes';
 
#----------------------------------------------------------------------------
$content = <<'TEXTILE';
Below is a blockquote:
 
bq. quoted text
 
A quote is above.
TEXTILE
$body = get( POST '/.jsrpc/render', [ content => $content ] );
eq_or_diff( $body, <<'HTML', $test );
<p>Below is a blockquote:</p>
 
<blockquote><p>quoted text</p></blockquote>
 
<p>A quote is above.</p>
HTML
 
#----------------------------------------------------------------------------
$test = 'Handle # as first character in a line while using Perl highlight';
 
# TODO: This test demonstrates that Syntax Highlight is adding an empty span.
# Investigate further and clean it up.
$content = <<'TEXTILE';
<pre lang="Perl">
# comment
</pre>
TEXTILE
$body = get( POST '/.jsrpc/render', [ content => $content ] );
eq_or_diff( $body, <<'HTML', $test );
<pre>
<span class="kateComment"><i>#&nbsp;comment</i></span><span class="kateComment"><i>
</i></span></pre>
HTML
 
#----------------------------------------------------------------------------
$test = 'Simple html table tags. Use textile escape ==';
 
# NOTE: The opening escape string '==' turns into a \n when textile
# is applied. colgroup was moved as it confused Defang.
$content = <<'TEXTILE';
==<table>
<tr>
<th>Vegetable</th>
</tr>
<tr>
<td>Mr Potato</td>
</tr>
</table>
==
TEXTILE
 
$expected = <<'HTML';
<table>
<tr>
<th>Vegetable</th>
</tr>
<tr>
<td>Mr Potato</td>
</tr>
</table>
HTML
 
# We expect textile to leave this table as is, EXCPEPT for the escape lines (==).
$body = get( POST '/.jsrpc/render', [ content => $content ] );
is( $body, $expected, $test );
 
#----------------------------------------------------------------------------
$test = 'Maintain complete set of html table tags. Use textile escape ==';
 
# NOTE: The opening escape string '==' turns into a \n when textile
# is applied. colgroup was removed as it confused Defang.
$content = <<'TEXTILE';
==<table>
<caption>Vegetable Price List</caption>
<colgroup>
<col /><col align="char" char="." />
</colgroup>
<thead>
<tr>
<th>Vegetable</th>
<th>Cost per kilo</th>
</tr>
</thead>
<tbody>
<tr>
<td>Lettuce</td>
<td>$1</td>
</tr>
<tr>
<td>Silver carrots</td>
<td>$10.50</td>
</tr>
<tr>
<td>Golden turnips</td>
<td>$108.00</td>
</tr>
</tbody>
</table>
==
TEXTILE
 
$expected = <<'HTML';
<table>
<caption>Vegetable Price List</caption>
<colgroup>
<col /><col align="char" char="." />
</colgroup>
<thead>
<tr>
<th>Vegetable</th>
<th>Cost per kilo</th>
</tr>
</thead>
<tbody>
<tr>
<td>Lettuce</td>
<td>$1</td>
</tr>
<tr>
<td>Silver carrots</td>
<td>$10.50</td>
</tr>
<tr>
<td>Golden turnips</td>
<td>$108.00</td>
</tr>
</tbody>
</table>
HTML
 
# We expect textile to leave this table as is, EXCPEPT for the escape lines (==).
$body = get( POST '/.jsrpc/render', [ content => $content ] );
is( $body, $expected, $test );
 
#-------------------------------------------------------------------------------
$test = 'POD while Textile is the main formatter';
$content = <<'TEXTILE';
{{pod}}
 
=head1 NAME
 
Some POD here
 
=cut
 
{{end}}
TEXTILE
$body = get( POST '/.jsrpc/render', [ content => $content ] );
like( $body, qr'<h1><a.*NAME.*/h1>'s, "POD: there is an h1 NAME" );
 
{
    $test = 'Simple SQL SELECT';
 
    $content = <<SQL;
<pre lang="SQL">
select * from foo
</pre>
SQL
 
    $expected = <<SQL;
<pre>
<b>select</b>&nbsp;*&nbsp;<b>from</b>&nbsp;foo
</pre>
SQL
 
    $test .= ' - run through all formatters';
    $got = get( POST '/.jsrpc/render', [ content => $content ] );
    is( $got, $expected, $test );
 
}
 
{
    $test = 'Single <code>';
 
    $content = <<HTML;
<pre lang="HTML">
<code>
Ha en god dag
</code>
</pre>
HTML
 
    $expected = <<'HTML';
<pre>
<b>&lt;code&gt;</b>
Ha&nbsp;en&nbsp;god&nbsp;dag
<b>&lt;/code&gt;</b>
</pre>
HTML
 
    # Now run through all formatters.
    $test .= ' - run through all formatters';
    $got = get( POST '/.jsrpc/render', [ content => $content ] );
    is( $got, $expected, $test );
}
 
 
$test = 'img src http not allowed';
$content = <<'HTML';
<img src="http://malicious.com/foto.jpg" />
HTML
$expected = '<p><img defang_src="http://malicious.com/foto.jpg" /></p>
';
$got = get( POST '/.jsrpc/render', [ content => $content ] );
eq_or_diff( $got, $expected, $test );
 
$test = 'img src https not allowed';
$content = <<'HTML';
<img src="https://malicious.com/foto.jpg" />
HTML
$expected = '<p><img defang_src="https://malicious.com/foto.jpg" /></p>
';
$got = get( POST '/.jsrpc/render', [ content => $content ] );
eq_or_diff( $got, $expected, $test );
 
$test = 'img src with bypass protocol not allowed';
$content = <<'HTML';
<img src="//malicious.com/foto.jpg" />
HTML
$expected = '<p><img defang_src="//malicious.com/foto.jpg" /></p>
';
$got = get( POST '/.jsrpc/render', [ content => $content ] );
eq_or_diff( $got, $expected, $test );
 
$test = 'remote img src allowed in .conf';
$content = <<'HTML';
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/P_hTFilWY9w&amp;hl=en"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/P_hTFilWY9w&amp;hl=en" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
HTML
$expected = $content;
$got = get( POST '/.jsrpc/render', [ content => $content ] );
is( $got, $expected, $test );