|
| 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 | +class phpbb_avatar_driver_local_test extends \phpbb_test_case |
| 15 | +{ |
| 16 | + /** @var array */ |
| 17 | + private $template_block_data = []; |
| 18 | + |
| 19 | + public function template_assign_block_vars($blockname, array $vararray) |
| 20 | + { |
| 21 | + $this->template_block_data[$blockname][] = $vararray; |
| 22 | + } |
| 23 | + |
| 24 | + protected static function get_avatar($category, $filename) |
| 25 | + { |
| 26 | + return [ |
| 27 | + 'file' => $category . '/' . $filename, |
| 28 | + 'filename' => $filename, |
| 29 | + 'name' => $filename, |
| 30 | + 'width' => 80, |
| 31 | + 'height' => 80, |
| 32 | + ]; |
| 33 | + } |
| 34 | + |
| 35 | + public static function data_prepare_form(): array |
| 36 | + { |
| 37 | + // PHP stores numeric category names like "2012" as integer array keys |
| 38 | + $numeric_first = [ |
| 39 | + '2012' => ['a.png' => self::get_avatar('2012', 'a.png')], |
| 40 | + '2013' => ['b.png' => self::get_avatar('2013', 'b.png')], |
| 41 | + 'animals' => ['c.png' => self::get_avatar('animals', 'c.png')], |
| 42 | + ]; |
| 43 | + |
| 44 | + $numeric_last = [ |
| 45 | + 'animals' => ['c.png' => self::get_avatar('animals', 'c.png')], |
| 46 | + 'zebras' => ['d.png' => self::get_avatar('zebras', 'd.png')], |
| 47 | + '2012' => ['a.png' => self::get_avatar('2012', 'a.png')], |
| 48 | + ]; |
| 49 | + |
| 50 | + return [ |
| 51 | + 'numeric first, nothing requested' => [$numeric_first, null, '2012', ['a.png']], |
| 52 | + 'numeric first, numeric requested' => [$numeric_first, '2013', '2013', ['b.png']], |
| 53 | + 'numeric first, non-numeric requested' => [$numeric_first, 'animals', 'animals', ['c.png']], |
| 54 | + 'numeric last, nothing requested' => [$numeric_last, null, 'animals', ['c.png']], |
| 55 | + 'numeric last, numeric requested' => [$numeric_last, '2012', '2012', ['a.png']], |
| 56 | + 'numeric last, non-numeric requested' => [$numeric_last, 'zebras', 'zebras', ['d.png']], |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dataProvider data_prepare_form |
| 62 | + */ |
| 63 | + public function test_prepare_form($avatar_list, $requested_category, $expected_category, $expected_avatars) |
| 64 | + { |
| 65 | + global $phpbb_root_path, $phpEx; |
| 66 | + |
| 67 | + $this->template_block_data = []; |
| 68 | + |
| 69 | + $config = new \phpbb\config\config(['avatar_gallery_path' => 'images/avatars/gallery']); |
| 70 | + |
| 71 | + $cache = $this->createMock('\phpbb\cache\driver\driver_interface'); |
| 72 | + $cache->method('get') |
| 73 | + ->willReturn($avatar_list); |
| 74 | + |
| 75 | + $template = $this->createMock('\phpbb\template\template'); |
| 76 | + $template->method('assign_block_vars') |
| 77 | + ->willReturnCallback([$this, 'template_assign_block_vars']); |
| 78 | + |
| 79 | + $user = $this->createMock('\phpbb\user'); |
| 80 | + $user->data = ['user_lang' => 'en']; |
| 81 | + |
| 82 | + $request = new \phpbb\request\request(null, false); |
| 83 | + $request->overwrite('avatar_local_cat', $requested_category); |
| 84 | + |
| 85 | + $driver = new \phpbb\avatar\driver\local( |
| 86 | + $config, |
| 87 | + new \FastImageSize\FastImageSize(), |
| 88 | + $phpbb_root_path, |
| 89 | + $phpEx, |
| 90 | + $this->createMock('\phpbb\path_helper'), |
| 91 | + $cache |
| 92 | + ); |
| 93 | + |
| 94 | + $error = []; |
| 95 | + $this->assertTrue($driver->prepare_form($request, $template, $user, ['avatar' => ''], $error)); |
| 96 | + $this->assertEquals([], $error); |
| 97 | + |
| 98 | + $selected_categories = []; |
| 99 | + foreach ($this->template_block_data['avatar_local_cats'] as $category) |
| 100 | + { |
| 101 | + if ($category['SELECTED']) |
| 102 | + { |
| 103 | + $selected_categories[] = (string) $category['NAME']; |
| 104 | + } |
| 105 | + } |
| 106 | + $this->assertCount(count($avatar_list), $this->template_block_data['avatar_local_cats']); |
| 107 | + $this->assertSame([$expected_category], $selected_categories); |
| 108 | + |
| 109 | + $this->assertArrayHasKey('avatar_local_row.avatar_local_col', $this->template_block_data, 'No avatars listed for the selected category'); |
| 110 | + $this->assertSame($expected_avatars, array_column($this->template_block_data['avatar_local_row.avatar_local_col'], 'AVATAR_FILE')); |
| 111 | + } |
| 112 | +} |
0 commit comments