Skip to content

Commit

Permalink
fix #1638 BlogHelper::getPostLinkUrl のユニットテスト実装 (#1781)
Browse files Browse the repository at this point in the history
  • Loading branch information
mb-t-goto committed Feb 1, 2023
1 parent 2204885 commit a3fbf7d
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 22 deletions.
3 changes: 2 additions & 1 deletion plugins/bc-blog/src/View/Helper/BlogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ public function getPostLink($post, $title, $options = [])
* @return string ブログ記事のURL
* @checked
* @noTodo
* @unitTest
*/
public function getPostLinkUrl($post, $base = true)
{
Expand All @@ -370,7 +371,7 @@ public function getPostLinkUrl($post, $base = true)
}
$sitesService = $this->getService(SitesServiceInterface::class);
$site = $sitesService->findByUrl($this->currentContent->url);
$contentUrl = $this->BcBaser->getContentsUrl($this->currentContent->url, !$this->isSameSiteBlogContent($post->blog_content_id), !empty($site->useSubDomain), false);
$contentUrl = $this->BcBaser->getContentsUrl($this->currentContent->url, !$this->isSameSiteBlogContent($post->blog_content_id), !empty($site->use_subdomain), false);
$url = $contentUrl . 'archives/' . $post->no;
if ($base) {
return $this->Url->build($url);
Expand Down
161 changes: 161 additions & 0 deletions plugins/bc-blog/tests/Scenario/MultiSiteBlogPostScenario.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
/**
* baserCMS : Based Website Development Project <https://basercms.net>
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
*
* @copyright Copyright (c) NPO baser foundation
* @link https://basercms.net baserCMS Project
* @since 5.0.0
* @license https://basercms.net/license/index.html MIT License
*/

namespace BcBlog\Test\Scenario;

use BaserCore\Test\Scenario\MultiSiteScenario;
use BcBlog\Test\Factory\BlogPostFactory;
use Cake\ORM\TableRegistry;
use CakephpFixtureFactories\Scenario\FixtureScenarioInterface;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

/**
* MultiSiteBlogPostScenario
*
* マルチサイトのデータセット
* site とそれに紐づく content / contentFolder を作成する
* content に紐づく blogContent / blogPost を作成する
* - /
* - /s/
* - /en/
* - /example.com/
* - /sub/
*
* 利用する場合は、テーブルの初期化に次のフィクスチャの定義が必要
* - plugin.BaserCore.Factory/Sites
* - plugin.BaserCore.Factory/Contents
* - plugin.BaserCore.Factory/ContentFolders
* - plugin.BcBlog.Factory/BlogContents
* - plugin.BcBlog.Factory/BlogPosts
*/
class MultiSiteBlogPostScenario implements FixtureScenarioInterface
{

/**
* Trait
*/
use ScenarioAwareTrait;

/**
* load
*/
public function load(...$args)
{
$this->loadFixtureScenario(MultiSiteScenario::class);
$this->createBlogContents();
$this->createBlogPosts();
}

/**
* ブログコンテンツを作成
*/
protected function createBlogContents()
{
$this->loadFixtureScenario(
BlogContentScenario::class,
6, // id
1, // siteId
1, // parentId
'news1', // name
'/news/' // url
);
$this->loadFixtureScenario(
BlogContentScenario::class,
7, // id
2, // siteId
1, // parentId
'news2', // name
'/s/news/' // url
);
$this->loadFixtureScenario(
BlogContentScenario::class,
8, // id
3, // siteId
1, // parentId
'news3', // name
'/en/news/' // url
);
$this->loadFixtureScenario(
BlogContentScenario::class,
9, // id
4, // siteId
1, // parentId
'news4', // name
'/example.com/news/' // url
);
$this->loadFixtureScenario(
BlogContentScenario::class,
10, // id
5, // siteId
1, // parentId
'news5', // name
'/sub/' // url
);
$contentsTable = TableRegistry::getTableLocator()->get('BaserCore.Contents');
$contentsTable->recover();
}

/**
* ブログ記事を作成
*/
protected function createBlogPosts()
{
BlogPostFactory::make([
'id' => 1,
'blog_content_id' => 6,
'no' => 3,
'name' => 'release',
'title' => 'プレスリリース',
'status' => 1,
])->persist();
BlogPostFactory::make([
'id' => 2,
'blog_content_id' => 7,
'no' => 4,
'name' => 'smartphone_release',
'title' => 'スマホサイトリリース',
'status' => 1,
])->persist();
BlogPostFactory::make([
'id' => 3,
'blog_content_id' => 8,
'no' => 5,
'name' => 'english_release',
'title' => '英語サイトリリース',
'status' => 1,
])->persist();
BlogPostFactory::make([
'id' => 4,
'blog_content_id' => 9,
'no' => 6,
'name' => 'another_domain_release',
'title' => '別サイトリリース',
'status' => 1,
])->persist();
BlogPostFactory::make([
'id' => 5,
'blog_content_id' => 10,
'no' => 7,
'name' => 'sub_domain_release',
'title' => '別サイトリリース',
'status' => 1,
])->persist();
BlogPostFactory::make([
'id' => 6,
'blog_content_id' => 11,
'no' => 3,
'name' => 'release',
'title' => 'プレスリリース',
'status' => 1,
])->persist();
}

}
55 changes: 34 additions & 21 deletions plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use BcBlog\Test\Factory\BlogContentFactory;
use BcBlog\Test\Factory\BlogPostFactory;
use BcBlog\Test\Scenario\BlogContentScenario;
use BcBlog\Test\Scenario\MultiSiteBlogPostScenario;
use BcBlog\Test\Scenario\MultiSiteBlogScenario;
use BcBlog\View\Helper\BlogHelper;
use Cake\Core\Configure;
Expand Down Expand Up @@ -232,39 +233,51 @@ public function testGetPostLink()
* ブログ記事のURLを取得する
*
* @param int $blogContentId ブログコンテンツID
* @param int $no ブログ記事NO
* @param string $baseUrl ベースURL
* @param bool $useBase ベースとなるURLを付与するかどうか
* @param string $expects 期待値
* @dataProvider getPostLinkUrlDataProvider
*/
public function testGetPostLinkUrl($blogContentId, $no, $base, $useBase, $expects)
public function testGetPostLinkUrl($blogContentId, $baseUrl, $useBase, $expects)
{
$this->markTestIncomplete('こちらのテストはまだ未確認です');
$this->truncateTable('contents');
$this->truncateTable('blog_contents');

// データ生成
$this->loadFixtureScenario(MultiSiteBlogPostScenario::class);

// ブログ記事を取得
$post = BlogPostFactory::find()->where(
['blog_content_id' => $blogContentId]
)->first();

// 現在のサイトを指定
$site = SiteFactory::get(1);
$request = $this->getRequest(
'/', [], 'GET', $baseUrl ? ['base' => $baseUrl] : []
)->withAttribute('currentSite', $site);
$this->Blog->getView()->setRequest($request);
$siteUrl = Configure::read('BcEnv.siteUrl');
Configure::write('BcEnv.siteUrl', 'http://main.com');
$this->loadFixtures('ContentBcContentsRoute', 'SiteBcContentsRoute', 'BlogContentMultiSite');
$this->Blog->request = $this->_getRequest('/');
$this->Blog->request->base = $base;
$post = ['BlogPost' => [
'blog_content_id' => $blogContentId,
'no' => $no,
]];
Configure::write('BcEnv.siteUrl', 'https://main.com');

// テスト対象メソッド
$result = $this->Blog->getPostLinkUrl($post, $useBase);
Configure::write('BcEnv.siteUrl', $siteUrl);
// Configure::write('BcEnv.siteUrl', $siteUrl);

$this->assertEquals($expects, $result, '記事へのリンクを正しく取得できません');
}

public function getPostLinkUrlDataProvider()
{
$this->markTestIncomplete('こちらのテストはまだ未確認です');
return [
[10, 3, '', false, false],
[1, 3, '', false, '/news/archives/3'],
[1, 3, '/sub', false, '/news/archives/3'],
[1, 3, '/sub', true, '/sub/news/archives/3'],
[3, 3, '', false, 'http://main.com/en/news/archives/3'],
[4, 3, '', false, 'http://sub.main.com/news/archives/3'],
[5, 3, '', false, 'http://another.com/news/archives/3'],
[6, 3, '', false, 'http://another.com/news/archives/3']
'コンテンツURLなし' => [11, '', false, false],
'ベースURLなし' => [6, '', false, '/news/archives/3'],
'ベースURLあり' => [6, '/sub', false, '/news/archives/3'],
'ベースURLあり、URL付与あり' => [6, '/sub', true, '/sub/news/archives/3'],
'sサイト' => [7, '', false, 'https://main.com/s/news/archives/4'],
'enサイト' => [8, '', false, 'https://main.com/en/news/archives/5'],
'別サイト' => [9, '', false, 'https://example.com/news/archives/6'],
'サブドメイン' => [10, '', false, 'https://sub.main.com/archives/7'],
];
}

Expand Down

0 comments on commit a3fbf7d

Please sign in to comment.