aboutsummaryrefslogtreecommitdiffstats
path: root/tests/text_formatter/s9e/default_formatting_test.php
blob: 1aa4f0bc3a150a32e42547874e39c4efd35fdf76 (plain)
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
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

class phpbb_textformatter_s9e_default_formatting_test extends phpbb_test_case
{
	public function test_bbcode_code_lang_is_saved()
	{
		$container = $this->get_test_case_helpers()->set_s9e_services();
		$parser    = $container->get('text_formatter.parser');

		$original = '[code]...[/code][code=php]...[/code]';
		$expected = '<r><CODE><s>[code]</s>...<e>[/code]</e></CODE><CODE lang="php"><s>[code=php]</s>...<e>[/code]</e></CODE></r>';

		$this->assertXmlStringEqualsXmlString($expected, $parser->parse($original));
	}

	/**
	* @dataProvider get_default_formatting_tests
	*/
	public function test_default_formatting($original, $expected, $setup = null)
	{
		$fixture   = __DIR__ . '/fixtures/default_formatting.xml';
		$container = $this->get_test_case_helpers()->set_s9e_services(null, $fixture);

		$parser   = $container->get('text_formatter.parser');
		$renderer = $container->get('text_formatter.renderer');

		if (isset($setup))
		{
			call_user_func($setup, $container);
		}

		$parsed_text = $parser->parse($original);

		$this->assertSame($expected, $renderer->render($parsed_text));
	}

