From 1d1f4d08e05c64ce87d844fac87b6b6312e280d3 Mon Sep 17 00:00:00 2001 From: Nick Liu Date: Mon, 20 Feb 2023 02:09:19 +0100 Subject: [PATCH] Add tests for `news` plugin category links --- .../unit/plugins/news/PluginNewsTest.php | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 e107_tests/tests/unit/plugins/news/PluginNewsTest.php diff --git a/e107_tests/tests/unit/plugins/news/PluginNewsTest.php b/e107_tests/tests/unit/plugins/news/PluginNewsTest.php new file mode 100644 index 0000000000..b9b212b3da --- /dev/null +++ b/e107_tests/tests/unit/plugins/news/PluginNewsTest.php @@ -0,0 +1,124 @@ +create( + ["list", "category"], + ["category_id" => 579773, "name" => "regression"], + ["full" => false, "amp" => "&", "equal" => "=", "encode" => true, "lan" => null]); + $this->assertEquals("news.php?list.579773.0", $output); + } + + public function testNewsFrontCategoryUrl() + { + $payload = $this->simulateShowNewsItem(); + $this->testNewsFrontCategoryUrlSef( + $payload, + "core", + "/news.php?list.{$payload['category_id']}.0" + ); + } + + public function testNewsFrontCategoryUrlCoreSef() + { + $payload = $this->simulateShowNewsItem(); + $this->testNewsFrontCategoryUrlSef( + $payload, + "core/sef", + "/news/category/{$payload['category_id']}/{$payload['category_sef']}" + ); + } + + public function testNewsFrontCategoryUrlCoreSefFull() + { + $payload = $this->simulateShowNewsItem(); + $this->testNewsFrontCategoryUrlSef( + $payload, + "core/sef_full", + "/news/category/{$payload['category_sef']}" + ); + } + + public function testNewsFrontCategoryUrlCoreSefNoid() + { + $payload = $this->simulateShowNewsItem(); + $this->testNewsFrontCategoryUrlSef( + $payload, + "core/sef_noid", + "/news/category/{$payload['category_sef']}.html" + ); + } + + /** + * @param $payload array + * @param $sefType string + * @param $expected string + * @return void + */ + private function testNewsFrontCategoryUrlSef($payload, $sefType, $expected) + { + $urlConfig = $oldUrlConfig = e107::getConfig()->get('url_config', array()); + $urlConfig["news"] = $sefType; + e107::getConfig()->set('url_config', $urlConfig); + $router = new eRouter(); + $router->loadConfig(true); + $oldRouter = e107::getUrl()->front()->getRouter(); + try + { + e107::getUrl()->front()->setRouter($router); + $output = e107::getUrl()->create('news/list/category', $payload); + $this->assertEquals($expected, $output); + } + finally + { + e107::getUrl()->front()->setRouter($oldRouter); + e107::getConfig()->set('url_config', $oldUrlConfig); + } + } + + /** + * @return array + */ + private function simulateShowNewsItem() + { + $rowCount = e107::getDb()->select("news", "news_id"); + $this->assertGreaterThanOrEqual( + 1, + $rowCount, + "This integration test requires at least one news item in the database" + ); + $rows = e107::getDb()->rows(); + include_once e_PLUGIN . "news/news.php"; + $news = new news_front(); + + $reflection = new ReflectionClass($news); + $property = $reflection->getProperty("subAction"); + $property->setAccessible(true); + $property->setValue($news, current($rows)["news_id"]); + + $method = $reflection->getMethod("renderViewTemplate"); + $method->setAccessible(true); + try + { + $method->invoke($news); + } + catch(ReflectionException $e) + { + $this->fail("ReflectionException: " . $e->getMessage()); + } + + $property = $reflection->getProperty("currentRow"); + $property->setAccessible(true); + + return $property->getValue($news); + } +} \ No newline at end of file