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

BcCustomContentControllerEventListener::startup()&setAdminMenu() のユニットテスト #4026

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BcCustomContentControllerEventListener extends \BaserCore\Event\BcControll
* Before render
* @checked
* @noTodo
* @unitTest
*/
public function startup(Event $event)
{
Expand All @@ -51,6 +52,7 @@ public function startup(Event $event)
* 管理画面メニュー用のデータをセットする
* @checked
* @noTodo
* @unitTest
*/
public function setAdminMenu()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?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 BcCustomContent\Test\TestCase\Event;

use BaserCore\Controller\Admin\UsersController;
use BaserCore\Service\BcDatabaseServiceInterface;
use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\TestSuite\BcTestCase;
use BcCustomContent\Event\BcCustomContentControllerEventListener;
use BcCustomContent\Service\CustomTablesServiceInterface;
use BcCustomContent\Test\Scenario\CustomContentsScenario;
use BcCustomContent\Test\Scenario\CustomEntriesScenario;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\Event\EventManager;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

/**
* Class BcCustomContentControllerEventListenerTest
*
* @property BcCustomContentControllerEventListener $BcCustomContentControllerEventListener
*/
class BcCustomContentControllerEventListenerTest extends BcTestCase
{
/**
* Trait
*/
use ScenarioAwareTrait;

/**
* @var UsersController
*/
public $UsersController;

/**
* @var EventManager|null
*/
public $eventManager;

/**
* setUp
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->loadFixtureScenario(InitAppScenario::class);
$this->eventManager = EventManager::instance();

$this->BcAuthenticationEventListener = new BcCustomContentControllerEventListener();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HungDV2022 変数名がおかしくないですか?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません。修正しました。

foreach ($this->BcAuthenticationEventListener->implementedEvents() as $key => $event) {
$this->eventManager->off($key);
}
$this->UsersController = new UsersController($this->loginAdmin($this->getRequest('/baser/admin/baser-core/users/')));
}

/**
* tearDown
*
* @return void
*/
public function tearDown(): void
{
$this->BcAuthenticationEventListener = null;
parent::tearDown();
}

/**
* test startup
*/
public function testStartupAnSetAdminMenu()
{
//データを生成
$dataBaseService = $this->getService(BcDatabaseServiceInterface::class);
$customTable = $this->getService(CustomTablesServiceInterface::class);
//カスタムテーブルとカスタムエントリテーブルを生成
$customTable->create([
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HungDV2022 このデータは使われてますか?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryuring
このデータは必要です。削除したらカスタムコンテンツをAdminMenuに反映しませんでした。

'id' => 1,
'name' => 'recruit_categories',
'title' => '求人情報',
'type' => '1',
'display_field' => 'title',
'has_child' => 0
]);
//フィクチャーからデーターを生成
$this->loadFixtureScenario(CustomContentsScenario::class);
$this->loadFixtureScenario(CustomEntriesScenario::class);

$listener = $this->getMockBuilder(BcCustomContentControllerEventListener::class)
->onlyMethods(['implementedEvents'])
->addMethods(['usersStartup'])
->getMock();

$listener->method('implementedEvents')
->willReturn(['Controller.BaserCore.Users.startup' => ['callable' => 'usersStartup']]);

$this->eventManager
->on($listener)
->on($this->BcAuthenticationEventListener)
->dispatch(new Event('Controller.startup', $this->UsersController, []));

//メーニューにカスタムタイトルがあるか確認
$menu = Configure::read('BcApp.adminNavigation.Contents');
$this->assertEquals('サービスタイトル', $menu['CustomContent1']['title']);
//不要なテーブルを削除
$dataBaseService->dropTable('custom_entry_1_recruit_categories');
}
}
Loading