From 9a058b544318889ef45a56f7a1e4414ac8b6f47a Mon Sep 17 00:00:00 2001 From: thangnnmd <150879641+thangnnmd@users.noreply.github.com> Date: Mon, 4 Mar 2024 08:26:40 +0700 Subject: [PATCH] unitTest BlogHelper::getCurrentBlogTag (#3151) Co-authored-by: thangnn --- .../bc-blog/src/Model/Table/BlogTagsTable.php | 9 ++---- .../bc-blog/src/View/Helper/BlogHelper.php | 5 ++-- .../TestCase/Model/BlogTagsTableTest.php | 8 +++-- .../TestCase/View/Helper/BlogHelperTest.php | 30 ++++++++++++------- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogTagsTable.php b/plugins/bc-blog/src/Model/Table/BlogTagsTable.php index d57aa462c7..36e3a94b65 100755 --- a/plugins/bc-blog/src/Model/Table/BlogTagsTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogTagsTable.php @@ -115,15 +115,12 @@ public function hasNewTagAddablePermission($userGroupId, $blogContentId) /** * 指定した名称のブログタグ情報を取得する * - * @param string $name - * @return array + * @param $name + * @return EntityInterface */ public function getByName($name) { - return $this->find('first', - conditions: ['BlogTag.name' => $name], - recursive: -1, - callbacks: false); + return $this->find()->where(['BlogTags.name' => $name])->first(); } /** diff --git a/plugins/bc-blog/src/View/Helper/BlogHelper.php b/plugins/bc-blog/src/View/Helper/BlogHelper.php index 775c1cb8c3..058db0e8dd 100755 --- a/plugins/bc-blog/src/View/Helper/BlogHelper.php +++ b/plugins/bc-blog/src/View/Helper/BlogHelper.php @@ -2054,14 +2054,15 @@ public function getPostCount() * 現在のブログタグアーカイブのブログタグ情報を取得する * * @return array + * @unitTest */ public function getCurrentBlogTag() { $blogTag = []; if ($this->isTag()) { $pass = $this->_View->getRequest()->getParam('pass'); - $name = isset($pass[1])? $pass[1] : ''; - $BlogTagModel = ClassRegistry::init('Blog.BlogTag'); + $name = isset($pass[1]) ? $pass[1] : ''; + $BlogTagModel = TableRegistry::getTableLocator()->get('BcBlog.BlogTags'); $blogTag = $BlogTagModel->getByName(rawurldecode($name)); } return $blogTag; diff --git a/plugins/bc-blog/tests/TestCase/Model/BlogTagsTableTest.php b/plugins/bc-blog/tests/TestCase/Model/BlogTagsTableTest.php index f3637e0b57..e2e2a0a332 100755 --- a/plugins/bc-blog/tests/TestCase/Model/BlogTagsTableTest.php +++ b/plugins/bc-blog/tests/TestCase/Model/BlogTagsTableTest.php @@ -149,9 +149,11 @@ public static function findCustomParamsDataProvider() */ public function testGetByName($name, $expects) { - $this->markTestIncomplete('こちらのテストはまだ未確認です'); - $result = $this->BlogTag->getByName($name); - $this->assertEquals($expects, (bool)$result); + BlogTagFactory::make(['name' => 'タグ1'])->persist(); + BlogTagFactory::make(['name' => 'タグ2'])->persist(); + $rs = $this->BlogTagsTable->getByName($name); + //戻り値を確認 + $this->assertEquals($expects, (bool)$rs); } public static function getByNameDataProvider() diff --git a/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php b/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php index 21ec54dc42..4cc4e57db9 100755 --- a/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php +++ b/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php @@ -1770,28 +1770,36 @@ public static function isTagDataProvider() */ public function testGetCurrentBlogTag($url, $type, $isTag, $expects) { - $this->markTestIncomplete('こちらのテストはまだ未確認です'); - $this->Blog->request = $this->_getRequest($url); - $this->View->set('blogArchiveType', $type); + //create data test + BlogTagFactory::make([ + 'id' => '1', + 'name' => '新製品', + ])->persist(); + SiteFactory::make(['id' => 1])->persist(); + $this->Blog->getView()->setRequest($this->getRequest($url)->withAttribute('currentSite', SiteFactory::get(1))); + $this->Blog->getView()->set('blogArchiveType', $type); + //check blog isTag $result = $this->Blog->isTag(); $this->assertEquals($isTag, $result); + //check data expects $result = $this->Blog->getCurrentBlogTag(); - $this->assertEquals($expects, $result); + $actual = (!empty($result)) ? $result->toArray() : []; + unset($actual['created']); + unset($actual['modified']); + $this->assertEquals($expects, $actual); } public static function getCurrentBlogTagDataProvider() { return [ - ['/news/archives/tag/新製品', 'tag', true, [ - 'BlogTag' => [ - 'name' => '新製品', + ['/news/archives/tag/新製品', 'tag', true, + [ 'id' => '1', - 'created' => '2015-08-10 18:57:47', - 'modified' => null, - ], - ]], + 'name' => '新製品', + ] + ], ['/news/archives/tag/test1', 'tag', true, []], ['/news/archives/category/test2', 'category', false, []], ];