Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlogHelper_getCategoryByName #3177

Merged
merged 9 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,21 +355,15 @@ public function hasChild($id)
* @param int $blogContentId
* @param string $name
* @param array $options
* @return array|null
* @return EntityInterface
HungDV2022 marked this conversation as resolved.
Show resolved Hide resolved
*/
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();
}

/**
* コピーする
*
Expand Down
6 changes: 4 additions & 2 deletions plugins/bc-blog/src/View/Helper/BlogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2022,15 +2022,17 @@ public function isSameSiteBlogContent($blogContentId)
* @param int $blogContentId
* @param string $categoryName
* @param array $options
* @return array
* @return EntityInterface
* @unitTest
HungDV2022 marked this conversation as resolved.
Show resolved Hide resolved
*/
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);
$blogCategoriesModel = TableRegistry::getTableLocator()->get('BcBlog.BlogCategories');
ryuring marked this conversation as resolved.
Show resolved Hide resolved
return $blogCategoriesModel->getByName($blogContentId, $categoryName, $options);
}

/**
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 @@ -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]
];
Expand Down
Loading