	public function get_default_formatting_tests()
	{
		return array(
			array(
				'[b]bold[/b]',
				'<span style="font-weight:bold">bold</span>'
			),
			array(
				'[u]underlined[/u]',
				'<span style="text-decoration:underline">underlined</span>'
			),
			array(
				'[i]italic[/i]',
				'<span style="font-style:italic">italic</span>'
			),
			array(
				'[color=#FF0000]colored[/color]',
				'<span style="color:#FF0000">colored</span>'
			),
			array(
				'[color=red]colored[/color]',
				'<span style="color:red">colored</span>'
			),
			array(
				'[size=75]smaller[/size]',
				'<span style="font-size: 75%; line-height: normal">smaller</span>'
			),
			array(
				'[quote]quoted[/quote]',
				'<blockquote class="uncited"><div>quoted</div></blockquote>'
			),
			array(
				'[quote="username"]quoted[/quote]',
				'<blockquote><div><cite>username wrote:</cite>quoted</div></blockquote>'
			),
			array(
				'[code]unparsed code[/code]',
				'<div class="codebox"><p>CODE: <a href="#" onclick="selectCode(this); return false;">Select all</a></p><pre><code>unparsed code</code></pre></div>'
			),
			array(
				'[list]no item[/list]',
				'<ul><li>no item</li></ul>'
			),
			array(
				'[*]unparsed',
				'[*]unparsed'
			),
			array(
				'[list][*]item[/list]',
				'<ul><li>item</li></ul>'
			),
			array(
				'[list][*]item[/*][/list]',
				'<ul><li>item</li></ul>'
			),
			array(
				'[list=1][*]item[/list]',
				'<ol style="list-style-type:decimal"><li>item</li></ol>'
			),
			array(
				'[list=a][*]item[/list]',
				'<ol style="list-style-type:lower-alpha"><li>item</li></ol>'
			),
			array(
				'[list=i][*]item[/list]',
				'<ol style="list-style-type:lower-roman"><li>item</li></ol>'
			),
			array(
				'[list=I][*]item[/list]',
				'<ol style="list-style-type:upper-roman"><li>item</li></ol>'
			),
			array(
				'[list=disc][*]item[/list]',
				'<ul style="list-style-type:disc"><li>item</li></ul>'
			),
			array(
				'[list=circle][*]item[/list]',
				'<ul style="list-style-type:circle"><li>item</li></ul>'
			),
			array(
				'[list=square][*]item[/list]',
				'<ul style="list-style-type:square"><li>item</li></ul>'
			),
			array(
				'[img]https://area51.phpbb.com/images/area51.png[/img]',
				'<img src="https://area51.phpbb.com/images/area51.png" class="postimage" alt="Image">'
			),
			array(
				'[url]https://area51.phpbb.com/[/url]',
				'<a href="https://area51.phpbb.com/" class="postlink">https://area51.phpbb.com/</a>'
			),
			array(
				'[url=https://area51.phpbb.com/]Area51[/url]',
				'<a href="https://area51.phpbb.com/" class="postlink">Area51</a>'
			),
			array(
				'[email]bbcode-test@phpbb.com[/email]',
				'<a href="mailto:bbcode-test@phpbb.com">bbcode-test@phpbb.com</a>'
			),
			array(
				'[email=bbcode-test@phpbb.com]Email[/email]',
				'<a href="mailto:bbcode-test@phpbb.com">Email</a>'
			),
			array(
				'[attachment=0]filename[/attachment]',
				'<div class="inline-attachment"><!-- ia0 -->filename<!-- ia0 --></div>'
			),
			array(
				// PHPBB3-1401 - correct: parsed
				'[quote="[test]test"]test [ test[/quote]',
				'<blockquote><div><cite>[test]test wrote:</cite>test [ test</div></blockquote>'
			),
			array(
				// PHPBB3-6117 - correct: parsed
				'[quote]test[/quote] test ] and [ test [quote]test[/quote]',
				'<blockquote class="uncited"><div>test</div></blockquote> test ] and [ test <blockquote class="uncited"><div>test</div></blockquote>'
			),
			array(
				// PHPBB3-6200 - correct: parsed
				'[quote="["]test[/quote]',
				'<blockquote><div><cite>[ wrote:</cite>test</div></blockquote>'
			),
			array(
				// PHPBB3-9364 - quoted: "test[/[/b]quote] test" / non-quoted: "[/quote] test" - also failed if layout distorted
				'[quote]test[/[/b]quote] test [/quote][/quote] test',
				'<blockquote class="uncited"><div>test[/[/b]quote] test </div></blockquote>[/quote] test'
			),
			array(
				// PHPBB3-8096 - first quote tag parsed, second quote tag unparsed
				'[quote="a"]a[/quote][quote="a]a[/quote]',
				'<blockquote><div><cite>a wrote:</cite>a</div></blockquote>[quote="a]a[/quote]'
			),
			array(
				// Allow textual bbcodes in textual bbcodes
				'[b]bold [i]bold + italic[/i][/b]',
				'<span style="font-weight:bold">bold <span style="font-style:italic">bold + italic</span></span>'
			),
			array(
				// Allow textual bbcodes in url with description
				'[url=https://area51.phpbb.com/]Area51 [i]italic[/i][/url]',
				'<a href="https://area51.phpbb.com/" class="postlink">Area51 <span style="font-style:italic">italic</span></a>'
			),
			array(
				// Allow url with description in textual bbcodes
				'[i]italic [url=https://area51.phpbb.com/]Area51[/url][/i]',
				'<span style="font-style:italic">italic <a href="https://area51.phpbb.com/" class="postlink">Area51</a></span>'
			),
			array(
				// Do not parse textual bbcodes in code
				'[code]unparsed code [b]bold [i]bold + italic[/i][/b][/code]',
				'<div class="codebox"><p>CODE: <a href="#" onclick="selectCode(this); return false;">Select all</a></p><pre><code>unparsed code [b]bold [i]bold + italic[/i][/b]</code></pre></div>'
			),
			array(
				// Do not parse quote bbcodes in code
				'[code]unparsed code [quote="username"]quoted[/quote][/code]',
				'<div class="codebox"><p>CODE: <a href="#" onclick="selectCode(this); return false;">Select all</a></p><pre><code>unparsed code [quote="username"]quoted[/quote]</code></pre></div>'
			),
			array(
				// Textual bbcode nesting into textual bbcode
				'[b]bold [i]bold + italic[/b] italic[/i]',
				'<span style="font-weight:bold">bold <span style="font-style:italic">bold + italic</span></span><span style="font-style:italic"> italic</span>'
			),
			array(
				"[code]\tline1\n  line2[/code]",
				'<div class="codebox"><p>CODE: <a href="#" onclick="selectCode(this); return false;">Select all</a></p><pre><code>' . "\tline1\n  line2</code></pre></div>"
			),
			array(
				"[code]\n\tline1\n  line2[/code]",
				'<div class="codebox"><p>CODE: <a href="#" onclick="selectCode(this); return false;">Select all</a></p><pre><code>' . "\tline1\n  line2</code></pre></div>"
			),
			array(
				'... http://example.org ...',
				'... <a href="http://example.org" class="postlink">http://example.org</a> ...'
			),
			array(
				'... www.example.org ...',
				'... <a href="http://www.example.org" class="postlink">www.example.org</a> ...'
			),
			array(
				// From make_clickable_test.php
				'www.phpbb.com/community/?',
				'<a href="http://www.phpbb.com/community/" class="postlink">www.phpbb.com/community/</a>?'
			),
			array(
				// From make_clickable_test.php
				'http://www.phpbb.com/community/path/to/long/url/file.ext#section',
				'<a href="http://www.phpbb.com/community/path/to/long/url/file.ext#section" class="postlink">http://www.phpbb.com/community/path/to/ ... xt#section</a>'
			),
			array(
				'http://localhost/ http://localhost/phpbb/ http://localhost/phpbb/viewforum.php?f=1',
				'<a href="http://localhost/" class="postlink">http://localhost/</a> <a href="http://localhost/phpbb/" class="postlink">http://localhost/phpbb/</a> <a href="http://localhost/phpbb/viewforum.php?f=1" class="postlink">viewforum.php?f=1</a>'
			),
			array(
				'http://localhost/phpbb/viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
				'<a href="http://localhost/phpbb/viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" class="postlink">viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxx ... xxxxxxxxxx</a>'
			),
			array(
				'[url]http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0[/url]',
				'<a href="http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0" class="postlink">http://example.org/0xxxxxxxxxxxxxxxxxxx ... xxxxxxxxx0</a>'
			),
			array(
				'[URL]http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0[/url]',
				'<a href="http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0" class="postlink">http://example.org/0xxxxxxxxxxxxxxxxxxx ... xxxxxxxxx0</a>'
			),
			array(
				'[url=http://example.org/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[/url]',
				'<a href="http://example.org/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" class="postlink">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</a>'
			),
			array(
				'[url=http://example.org/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]http://example.org/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[/url]',
				'<a href="http://example.org/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" class="postlink">http://example.org/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</a>'
			),
			array(
				'[quote="[url=http://example.org]xxx[/url]"]...[/quote]',
				'<blockquote><div><cite><a href="http://example.org" class="postlink">xxx</a> wrote:</cite>...</div></blockquote>'
			),
			array(
				'[quote="[url]http://example.org[/url]"]...[/quote]',
				'<blockquote><div><cite><a href="http://example.org" class="postlink">http://example.org</a> wrote:</cite>...</div></blockquote>'
			),
			array(
				'[quote=http://example.org]...[/quote]',
				'<blockquote><div><cite><a href="http://example.org" class="postlink">http://example.org</a> wrote:</cite>...</div></blockquote>'
			),
			array(
				"[quote]\nThis is a long quote that is definitely going to exceed 80 characters\n[/quote]\n\nFollowed by a reply",
				"<blockquote class=\"uncited\"><div>\nThis is a long quote that is definitely going to exceed 80 characters\n</div></blockquote>\n\nFollowed by a reply"
			),
			array(
				'[quote=Username post_id=123]...[/quote]',
				'<blockquote><div><cite>Username wrote: <a href="phpBB/viewtopic.php?p=123#p123" data-post-id="123" onclick="if(document.getElementById(hash.substr(1)))href=hash">↑</a></cite>...</div></blockquote>'
			),
			array(
				// Users are not allowed to submit their own URL for the post
				'[quote="Username" post_url="http://fake.example.org"]...[/quote]',
				'<blockquote><div><cite>Username wrote:</cite>...</div></blockquote>'
			),
			array(
				'[quote=Username time=58705871]...[/quote]',
				'<blockquote><div><cite>Username wrote:<div class="responsive-hide">1971-11-11 11:11:11</div></cite>...</div></blockquote>'
			),
			array(
				'[quote=Username user_id=123]...[/quote]',
				'<blockquote><div><cite><a href="phpBB/memberlist.php?mode=viewprofile&amp;u=123">Username</a> wrote:</cite>...</div></blockquote>'
			),
			array(
				// Users are not allowed to submit their own URL for the profile
				'[quote=Username profile_url=http://fake.example.org]...[/quote]',
				'<blockquote><div><cite>Username wrote:</cite>...</div></blockquote>'
			),
			array(
				// From phpbb_textformatter_s9e_utils_test::test_generate_quote()
				'[quote=\'[quote="foo"]\']...[/quote]',
				'<blockquote><div><cite>[quote="foo"] wrote:</cite>...</div></blockquote>'
			),
			array(
				"Emoji: \xF0\x9F\x98\x80",
				'Emoji: <img alt="' . "\xF0\x9F\x98\x80" . '" class="emoji smilies" draggable="false" src="//twemoji.maxcdn.com/2/svg/1f600.svg">'
			),
			array(
				"Emoji: \xF0\x9F\x98\x80",
				"Emoji: \xF0\x9F\x98\x80",
				function ($container)
				{
					$container->get('text_formatter.renderer')->set_viewsmilies(false);
				}
			),
		);
	}
}