From b4b0fd49028fd99161244b17cc181e7ed8cc914a Mon Sep 17 00:00:00 2001 From: HungDV2022 Date: Mon, 18 Nov 2024 13:35:05 +0900 Subject: [PATCH 1/2] =?UTF-8?q?BcCustomContentControllerEventListener::sta?= =?UTF-8?q?rtup()&setAdminMenu()=E3=80=80=E3=81=AE=E3=83=A6=E3=83=8B?= =?UTF-8?q?=E3=83=83=E3=83=88=E3=83=86=E3=82=B9=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...BcCustomContentControllerEventListener.php | 2 + ...stomContentControllerEventListenerTest.php | 118 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 plugins/bc-custom-content/tests/TestCase/Event/BcCustomContentControllerEventListenerTest.php diff --git a/plugins/bc-custom-content/src/Event/BcCustomContentControllerEventListener.php b/plugins/bc-custom-content/src/Event/BcCustomContentControllerEventListener.php index a1377bb964..1458f8687f 100755 --- a/plugins/bc-custom-content/src/Event/BcCustomContentControllerEventListener.php +++ b/plugins/bc-custom-content/src/Event/BcCustomContentControllerEventListener.php @@ -38,6 +38,7 @@ class BcCustomContentControllerEventListener extends \BaserCore\Event\BcControll * Before render * @checked * @noTodo + * @unitTest */ public function startup(Event $event) { @@ -51,6 +52,7 @@ public function startup(Event $event) * 管理画面メニュー用のデータをセットする * @checked * @noTodo + * @unitTest */ public function setAdminMenu() { diff --git a/plugins/bc-custom-content/tests/TestCase/Event/BcCustomContentControllerEventListenerTest.php b/plugins/bc-custom-content/tests/TestCase/Event/BcCustomContentControllerEventListenerTest.php new file mode 100644 index 0000000000..5c68d259b9 --- /dev/null +++ b/plugins/bc-custom-content/tests/TestCase/Event/BcCustomContentControllerEventListenerTest.php @@ -0,0 +1,118 @@ + + * Copyright (c) NPO baser foundation + * + * @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(); + 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([ + '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'); + } +} From 3471fcdd92e0439bb81cc1bb4699ed86f5b1e65d Mon Sep 17 00:00:00 2001 From: HungDV2022 Date: Thu, 21 Nov 2024 11:20:14 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E3=83=A6=E3=83=8B=E3=83=83=E3=83=88?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...stomContentControllerEventListenerTest.php | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/plugins/bc-custom-content/tests/TestCase/Event/BcCustomContentControllerEventListenerTest.php b/plugins/bc-custom-content/tests/TestCase/Event/BcCustomContentControllerEventListenerTest.php index 5c68d259b9..db5665b0a9 100644 --- a/plugins/bc-custom-content/tests/TestCase/Event/BcCustomContentControllerEventListenerTest.php +++ b/plugins/bc-custom-content/tests/TestCase/Event/BcCustomContentControllerEventListenerTest.php @@ -13,12 +13,12 @@ use BaserCore\Controller\Admin\UsersController; use BaserCore\Service\BcDatabaseServiceInterface; +use BaserCore\Test\Factory\ContentFactory; 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 BcCustomContent\Test\Factory\CustomContentFactory; use Cake\Core\Configure; use Cake\Event\Event; use Cake\Event\EventManager; @@ -57,8 +57,8 @@ public function setUp(): void $this->loadFixtureScenario(InitAppScenario::class); $this->eventManager = EventManager::instance(); - $this->BcAuthenticationEventListener = new BcCustomContentControllerEventListener(); - foreach ($this->BcAuthenticationEventListener->implementedEvents() as $key => $event) { + $this->BcCustomContentControllerEventListener = new BcCustomContentControllerEventListener(); + foreach ($this->BcCustomContentControllerEventListener->implementedEvents() as $key => $event) { $this->eventManager->off($key); } $this->UsersController = new UsersController($this->loginAdmin($this->getRequest('/baser/admin/baser-core/users/'))); @@ -71,7 +71,7 @@ public function setUp(): void */ public function tearDown(): void { - $this->BcAuthenticationEventListener = null; + $this->BcCustomContentControllerEventListener = null; parent::tearDown(); } @@ -84,17 +84,15 @@ public function testStartupAnSetAdminMenu() $dataBaseService = $this->getService(BcDatabaseServiceInterface::class); $customTable = $this->getService(CustomTablesServiceInterface::class); //カスタムテーブルとカスタムエントリテーブルを生成 - $customTable->create([ - 'id' => 1, - 'name' => 'recruit_categories', - 'title' => '求人情報', - 'type' => '1', - 'display_field' => 'title', - 'has_child' => 0 - ]); - //フィクチャーからデーターを生成 - $this->loadFixtureScenario(CustomContentsScenario::class); - $this->loadFixtureScenario(CustomEntriesScenario::class); + $customTable->create(['id' => 1, 'name' => 'recruit_categories']); + CustomContentFactory::make(['id' => 1, 'custom_table_id' => 1])->persist(); + ContentFactory::make([ + 'plugin' => 'BcCustomContent', + 'type' => 'CustomContent', + 'site_id' => 1, + 'title' => 'サービスタイトル', + 'entity_id' => 1, + ])->persist(); $listener = $this->getMockBuilder(BcCustomContentControllerEventListener::class) ->onlyMethods(['implementedEvents']) @@ -106,7 +104,7 @@ public function testStartupAnSetAdminMenu() $this->eventManager ->on($listener) - ->on($this->BcAuthenticationEventListener) + ->on($this->BcCustomContentControllerEventListener) ->dispatch(new Event('Controller.startup', $this->UsersController, [])); //メーニューにカスタムタイトルがあるか確認