Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
58.46% |
76 / 130 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| phpbb_avatar_driver_gravatar_test | |
58.46% |
76 / 130 |
|
85.71% |
6 / 7 |
10.51 | |
0.00% |
0 / 1 |
| getDataSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setUp | |
100.00% |
36 / 36 |
|
100.00% |
1 / 1 |
1 | |||
| template_assign_vars | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| data_prepare_form | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
2 | |||
| test_prepare_form | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
1 | |||
| test_gravatar_misc | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| test_get_data | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | use phpbb\avatar\driver\gravatar; |
| 4 | use phpbb\request\request; |
| 5 | use phpbb\request\request_interface; |
| 6 | |
| 7 | /** |
| 8 | * |
| 9 | * This file is part of the phpBB Forum Software package. |
| 10 | * |
| 11 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 12 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 13 | * |
| 14 | * For full copyright and license information, please see |
| 15 | * the docs/CREDITS.txt file. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | class phpbb_avatar_driver_gravatar_test extends \phpbb_database_test_case |
| 20 | { |
| 21 | |
| 22 | /** @var \phpbb\config\config */ |
| 23 | private $config; |
| 24 | |
| 25 | /** @var gravatar */ |
| 26 | private $gravatar; |
| 27 | |
| 28 | /** @var request_interface */ |
| 29 | private $request; |
| 30 | |
| 31 | /** @var \phpbb\template\template */ |
| 32 | private $template; |
| 33 | |
| 34 | /** @var \phpbb\user */ |
| 35 | private $user; |
| 36 | |
| 37 | private $template_data = []; |
| 38 | |
| 39 | public function getDataSet() |
| 40 | { |
| 41 | return $this->createXMLDataSet(__DIR__ . '/fixtures/users.xml'); |
| 42 | } |
| 43 | |
| 44 | protected function setUp(): void |
| 45 | { |
| 46 | global $phpbb_root_path, $phpEx; |
| 47 | |
| 48 | $this->config = new \phpbb\config\config(array()); |
| 49 | $this->request = $this->getMockBuilder(request::class) |
| 50 | ->disableOriginalConstructor() |
| 51 | ->onlyMethods(['get_super_global']) |
| 52 | ->getMock(); |
| 53 | $this->request->method('get_super_global') |
| 54 | ->willReturn([]); |
| 55 | $this->template = $this->getMockBuilder(\phpbb\template\twig\twig::class) |
| 56 | ->disableOriginalConstructor() |
| 57 | ->onlyMethods(['assign_vars']) |
| 58 | ->getMock(); |
| 59 | $this->template->method('assign_vars') |
| 60 | ->will($this->returnCallback([$this, 'template_assign_vars'])); |
| 61 | $this->user = $this->getMockBuilder(\phpbb\user::class) |
| 62 | ->disableOriginalConstructor() |
| 63 | ->getMock(); |
| 64 | $imagesize = new \FastImageSize\FastImageSize(); |
| 65 | $cache = $this->createMock('\phpbb\cache\driver\driver_interface'); |
| 66 | $path_helper = new \phpbb\path_helper( |
| 67 | new \phpbb\symfony_request( |
| 68 | $this->request |
| 69 | ), |
| 70 | $this->request, |
| 71 | $phpbb_root_path, |
| 72 | $phpEx |
| 73 | ); |
| 74 | |
| 75 | global $phpbb_dispatcher; |
| 76 | $phpbb_dispatcher = $this->getMockBuilder(\phpbb\event\dispatcher::class) |
| 77 | ->disableOriginalConstructor() |
| 78 | ->onlyMethods(['trigger_event']) |
| 79 | ->getMock(); |
| 80 | $phpbb_dispatcher->method('trigger_event') |
| 81 | ->willReturnArgument(1); |
| 82 | |
| 83 | $this->gravatar = new gravatar($this->config, $imagesize, $phpbb_root_path, $phpEx, $path_helper, $cache); |
| 84 | $this->gravatar->set_name('avatar.driver.gravatar'); |
| 85 | } |
| 86 | |
| 87 | public function template_assign_vars($data) |
| 88 | { |
| 89 | $this->template_data = array_merge($this->template_data, $data); |
| 90 | } |
| 91 | |
| 92 | public static function data_prepare_form(): array |
| 93 | { |
| 94 | return [ |
| 95 | [ |
| 96 | // Only default empty values, no request data |
| 97 | [ |
| 98 | 'AVATAR_GRAVATAR_WIDTH' => '', |
| 99 | 'AVATAR_GRAVATAR_HEIGHT' => '', |
| 100 | 'AVATAR_GRAVATAR_EMAIL' => '', |
| 101 | ], |
| 102 | [], |
| 103 | [ |
| 104 | 'avatar_type' => '', |
| 105 | 'avatar_width' => '', |
| 106 | 'avatar_height' => '', |
| 107 | ] |
| 108 | ], |
| 109 | [ |
| 110 | // Only default empty values, request data set |
| 111 | [ |
| 112 | 'AVATAR_GRAVATAR_WIDTH' => '80', |
| 113 | 'AVATAR_GRAVATAR_HEIGHT' => '90', |
| 114 | 'AVATAR_GRAVATAR_EMAIL' => '', |
| 115 | ], |
| 116 | [ |
| 117 | request_interface::POST => [ |
| 118 | 'avatar_type' => 'avatar.driver.gravatar', |
| 119 | 'avatar_gravatar_width' => '80', |
| 120 | 'avatar_gravatar_height' => '90', |
| 121 | ], |
| 122 | ], |
| 123 | [ |
| 124 | 'avatar_type' => '', |
| 125 | 'avatar_width' => '80', |
| 126 | 'avatar_height' => '90', |
| 127 | ] |
| 128 | ], |
| 129 | [ |
| 130 | // Only default empty values, request data set |
| 131 | [ |
| 132 | 'AVATAR_GRAVATAR_WIDTH' => '70', |
| 133 | 'AVATAR_GRAVATAR_HEIGHT' => '60', |
| 134 | 'AVATAR_GRAVATAR_EMAIL' => 'bar@foo.com', |
| 135 | ], |
| 136 | [ |
| 137 | request_interface::POST => [ |
| 138 | 'avatar_type' => 'avatar.driver.gravatar', |
| 139 | 'avatar_gravatar_width' => '80', |
| 140 | 'avatar_gravatar_height' => '90', |
| 141 | ], |
| 142 | ], |
| 143 | [ |
| 144 | 'avatar_type' => 'avatar.driver.gravatar', |
| 145 | 'avatar' => 'bar@foo.com', |
| 146 | 'avatar_width' => '70', |
| 147 | 'avatar_height' => '60', |
| 148 | ] |
| 149 | ], |
| 150 | ]; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @dataProvider data_prepare_form |
| 155 | */ |
| 156 | public function test_prepare_form($expected_vars, $request_data, $row) |
| 157 | { |
| 158 | $error = []; |
| 159 | $this->template_data = []; |
| 160 | |
| 161 | $request = $this->getMockBuilder(request::class) |
| 162 | ->disableOriginalConstructor() |
| 163 | ->onlyMethods(['get_super_global']) |
| 164 | ->getMock(); |
| 165 | $request->method('get_super_global') |
| 166 | ->willReturn([]); |
| 167 | |
| 168 | $requestInputReflection = new \ReflectionProperty($request, 'input'); |
| 169 | $request_data[request_interface::GET] = $request_data[request_interface::GET] ?? []; |
| 170 | $request_data[request_interface::POST] = $request_data[request_interface::POST] ?? []; |
| 171 | $request_data[request_interface::REQUEST] = $request_data[request_interface::GET] + $request_data[request_interface::POST]; |
| 172 | $requestInputReflection->setValue($request, $request_data); |
| 173 | $requestTypeCastHelperReflection = new \ReflectionProperty($request, 'type_cast_helper'); |
| 174 | $requestTypeCastHelperReflection->setValue($request, new \phpbb\request\type_cast_helper()); |
| 175 | |
| 176 | $this->gravatar->prepare_form($request, $this->template, $this->user, $row, $error); |
| 177 | |
| 178 | // Error not touched by gravatar |
| 179 | $this->assertEquals([], $error); |
| 180 | |
| 181 | $this->assertEquals($expected_vars, $this->template_data); |
| 182 | } |
| 183 | |
| 184 | public function test_gravatar_misc(): void |
| 185 | { |
| 186 | $this->assertEquals('ucp_avatar_options_gravatar.html', $this->gravatar->get_template_name()); |
| 187 | $this->assertEquals('acp_avatar_options_gravatar.html', $this->gravatar->get_acp_template_name()); |
| 188 | |
| 189 | $row = [ |
| 190 | 'avatar_type' => 'avatar.driver.gravatar', |
| 191 | 'avatar' => 'bar@foo.com', |
| 192 | 'avatar_width' => '70', |
| 193 | 'avatar_height' => '60', |
| 194 | ]; |
| 195 | $this->assertEquals('<img class="gravatar" src="//gravatar.com/avatar/e0ee9d02824d4320a999507150c5b8a371c635c41f645ba3a7205f36384dc199?s=70" width="70" height="60" alt="" />', $this->gravatar->get_custom_html($this->user, $row)); |
| 196 | } |
| 197 | |
| 198 | public function test_get_data(): void |
| 199 | { |
| 200 | $row = [ |
| 201 | 'avatar_type' => 'avatar.driver.gravatar', |
| 202 | 'avatar' => 'bar@foo.com', |
| 203 | 'avatar_width' => '70', |
| 204 | 'avatar_height' => '60', |
| 205 | ]; |
| 206 | |
| 207 | $this->assertEquals([ |
| 208 | 'src' => '//gravatar.com/avatar/e0ee9d02824d4320a999507150c5b8a371c635c41f645ba3a7205f36384dc199?s=70', |
| 209 | 'width' => '70', |
| 210 | 'height' => '60', |
| 211 | ], $this->gravatar->get_data($row)); |
| 212 | } |
| 213 | } |