aboutsummaryrefslogtreecommitdiffstats
path: root/tests/avatar/manager_test.php
blob: 68636f2532fddffa4d6968c025ca17f0efbe3a7d (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
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
449
450
451
452
<?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 dirname(__FILE__) . '/driver/foobar.php';

class phpbb_avatar_manager_test extends \phpbb_database_test_case
{
	/** @var \phpbb\avatar\manager */
	protected $manager;
	protected $avatar_foobar;
	protected $avatar_barfoo;

	public function getDataSet()
	{
		return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/users.xml');
	}

	public function setUp()
	{
		global $phpbb_root_path, $phpEx;

		// Mock phpbb_container
		$phpbb_container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
		$phpbb_container->expects($this->any())
			->method('get')
			->will($this->returnArgument(0));

		$filesystem = new \phpbb\filesystem\filesystem();

		// Prepare dependencies for avatar manager and driver
		$this->config = new \phpbb\config\config(array());
		$cache = $this->createMock('\phpbb\cache\driver\driver_interface');
		$path_helper =  new \phpbb\path_helper(
			new \phpbb\symfony_request(
				new phpbb_mock_request()
			),
			$filesystem,
			$this->createMock('\phpbb\request\request'),
			$phpbb_root_path,
			$phpEx
		);

		$guessers = array(
			new \Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser(),
			new \Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser(),
			new \phpbb\mimetype\extension_guesser,
			new \phpbb\mimetype\content_guesser,
		);
		$guesser = new \phpbb\mimetype\guesser($guessers);
		$imagesize = new \FastImageSize\FastImageSize();

		$dispatcher = new phpbb_mock_event_dispatcher();

		// $this->avatar_foobar will be needed later on
		$this->avatar_foobar = $this->getMockBuilder('\phpbb\avatar\driver\foobar')
			->setMethods(array('get_name'))
			->setConstructorArgs(array($this->config, $imagesize, $phpbb_root_path, $phpEx, $path_helper, $cache))
			->getMock();
		$this->avatar_foobar->expects($this->any())
			->method('get_name')
			->will($this->returnValue('avatar.driver.foobar'));
		// barfoo driver can't be mocked with constructor arguments
		$this->avatar_barfoo = $this->getMockBuilder('\phpbb\avatar\driver\barfoo')
			->setMethods(array('get_name', 'get_config_name'))
			->getMock();
		$this->avatar_barfoo->expects($this->any())
			->method('get_name')
			->will($this->returnValue('avatar.driver.barfoo'));
		$this->avatar_barfoo->expects($this->any())
			->method('get_config_name')
			->will($this->returnValue('barfoo'));
		$avatar_drivers = array($this->avatar_foobar, $this->avatar_barfoo);

		$files_factory = new \phpbb\files\factory($phpbb_container);

		foreach ($this->avatar_drivers() as $driver)
		{
			if ($driver !== 'upload')
			{
				$cur_avatar = $this->getMockBuilder('\phpbb\avatar\driver\\' . $driver)
					->setMethods(array('get_name'))
					->setConstructorArgs(array($this->config, $imagesize, $phpbb_root_path, $phpEx, $path_helper, $cache))
					->getMock();
			}
			else
			{
				$cur_avatar = $this->getMockBuilder('\phpbb\avatar\driver\\' . $driver)
				->setMethods(array('get_name'))
				->setConstructorArgs(array($this->config, $phpbb_root_path, $phpEx, $filesystem, $path_helper, $dispatcher, $files_factory, $cache))
				->getMock();
			}
			$cur_avatar->expects($this->any())
				->method('get_name')
				->will($this->returnValue('avatar.driver.' . $driver));
			$this->config['allow_avatar_' . get_class($cur_avatar)] = $driver == 'gravatar';
			$avatar_drivers[] = $cur_avatar;
		}

		$this->config['allow_avatar_' . get_class($this->avatar_foobar)] = true;
		$this->config['allow_avatar_' . get_class($this->avatar_barfoo)] = false;

		// Set up avatar manager
		$this->manager = new \phpbb\avatar\manager($this->config, $dispatcher, $avatar_drivers);
		$this->db = $this->new_dbal();
		$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
		$lang = new \phpbb\language\language($lang_loader);
		$this->user = new \phpbb\user($lang, '\phpbb\datetime');
	}

	protected function avatar_drivers()
	{
		return array(
			'local',
			'upload',
			'remote',
			'gravatar',
		);
	}

	public function test_get_all_drivers()
	{
		$drivers = $this->manager->get_all_drivers();
		$this->assertEquals(array(
			'avatar.driver.barfoo' => 'avatar.driver.barfoo',
			'avatar.driver.foobar' => 'avatar.driver.foobar',
			'avatar.driver.local' => 'avatar.driver.local',
			'avatar.driver.remote' => 'avatar.driver.remote',
			'avatar.driver.upload' => 'avatar.driver.upload',
			'avatar.driver.gravatar' => 'avatar.driver.gravatar',
		), $drivers);
	}

	public function test_get_enabled_drivers()
	{
		$drivers = $this->manager->get_enabled_drivers();
		$this->assertArrayHasKey('avatar.driver.foobar', $drivers);
		$this->assertArrayNotHasKey('avatar.driver.barfoo', $drivers);
		$this->assertEquals('avatar.driver.foobar', $drivers['avatar.driver.foobar']);
	}

	public function get_driver_data_enabled()
	{
		return array(
			array('avatar.driver.foobar', 'avatar.driver.foobar'),
			array('avatar.driver.gravatar', 'avatar.driver.gravatar'),
			array('avatar.driver.foo_wrong', null),
			array('avatar.driver.local', null),
			array(AVATAR_GALLERY, null),
			array(AVATAR_UPLOAD, null),
			array(AVATAR_REMOTE, null),
		);
	}

	/**
	* @dataProvider get_driver_data_enabled
	*/
	public function test_get_driver_enabled($driver_name, $expected)
	{
		$driver = $this->manager->get_driver($driver_name);
		$this->assertEquals($expected, ($driver === null) ? null : $driver->get_name());
	}

	public function get_driver_data_all()
	{
		return array(
			array('avatar.driver.foobar', 'avatar.driver.foobar'),
			array('avatar.driver.foo_wrong', null),
			array('avatar.driver.local', 'avatar.driver.local'),
			array(AVATAR_GALLERY, 'avatar.driver.local'),
			array(AVATAR_UPLOAD, 'avatar.driver.upload'),
			array(AVATAR_REMOTE, 'avatar.driver.remote'),
		);
	}

	/**
	* @dataProvider get_driver_data_all
	*/
	public function test_get_driver_all($driver_name, $expected)
	{
		$driver = $this->manager->get_driver($driver_name, false);
		$this->assertEquals($expected, ($driver === null) ? $driver : $driver->get_name());
	}

	public function test_get_avatar_settings()
	{
		$avatar_settings = $this->manager->get_avatar_settings($this->avatar_foobar);

		$expected_settings = array(
			'allow_avatar_' . get_class($this->avatar_foobar)	=> array('lang' => 'ALLOW_' . strtoupper(get_class($this->avatar_foobar)), 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
		);

		$this->assertEquals($expected_settings, $avatar_settings);
	}

	public function database_row_data()
	{
		return array(
			array(
				array(
					'user_avatar'		=> '',
					'user_avatar_type'	=> '',
					'user_avatar_width'	=> '',
					'user_avatar_height'	=> '',
					'group_avatar'		=> '',
				),
				array(
					'user_avatar'		=> '',
					'user_avatar_type'	=> '',
					'user_avatar_width'	=> '',
					'user_avatar_height'	=> '',
					'group_avatar'		=> '',
				),
				'foobar',
			),
			array(
				array(),
				array(
					'avatar'			=> '',
					'avatar_type'		=> '',
					'avatar_width'		=> 0,
					'avatar_height'		=> 0,
				),
			),
			array(
				array(
					'user_avatar'	=> '',
					'user_id'	=> 5,
					'group_id'	=> 4,
				),
				array(
					'user_avatar'	=> '',
					'user_id'	=> 5,
					'group_id'	=> 4,
				),
			),
			array(
				array(
					'user_avatar'	=> '',
					'user_id'	=> 5,
					'group_id'	=> 4,
				),
				array(
					'avatar'	=> '',
					'id'		=> 5,
					'group_id'	=> 4,
				),
				'user',
			),
			array(
				array(
					'group_avatar'	=> '',
					'user_id'	=> 5,
					'group_id'	=> 4,
				),
				array(
					'avatar'	=> '',
					'id'		=> 'g4',
					'user_id'	=> 5,
				),
				'group',
			),
		);
	}

	/**
	* @dataProvider database_row_data
	*/
	public function test_clean_row(array $input, array $output, $prefix = '')
	{
		$cleaned_row = \phpbb\avatar\manager::clean_row($input, $prefix);
		foreach ($output as $key => $value)
		{
			$this->assertArrayHasKey($key, $cleaned_row);
			$this->assertEquals($cleaned_row[$key], $value);
		}
	}

	public function test_clean_driver_name()
	{
		$this->assertEquals('avatar.driver.local', $this->manager->clean_driver_name('avatar_driver_local'));
	}

	public function test_prepare_driver_name()
	{
		$this->assertEquals('avatar_driver_local', $this->manager->prepare_driver_name('avatar.driver.local'));
	}

	public function test_localize_errors()
	{
		global $phpbb_root_path, $phpEx;

		$user = $this->getMockBuilder('\phpbb\user')
			->setMethods(array())
			->setConstructorArgs(array(new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)), '\phpbb\datetime'))
			->getMock();
		$lang_array = array(
			array('FOOBAR_OFF', 'foobar_off'),
			array('FOOBAR_EXPLAIN', 'FOOBAR_EXPLAIN %s'),
		);
		$user->expects($this->any())
			->method('lang')
			->will($this->returnValueMap($lang_array));

		// Pass error as string
		$this->assertEquals(array('foobar_off'), $this->manager->localize_errors($user, array('FOOBAR_OFF')));

		// Pass error as array for vsprintf()
		$this->assertEquals(array('FOOBAR_EXPLAIN foo'), $this->manager->localize_errors($user, array(array('FOOBAR_EXPLAIN', 'foo'))));

		// Pass both types
		$this->assertEquals(array('foobar_off', 'FOOBAR_EXPLAIN foo'), $this->manager->localize_errors($user, array(
			'FOOBAR_OFF',
			array('FOOBAR_EXPLAIN', 'foo'),
		)));
	}

	public function data_handle_avatar_delete()
	{
		return array(
			array(
				array(
					'avatar'		=> '',
					'avatar_type'	=> '',
					'avatar_width'	=> 0,
					'avatar_height'	=> 0,
				), 1, array(
					'avatar'		=> 'foobar@example.com',
					'avatar_type'	=> 'avatar.driver.gravatar',
					'avatar_width'	=> '16',
					'avatar_height'	=> '16',
				), USERS_TABLE, 'user_',
			),
			array(
				array(
					'avatar'		=> '',
					'avatar_type'	=> '',
					'avatar_width'	=> 0,
					'avatar_height'	=> 0,
				), 5, array(
					'avatar'		=> 'g5_1414350991.jpg',
					'avatar_type'	=> 'avatar.driver.upload',
					'avatar_width'	=> '80',
					'avatar_height'	=> '80'
				), GROUPS_TABLE, 'group_',
			),
		);
	}

	/**
	* @dataProvider data_handle_avatar_delete
	*/
	public function test_handle_avatar_delete($expected, $id, $avatar_data, $table, $prefix)
	{
		$this->config['allow_avatar_gravatar'] = true;
		$this->assertNull($this->manager->handle_avatar_delete($this->db, $this->user, $avatar_data, $table, $prefix));

		$sql = 'SELECT * FROM ' . $table . '
				WHERE ' . $prefix . 'id = ' . $id;
		$result = $this->db->sql_query_limit($sql, 1);

		$row = $this->manager->clean_row($this->db->sql_fetchrow($result), substr($prefix, 0, -1));
		$this->db->sql_freeresult($result);

		foreach ($expected as $key => $value)
		{
			$this->assertEquals($value, $row[$key]);
		}
	}

	/**
	 * @dependsOn test_handle_avatar_delete
	 */
	public function test_user_group_avatar_deleted()
	{
		$sql = 'SELECT * FROM ' . USERS_TABLE . '
				WHERE user_id = 3';
		$result = $this->db->sql_query_limit($sql, 1);
		$row = $this->manager->clean_row($this->db->sql_fetchrow($result), 'user');
		$this->db->sql_freeresult($result);

		$this->assertEquals(array(
			'avatar'		=> '',
			'avatar_type'	=> '',
			'avatar_width'	=> 0,
			'avatar_height'	=> 0,
		), $row);
	}

	public function data_remote_avatar_url()
	{
		return array(
			array('127.0.0.1:91?foo.jpg', 80, 80, array('AVATAR_URL_INVALID')),
			array(gethostbyname('secure.gravatar.com') . '/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', 80, 80, array('AVATAR_URL_INVALID')),
			array('secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', 80, 80),
			array(gethostbyname('secure.gravatar.com') . ':120/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', 80, 80, array('AVATAR_URL_INVALID')),
			array('secure.gravatar.com:80/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', 80, 80, array('AVATAR_URL_INVALID')),
			array('secure.gravatar.com:80?55502f40dc8b7c769880b10874abc9d0.jpg', 80, 80, array('AVATAR_URL_INVALID')),
			array('secure.gravatar.com?55502f40dc8b7c769880b10874abc9d0.jpg', 80, 80, array('AVATAR_URL_INVALID')), // should be a 404
			array('2001:db8:0:0:0:0:2:1/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', 80, 80, array('AVATAR_URL_INVALID')),
			array('secure.gravatar.com/2001:db8:0:0:0:0:2:1/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', 80, 80, array('AVATAR_URL_INVALID')),
			array('secure.gravatar.com/127.0.0.1:80/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', 80, 80, array('AVATAR_URL_INVALID')),
		);
	}

	/**
	 * @dataProvider data_remote_avatar_url
	 */
	public function test_remote_avatar_url($url, $width, $height, $expected_error = array())
	{
		global $phpbb_root_path, $phpEx;

		if (!function_exists('get_preg_expression'))
		{
			require($phpbb_root_path . 'includes/functions.' . $phpEx);
		}

		$this->config['server_name'] = 'foobar.com';

		/** @var \phpbb\avatar\driver\remote $remote_avatar */
		$remote_avatar = $this->manager->get_driver('avatar.driver.remote', false);

		$request = new phpbb_mock_request(array(), array(
			'avatar_remote_url'		=> $url,
			'avatar_remote_width'	=> $width,
			'avatar_remote_height'	=> $height,
		));

		$row = array();
		$error = array();

		$return = $remote_avatar->process_form($request, null, $this->user, $row, $error);
		if (count($expected_error) > 0)
		{
			$this->assertFalse($return);
		}
		else
		{
			$this->assertNotEquals(false, $return);
		}
		$this->assertSame($expected_error, $error);
	}
}