aboutsummaryrefslogtreecommitdiffstats
path: root/tests/avatar/manager_test.php
blob: ec2cba3f53f7f967cdf00c14dfbe2891a8609a3f (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
<?php
/**
*
* @package testing
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

require_once dirname(__FILE__) . '/driver/foobar.php';

class phpbb_avatar_manager_test extends PHPUnit_Framework_TestCase
{
	public function setUp()
	{
		global $phpbb_root_path, $phpEx;

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

		// Prepare dependencies for avatar manager and driver
		$config = new phpbb_config(array());
		$request = $this->getMock('phpbb_request');
		$cache = $this->getMock('phpbb_cache_driver_interface');

		// $this->avatar_foobar will be needed later on
		$this->avatar_foobar = $this->getMock('phpbb_avatar_driver_foobar', array('get_name'), array($config, $phpbb_root_path, $phpEx, $cache));
		$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->getMock('phpbb_avatar_driver_barfoo', array('get_name'));
		$this->avatar_barfoo->expects($this->any())
			->method('get_name')
			->will($this->returnValue('avatar.driver.barfoo'));
		$avatar_drivers = array($this->avatar_foobar, $this->avatar_barfoo);

		foreach ($this->avatar_drivers() as $driver)
		{
			$cur_avatar = $this->getMock('phpbb_avatar_driver_' . $driver, array('get_name'), array($config, $phpbb_root_path, $phpEx, $cache));
			$cur_avatar->expects($this->any())
				->method('get_name')
				->will($this->returnValue('avatar.driver.' . $driver));
			$config['allow_avatar_' . get_class($cur_avatar)] = false;
			$avatar_drivers[] = $cur_avatar;
		}

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

		// Set up avatar manager
		$this->manager = new phpbb_avatar_manager($config, $avatar_drivers, $this->phpbb_container);
	}

	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.foo_wrong', null),
			array('avatar.driver.local', null),
			array(AVATAR_GALLERY, null),
			array(AVATAR_UPLOAD, null),
			array(AVATAR_REMOTE, null),
			array(AVATAR_GALLERY, 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);
	}

	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'),
			array(AVATAR_GALLERY, 'avatar.driver.local'),
		);
	}

	/**
	* @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);
	}

	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' => false),
		);

		$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'	=> '',
				),
				array(
					'avatar'			=> '',
					'avatar_type'		=> '',
					'avatar_width'		=> '',
					'avatar_height'		=> '',
				),
			),
			array(
				array(
					'group_avatar'			=> '',
					'group_avatar_type'		=> '',
					'group_avatar_width'	=> '',
					'group_avatar_height'	=> '',
				),
				array(
					'avatar'			=> '',
					'avatar_type'		=> '',
					'avatar_width'		=> '',
					'avatar_height'		=> '',
				),
			),
			array(
				array(),
				array(
					'avatar'			=> '',
					'avatar_type'		=> '',
					'avatar_width'		=> '',
					'avatar_height'		=> '',
				),
			),
			array(
				array(
					'foobar_avatar'			=> '',
					'foobar_avatar_type'	=> '',
					'foobar_avatar_width'	=> '',
					'foobar_avatar_height'	=> '',
				),
				array(
					'foobar_avatar'			=> '',
					'foobar_avatar_type'	=> '',
					'foobar_avatar_width'	=> '',
					'foobar_avatar_height'	=> '',
				),
			),
		);
	}

	/**
	* @dataProvider database_row_data
	*/
	public function test_clean_row(array $input, array $output)
	{
		$cleaned_row = array();

		$cleaned_row = phpbb_avatar_manager::clean_row($input);
		foreach ($output as $key => $null)
		{
			$this->assertArrayHasKey($key, $cleaned_row);
		}
	}

	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()
	{
		$user = $this->getMock('phpbb_user');
		$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'),
		)));
	}
}