From ea9621c550e48ffd3c63cc98d749bfeb69e3a7ef Mon Sep 17 00:00:00 2001 From: thangnn Date: Wed, 28 Feb 2024 13:18:38 +0700 Subject: [PATCH 1/7] add unitTest_BlogHelper_getCategoryByName --- .../bc-blog/src/View/Helper/BlogHelper.php | 10 +++++-- .../TestCase/View/Helper/BlogHelperTest.php | 26 +++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/plugins/bc-blog/src/View/Helper/BlogHelper.php b/plugins/bc-blog/src/View/Helper/BlogHelper.php index f16352a58f..48cb6ee90b 100755 --- a/plugins/bc-blog/src/View/Helper/BlogHelper.php +++ b/plugins/bc-blog/src/View/Helper/BlogHelper.php @@ -2022,7 +2022,8 @@ public function isSameSiteBlogContent($blogContentId) * @param int $blogContentId * @param string $categoryName * @param array $options - * @return array + * @return EntityInterface + * @unitTest */ public function getCategoryByName($blogContentId, $categoryName = '', $options = []) { @@ -2030,7 +2031,12 @@ public function getCategoryByName($blogContentId, $categoryName = '', $options = $pass = $this->_View->getRequest()->getParam('pass'); $categoryName = $pass[count($pass) - 1]; } - return ClassRegistry::init('Blog.BlogCategory')->getByName($blogContentId, $categoryName, $options); + + $blogCategoriesModel = TableRegistry::getTableLocator()->get('BcBlog.BlogCategories'); + return $blogCategoriesModel->find()->where([ + 'BlogCategories.blog_content_id' => $blogContentId, + 'BlogCategories.name' => urlencode($categoryName), + ])->first(); } /** diff --git a/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php b/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php index 8775c5bf4f..6dc86a554b 100755 --- a/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php +++ b/plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php @@ -1530,23 +1530,33 @@ public function testIsSameSiteBlogContent() } /** - * testGetCategoryByName - * @dataProvider getCategoryByName + * getCategoryByName + * @dataProvider getCategoryByNameDataprovider */ public function testGetCategoryByName($blogCategoryId, $type, $pass, $name, $expects) { - $this->markTestIncomplete('こちらのテストはまだ未確認です'); - $this->Blog->request = $this->_getRequest('/'); - $this->View->set('blogArchiveType', $type); - $this->Blog->request->params['pass'][1] = $pass; + //データ生成 + BlogCategoryFactory::make([ + 'blog_content_id' => 1, + 'name' => 'child', + ])->persist(); + + SiteFactory::make(['id' => 1])->persist(); + $this->Blog->getView()->setRequest($this->getRequest('/')->withAttribute('currentSite', SiteFactory::get(1))); + $this->Blog->getView()->set('blogArchiveType', $type); + + //pass param + $this->Blog->getView()->setRequest($this->getRequest()->withParam('pass', $pass)); $result = $this->Blog->getCategoryByName($blogCategoryId, $name); + + //check result $this->assertEquals($expects, (bool)$result); } - public static function getCategoryByName() + public static function getCategoryByNameDataprovider() { return [ - [1, 'category', 'child', '', true], + [1, 'category', ['child'], '', true], [1, 'hoge', '', 'child', true], [1, 'hoge', '', '', false] ]; From 6e9d6d9dc739549daab04a18414810b307c96a6f Mon Sep 17 00:00:00 2001 From: thangnn Date: Wed, 28 Feb 2024 14:50:33 +0700 Subject: [PATCH 2/7] modified --- .../src/Model/Table/BlogCategoriesTable.php | 15 +++++---------- plugins/bc-blog/src/View/Helper/BlogHelper.php | 6 +----- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php index 92898b2762..f60b4463f0 100755 --- a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php @@ -355,19 +355,14 @@ public function hasChild($id) * @param int $blogContentId * @param string $name * @param array $options - * @return array|null + * @return EntityInterface */ public function getByName($blogContentId, $name, $options = []) { - $options = array_merge([ - 'conditions' => [ - 'BlogCategory.blog_content_id' => $blogContentId, - 'BlogCategory.name' => urlencode($name), - ], - 'recursive' => -1 - ], $options); - $this->unbindModel(['hasMany' => ['BlogPost']]); - return $this->find('first', $options); + return $this->find()->where([ + 'BlogCategories.blog_content_id' => $blogContentId, + 'BlogCategories.name' => urlencode($name), + ])->first(); } /** diff --git a/plugins/bc-blog/src/View/Helper/BlogHelper.php b/plugins/bc-blog/src/View/Helper/BlogHelper.php index 48cb6ee90b..2d86e4c038 100755 --- a/plugins/bc-blog/src/View/Helper/BlogHelper.php +++ b/plugins/bc-blog/src/View/Helper/BlogHelper.php @@ -2031,12 +2031,8 @@ public function getCategoryByName($blogContentId, $categoryName = '', $options = $pass = $this->_View->getRequest()->getParam('pass'); $categoryName = $pass[count($pass) - 1]; } - $blogCategoriesModel = TableRegistry::getTableLocator()->get('BcBlog.BlogCategories'); - return $blogCategoriesModel->find()->where([ - 'BlogCategories.blog_content_id' => $blogContentId, - 'BlogCategories.name' => urlencode($categoryName), - ])->first(); + return $blogCategoriesModel->getByName($blogContentId, $categoryName, $options); } /** From 078fbda850553ab3ebca41d83c11c834e7112382 Mon Sep 17 00:00:00 2001 From: thangnn Date: Wed, 28 Feb 2024 14:52:06 +0700 Subject: [PATCH 3/7] modified --- plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php index f60b4463f0..44135afbf5 100755 --- a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php @@ -364,7 +364,6 @@ public function getByName($blogContentId, $name, $options = []) 'BlogCategories.name' => urlencode($name), ])->first(); } - /** * コピーする * From 3d3beae8bff16a938838b97e4d8584046194a941 Mon Sep 17 00:00:00 2001 From: thangnn Date: Wed, 28 Feb 2024 22:30:53 +0700 Subject: [PATCH 4/7] add comment and add unitTest_BlogCategoriesTable_getByName --- plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php | 3 +++ plugins/bc-blog/src/View/Helper/BlogHelper.php | 2 ++ .../tests/TestCase/Model/BlogCategoriesTableTest.php | 8 ++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php index 44135afbf5..63410b7e9d 100755 --- a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php @@ -355,6 +355,9 @@ public function hasChild($id) * @param int $blogContentId * @param string $name * @param array $options + * @notodo + * @checked + * @unitTest * @return EntityInterface */ public function getByName($blogContentId, $name, $options = []) diff --git a/plugins/bc-blog/src/View/Helper/BlogHelper.php b/plugins/bc-blog/src/View/Helper/BlogHelper.php index 2d86e4c038..91010b442d 100755 --- a/plugins/bc-blog/src/View/Helper/BlogHelper.php +++ b/plugins/bc-blog/src/View/Helper/BlogHelper.php @@ -2023,6 +2023,8 @@ public function isSameSiteBlogContent($blogContentId) * @param string $categoryName * @param array $options * @return EntityInterface + * @notodo + * @checked * @unitTest */ public function getCategoryByName($blogContentId, $categoryName = '', $options = []) diff --git a/plugins/bc-blog/tests/TestCase/Model/BlogCategoriesTableTest.php b/plugins/bc-blog/tests/TestCase/Model/BlogCategoriesTableTest.php index 87d1d725a1..8c7cfcccdb 100755 --- a/plugins/bc-blog/tests/TestCase/Model/BlogCategoriesTableTest.php +++ b/plugins/bc-blog/tests/TestCase/Model/BlogCategoriesTableTest.php @@ -498,6 +498,7 @@ public function testHasChild() /** * カテゴリ名よりカテゴリを取得する + * getByName * @dataProvider getByNameDataProvider * @param int $blogCategoryId * @param string $name @@ -505,8 +506,11 @@ public function testHasChild() */ public function testGetByName($blogCategoryId, $name, $expects) { - $this->markTestIncomplete('こちらのテストはまだ未確認です'); - $result = $this->BlogCategory->getByName($blogCategoryId, $name); + //データ生成 + BlogCategoryFactory::make(['name' => 'child', 'blog_content_id' => 1])->persist(); + BlogCategoryFactory::make(['name' => 'name', 'blog_content_id' => 2])->persist(); + $result = $this->BlogCategoriesTable->getByName($blogCategoryId, $name); + //戻り値を確認 $this->assertEquals($expects, (bool)$result); } From af68707dd979caa1849cd5b5e29b3fef95787c07 Mon Sep 17 00:00:00 2001 From: thangnn Date: Thu, 29 Feb 2024 10:01:57 +0700 Subject: [PATCH 5/7] modified --- plugins/bc-blog/src/View/Helper/BlogHelper.php | 2 +- .../bc-blog/tests/TestCase/Model/BlogCategoriesTableTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/bc-blog/src/View/Helper/BlogHelper.php b/plugins/bc-blog/src/View/Helper/BlogHelper.php index 91010b442d..50fda53a7b 100755 --- a/plugins/bc-blog/src/View/Helper/BlogHelper.php +++ b/plugins/bc-blog/src/View/Helper/BlogHelper.php @@ -2023,8 +2023,8 @@ public function isSameSiteBlogContent($blogContentId) * @param string $categoryName * @param array $options * @return EntityInterface - * @notodo * @checked + * @notodo * @unitTest */ public function getCategoryByName($blogContentId, $categoryName = '', $options = []) diff --git a/plugins/bc-blog/tests/TestCase/Model/BlogCategoriesTableTest.php b/plugins/bc-blog/tests/TestCase/Model/BlogCategoriesTableTest.php index 8c7cfcccdb..78d52ccb1a 100755 --- a/plugins/bc-blog/tests/TestCase/Model/BlogCategoriesTableTest.php +++ b/plugins/bc-blog/tests/TestCase/Model/BlogCategoriesTableTest.php @@ -498,7 +498,6 @@ public function testHasChild() /** * カテゴリ名よりカテゴリを取得する - * getByName * @dataProvider getByNameDataProvider * @param int $blogCategoryId * @param string $name From d9ad6a7f82bbe74c0a51baa082732e045b426755 Mon Sep 17 00:00:00 2001 From: thangnn Date: Thu, 29 Feb 2024 12:21:54 +0700 Subject: [PATCH 6/7] changed by r1506962929 --- plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php index 63410b7e9d..1240a1c733 100755 --- a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php @@ -362,10 +362,12 @@ public function hasChild($id) */ public function getByName($blogContentId, $name, $options = []) { - return $this->find()->where([ + $conditions = array_merge([ 'BlogCategories.blog_content_id' => $blogContentId, 'BlogCategories.name' => urlencode($name), - ])->first(); + ], $options); + return $this->find('all')->where($conditions)->first(); + } /** * コピーする From d06bb93277bacbe6765f46a49befc04e70d97fdf Mon Sep 17 00:00:00 2001 From: thangnn Date: Mon, 4 Mar 2024 11:13:45 +0700 Subject: [PATCH 7/7] modified by comment ticket --- .../src/Model/Table/BlogCategoriesTable.php | 15 +++++++++------ plugins/bc-blog/src/View/Helper/BlogHelper.php | 7 ++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php index 1553022c05..6b5cac670a 100755 --- a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php @@ -358,18 +358,21 @@ public function hasChild($id) * @param int $blogContentId * @param string $name * @param array $options - * @notodo + * @return EntityInterface * @checked + * @noTodo * @unitTest - * @return EntityInterface */ public function getByName($blogContentId, $name, $options = []) { - $conditions = array_merge([ - 'BlogCategories.blog_content_id' => $blogContentId, - 'BlogCategories.name' => urlencode($name), + $options = array_merge([ + 'conditions' => [ + 'BlogCategories.blog_content_id' => $blogContentId, + 'BlogCategories.name' => urlencode($name), + ], + 'recursive' => -1 ], $options); - return $this->find('all')->where($conditions)->first(); + return $this->find('all', $options)->first(); } /** diff --git a/plugins/bc-blog/src/View/Helper/BlogHelper.php b/plugins/bc-blog/src/View/Helper/BlogHelper.php index 368cba151a..82d8560e1e 100755 --- a/plugins/bc-blog/src/View/Helper/BlogHelper.php +++ b/plugins/bc-blog/src/View/Helper/BlogHelper.php @@ -2026,17 +2026,18 @@ public function isSameSiteBlogContent($blogContentId) * @param array $options * @return EntityInterface * @checked - * @notodo + * @noTodo * @unitTest */ + public function getCategoryByName($blogContentId, $categoryName = '', $options = []) { if (!$categoryName && $this->getBlogArchiveType() === 'category') { $pass = $this->_View->getRequest()->getParam('pass'); $categoryName = $pass[count($pass) - 1]; } - $blogCategoriesModel = TableRegistry::getTableLocator()->get('BcBlog.BlogCategories'); - return $blogCategoriesModel->getByName($blogContentId, $categoryName, $options); + $blogCategoriesTable = TableRegistry::getTableLocator()->get('BcBlog.BlogCategories'); + return $blogCategoriesTable->getByName($blogContentId, $categoryName, $options); } /**