From 5deeea025fecc1d5917d8ff17381a34cec1d3dda Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 12 Jun 2025 20:59:22 +0200 Subject: [PATCH] [ticket/17519] Add unit tests for task wrapper PHPBB-17519 --- tests/cron/wrapper_test.php | 142 ++++++++++++++++++ tests/template/extension_test.php | 2 +- tests/template/template_includecss_test.php | 2 +- tests/template/template_test_case.php | 8 +- .../template/template_test_case_with_tree.php | 2 +- 5 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 tests/cron/wrapper_test.php diff --git a/tests/cron/wrapper_test.php b/tests/cron/wrapper_test.php new file mode 100644 index 0000000000..aca299782d --- /dev/null +++ b/tests/cron/wrapper_test.php @@ -0,0 +1,142 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +require_once __DIR__ . '/../template/template_test_case.php'; + +class phpbb_cron_wrapper_test extends phpbb_template_template_test_case +{ + private $task; + private $routing_helper; + private $wrapper; + + protected function setUp(): void + { + global $phpbb_root_path; + + // Test the engine can be used + $this->setup_engine([], $phpbb_root_path . 'styles/all/template'); + + $this->template->clear_cache(); + + global $phpbb_filesystem; + + $phpbb_filesystem = new \phpbb\filesystem\filesystem(); + + $this->task = $this->createMock(\phpbb\cron\task\task::class); + $this->routing_helper = $this->createMock(\phpbb\routing\helper::class); + + $this->wrapper = new \phpbb\cron\task\wrapper( + $this->task, + $this->routing_helper, + '/phpbb/', + 'php', + $this->template + ); + } + + public function test_generate_template_pagination() + { + $this->task = $this->createMock(\phpbb\cron\task\parametrized::class); + $this->task->expects($this->any()) + ->method('get_parameters') + ->willReturn(['f' => '5']); + $this->task->expects($this->any()) + ->method('get_name') + ->willReturn('test_task'); + $this->routing_helper = $this->createMock(\phpbb\routing\helper::class); + $this->routing_helper->expects($this->any()) + ->method('route') + ->with('phpbb_cron_run', ['cron_type' => 'test_task', 'f' => '5']) + ->willReturn('app.php/cron/foo?f=5'); + + $this->wrapper = new \phpbb\cron\task\wrapper( + $this->task, + $this->routing_helper, + '/phpbb/', + 'php', + $this->template + ); + + $this->assertEquals('', str_replace(["\n", "\t"], '', $this->wrapper->get_html_tag())); + } + + public function test_is_parametrized_false() + { + $this->assertFalse($this->wrapper->is_parametrized()); + } + + public function test_is_ready() + { + $this->task->method('is_runnable')->willReturn(true); + $this->task->method('should_run')->willReturn(true); + + $this->assertTrue($this->wrapper->is_ready()); + } + + public function test_get_url_non_parametrized() + { + $this->task->method('get_name')->willReturn('test_task'); + $this->routing_helper->expects($this->once()) + ->method('route') + ->with('phpbb_cron_run', ['cron_type' => 'test_task']) + ->willReturn('/cron/url'); + + $this->assertEquals('/cron/url', $this->wrapper->get_url()); + } + + public function test_get_html_tag() + { + $this->template = $this->createMock(\phpbb\template\template::class); + $this->wrapper = new \phpbb\cron\task\wrapper( + $this->task, + $this->routing_helper, + '/phpbb/', + 'php', + $this->template + ); + + $this->template->expects($this->once()) + ->method('set_filenames'); + $this->template->expects($this->once()) + ->method('assign_var'); + $this->template->expects($this->once()) + ->method('assign_display') + ->willReturn(''); + + $this->assertEquals('', $this->wrapper->get_html_tag()); + } + + public function test_call_forwards_to_task() + { + $this->task = $this->getMockBuilder(\phpbb\cron\task\task::class) + ->disableOriginalConstructor() + ->setMethods(['get_name', 'run', 'is_runnable', 'should_run', 'some_method']) + ->getMock(); + $this->routing_helper = $this->createMock(\phpbb\routing\helper::class); + + $this->wrapper = new \phpbb\cron\task\wrapper( + $this->task, + $this->routing_helper, + '/phpbb/', + 'php', + $this->template + ); + $this->task->expects($this->once()) + ->method('some_method') + ->with('arg1', 'arg2') + ->willReturn('result'); + + $result = $this->wrapper->some_method('arg1', 'arg2'); + $this->assertEquals('result', $result); + } +} diff --git a/tests/template/extension_test.php b/tests/template/extension_test.php index 6308928325..6b899d2474 100644 --- a/tests/template/extension_test.php +++ b/tests/template/extension_test.php @@ -15,7 +15,7 @@ require_once __DIR__ . '/template_test_case.php'; class phpbb_template_extension_test extends phpbb_template_template_test_case { - protected function setup_engine(array $new_config = array()) + protected function setup_engine(array $new_config = array(), string $template_path = '') { global $config, $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx; diff --git a/tests/template/template_includecss_test.php b/tests/template/template_includecss_test.php index c767986f85..891a555fc0 100644 --- a/tests/template/template_includecss_test.php +++ b/tests/template/template_includecss_test.php @@ -21,7 +21,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te /** @var string */ protected $parent_template_path; - protected function setup_engine(array $new_config = array()) + protected function setup_engine(array $new_config = array(), string $template_path = '') { global $phpbb_root_path, $phpEx, $user; diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index 1cb6cbe799..375221702c 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -1,4 +1,7 @@ template_path = $this->test_path . '/templates'; + $this->template_path = $template_path ?: $this->test_path . '/templates'; $container = new phpbb_mock_container_builder(); $cache_path = $phpbb_root_path . 'cache/twig'; diff --git a/tests/template/template_test_case_with_tree.php b/tests/template/template_test_case_with_tree.php index 4a1764053b..2b012a29fb 100644 --- a/tests/template/template_test_case_with_tree.php +++ b/tests/template/template_test_case_with_tree.php @@ -15,7 +15,7 @@ require_once __DIR__ . '/template_test_case.php'; class phpbb_template_template_test_case_with_tree extends phpbb_template_template_test_case { - protected function setup_engine(array $new_config = array()) + protected function setup_engine(array $new_config = array(), string $template_path = '') { global $phpbb_root_path, $phpEx, $user;