Skip to content

Commit

Permalink
fix baserproject#2015 BlogPostsAdminService::getPublishLink のユニットテスト実装
Browse files Browse the repository at this point in the history
  • Loading branch information
Đỗ Văn Hùng authored and Đỗ Văn Hùng committed May 16, 2023
1 parent 45047fd commit 88178f7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function getViewVarsForEdit(ServerRequest $request, EntityInterface $post
* @return string
* @checked
* @noTodo
* @unitTest
*/
public function getPublishLink(BlogPost $post)
{
Expand Down
52 changes: 52 additions & 0 deletions plugins/bc-blog/tests/Scenario/BlogPostsAdminScenario.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\Factory\ContentFactory;
use BcBlog\Test\Factory\BlogContentFactory;
use BcBlog\Test\Factory\BlogPostFactory;
use CakephpFixtureFactories\Scenario\FixtureScenarioInterface;

/**
* BlogPostsAdminScenario
*
* 利用する場合は、テーブルの初期化に次のフィクスチャの定義が必要
* 'plugin.BcBlog.Factory/BlogPosts',
* 'plugin.BcBlog.Factory/BlogContents',
* 'plugin.BaserCore.Factory/Contents',
*/
class BlogPostsAdminScenario implements FixtureScenarioInterface
{

/**
* load
*/
public function load(...$args)
{
ContentFactory::make([
'id' => 100,
'url' => '/index',
'site_id' => 1,
'status' => true,
'entity_id' => 1,
'plugin' => 'BcBlog',
'type' => 'BlogContent',
'lft' => '1',
'rght' => '2',
'publish_begin' => '2020-01-27 12:00:00',
'publish_end' => '9000-01-27 12:00:00'
])->persist();
BlogPostFactory::make(['id' => 1, 'blog_content_id' => 1, 'no' => 1, 'status' => true])->persist();
BlogContentFactory::make(['id' => 1])->persist();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@

namespace BcBlog\Test\TestCase\Service\Admin;

use BaserCore\Service\UsersService;
use BaserCore\Service\UsersServiceInterface;
use BaserCore\Test\Factory\ContentFactory;
use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\TestSuite\BcTestCase;
use BcBlog\Service\Admin\BlogPostsAdminService;
use BcBlog\Service\Admin\BlogPostsAdminServiceInterface;
use BcBlog\Test\Factory\BlogContentFactory;
use BcBlog\Test\Factory\BlogPostFactory;
use BcBlog\Service\BlogPostsServiceInterface;
use BcBlog\Test\Scenario\BlogPostsAdminScenario;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

/**
* Class BlogContentsAdminServiceTest
* @property BlogPostsAdminService $BlogPostsAdminService
*/
class BlogPostsAdminServiceTest extends BcTestCase
{
Expand All @@ -47,6 +46,7 @@ class BlogPostsAdminServiceTest extends BcTestCase
'plugin.BaserCore.Factory/Contents',
'plugin.BcBlog.Factory/BlogPosts',
'plugin.BcBlog.Factory/BlogContents',
'plugin.BaserCore.Factory/Dblogs',
];

/**
Expand All @@ -58,6 +58,7 @@ public function setUp(): void
{
$this->setFixtureTruncate();
parent::setUp();
$this->BlogPostsAdminService = $this->getService(BlogPostsAdminServiceInterface::class);
}

/**
Expand All @@ -76,24 +77,12 @@ public function tearDown(): void
public function test_getViewVarsForIndex()
{
// データを作成する
ContentFactory::make([
'id' => 4,
'url' => '/index',
'site_id' => 1,
'status' => true,
'entity_id' => 1,
'plugin' => 'BcBlog',
'type' => 'BlogContent'
])->persist();
BlogContentFactory::make(['id' => 1])->persist();
$this->loadFixtureScenario(BlogPostsAdminScenario::class);
$this->loadFixtureScenario(InitAppScenario::class);

// 対象メーソドを実行する
/** @var BlogPostsAdminService $service */
$service = $this->getService(BlogPostsAdminServiceInterface::class);
$post = ['id' => 1];
$request = $this->loginAdmin($this->getRequest()->withParam('pass.0', 1));
$result = $service->getViewVarsForIndex($post, $request);
$result = $this->BlogPostsAdminService->getViewVarsForIndex($post, $request);

// 戻り値の中身を確認する
$this->assertEquals($post, $result['posts']);
Expand All @@ -107,29 +96,19 @@ public function test_getViewVarsForIndex()
*/
public function test_getViewVarsForAdd()
{
//サービスクラス
$blogPostsService = $this->getService(BlogPostsServiceInterface::class);
$userService = $this->getService(UsersServiceInterface::class);

// データを作成する
ContentFactory::make([
'id' => 4,
'url' => '/index',
'site_id' => 1,
'status' => true,
'entity_id' => 1,
'plugin' => 'BcBlog',
'type' => 'BlogContent'
])->persist();
BlogPostFactory::make(['id' => 1])->persist();
BlogContentFactory::make(['id' => 1])->persist();
$this->loadFixtureScenario(BlogPostsAdminScenario::class);
$this->loadFixtureScenario(InitAppScenario::class);

// 対象メーソドを実行する
/** @var BlogPostsAdminService $service */
$service = $this->getService(BlogPostsAdminServiceInterface::class);
$request = $this->getRequest('/baser/admin')->withParam('pass.0', 1);
$post = BlogPostFactory::get(1);
/** @var UsersService $userService */
$userService = $this->getService(UsersServiceInterface::class);

$post = $blogPostsService->get(1);
$user = $userService->get(1);
$result = $service->getViewVarsForAdd($request, $post, $user);
$result = $this->BlogPostsAdminService->getViewVarsForAdd($request, $post, $user);

// 戻り値の中身を確認する
$this->assertEquals($post, $result['post']);
Expand All @@ -148,29 +127,20 @@ public function test_getViewVarsForAdd()
*/
public function test_getViewVarsForEdit()
{
//サービスクラス
$userService = $this->getService(UsersServiceInterface::class);
$blogPostsService = $this->getService(BlogPostsServiceInterface::class);
$userService = $this->getService(UsersServiceInterface::class);

// データを作成する
ContentFactory::make([
'id' => 4,
'url' => '/index',
'site_id' => 1,
'status' => true,
'entity_id' => 1,
'plugin' => 'BcBlog',
'type' => 'BlogContent'
])->persist();
BlogPostFactory::make(['id' => 1, 'blog_content_id' => 1])->persist();
BlogContentFactory::make(['id' => 1])->persist();
$this->loadFixtureScenario(BlogPostsAdminScenario::class);
$this->loadFixtureScenario(InitAppScenario::class);

// 対象メーソドを実行する
/** @var BlogPostsAdminService $service */
$service = $this->getService(BlogPostsAdminServiceInterface::class);
$request = $this->getRequest('/baser/admin')->withParam('pass.0', 1);
$post = BlogPostFactory::get(1);
/** @var UsersService $userService */
$userService = $this->getService(UsersServiceInterface::class);

$post = $blogPostsService->get(1);
$user = $userService->get(1);
$result = $service->getViewVarsForEdit($request, $post, $user);
$result = $this->BlogPostsAdminService->getViewVarsForEdit($request, $post, $user);

// 戻り値の中身を確認する
$this->assertEquals($post, $result['post']);
Expand All @@ -190,7 +160,22 @@ public function test_getViewVarsForEdit()
*/
public function test_getPublishLink()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
// データを作成する
$this->loadFixtureScenario(BlogPostsAdminScenario::class);
$this->loadFixtureScenario(InitAppScenario::class);
//サービスクラス
$blogPostsService = $this->getService(BlogPostsServiceInterface::class);

//BlogPostsがPublishの場合、
$post = $blogPostsService->get(1);
$rs = $this->BlogPostsAdminService->getPublishLink($post);
$this->assertEquals($rs, 'https://localhost/archives/1');

//BlogPostsがunpublishの場合、
$blogPostsService->unpublish(1);
$post = $blogPostsService->get(1);
$rs = $this->BlogPostsAdminService->getPublishLink($post);
$this->assertEquals($rs, '');
}

/**
Expand Down

0 comments on commit 88178f7

Please sign in to comment.