Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
57.77% |
145 / 251 |
|
77.78% |
14 / 18 |
CRAP | |
0.00% |
0 / 1 |
| phpbb_avatar_manager_test | |
57.77% |
145 / 251 |
|
77.78% |
14 / 18 |
72.07 | |
0.00% |
0 / 1 |
| getDataSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setUp | |
100.00% |
67 / 67 |
|
100.00% |
1 / 1 |
3 | |||
| avatar_drivers | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| test_get_all_drivers | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| test_get_enabled_drivers | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| get_driver_data_enabled | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| test_get_driver_enabled | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| get_driver_data_all | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| test_get_driver_all | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| test_get_avatar_settings | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| database_row_data | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
2 | |||
| test_clean_row | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| test_clean_driver_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| test_prepare_driver_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| test_localize_errors | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
1 | |||
| data_handle_avatar_delete | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
2 | |||
| test_handle_avatar_delete | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| test_user_group_avatar_deleted | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | require_once __DIR__ . '/driver/foobar.php'; |
| 15 | require_once __DIR__ . '/driver/barfoo.php'; |
| 16 | |
| 17 | class phpbb_avatar_manager_test extends \phpbb_database_test_case |
| 18 | { |
| 19 | /** @var \phpbb\avatar\manager */ |
| 20 | protected $manager; |
| 21 | protected $avatar_foobar; |
| 22 | protected $avatar_barfoo; |
| 23 | protected $config; |
| 24 | protected $db; |
| 25 | protected $user; |
| 26 | |
| 27 | public function getDataSet() |
| 28 | { |
| 29 | return $this->createXMLDataSet(__DIR__ . '/fixtures/users.xml'); |
| 30 | } |
| 31 | |
| 32 | protected function setUp(): void |
| 33 | { |
| 34 | global $phpbb_dispatcher, $phpbb_root_path, $phpEx; |
| 35 | |
| 36 | // Mock phpbb_container |
| 37 | $phpbb_container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
| 38 | $phpbb_container->expects($this->any()) |
| 39 | ->method('get') |
| 40 | ->will($this->returnArgument(0)); |
| 41 | |
| 42 | $storage = $this->createMock('\phpbb\storage\storage'); |
| 43 | |
| 44 | // Prepare dependencies for avatar manager and driver |
| 45 | $this->config = new \phpbb\config\config(array()); |
| 46 | $cache = $this->createMock('\phpbb\cache\driver\driver_interface'); |
| 47 | $path_helper = new \phpbb\path_helper( |
| 48 | new \phpbb\symfony_request( |
| 49 | new phpbb_mock_request() |
| 50 | ), |
| 51 | $this->createMock('\phpbb\request\request'), |
| 52 | $phpbb_root_path, |
| 53 | $phpEx |
| 54 | ); |
| 55 | |
| 56 | $imagesize = $this->getMockBuilder('\FastImageSize\FastImageSize') |
| 57 | ->onlyMethods(['getImageSize']) |
| 58 | ->getMock(); |
| 59 | $imagesize->method('getImageSize') |
| 60 | ->willReturn(['width' => 80, 'height' => 80, 'mime' => 'image/jpeg']); |
| 61 | |
| 62 | $dispatcher = new phpbb_mock_event_dispatcher(); |
| 63 | $phpbb_dispatcher = $dispatcher; |
| 64 | |
| 65 | $controller_helper = $this->createMock('\phpbb\controller\helper'); |
| 66 | $routing_helper = $this->createMock('\phpbb\routing\helper'); |
| 67 | |
| 68 | // $this->avatar_foobar will be needed later on |
| 69 | $this->avatar_foobar = $this->getMockBuilder('\phpbb\avatar\driver\foobar') |
| 70 | ->onlyMethods(array('get_name')) |
| 71 | ->setConstructorArgs(array($this->config, $imagesize, $phpbb_root_path, $phpEx, $path_helper, $cache)) |
| 72 | ->getMock(); |
| 73 | $this->avatar_foobar->expects($this->any()) |
| 74 | ->method('get_name') |
| 75 | ->will($this->returnValue('avatar.driver.foobar')); |
| 76 | $this->avatar_barfoo = $this->getMockBuilder('\phpbb\avatar\driver\barfoo') |
| 77 | ->onlyMethods(array('get_name', 'get_config_name')) |
| 78 | ->setConstructorArgs(array($this->config, $imagesize, $phpbb_root_path, $phpEx, $path_helper, $cache)) |
| 79 | ->getMock(); |
| 80 | $this->avatar_barfoo->expects($this->any()) |
| 81 | ->method('get_name') |
| 82 | ->will($this->returnValue('avatar.driver.barfoo')); |
| 83 | $this->avatar_barfoo->expects($this->any()) |
| 84 | ->method('get_config_name') |
| 85 | ->will($this->returnValue('barfoo')); |
| 86 | $avatar_drivers = array($this->avatar_foobar, $this->avatar_barfoo); |
| 87 | |
| 88 | $files_factory = new \phpbb\files\factory($phpbb_container); |
| 89 | |
| 90 | $php_ini = new \bantu\IniGetWrapper\IniGetWrapper; |
| 91 | |
| 92 | foreach ($this->avatar_drivers() as $driver) |
| 93 | { |
| 94 | if ($driver !== 'upload') |
| 95 | { |
| 96 | $cur_avatar = $this->getMockBuilder('\phpbb\avatar\driver\\' . $driver) |
| 97 | ->onlyMethods(array('get_name')) |
| 98 | ->setConstructorArgs(array($this->config, $imagesize, $phpbb_root_path, $phpEx, $path_helper, $cache)) |
| 99 | ->getMock(); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | $cur_avatar = $this->getMockBuilder('\phpbb\avatar\driver\\' . $driver) |
| 104 | ->onlyMethods(array('get_name')) |
| 105 | ->setConstructorArgs(array($this->config, $phpbb_root_path, $phpEx, $storage, $path_helper, $routing_helper, $dispatcher, $files_factory, $php_ini)) |
| 106 | ->getMock(); |
| 107 | } |
| 108 | $cur_avatar->expects($this->any()) |
| 109 | ->method('get_name') |
| 110 | ->will($this->returnValue('avatar.driver.' . $driver)); |
| 111 | $this->config['allow_avatar_' . get_class($cur_avatar)] = $driver == 'gravatar'; |
| 112 | $avatar_drivers[] = $cur_avatar; |
| 113 | } |
| 114 | |
| 115 | $this->config['allow_avatar_' . get_class($this->avatar_foobar)] = true; |
| 116 | $this->config['allow_avatar_' . get_class($this->avatar_barfoo)] = false; |
| 117 | |
| 118 | // Set up avatar manager |
| 119 | $this->manager = new \phpbb\avatar\manager($this->config, $dispatcher, $avatar_drivers); |
| 120 | $this->db = $this->new_dbal(); |
| 121 | $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); |
| 122 | $lang = new \phpbb\language\language($lang_loader); |
| 123 | $this->user = new \phpbb\user($lang, '\phpbb\datetime'); |
| 124 | } |
| 125 | |
| 126 | protected function avatar_drivers() |
| 127 | { |
| 128 | return array( |
| 129 | 'local', |
| 130 | 'upload', |
| 131 | 'gravatar', |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | public function test_get_all_drivers() |
| 136 | { |
| 137 | $drivers = $this->manager->get_all_drivers(); |
| 138 | $this->assertEquals(array( |
| 139 | 'avatar.driver.barfoo' => 'avatar.driver.barfoo', |
| 140 | 'avatar.driver.foobar' => 'avatar.driver.foobar', |
| 141 | 'avatar.driver.local' => 'avatar.driver.local', |
| 142 | 'avatar.driver.upload' => 'avatar.driver.upload', |
| 143 | 'avatar.driver.gravatar' => 'avatar.driver.gravatar', |
| 144 | ), $drivers); |
| 145 | } |
| 146 | |
| 147 | public function test_get_enabled_drivers() |
| 148 | { |
| 149 | $drivers = $this->manager->get_enabled_drivers(); |
| 150 | $this->assertArrayHasKey('avatar.driver.foobar', $drivers); |
| 151 | $this->assertArrayNotHasKey('avatar.driver.barfoo', $drivers); |
| 152 | $this->assertEquals('avatar.driver.foobar', $drivers['avatar.driver.foobar']); |
| 153 | } |
| 154 | |
| 155 | public static function get_driver_data_enabled() |
| 156 | { |
| 157 | return array( |
| 158 | array('avatar.driver.foobar', 'avatar.driver.foobar'), |
| 159 | array('avatar.driver.gravatar', 'avatar.driver.gravatar'), |
| 160 | array('avatar.driver.foo_wrong', null), |
| 161 | array('avatar.driver.local', null), |
| 162 | array(AVATAR_GALLERY, null), |
| 163 | array(AVATAR_UPLOAD, null), |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @dataProvider get_driver_data_enabled |
| 169 | */ |
| 170 | public function test_get_driver_enabled($driver_name, $expected) |
| 171 | { |
| 172 | $driver = $this->manager->get_driver($driver_name); |
| 173 | $this->assertEquals($expected, ($driver === null) ? null : $driver->get_name()); |
| 174 | } |
| 175 | |
| 176 | public static function get_driver_data_all() |
| 177 | { |
| 178 | return array( |
| 179 | array('avatar.driver.foobar', 'avatar.driver.foobar'), |
| 180 | array('avatar.driver.foo_wrong', null), |
| 181 | array('avatar.driver.local', 'avatar.driver.local'), |
| 182 | array(AVATAR_GALLERY, 'avatar.driver.local'), |
| 183 | array(AVATAR_UPLOAD, 'avatar.driver.upload'), |
| 184 | ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @dataProvider get_driver_data_all |
| 189 | */ |
| 190 | public function test_get_driver_all($driver_name, $expected) |
| 191 | { |
| 192 | $driver = $this->manager->get_driver($driver_name, false); |
| 193 | $this->assertEquals($expected, ($driver === null) ? $driver : $driver->get_name()); |
| 194 | } |
| 195 | |
| 196 | public function test_get_avatar_settings() |
| 197 | { |
| 198 | $avatar_settings = $this->manager->get_avatar_settings($this->avatar_foobar); |
| 199 | |
| 200 | $expected_settings = [ |
| 201 | 'allow_avatar_' . get_class($this->avatar_foobar) => [ |
| 202 | 'lang' => 'ALLOW_' . strtoupper(get_class($this->avatar_foobar)), |
| 203 | 'validate' => 'bool', |
| 204 | 'type' => 'radio:yes_no', |
| 205 | 'explain' => true |
| 206 | ], |
| 207 | ]; |
| 208 | |
| 209 | $this->assertEquals($expected_settings, $avatar_settings); |
| 210 | } |
| 211 | |
| 212 | public static function database_row_data() |
| 213 | { |
| 214 | return array( |
| 215 | array( |
| 216 | array( |
| 217 | 'user_avatar' => '', |
| 218 | 'user_avatar_type' => '', |
| 219 | 'user_avatar_width' => '', |
| 220 | 'user_avatar_height' => '', |
| 221 | 'group_avatar' => '', |
| 222 | ), |
| 223 | array(), |
| 224 | 'foobar', |
| 225 | ), |
| 226 | array( |
| 227 | array(), |
| 228 | array( |
| 229 | 'avatar' => '', |
| 230 | 'avatar_type' => '', |
| 231 | 'avatar_width' => 0, |
| 232 | 'avatar_height' => 0, |
| 233 | ), |
| 234 | ), |
| 235 | array( |
| 236 | array( |
| 237 | 'user_avatar' => '', |
| 238 | 'user_id' => 5, |
| 239 | 'group_id' => 4, |
| 240 | ), |
| 241 | array( |
| 242 | 'user_id' => 5, |
| 243 | 'group_id' => 4, |
| 244 | ), |
| 245 | ), |
| 246 | array( |
| 247 | array( |
| 248 | 'user_avatar' => '', |
| 249 | 'user_id' => 5, |
| 250 | 'group_id' => 4, |
| 251 | ), |
| 252 | array( |
| 253 | 'avatar' => '', |
| 254 | 'id' => 5, |
| 255 | 'group_id' => 4, |
| 256 | ), |
| 257 | 'user', |
| 258 | ), |
| 259 | array( |
| 260 | array( |
| 261 | 'group_avatar' => '', |
| 262 | 'user_id' => 5, |
| 263 | 'group_id' => 4, |
| 264 | ), |
| 265 | array( |
| 266 | 'avatar' => '', |
| 267 | 'id' => 'g4', |
| 268 | 'user_id' => 5, |
| 269 | ), |
| 270 | 'group', |
| 271 | ), |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @dataProvider database_row_data |
| 277 | */ |
| 278 | public function test_clean_row(array $input, array $output, $prefix = '') |
| 279 | { |
| 280 | $cleaned_row = \phpbb\avatar\manager::clean_row($input, $prefix); |
| 281 | foreach ($output as $key => $value) |
| 282 | { |
| 283 | $this->assertArrayHasKey($key, $cleaned_row); |
| 284 | $this->assertEquals($cleaned_row[$key], $value); |
| 285 | } |
| 286 | |
| 287 | if (empty($output)) |
| 288 | { |
| 289 | $this->assertEquals($output, $cleaned_row); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | public function test_clean_driver_name() |
| 294 | { |
| 295 | $this->assertEquals('avatar.driver.local', $this->manager->clean_driver_name('avatar_driver_local')); |
| 296 | } |
| 297 | |
| 298 | public function test_prepare_driver_name() |
| 299 | { |
| 300 | $this->assertEquals('avatar_driver_local', $this->manager->prepare_driver_name('avatar.driver.local')); |
| 301 | } |
| 302 | |
| 303 | public function test_localize_errors() |
| 304 | { |
| 305 | global $phpbb_root_path, $phpEx; |
| 306 | |
| 307 | $user = $this->getMockBuilder('\phpbb\user') |
| 308 | ->onlyMethods(['lang']) |
| 309 | ->setConstructorArgs(array(new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)), '\phpbb\datetime')) |
| 310 | ->getMock(); |
| 311 | $lang_array = array( |
| 312 | array('FOOBAR_OFF', 'foobar_off'), |
| 313 | array('FOOBAR_EXPLAIN', 'FOOBAR_EXPLAIN %s'), |
| 314 | ); |
| 315 | $user->expects($this->any()) |
| 316 | ->method('lang') |
| 317 | ->will($this->returnValueMap($lang_array)); |
| 318 | |
| 319 | // Pass error as string |
| 320 | $this->assertEquals(array('foobar_off'), $this->manager->localize_errors($user, array('FOOBAR_OFF'))); |
| 321 | |
| 322 | // Pass error as array for vsprintf() |
| 323 | $this->assertEquals(array('FOOBAR_EXPLAIN foo'), $this->manager->localize_errors($user, array(array('FOOBAR_EXPLAIN', 'foo')))); |
| 324 | |
| 325 | // Pass both types |
| 326 | $this->assertEquals(array('foobar_off', 'FOOBAR_EXPLAIN foo'), $this->manager->localize_errors($user, array( |
| 327 | 'FOOBAR_OFF', |
| 328 | array('FOOBAR_EXPLAIN', 'foo'), |
| 329 | ))); |
| 330 | } |
| 331 | |
| 332 | public static function data_handle_avatar_delete() |
| 333 | { |
| 334 | return array( |
| 335 | array( |
| 336 | array( |
| 337 | 'avatar' => '', |
| 338 | 'avatar_type' => '', |
| 339 | 'avatar_width' => 0, |
| 340 | 'avatar_height' => 0, |
| 341 | ), |
| 342 | array( |
| 343 | 'id' => 1, |
| 344 | 'avatar' => 'foobar@example.com', |
| 345 | 'avatar_type' => 'avatar.driver.gravatar', |
| 346 | 'avatar_width' => '16', |
| 347 | 'avatar_height' => '16', |
| 348 | ), USERS_TABLE, 'user_', |
| 349 | ), |
| 350 | array( |
| 351 | array( |
| 352 | 'avatar' => '', |
| 353 | 'avatar_type' => '', |
| 354 | 'avatar_width' => 0, |
| 355 | 'avatar_height' => 0, |
| 356 | ), |
| 357 | array( |
| 358 | 'id' => 5, |
| 359 | 'avatar' => 'g5_1414350991.jpg', |
| 360 | 'avatar_type' => 'avatar.driver.upload', |
| 361 | 'avatar_width' => '80', |
| 362 | 'avatar_height' => '80' |
| 363 | ), GROUPS_TABLE, 'group_', |
| 364 | ), |
| 365 | ); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * @dataProvider data_handle_avatar_delete |
| 370 | */ |
| 371 | public function test_handle_avatar_delete($expected, $avatar_data, $table, $prefix) |
| 372 | { |
| 373 | $this->config['allow_avatar_gravatar'] = true; |
| 374 | $this->assertNull($this->manager->handle_avatar_delete($this->db, $this->user, $avatar_data, $table, $prefix)); |
| 375 | |
| 376 | $sql = 'SELECT * FROM ' . $table . ' |
| 377 | WHERE ' . $prefix . 'id = ' . (int) $avatar_data['id']; |
| 378 | $result = $this->db->sql_query_limit($sql, 1); |
| 379 | |
| 380 | $row = $this->manager->clean_row($this->db->sql_fetchrow($result), substr($prefix, 0, -1)); |
| 381 | $this->db->sql_freeresult($result); |
| 382 | |
| 383 | foreach ($expected as $key => $value) |
| 384 | { |
| 385 | $this->assertEquals($value, $row[$key]); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * @dependsOn test_handle_avatar_delete |
| 391 | */ |
| 392 | public function test_user_group_avatar_deleted() |
| 393 | { |
| 394 | $sql = 'SELECT * FROM ' . USERS_TABLE . ' |
| 395 | WHERE user_id = 3'; |
| 396 | $result = $this->db->sql_query_limit($sql, 1); |
| 397 | $row = $this->manager->clean_row($this->db->sql_fetchrow($result), 'user'); |
| 398 | $this->db->sql_freeresult($result); |
| 399 | |
| 400 | $this->assertEquals(array( |
| 401 | 'avatar' => '', |
| 402 | 'avatar_type' => '', |
| 403 | 'avatar_width' => 0, |
| 404 | 'avatar_height' => 0, |
| 405 | ), $row); |
| 406 | } |
| 407 | } |