diff --git a/phpBB/phpbb/avatar/driver/gravatar.php b/phpBB/phpbb/avatar/driver/gravatar.php index 3e4e7ff98ba..c49e29c20c8 100644 --- a/phpBB/phpbb/avatar/driver/gravatar.php +++ b/phpBB/phpbb/avatar/driver/gravatar.php @@ -21,7 +21,7 @@ class gravatar extends \phpbb\avatar\driver\driver /** * The URL for the gravatar service */ - const GRAVATAR_URL = '//secure.gravatar.com/avatar/'; + const GRAVATAR_URL = '//gravatar.com/avatar/'; /** * {@inheritdoc} @@ -29,7 +29,7 @@ class gravatar extends \phpbb\avatar\driver\driver public function get_data($row) { return array( - 'src' => $row['avatar'], + 'src' => $this->get_gravatar_url($row), 'width' => $row['avatar_width'], 'height' => $row['avatar_height'], ); @@ -53,7 +53,7 @@ public function prepare_form($request, $template, $user, $row, &$error) { $template->assign_vars(array( 'AVATAR_GRAVATAR_WIDTH' => (($row['avatar_type'] == $this->get_name() || $row['avatar_type'] == 'gravatar') && $row['avatar_width']) ? $row['avatar_width'] : $request->variable('avatar_gravatar_width', ''), - 'AVATAR_GRAVATAR_HEIGHT' => (($row['avatar_type'] == $this->get_name() || $row['avatar_type'] == 'gravatar') && $row['avatar_height']) ? $row['avatar_height'] : $request->variable('avatar_gravatar_width', ''), + 'AVATAR_GRAVATAR_HEIGHT' => (($row['avatar_type'] == $this->get_name() || $row['avatar_type'] == 'gravatar') && $row['avatar_height']) ? $row['avatar_height'] : $request->variable('avatar_gravatar_height', ''), 'AVATAR_GRAVATAR_EMAIL' => (($row['avatar_type'] == $this->get_name() || $row['avatar_type'] == 'gravatar') && $row['avatar']) ? $row['avatar'] : '', )); @@ -175,7 +175,7 @@ protected function get_gravatar_url($row) global $phpbb_dispatcher; $url = self::GRAVATAR_URL; - $url .= md5(strtolower(trim($row['avatar']))); + $url .= hash('sha256', strtolower(trim($row['avatar']))); if ($row['avatar_width'] || $row['avatar_height']) { diff --git a/tests/avatar/driver_gravatar_test.php b/tests/avatar/driver_gravatar_test.php new file mode 100644 index 00000000000..d0a83c2f22e --- /dev/null +++ b/tests/avatar/driver_gravatar_test.php @@ -0,0 +1,217 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_avatar_driver_gravatar_test extends \phpbb_database_test_case +{ + + /** @var \phpbb\config\config */ + private $config; + + /** @var gravatar */ + private $gravatar; + + /** @var request_interface */ + private $request; + + /** @var \phpbb\template\template */ + private $template; + + /** @var \phpbb\user */ + private $user; + + private $template_data = []; + + public function getDataSet() + { + return $this->createXMLDataSet(__DIR__ . '/fixtures/users.xml'); + } + + protected function setUp(): void + { + global $phpbb_root_path, $phpEx; + + $this->config = new \phpbb\config\config(array()); + $this->request = $this->getMockBuilder(request::class) + ->disableOriginalConstructor() + ->setMethods(['get_super_global']) + ->getMock(); + $this->request->method('get_super_global') + ->willReturn([]); + $this->template = $this->getMockBuilder(\phpbb\template\twig\twig::class) + ->disableOriginalConstructor() + ->setMethods(['assign_vars']) + ->getMock(); + $this->template->method('assign_vars') + ->will($this->returnCallback([$this, 'template_assign_vars'])); + $this->user = $this->getMockBuilder(\phpbb\user::class) + ->disableOriginalConstructor() + ->getMock(); + $filesystem = new \phpbb\filesystem\filesystem(); + $imagesize = new \FastImageSize\FastImageSize(); + $cache = $this->createMock('\phpbb\cache\driver\driver_interface'); + $path_helper = new \phpbb\path_helper( + new \phpbb\symfony_request( + $this->request + ), + $filesystem, + $this->request, + $phpbb_root_path, + $phpEx + ); + + global $phpbb_dispatcher; + $phpbb_dispatcher = $this->getMockBuilder(\phpbb\event\dispatcher::class) + ->disableOriginalConstructor() + ->setMethods(['trigger_event']) + ->getMock(); + $phpbb_dispatcher->method('trigger_event') + ->willReturnArgument(1); + + $this->gravatar = new gravatar($this->config, $imagesize, $phpbb_root_path, $phpEx, $path_helper, $cache); + $this->gravatar->set_name('avatar.driver.gravatar'); + } + + public function template_assign_vars($data) + { + $this->template_data = array_merge($this->template_data, $data); + } + + public function data_prepare_form(): array + { + return [ + [ + // Only default empty values, no request data + [ + 'AVATAR_GRAVATAR_WIDTH' => '', + 'AVATAR_GRAVATAR_HEIGHT' => '', + 'AVATAR_GRAVATAR_EMAIL' => '', + ], + [], + [ + 'avatar_type' => '', + 'avatar_width' => '', + 'avatar_height' => '', + ] + ], + [ + // Only default empty values, request data set + [ + 'AVATAR_GRAVATAR_WIDTH' => '80', + 'AVATAR_GRAVATAR_HEIGHT' => '90', + 'AVATAR_GRAVATAR_EMAIL' => '', + ], + [ + request_interface::POST => [ + 'avatar_type' => 'avatar.driver.gravatar', + 'avatar_gravatar_width' => '80', + 'avatar_gravatar_height' => '90', + ], + ], + [ + 'avatar_type' => '', + 'avatar_width' => '80', + 'avatar_height' => '90', + ] + ], + [ + // Only default empty values, request data set + [ + 'AVATAR_GRAVATAR_WIDTH' => '70', + 'AVATAR_GRAVATAR_HEIGHT' => '60', + 'AVATAR_GRAVATAR_EMAIL' => 'bar@foo.com', + ], + [ + request_interface::POST => [ + 'avatar_type' => 'avatar.driver.gravatar', + 'avatar_gravatar_width' => '80', + 'avatar_gravatar_height' => '90', + ], + ], + [ + 'avatar_type' => 'avatar.driver.gravatar', + 'avatar' => 'bar@foo.com', + 'avatar_width' => '70', + 'avatar_height' => '60', + ] + ], + ]; + } + + /** + * @dataProvider data_prepare_form + */ + public function test_prepare_form($expected_vars, $request_data, $row) + { + $error = []; + $this->template_data = []; + + $request = $this->getMockBuilder(request::class) + ->disableOriginalConstructor() + ->setMethods(['get_super_global']) + ->getMock(); + $request->method('get_super_global') + ->willReturn([]); + + $requestInputReflection = new \ReflectionProperty($request, 'input'); + $requestInputReflection->setAccessible(true); + $request_data[request_interface::GET] = $request_data[request_interface::GET] ?? []; + $request_data[request_interface::POST] = $request_data[request_interface::POST] ?? []; + $request_data[request_interface::REQUEST] = $request_data[request_interface::GET] + $request_data[request_interface::POST]; + $requestInputReflection->setValue($request, $request_data); + $requestTypeCastHelperReflection = new \ReflectionProperty($request, 'type_cast_helper'); + $requestTypeCastHelperReflection->setAccessible(true); + $requestTypeCastHelperReflection->setValue($request, new \phpbb\request\type_cast_helper()); + + $this->gravatar->prepare_form($request, $this->template, $this->user, $row, $error); + + // Error not touched by gravatar + $this->assertEquals([], $error); + + $this->assertEquals($expected_vars, $this->template_data); + } + + public function test_gravatar_misc(): void + { + $this->assertEquals('ucp_avatar_options_gravatar.html', $this->gravatar->get_template_name()); + $this->assertEquals('acp_avatar_options_gravatar.html', $this->gravatar->get_acp_template_name()); + + $row = [ + 'avatar_type' => 'avatar.driver.gravatar', + 'avatar' => 'bar@foo.com', + 'avatar_width' => '70', + 'avatar_height' => '60', + ]; + $this->assertEquals('', $this->gravatar->get_custom_html($this->user, $row)); + } + + public function test_get_data(): void + { + $row = [ + 'avatar_type' => 'avatar.driver.gravatar', + 'avatar' => 'bar@foo.com', + 'avatar_width' => '70', + 'avatar_height' => '60', + ]; + + $this->assertEquals([ + 'src' => '//gravatar.com/avatar/e0ee9d02824d4320a999507150c5b8a371c635c41f645ba3a7205f36384dc199?s=70', + 'width' => '70', + 'height' => '60', + ], $this->gravatar->get_data($row)); + } +}