Skip to content

Commit

Permalink
unitTest BlogHelper::getCurrentBlogTag (#3151)
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 4, 2024
1 parent 310a0de commit 9a058b5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
9 changes: 3 additions & 6 deletions plugins/bc-blog/src/Model/Table/BlogTagsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions plugins/bc-blog/src/View/Helper/BlogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions plugins/bc-blog/tests/TestCase/Model/BlogTagsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 19 additions & 11 deletions plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, []],
];
Expand Down

0 comments on commit 9a058b5

Please sign in to comment.