Skip to content

Commit

Permalink
BlogHelper_getCategoryByName (#3177)
Browse files Browse the repository at this point in the history
Co-authored-by: thangnn <[email protected]>
  • Loading branch information
thangnnmd and thangnn authored Mar 5, 2024
1 parent 9a058b5 commit 8135483
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
14 changes: 8 additions & 6 deletions plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,23 @@ public function hasChild($id)
* @param int $blogContentId
* @param string $name
* @param array $options
* @return array|null
* @return EntityInterface
* @checked
* @noTodo
* @unitTest
*/
public function getByName($blogContentId, $name, $options = [])
{
$options = array_merge([
'conditions' => [
'BlogCategory.blog_content_id' => $blogContentId,
'BlogCategory.name' => urlencode($name),
'BlogCategories.blog_content_id' => $blogContentId,
'BlogCategories.name' => urlencode($name),
],
'recursive' => -1
], $options);
$this->unbindModel(['hasMany' => ['BlogPost']]);
return $this->find('first', $options);
}
return $this->find('all', $options)->first();

}
/**
* コピーする
*
Expand Down
9 changes: 7 additions & 2 deletions plugins/bc-blog/src/View/Helper/BlogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2024,15 +2024,20 @@ public function isSameSiteBlogContent($blogContentId)
* @param int $blogContentId
* @param string $categoryName
* @param array $options
* @return array
* @return EntityInterface
* @checked
* @noTodo
* @unitTest
*/

public function getCategoryByName($blogContentId, $categoryName = '', $options = [])
{
if (!$categoryName && $this->getBlogArchiveType() === 'category') {
$pass = $this->_View->getRequest()->getParam('pass');
$categoryName = $pass[count($pass) - 1];
}
return ClassRegistry::init('Blog.BlogCategory')->getByName($blogContentId, $categoryName, $options);
$blogCategoriesTable = TableRegistry::getTableLocator()->get('BcBlog.BlogCategories');
return $blogCategoriesTable->getByName($blogContentId, $categoryName, $options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,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);
}

Expand Down
26 changes: 18 additions & 8 deletions plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1532,23 +1532,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]
];
Expand Down

0 comments on commit 8135483

Please sign in to comment.