aboutsummaryrefslogtreecommitdiffstats
path: root/tests/text_formatter/s9e/factory_test.php
blob: 0d780a19a947d27e44984dab10498228c8ea3dc2 (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
<?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.
*
*/

require_once __DIR__ . '/../../test_framework/phpbb_database_test_case.php';

class phpbb_textformatter_s9e_factory_test extends phpbb_database_test_case
{
	public function setUp()
	{
		$this->cache = new phpbb_mock_cache;
		$this->dispatcher = new phpbb_mock_event_dispatcher;
		parent::setUp();
	}

	public function getDataSet()
	{
		return $this->createXMLDataSet(__DIR__ . '/fixtures/factory.xml');
	}

	public function get_cache_dir()
	{
		return __DIR__ . '/../../tmp/';
	}

	public function get_factory($styles_path = null)
	{
		global $config, $phpbb_root_path, $request, $symfony_request, $user;

		if (!isset($styles_path))
		{
			$styles_path = $phpbb_root_path . 'styles/';
		}

		$this->cache = new phpbb_mock_cache;
		$dal = new \phpbb\textformatter\data_access(
			$this->new_dbal(),
			'phpbb_bbcodes',
			'phpbb_smilies',
			'phpbb_styles',
			'phpbb_words',
			$styles_path
		);
		$factory = new \phpbb\textformatter\s9e\factory(
			$dal,
			$this->cache,
			$this->dispatcher,
			new \phpbb\config\config(array('allowed_schemes_links' => 'http,https,ftp')),
			new \phpbb\textformatter\s9e\link_helper,
			$this->getMockBuilder('phpbb\\log\\log_interface')->getMock(),
			$this->get_cache_dir(),
			'_foo_parser',
			'_foo_renderer'
		);

		// Global objects required by generate_board_url()
		$config = new \phpbb\config\config(array(
			'script_path'           => '/phpbb',
			'server_name'           => 'localhost',
			'server_port'           => 80,
			'server_protocol'       => 'http://',
		));
		$request = new phpbb_mock_request;
		$symfony_request = new \phpbb\symfony_request($request);
		$user = new phpbb_mock_user;

		return $factory;
	}

	public function run_configurator_assertions($configurator)
	{
		$this->assertInstanceOf('s9e\\TextFormatter\\Configurator', $configurator);

		$this->assertTrue(isset($configurator->plugins['Autoemail']));
		$this->assertTrue(isset($configurator->plugins['Autolink']));

		$this->assertTrue(isset($configurator->BBCodes['B']));
		$this->assertTrue(isset($configurator->BBCodes['CODE']));
		$this->assertTrue(isset($configurator->BBCodes['COLOR']));
		$this->assertTrue(isset($configurator->BBCodes['EMAIL']));
		$this->assertTrue(isset($configurator->BBCodes['FLASH']));
		$this->assertTrue(isset($configurator->BBCodes['I']));
		$this->assertTrue(isset($configurator->BBCodes['IMG']));
		$this->assertTrue(isset($configurator->BBCodes['LIST']));
		$this->assertTrue(isset($configurator->BBCodes['*']));
		$this->assertTrue(isset($configurator->BBCodes['QUOTE']));
		$this->assertTrue(isset($configurator->BBCodes['SIZE']));
		$this->assertTrue(isset($configurator->BBCodes['U']));
		$this->assertTrue(isset($configurator->BBCodes['URL']));

		// This custom BBCode should be set
		$this->assertTrue(isset($configurator->BBCodes['CUSTOM']));

		$this->assertTrue(isset($configurator->Emoticons[':D']));
	}

	public function test_get_configurator()
	{
		$configurator = $this->get_factory()->get_configurator();
		$this->run_configurator_assertions($configurator);

		// Test with twigified bbcode.html
		$configurator = $this->get_factory(__DIR__ . '/fixtures/styles/')->get_configurator();
		$this->run_configurator_assertions($configurator);

	}

	public function test_regenerate()
	{
		extract($this->get_factory()->regenerate());

		$this->assertInstanceOf('s9e\\TextFormatter\\Parser', $parser);
		$this->assertInstanceOf('s9e\\TextFormatter\\Renderer', $renderer);

		$renderer_data = $this->cache->get('_foo_renderer');
		$this->assertEquals($parser, $this->cache->get('_foo_parser'), 'The parser was not cached');
		$this->assertEquals(get_class($renderer), $renderer_data['class']);
		$this->assertInstanceOf('s9e\\TextFormatter\\Plugins\\Censor\\Helper', $renderer_data['censor']);

		$file = $this->get_cache_dir() . get_class($renderer) . '.php';
		$this->assertFileExists($file);
		unlink($file);
	}

	public function test_tidy()
	{
		$factory = $this->get_factory();

		// Create a fake "old" cache file
		$old_file = $this->get_cache_dir() . 's9e_foo.php';
		touch($old_file);

		// Create a current renderer
		extract($factory->regenerate());
		$new_file = $this->get_cache_dir() . get_class($renderer) . '.php';

		// Tidy the cache
		$factory->tidy();

		$this->assertFileExists($new_file, 'The current renderer has been deleted');
		$this->assertFileNotExists($old_file, 'The old renderer has not been deleted');

		unlink($new_file);
	}

	public function test_local_url()
	{
		global $config, $user, $request, $symfony_request;
		$config = new \phpbb\config\config(array(
			'force_server_vars' => true,
			'server_protocol' => 'http://',
			'server_name' => 'path',
			'server_port' => 80,
			'script_path' => '/to',
			'cookie_secure' => false
		));
		$user = new phpbb_mock_user;
		$request = new phpbb_mock_request;
		$symfony_request = new \phpbb\symfony_request($request);

		$fixture = __DIR__ . '/fixtures/local_url.xml';
		$renderer = $this->get_test_case_helpers()->set_s9e_services(null, $fixture)->get('text_formatter.renderer');

		$this->assertSame(
			'<a href="http://path/to/foo">http://path/to/foo</a>',
			$renderer->render('<r><LOCAL content="foo"><s>[local]</s>foo<e>[/local]</e></LOCAL></r>')
		);
	}

	public function test_smilies_special_chars()
	{
		// Use a smiley that contains every special chars in every field
		$fixture = __DIR__ . '/fixtures/smilies_special_chars.xml';
		$renderer = $this->get_test_case_helpers()->set_s9e_services(null, $fixture)->get('text_formatter.renderer');

		$this->assertSame(
			'<img class="smilies" src="phpBB/images/smilies/%22%27%3C&amp;%3E.png" width="15" height="17" alt="&quot;\'&lt;&amp;&gt;" title="&quot;\'&lt;&amp;&gt;">',
			$renderer->render('<r><E>"\'&lt;&amp;&gt;</E></r>')
		);
	}

	public function test_duplicate_smilies()
	{
		$fixture = __DIR__ . '/fixtures/smilies_duplicate.xml';
		$parser = $this->get_test_case_helpers()->set_s9e_services(null, $fixture)->get('text_formatter.parser');

		$this->assertSame(
			'<r><E>:)</E></r>',
			$parser->parse(':)')
		);
	}

	/**
	* @testdox {INTTEXT} is supported in custom BBCodes
	*/
	public function test_inttext_token()
	{
		$fixture = __DIR__ . '/fixtures/inttext_token.xml';
		$container = $this->get_test_case_helpers()->set_s9e_services(null, $fixture);
		$parser = $container->get('text_formatter.parser');
		$renderer = $container->get('text_formatter.renderer');

		$original = '[spoiler=ɎɆS]text[/spoiler]';
		$expected = '<div class="spoiler"><div class="title">ɎɆS</div><div class="content">text</div></div>';
		$this->assertSame($expected, $renderer->render($parser->parse($original)));

		$original = '[spoiler=N:O:P:E]text[/spoiler]';
		$expected = $original;
		$this->assertSame($expected, $renderer->render($parser->parse($original)));
	}

	/**
	* @testdox Preserves comments in custom BBCodes
	*/
	public function test_preserve_comments()
	{
		$fixture = __DIR__ . '/fixtures/preserve_comments.xml';
		$container = $this->get_test_case_helpers()->set_s9e_services(null, $fixture);
		$parser = $container->get('text_formatter.parser');
		$renderer = $container->get('text_formatter.renderer');

		$original = '[X]';
		$expected = '<!-- comment -->';
		$this->assertSame($expected, $renderer->render($parser->parse($original)));
	}

	/**
	* @testdox Accepts unsafe custom BBCodes
	*/
	public function test_unsafe_bbcode()
	{
		$fixture = __DIR__ . '/fixtures/unsafe_bbcode.xml';
		$container = $this->get_test_case_helpers()->set_s9e_services(null, $fixture);
		$parser = $container->get('text_formatter.parser');
		$renderer = $container->get('text_formatter.renderer');

		$original = '[xss=javascript:alert(1)]text[/xss]';
		$expected = '<a href="javascript:alert(1)">text</a>';
		$this->assertSame($expected, $renderer->render($parser->parse($original)));
	}

	/**
	* @testdox Accepts unsafe default BBCodes
	*/
	public function test_unsafe_default_bbcodes()
	{
		$fixture   = __DIR__ . '/fixtures/unsafe_default_bbcodes.xml';
		$style_dir = __DIR__ . '/fixtures/styles/';
		$container = $this->get_test_case_helpers()->set_s9e_services(null, $fixture, $style_dir);
		$parser    = $container->get('text_formatter.parser');
		$renderer  = $container->get('text_formatter.renderer');

		$original = '[b]alert(1)[/b]';
		$expected = '<script>alert(1)</script>';
		$this->assertSame($expected, $renderer->render($parser->parse($original)));
	}

	/**
	* @testdox Logs malformed BBCodes
	*/
	public function test_malformed_bbcodes()
	{
		$log = $this->getMockBuilder('phpbb\\log\\log_interface')->getMock();
		$log->expects($this->once())
			->method('add')
			->with('critical', null, null, 'LOG_BBCODE_CONFIGURATION_ERROR', false, ['[x !x]{TEXT}[/x]', 'Cannot interpret the BBCode definition']);

		$container = new phpbb_mock_container_builder;
		$container->set('log', $log);

		$fixture   = __DIR__ . '/fixtures/malformed_bbcode.xml';
		$this->get_test_case_helpers()->set_s9e_services($container, $fixture);
	}

	/**
	* @testdox get_configurator() triggers events before and after configuration
	*/
	public function test_configure_events()
	{
		$this->dispatcher = $this->getMock('phpbb\\event\\dispatcher_interface');
		$this->dispatcher
			->expects($this->at(0))
			->method('trigger_event')
			->with(
				'core.text_formatter_s9e_configure_before',
				$this->callback(array($this, 'configure_event_callback'))
			)
			->will($this->returnArgument(1));
		$this->dispatcher
			->expects($this->at(1))
			->method('trigger_event')
			->with(
				'core.text_formatter_s9e_configure_after',
				$this->callback(array($this, 'configure_event_callback'))
			)
			->will($this->returnArgument(1));

		$this->get_factory()->get_configurator();
	}

	public function configure_event_callback($vars)
	{
		return isset($vars['configurator']) && $vars['configurator'] instanceof \s9e\TextFormatter\Configurator;
	}
}