Skip to content

Commit

Permalink
CustomContentsController::index のユニットテスト (#2753)
Browse files Browse the repository at this point in the history
Co-authored-by: Đỗ Văn Hùng <[email protected]>
  • Loading branch information
HungDV2022 and dovanhung authored Sep 25, 2023
1 parent 4efa970 commit 284e92e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function initialize(): void
*
* @param CustomContentFrontServiceInterface $service
* @return \Cake\Http\Response
* @checked
* @noTodo
* @unitTest
*/
public function index(CustomContentFrontServiceInterface $service)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ public function load(...$args)
'id' => 1,
'custom_table_id' => 1,
'description' => 'サービステスト',
'template' => 'template_1',
'template' => 'default',
"widget_area" => null,
"list_count" => 10,
"list_order" => "id",
"list_direction" => "DESC"
])->persist();
ContentFactory::make([
'id' => 1,
'url' => '/',
'name' => '',
'id' => 100,
'url' => '/test/',
'name' => 'test',
'plugin' => 'BcCustomContent',
'type' => 'CustomContent',
'site_id' => 1,
Expand All @@ -46,17 +50,19 @@ public function load(...$args)
'lft' => 1,
'rght' => 2,
'entity_id' => 1,
"level" => 1,
'layout_template' => 'default',
'status' => true
])->persist();

CustomContentFactory::make([
'id' => 2,
'custom_table_id' => 2,
'description' => '求人',
'template' => 'template_2',
'template' => 'default',
])->persist();
ContentFactory::make([
'id' => 2,
'id' => 102,
'url' => '/recruit/',
'plugin' => 'BcCustomContent',
'type' => 'CustomContent',
Expand All @@ -66,6 +72,32 @@ public function load(...$args)
'lft' => 3,
'rght' => 4,
'entity_id' => 2,
'layout_template' => 'default',
'status' => true
])->persist();

CustomContentFactory::make([
'id' => 3,
'description' => 'サービステスト',
'template' => 'default',
"widget_area" => null,
"list_count" => 10,
"list_order" => "id",
"list_direction" => "DESC"
])->persist();
ContentFactory::make([
'url' => '/test-false/',
'name' => 'test-false',
'plugin' => 'BcCustomContent',
'type' => 'CustomContent',
'site_id' => 1,
'parent_id' => null,
'title' => 'サービスタイトル',
'lft' => 11,
'rght' => 21,
'entity_id' => 3,
"level" => 1,
'layout_template' => 'default',
'status' => true
])->persist();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@

namespace BcCustomContent\Test\TestCase\Controller;

use BaserCore\Service\BcDatabaseServiceInterface;
use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\TestSuite\BcTestCase;
use BaserCore\Utility\BcContainerTrait;
use BcCustomContent\Controller\CustomContentController;
use BcCustomContent\Service\CustomTablesServiceInterface;
use BcCustomContent\Test\Factory\CustomLinkFactory;
use BcCustomContent\Test\Scenario\CustomContentsScenario;
use BcCustomContent\Test\Scenario\CustomEntriesScenario;
use BcCustomContent\Test\Scenario\CustomFieldsScenario;
use Cake\Http\ServerRequest;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

Expand Down Expand Up @@ -74,7 +81,45 @@ public function test_initialize()
*/
public function test_index()
{
$this->markTestIncomplete('テストが未実装です');
$this->enableSecurityToken();
$this->enableCsrfToken();
//データーを生成
$this->loadFixtureScenario(InitAppScenario::class);
$dataBaseService = $this->getService(BcDatabaseServiceInterface::class);
$customTable = $this->getService(CustomTablesServiceInterface::class);

//カスタムテーブルとカスタムエントリテーブルを生成
$customTable->create([
'id' => 1,
'name' => 'recruit_categories',
'title' => '求人情報',
'type' => '1',
'display_field' => 'title',
'publish_begin' => '2021-10-01 00:00:00',
'publish_end' => '9999-11-30 23:59:59',
'has_child' => 0
]);
$this->loadFixtureScenario(CustomContentsScenario::class);
$this->loadFixtureScenario(CustomEntriesScenario::class);
$this->loadFixtureScenario(CustomFieldsScenario::class);

//対象URLをコル
$this->get('/test/');
$vars = $this->_controller->viewBuilder()->getVars();
$this->assertResponseCode(200);
$this->assertEquals('サービスタイトル', $vars['title']);
$this->assertNotNull($vars['customContent']);
$this->assertNotNull($vars['customEntries']);

//存在しないURLを指定した場合、
$this->get('/test-false/');
$this->assertResponseCode(404);
$this->assertEquals(
'カスタムコンテンツにカスタムテーブルが紐付けられていません。カスタムコンテンツの編集画面よりカスタムテーブルを選択してください。',
$_SESSION['Flash']['flash'][0]['message']
);
//不要なテーブルを削除
$dataBaseService->dropTable('custom_entry_1_recruit_categories');
}

/**
Expand Down

0 comments on commit 284e92e

Please sign in to comment.