From ea334e43bb6e231bb33f19c295982b16ed6692ca Mon Sep 17 00:00:00 2001 From: Bugo Date: Fri, 20 Dec 2024 07:21:55 +0500 Subject: [PATCH] Update tests --- composer.json | 6 +- tests/Handlers/SearchTermHandlerTest.php | 25 +++++-- tests/Handlers/TagHandlerTest.php | 87 ++++++++++++++++++++++-- tests/Handlers/TopicHandlerTest.php | 2 +- tests/Pest.php | 5 ++ tests/Utils/CopyrightTest.php | 9 ++- 6 files changed, 116 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index 4f6ad71..5a59a34 100644 --- a/composer.json +++ b/composer.json @@ -24,9 +24,9 @@ "scripts": { "check": "vendor/bin/rector process --dry-run --clear-cache", "tests": "vendor/bin/pest --colors=always", - "tests-coverage": "vendor/bin/pest --colors=always --coverage --min=70", - "tests-coverage-clover": "vendor/bin/pest --colors=always --min=70 --coverage-clover coverage.xml", - "tests-coverage-html": "vendor/bin/pest --colors=always --min=70 --coverage-html coverage", + "tests-coverage": "vendor/bin/pest --colors=always --coverage --min=90", + "tests-coverage-clover": "vendor/bin/pest --colors=always --min=90 --coverage-clover coverage.xml", + "tests-coverage-html": "vendor/bin/pest --colors=always --min=90 --coverage-html coverage", "post-update-cmd": "cd src/Sources/Optimus && composer update --no-dev -o" }, "config": { diff --git a/tests/Handlers/SearchTermHandlerTest.php b/tests/Handlers/SearchTermHandlerTest.php index 2e3efb8..0503c71 100644 --- a/tests/Handlers/SearchTermHandlerTest.php +++ b/tests/Handlers/SearchTermHandlerTest.php @@ -155,13 +155,26 @@ public function insert( }); }); -it('checks showChart method', function () { - Config::$modSettings['optimus_log_search'] = true; +describe('showChart method', function () { + it('checks case with disabled optimus_log_search', function () { + Utils::$context['template_layers'] = []; - Utils::$context['search_terms'] = [['test']]; + Config::$modSettings['optimus_log_search'] = false; + + $showChart = new ReflectionMethod($this->handler, 'showChart'); + $showChart->invoke($this->handler); + + expect(Utils::$context['template_layers'])->toBeEmpty(); + }); - $showChart = new ReflectionMethod($this->handler, 'showChart'); - $showChart->invoke($this->handler); + it('checks normal case', function () { + Config::$modSettings['optimus_log_search'] = true; + + Utils::$context['search_terms'] = [['test']]; - expect(Utils::$context['template_layers'])->toContain('search_terms'); + $showChart = new ReflectionMethod($this->handler, 'showChart'); + $showChart->invoke($this->handler); + + expect(Utils::$context['template_layers'])->toContain('search_terms'); + }); }); diff --git a/tests/Handlers/TagHandlerTest.php b/tests/Handlers/TagHandlerTest.php index 835fffa..686953e 100644 --- a/tests/Handlers/TagHandlerTest.php +++ b/tests/Handlers/TagHandlerTest.php @@ -64,23 +64,36 @@ public function testQuery($query, $params = []): array ]; } - if (str_contains($query, 'SELECT name')) { - return ['Keyword 1']; - } - - if (str_contains($query, 'WHERE name LIKE {string:search}')) { + if (str_contains($query, 'SELECT id, name')) { return [ ['id' => '1', 'name' => 'Keyword 1'], ['id' => '2', 'name' => 'Keyword 2'], ]; } + if (str_contains($query, 'SELECT name')) { + return ['Keyword 1']; + } + if (str_contains($query, 'WHERE name = {string:name}')) { return ['id' => '1']; } +/* if (str_contains($query, 'DELETE FROM {db_prefix}optimus_log_keywords')) { + return ['1']; + } */ + return []; } + + public function fetch_row($result): array|false|null + { + if ($result === ['id' => '1']) { + return ['1']; + } + + return $result ?? false; + } }; }); @@ -191,6 +204,7 @@ public function testQuery($query, $params = []): array test('prepareDisplayContext method', function () { Config::$modSettings['optimus_show_keywords_block'] = false; + Config::$modSettings['optimus_use_color_tags'] = true; expect($this->handler->prepareDisplayContext([]))->toBeNull(); @@ -211,7 +225,11 @@ public function testQuery($query, $params = []): array }); test('createTopic method', function () { - expect($this->handler->createTopic([], [], [])) + Config::$modSettings['optimus_allow_change_topic_keywords'] = true; + + $_REQUEST['optimus_keywords'] = 'key_1,key_2'; + + expect($this->handler->createTopic([], ['id' => 1], ['id' => 1])) ->toBeNull(); }); @@ -234,6 +252,7 @@ public function testQuery($query, $params = []): array Config::$modSettings['optimus_allow_change_topic_keywords'] = true; Utils::$context['is_new_topic'] = true; + Utils::$context['is_first_post'] = true; $this->request = Request::createFromGlobals(); $this->request->request->set('optimus_keywords', 'bar'); @@ -330,3 +349,59 @@ public function testQuery($query, $params = []): array expect(isset(Utils::$context['optimus_keywords']))->toBeFalse(); }); }); + +test('getAllKeywords method', function () { + $getAllKeywords = new ReflectionMethod($this->handler, 'getAllKeywords'); + $result = $getAllKeywords->invoke($this->handler); + + expect($result)->toBeArray(); +}); + +test('loadAssets method', function () { + $loadAssets = new ReflectionMethod($this->handler, 'loadAssets'); + $loadAssets->invoke($this->handler); + + expect(Utils::$context['css_files'])->toHaveKey('virtual-select.min_css') + ->and(Utils::$context['javascript_files'])->toHaveKey('virtual-select.min_js'); +}); + +test('getIdByName method', function () { + $getIdByName = new ReflectionMethod($this->handler, 'getIdByName'); + $result = $getIdByName->invoke($this->handler, 'id'); + + expect($result)->toBe(1); +}); + +test('addToDatabase method', function () { + $addToDatabase = new ReflectionMethod($this->handler, 'addToDatabase'); + $result = $addToDatabase->invoke($this->handler, 'test'); + + expect($result)->toBe(1); +}); + +test('preparedKeywords method', function () { + $_REQUEST['optimus_keywords'] = 'foo,bar'; + + $preparedKeywords = new ReflectionMethod($this->handler, 'preparedKeywords'); + $result = $preparedKeywords->invoke($this->handler); + + expect($result)->toBe(['foo', 'bar']); +}); + +test('modify method', function () { + Config::$modSettings['optimus_allow_change_topic_keywords'] = true; + + $_REQUEST['optimus_keywords'] = 'key_1,key_2'; + + $modify = new ReflectionMethod($this->handler, 'modify'); + $result = $modify->invoke($this->handler, 1, 1); + + expect($result)->toBeNull(); +}); + +test('remove method', function () { + $remove = new ReflectionMethod($this->handler, 'remove'); + $result = $remove->invoke($this->handler, [1, 2], 1); + + expect($result)->toBeNull(); +}); diff --git a/tests/Handlers/TopicHandlerTest.php b/tests/Handlers/TopicHandlerTest.php index 777f1b2..b01a7fe 100644 --- a/tests/Handlers/TopicHandlerTest.php +++ b/tests/Handlers/TopicHandlerTest.php @@ -151,7 +151,7 @@ public function testQuery($query, $params = []): array }); test('modifyPost method', function () { - expect($this->handler->modifyPost([], [], [], []))->toBeNull(); + expect($this->handler->modifyPost([], [], ['id' => 10], ['id' => 1, 'first_msg' => 10]))->toBeNull(); }); test('postEnd method - create', function () { diff --git a/tests/Pest.php b/tests/Pest.php index 4d06a3a..0f7e18d 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -50,6 +50,9 @@ User::$info['language'] = 'english'; Lang::$txt['lang_dictionary'] = 'en'; + Lang::$txt['no_matches'] = 'No matches'; + Lang::$txt['search'] = 'Search'; + Lang::$txt['remove'] = 'Remove'; Config::$boardurl = 'https://example.com'; Config::$scripturl = Config::$boardurl . '/index.php'; @@ -62,6 +65,7 @@ Utils::$context['forum_name'] = 'Test Forum'; Utils::$context['admin_menu_name'] = 'admin'; + Utils::$context['right_to_left'] = false; Utils::$smcFunc['substr'] = fn($string, $offset, $length) => substr($string, $offset, $length); Utils::$smcFunc['strlen'] = fn($string) => strlen($string); @@ -132,6 +136,7 @@ function cache_get_data(string $key, int $ttl = 120): ?array { if ($key == 'optimus_search_terms') return null; if ($key == 'optimus_topic_keywords') return null; + if ($key == 'optimus_all_keywords') return null; return []; } diff --git a/tests/Utils/CopyrightTest.php b/tests/Utils/CopyrightTest.php index fa12163..e708756 100644 --- a/tests/Utils/CopyrightTest.php +++ b/tests/Utils/CopyrightTest.php @@ -14,8 +14,13 @@ $link = Copyright::getLink(); - expect($link)->toContain('https://dragomano.ru/mods/optimus') - ->and($link)->toContain(' © 2010–' . date('Y') . ', Bugo'); + expect($link)->toContain('https://dragomano.ru/mods/optimus'); unset(Lang::$txt['lang_dictionary']); }); + +it('gets years', function () { + $link = Copyright::getYears(); + + expect($link)->toContain(' © 2010–' . date('Y') . ', Bugo'); +});