From cfccde03b4df87462645a8fea650cb5f7a8cfd11 Mon Sep 17 00:00:00 2001 From: Bugo Date: Tue, 17 Dec 2024 09:34:16 +0500 Subject: [PATCH] Update tests --- tests/Handlers/ErrorPageHandlerTest.php | 61 ++++++++++----------- tests/Services/FileSystemTest.php | 5 +- tests/Services/SitemapDataServiceTest.php | 1 - tests/Services/XmlGeneratorTest.php | 65 +++++++++++++++++++++++ 4 files changed, 99 insertions(+), 33 deletions(-) create mode 100644 tests/Services/XmlGeneratorTest.php diff --git a/tests/Handlers/ErrorPageHandlerTest.php b/tests/Handlers/ErrorPageHandlerTest.php index 5243ef4..788056f 100644 --- a/tests/Handlers/ErrorPageHandlerTest.php +++ b/tests/Handlers/ErrorPageHandlerTest.php @@ -2,6 +2,7 @@ use Bugo\Compat\Board; use Bugo\Compat\Config; +use Bugo\Compat\Lang; use Bugo\Compat\Theme; use Bugo\Compat\Utils; use Bugo\Optimus\Handlers\ErrorPageHandler; @@ -13,63 +14,63 @@ }); describe('handleWrongActions method', function () { - beforeEach(function () { - Config::$modSettings['optimus_errors_for_wrong_actions'] = false; + beforeEach(function () { + Config::$modSettings['optimus_errors_for_wrong_actions'] = false; - Theme::$current->settings['catch_action'] = []; - }); + Theme::$current->settings['catch_action'] = []; + }); - it('sets fatal_error template when setting is enabled', function () { - Config::$modSettings['optimus_errors_for_wrong_actions'] = true; + it('sets fatal_error template when setting is enabled', function () { + Config::$modSettings['optimus_errors_for_wrong_actions'] = true; - $this->handler->handleWrongActions(); + $this->handler->handleWrongActions(); - expect(Theme::$current->settings['catch_action']['sub_template']) - ->toBe('fatal_error'); - }); + expect(Theme::$current->settings['catch_action']['sub_template']) + ->toBe('fatal_error'); + }); - it('does not set template when setting is disabled', function () { - Config::$modSettings['optimus_errors_for_wrong_actions'] = false; + it('does not set template when setting is disabled', function () { + Config::$modSettings['optimus_errors_for_wrong_actions'] = false; - $this->handler->handleWrongActions(); + $this->handler->handleWrongActions(); - expect(Theme::$current->settings['catch_action']) - ->toBeEmpty(); - }); + expect(Theme::$current->settings['catch_action']) + ->toBeEmpty(); + }); - it('handles null catch_action settings', function () { - Config::$modSettings['optimus_errors_for_wrong_actions'] = true; - Theme::$current->settings['catch_action'] = null; + it('handles null catch_action settings', function () { + Config::$modSettings['optimus_errors_for_wrong_actions'] = true; + Theme::$current->settings['catch_action'] = null; - $this->handler->handleWrongActions(); + $this->handler->handleWrongActions(); - expect(Theme::$current->settings['catch_action']) - ->toBeArray() - ->toHaveKey('sub_template', 'fatal_error'); - }); + expect(Theme::$current->settings['catch_action']) + ->toBeArray() + ->toHaveKey('sub_template', 'fatal_error'); + }); }); describe('handleWrongBoardsTopics method', function () { - it('checks case when board_info[error] = exist', function () { + it('checks case when board does not exist', function () { Config::$modSettings['optimus_errors_for_wrong_boards_topics'] = true; Board::$info['error'] = 'exist'; $this->handler->handleWrongBoardsTopics(); - expect(Utils::$context['error_link']) - ->toBe('javascript:history.go(-1)'); + expect(Utils::$context['page_title']) + ->toBe(Lang::$txt["optimus_404_page_title"]); }); - it('checks case when board_info[error] = access', function () { + it('checks case when board has no access', function () { Config::$modSettings['optimus_errors_for_wrong_boards_topics'] = true; Board::$info['error'] = 'access'; $this->handler->handleWrongBoardsTopics(); - expect(Utils::$context['error_link']) - ->toBe('javascript:history.go(-1)'); + expect(Utils::$context['page_title']) + ->toBe(Lang::$txt["optimus_403_page_title"]); }); it('checks case with disabled setting', function () { diff --git a/tests/Services/FileSystemTest.php b/tests/Services/FileSystemTest.php index 4127b5f..99a2ad9 100644 --- a/tests/Services/FileSystemTest.php +++ b/tests/Services/FileSystemTest.php @@ -1,6 +1,7 @@ tempDir = sys_get_temp_dir() . '/optimus_test_' . uniqid(); @@ -28,7 +29,7 @@ }); it('writes gzipped file successfully', function () { - if (!function_exists('gzopen')) { + if (! function_exists('gzopen')) { $this->markTestSkipped('Gzip functions are not available'); } @@ -51,7 +52,7 @@ try { expect(fn() => $fileSystem->writeFile('test.txt', 'content')) - ->toThrow(RuntimeException::class); + ->toThrow(FileSystemException::class); } finally { restore_error_handler(); } diff --git a/tests/Services/SitemapDataServiceTest.php b/tests/Services/SitemapDataServiceTest.php index 818ec42..0745c73 100644 --- a/tests/Services/SitemapDataServiceTest.php +++ b/tests/Services/SitemapDataServiceTest.php @@ -121,7 +121,6 @@ public function testQuery($query, $params = []): array $topics = $topicsProperty->getValue($this->sitemapDataService); - expect($topics)->toHaveCount(2) ->and($topics[1])->toHaveKeys(['url', 'last_date', 'num_replies', 'subject']) ->and($topics[1]['subject'])->toBe('Test Topic') diff --git a/tests/Services/XmlGeneratorTest.php b/tests/Services/XmlGeneratorTest.php new file mode 100644 index 0000000..8b6c2d6 --- /dev/null +++ b/tests/Services/XmlGeneratorTest.php @@ -0,0 +1,65 @@ +xmlGenerator = new XmlGenerator('https://example.com'); +}); + +it('generates XML from array with default root element', function () { + $data = [ + ['loc' => 'https://example.com/item1', 'lastmod' => date('Y-m-d')], + ['loc' => 'https://example.com/item2', 'lastmod' => date('Y-m-d')], + ]; + + $xml = $this->xmlGenerator->generate($data); + + expect($xml)->toContain('') + ->and($xml)->toContain('') + ->and($xml)->toContain('') + ->and($xml)->toContain('https://example.com/item1') + ->and($xml)->toContain('' . date('Y-m-d') . '') + ->and($xml)->toContain('https://example.com/item2') + ->and($xml)->toContain('' . date('Y-m-d') . '') + ->and($xml)->toContain(''); +}); + +it('generates XML with custom root element', function () { + $data = [ + ['loc' => 'https://example.com/item1', 'lastmod' => date('Y-m-d')], + ]; + + $xml = $this->xmlGenerator->generate($data, ['isIndex' => true]); + + expect($xml)->toContain('') + ->and($xml)->toContain('') + ->and($xml)->toContain('') + ->and($xml)->toContain('https://example.com/item1') + ->and($xml)->toContain('' . date('Y-m-d') . '') + ->and($xml)->toContain(''); +}); + +it('generates XML with extended data types', function () { + $data = [ + [ + 'loc' => 'https://example.com/item1', + 'image:image' => ['image:loc' => 'https://example.com/image.png'], + 'mobile:mobile' => ['mobile:loc' => 'https://example.com/mobile'], + ], + ]; + + $xml = $this->xmlGenerator->generate($data, ['mobile' => true, 'images' => true]); + + expect($xml)->toContain('http://www.google.com/schemas/sitemap-mobile/1.0') + ->and($xml)->toContain('http://www.google.com/schemas/sitemap-image/1.1') + ->and($xml)->toContain('https://example.com/image.png') + ->and($xml)->toContain('https://example.com/mobile'); +}); + +it('generates empty XML node for invalid data', function () { + $data = [['one', 'two', 'three']]; + + expect($this->xmlGenerator->generate($data, ['isIndex' => true])) + ->toThrow(XmlGeneratorException::class); +})->throws(XmlGeneratorException::class);