From efece4136b131b0db3c2490d095bf3a85ec3fe7b Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 27 Jun 2024 22:13:54 +0200 Subject: [PATCH 1/5] [ticket/17353] Add test for gravatar driver and fix request var issue PHPBB-17353 --- phpBB/phpbb/avatar/driver/gravatar.php | 2 +- tests/avatar/driver_gravatar_test.php | 200 +++++++++++++++++++++++++ 2 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 tests/avatar/driver_gravatar_test.php diff --git a/phpBB/phpbb/avatar/driver/gravatar.php b/phpBB/phpbb/avatar/driver/gravatar.php index 3e4e7ff98ba..3a9b80a16d9 100644 --- a/phpBB/phpbb/avatar/driver/gravatar.php +++ b/phpBB/phpbb/avatar/driver/gravatar.php @@ -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'] : '', )); diff --git a/tests/avatar/driver_gravatar_test.php b/tests/avatar/driver_gravatar_test.php new file mode 100644 index 00000000000..29417a508ac --- /dev/null +++ b/tests/avatar/driver_gravatar_test.php @@ -0,0 +1,200 @@ + + * @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() + ->onlyMethods(['get_super_global']) + ->getMock(); + $this->request->method('get_super_global') + ->willReturn([]); + $this->template = $this->getMockBuilder(\phpbb\template\twig\twig::class) + ->disableOriginalConstructor() + ->onlyMethods(['assign_vars']) + ->getMock(); + $this->template->method('assign_vars') + ->will($this->returnCallback([$this, 'template_assign_vars'])); + $this->user = $this->getMockBuilder(\phpbb\user::class) + ->disableOriginalConstructor() + ->getMock(); + $imagesize = new \FastImageSize\FastImageSize(); + $cache = $this->createMock('\phpbb\cache\driver\driver_interface'); + $path_helper = new \phpbb\path_helper( + new \phpbb\symfony_request( + $this->request + ), + $this->request, + $phpbb_root_path, + $phpEx + ); + + $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() + ->onlyMethods(['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()); + + + global $phpbb_dispatcher; + $phpbb_dispatcher = $this->getMockBuilder(\phpbb\event\dispatcher::class) + ->disableOriginalConstructor() + ->onlyMethods(['trigger_event']) + ->getMock(); + $phpbb_dispatcher->method('trigger_event') + ->willReturn($this->returnArgument(1)); + + $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)); + } +} From 40881b537e3aeffa5ca4f6b60daa6d1520b8b2fd Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 28 Jun 2024 14:58:17 +0200 Subject: [PATCH 2/5] [ticket/17353] Return image src for gravatar image PHPBB-17353 --- phpBB/phpbb/avatar/driver/gravatar.php | 2 +- tests/avatar/driver_gravatar_test.php | 33 +++++++++++++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/phpBB/phpbb/avatar/driver/gravatar.php b/phpBB/phpbb/avatar/driver/gravatar.php index 3a9b80a16d9..25e5bfb6e5b 100644 --- a/phpBB/phpbb/avatar/driver/gravatar.php +++ b/phpBB/phpbb/avatar/driver/gravatar.php @@ -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'], ); diff --git a/tests/avatar/driver_gravatar_test.php b/tests/avatar/driver_gravatar_test.php index 29417a508ac..383664fe9c0 100644 --- a/tests/avatar/driver_gravatar_test.php +++ b/tests/avatar/driver_gravatar_test.php @@ -72,6 +72,14 @@ protected function setUp(): void $phpEx ); + global $phpbb_dispatcher; + $phpbb_dispatcher = $this->getMockBuilder(\phpbb\event\dispatcher::class) + ->disableOriginalConstructor() + ->onlyMethods(['trigger_event']) + ->getMock(); + $phpbb_dispatcher->method('trigger_event') + ->willReturn($this->returnArgument(1)); + $this->gravatar = new gravatar($this->config, $imagesize, $phpbb_root_path, $phpEx, $path_helper, $cache); $this->gravatar->set_name('avatar.driver.gravatar'); } @@ -180,15 +188,6 @@ 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()); - - global $phpbb_dispatcher; - $phpbb_dispatcher = $this->getMockBuilder(\phpbb\event\dispatcher::class) - ->disableOriginalConstructor() - ->onlyMethods(['trigger_event']) - ->getMock(); - $phpbb_dispatcher->method('trigger_event') - ->willReturn($this->returnArgument(1)); - $row = [ 'avatar_type' => 'avatar.driver.gravatar', 'avatar' => 'bar@foo.com', @@ -197,4 +196,20 @@ public function test_gravatar_misc(): void ]; $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' => '//secure.gravatar.com/avatar/dc8a42aba3651b0b1f088ef928ff3b1d?s=70', + 'width' => '70', + 'height' => '60', + ], $this->gravatar->get_data($row)); + } } From 5013075af7c74fd060d5626c08bdd11003bf51a3 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 30 Jun 2024 11:57:35 +0200 Subject: [PATCH 3/5] [ticket/17355] Change gravatar URL to new format PHPBB-17355 --- phpBB/phpbb/avatar/driver/gravatar.php | 4 ++-- tests/avatar/driver_gravatar_test.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/phpbb/avatar/driver/gravatar.php b/phpBB/phpbb/avatar/driver/gravatar.php index 25e5bfb6e5b..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} @@ -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 index 383664fe9c0..a79e0d51151 100644 --- a/tests/avatar/driver_gravatar_test.php +++ b/tests/avatar/driver_gravatar_test.php @@ -207,7 +207,7 @@ public function test_get_data(): void ]; $this->assertEquals([ - 'src' => '//secure.gravatar.com/avatar/dc8a42aba3651b0b1f088ef928ff3b1d?s=70', + 'src' => '//gravatar.com/avatar/e0ee9d02824d4320a999507150c5b8a371c635c41f645ba3a7205f36384dc199?s=70', 'width' => '70', 'height' => '60', ], $this->gravatar->get_data($row)); From bde7b119b2983b55fc6084a046fba556875a9871 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 30 Jun 2024 12:34:54 +0200 Subject: [PATCH 4/5] [ticket/17355] Update tests after changing gravatar URL PHPBB-17355 --- tests/avatar/driver_gravatar_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/avatar/driver_gravatar_test.php b/tests/avatar/driver_gravatar_test.php index a79e0d51151..fbc50d5623c 100644 --- a/tests/avatar/driver_gravatar_test.php +++ b/tests/avatar/driver_gravatar_test.php @@ -194,7 +194,7 @@ public function test_gravatar_misc(): void 'avatar_width' => '70', 'avatar_height' => '60', ]; - $this->assertEquals('', $this->gravatar->get_custom_html($this->user, $row)); + $this->assertEquals('', $this->gravatar->get_custom_html($this->user, $row)); } public function test_get_data(): void From 99b745982e9aee1dc379c738bc2e16bfdc5967de Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 3 Jul 2024 21:44:40 +0200 Subject: [PATCH 5/5] [ticket/17353] Clean up test code for compatibility with 3.3.x PHPBB-17353 --- tests/avatar/driver_gravatar_test.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/avatar/driver_gravatar_test.php b/tests/avatar/driver_gravatar_test.php index fbc50d5623c..d0a83c2f22e 100644 --- a/tests/avatar/driver_gravatar_test.php +++ b/tests/avatar/driver_gravatar_test.php @@ -48,25 +48,27 @@ protected function setUp(): void $this->config = new \phpbb\config\config(array()); $this->request = $this->getMockBuilder(request::class) ->disableOriginalConstructor() - ->onlyMethods(['get_super_global']) + ->setMethods(['get_super_global']) ->getMock(); $this->request->method('get_super_global') ->willReturn([]); $this->template = $this->getMockBuilder(\phpbb\template\twig\twig::class) ->disableOriginalConstructor() - ->onlyMethods(['assign_vars']) + ->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 @@ -75,10 +77,10 @@ protected function setUp(): void global $phpbb_dispatcher; $phpbb_dispatcher = $this->getMockBuilder(\phpbb\event\dispatcher::class) ->disableOriginalConstructor() - ->onlyMethods(['trigger_event']) + ->setMethods(['trigger_event']) ->getMock(); $phpbb_dispatcher->method('trigger_event') - ->willReturn($this->returnArgument(1)); + ->willReturnArgument(1); $this->gravatar = new gravatar($this->config, $imagesize, $phpbb_root_path, $phpEx, $path_helper, $cache); $this->gravatar->set_name('avatar.driver.gravatar'); @@ -160,7 +162,7 @@ public function test_prepare_form($expected_vars, $request_data, $row) $request = $this->getMockBuilder(request::class) ->disableOriginalConstructor() - ->onlyMethods(['get_super_global']) + ->setMethods(['get_super_global']) ->getMock(); $request->method('get_super_global') ->willReturn([]); @@ -194,7 +196,7 @@ public function test_gravatar_misc(): void 'avatar_width' => '70', 'avatar_height' => '60', ]; - $this->assertEquals('', $this->gravatar->get_custom_html($this->user, $row)); + $this->assertEquals('', $this->gravatar->get_custom_html($this->user, $row)); } public function test_get_data(): void