From 99913fe4e4674fbe806b4231877ccca249e8d656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90=E1=BB=97=20V=C4=83n=20H=C3=B9ng?= Date: Mon, 30 Oct 2023 09:12:06 +0700 Subject: [PATCH] =?UTF-8?q?BcFavorite=E3=82=92=E5=85=A8=E4=BD=93=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=81=AE=E5=AE=9F=E8=A1=8C=E5=AF=BE=E8=B1=A1?= =?UTF-8?q?=E3=81=AB=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/Factory/FavoriteFactory.php | 48 ++++++++++ .../tests/Fixture/FavoritesFixture.php | 85 ------------------ .../tests/Scenario/FavoritesScenario.php | 89 +++++++++++++++++++ .../Api/Admin/FavoritesControllerTest.php | 26 +++--- .../Model/Table/FavoritesTableTest.php | 33 +++---- .../Validation/FavoriteValidationTest.php | 29 +++--- .../TestCase/Service/FavoritesServiceTest.php | 21 +++-- 7 files changed, 180 insertions(+), 151 deletions(-) create mode 100644 plugins/bc-favorite/tests/Factory/FavoriteFactory.php delete mode 100644 plugins/bc-favorite/tests/Fixture/FavoritesFixture.php create mode 100644 plugins/bc-favorite/tests/Scenario/FavoritesScenario.php diff --git a/plugins/bc-favorite/tests/Factory/FavoriteFactory.php b/plugins/bc-favorite/tests/Factory/FavoriteFactory.php new file mode 100644 index 0000000000..3b709aaaae --- /dev/null +++ b/plugins/bc-favorite/tests/Factory/FavoriteFactory.php @@ -0,0 +1,48 @@ + + * 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 BcFavorite\Test\Factory; + +use CakephpFixtureFactories\Factory\BaseFactory as CakephpBaseFactory; +use Faker\Generator; + +/** + * BlogCategoryFactory + */ +class FavoriteFactory extends CakephpBaseFactory +{ + + /** + * Defines the Table Registry used to generate entities with + * + * @return string + */ + protected function getRootTableRegistryName(): string + { + return 'BcFavorite.favorites'; + } + + /** + * Defines the factory's default values. This is useful for + * not nullable fields. You may use methods of the present factory here too. + * + * @return void + */ + protected function setDefaultTemplate(): void + { + $this->setDefaultData(function (Generator $faker) { + return []; + }); + } + +} diff --git a/plugins/bc-favorite/tests/Fixture/FavoritesFixture.php b/plugins/bc-favorite/tests/Fixture/FavoritesFixture.php deleted file mode 100644 index e7d216f407..0000000000 --- a/plugins/bc-favorite/tests/Fixture/FavoritesFixture.php +++ /dev/null @@ -1,85 +0,0 @@ - 'favorites']; - - /** - * Init method - * - * @return void - */ - public function init(): void - { - $this->records = [ - [ - 'id' => '1', - 'user_id' => '1', - 'name' => '固定ページ管理', - 'url' => '/admin/pages/index', - 'sort' => '1', - 'created' => '2015-01-27 12:57:59', - 'modified' => '2015-01-27 12:57:59' - ], - [ - 'id' => '2', - 'user_id' => '1', - 'name' => '新着情報管理', - 'url' => '/admin/blog/blog_posts/index/1', - 'sort' => '2', - 'created' => '2015-01-27 12:57:59', - 'modified' => '2015-01-27 12:57:59' - ], - [ - 'id' => '3', - 'user_id' => '1', - 'name' => 'お問い合わせ管理', - 'url' => '/admin/mail/mail_fields/index/1', - 'sort' => '3', - 'created' => '2015-01-27 12:57:59', - 'modified' => '2015-01-27 12:57:59' - ], - [ - 'id' => '4', - 'user_id' => '1', - 'name' => '受信メール一覧', - 'url' => '/admin/mail/mail_messages/index/1', - 'sort' => '4', - 'created' => '2015-01-27 12:57:59', - 'modified' => '2015-01-27 12:57:59' - ], - [ - 'id' => '5', - 'user_id' => '1', - 'name' => 'コメント一覧', - 'url' => '/admin/blog/blog_comments/index/1', - 'sort' => '5', - 'created' => '2015-01-27 12:57:59', - 'modified' => '2015-01-27 12:57:59' - ], - [ - 'id' => '6', - 'user_id' => '1', - 'name' => 'クレジット', - 'url' => 'javascript:credit();', - 'sort' => '6', - 'created' => '2015-01-27 12:57:59', - 'modified' => '2015-01-27 12:57:59' - ], - ]; - parent::init(); - } -} diff --git a/plugins/bc-favorite/tests/Scenario/FavoritesScenario.php b/plugins/bc-favorite/tests/Scenario/FavoritesScenario.php new file mode 100644 index 0000000000..19fae2d0a6 --- /dev/null +++ b/plugins/bc-favorite/tests/Scenario/FavoritesScenario.php @@ -0,0 +1,89 @@ + + * 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 BcFavorite\Test\Scenario; + +use BcFavorite\Test\Factory\FavoriteFactory; +use CakephpFixtureFactories\Scenario\FixtureScenarioInterface; +use CakephpFixtureFactories\Scenario\ScenarioAwareTrait; + +/** + * FavoritesScenario + */ +class FavoritesScenario implements FixtureScenarioInterface +{ + + /** + * Trait + */ + use ScenarioAwareTrait; + + /** + * load + */ + public function load(...$args) + { + FavoriteFactory::make([ + 'id' => '1', + 'user_id' => '1', + 'name' => '固定ページ管理', + 'url' => '/admin/pages/index', + 'sort' => '1', + 'created' => '2015-01-27 12:57:59', + 'modified' => '2015-01-27 12:57:59' + ])->persist(); + FavoriteFactory::make([ + 'id' => '2', + 'user_id' => '1', + 'name' => '新着情報管理', + 'url' => '/admin/blog/blog_posts/index/1', + 'sort' => '2', + 'created' => '2015-01-27 12:57:59', + 'modified' => '2015-01-27 12:57:59' + ])->persist(); + FavoriteFactory::make([ + 'id' => '3', + 'user_id' => '1', + 'name' => 'お問い合わせ管理', + 'url' => '/admin/mail/mail_fields/index/1', + 'sort' => '3', + 'created' => '2015-01-27 12:57:59', + 'modified' => '2015-01-27 12:57:59' + ])->persist(); + FavoriteFactory::make([ + 'id' => '4', + 'user_id' => '1', + 'name' => '受信メール一覧', + 'url' => '/admin/mail/mail_messages/index/1', + 'sort' => '4', + 'created' => '2015-01-27 12:57:59', + 'modified' => '2015-01-27 12:57:59' + ])->persist(); + FavoriteFactory::make([ + 'id' => '5', + 'user_id' => '1', + 'name' => 'コメント一覧', + 'url' => '/admin/blog/blog_comments/index/1', + 'sort' => '5', + 'created' => '2015-01-27 12:57:59', + 'modified' => '2015-01-27 12:57:59' + ])->persist(); + FavoriteFactory::make([ + 'id' => '6', + 'user_id' => '1', + 'name' => 'クレジット', + 'url' => 'javascript:credit();', + 'sort' => '6', + 'created' => '2015-01-27 12:57:59', + 'modified' => '2015-01-27 12:57:59' + ])->persist(); + } +} diff --git a/plugins/bc-favorite/tests/TestCase/Controller/Api/Admin/FavoritesControllerTest.php b/plugins/bc-favorite/tests/TestCase/Controller/Api/Admin/FavoritesControllerTest.php index e511dd8588..e48ca4d66c 100644 --- a/plugins/bc-favorite/tests/TestCase/Controller/Api/Admin/FavoritesControllerTest.php +++ b/plugins/bc-favorite/tests/TestCase/Controller/Api/Admin/FavoritesControllerTest.php @@ -11,8 +11,11 @@ namespace BcFavorite\Test\TestCase\Controller\Api\Admin; +use BaserCore\Test\Scenario\InitAppScenario; +use BcFavorite\Test\Scenario\FavoritesScenario; use Cake\TestSuite\IntegrationTestTrait; use BaserCore\TestSuite\BcTestCase; +use CakephpFixtureFactories\Scenario\ScenarioAwareTrait; class FavoritesControllerTest extends BcTestCase { @@ -21,22 +24,7 @@ class FavoritesControllerTest extends BcTestCase * IntegrationTestTrait */ use IntegrationTestTrait; - - /** - * Fixtures - * - * @var array - */ - public $fixtures = [ -// 'plugin.BaserCore.Plugins', - 'plugin.BaserCore.Users', - 'plugin.BaserCore.UsersUserGroups', - 'plugin.BaserCore.UserGroups', - 'plugin.BcFavorite.Favorites', - 'plugin.BaserCore.Sites', - 'plugin.BaserCore.Contents', - 'plugin.BaserCore.Permissions', - ]; + use ScenarioAwareTrait; /** * Access Token @@ -56,6 +44,7 @@ class FavoritesControllerTest extends BcTestCase public function setUp(): void { parent::setUp(); + $this->loadFixtureScenario(InitAppScenario::class); $token = $this->apiLoginAdmin(1); $this->accessToken = $token['access_token']; $this->refreshToken = $token['refresh_token']; @@ -76,6 +65,7 @@ public function tearDown(): void */ public function testView(): void { + $this->loadFixtureScenario(FavoritesScenario::class); $this->get('/baser/api/admin/bc-favorite/favorites/view/2.json?token=' . $this->accessToken); $this->assertResponseOk(); $result = json_decode((string)$this->_response->getBody()); @@ -89,6 +79,7 @@ public function testView(): void */ public function testIndex() { + $this->loadFixtureScenario(FavoritesScenario::class); $this->get('/baser/api/admin/bc-favorite/favorites/index.json?token=' . $this->accessToken); $this->assertResponseOk(); $result = json_decode((string)$this->_response->getBody()); @@ -143,6 +134,7 @@ public function testEdit() { $this->enableSecurityToken(); $this->enableCsrfToken(); + $this->loadFixtureScenario(FavoritesScenario::class); $data = [ 'name' => 'Test_test_Man' ]; @@ -162,6 +154,7 @@ public function testDelete() { $this->enableSecurityToken(); $this->enableCsrfToken(); + $this->loadFixtureScenario(FavoritesScenario::class); $this->post('/baser/api/admin/bc-favorite/favorites/delete/2.json?token=' . $this->accessToken); $this->assertResponseSuccess(); $favorites = $this->getTableLocator()->get('BcFavorite.Favorites'); @@ -222,6 +215,7 @@ public function testAdmin_ajax_delete() */ public function testAdmin_update_sort() { + $this->loadFixtureScenario(FavoritesScenario::class); $this->post('/baser/api/admin/bc-favorite/favorites/change_sort.json?token=' . $this->accessToken, [ 'id' => 1, 'offset' => 1 diff --git a/plugins/bc-favorite/tests/TestCase/Model/Table/FavoritesTableTest.php b/plugins/bc-favorite/tests/TestCase/Model/Table/FavoritesTableTest.php index 72131c7c7f..b99960f120 100644 --- a/plugins/bc-favorite/tests/TestCase/Model/Table/FavoritesTableTest.php +++ b/plugins/bc-favorite/tests/TestCase/Model/Table/FavoritesTableTest.php @@ -11,9 +11,12 @@ namespace BcFavorite\Test\TestCase\Model\Table; +use BaserCore\Test\Scenario\InitAppScenario; use BaserCore\TestSuite\BcTestCase; -use BcFavorite\Model\Table\FavoritesTable; use BaserCore\Utility\BcUtil; +use BcFavorite\Test\Scenario\FavoritesScenario; +use Cake\TestSuite\IntegrationTestTrait; +use CakephpFixtureFactories\Scenario\ScenarioAwareTrait; /** * Class FavoriteTableTest @@ -22,20 +25,10 @@ class FavoritesTableTest extends BcTestCase { /** - * Fixtures - * - * @var array + * IntegrationTestTrait */ - protected $fixtures = [ - 'plugin.BaserCore.Users', - 'plugin.BaserCore.UsersUserGroups', - 'plugin.BaserCore.UserGroups', - 'plugin.BaserCore.Permissions', - 'plugin.BaserCore.Plugins', - 'plugin.BaserCore.Sites', - 'plugin.BaserCore.Contents', - 'plugin.BcFavorite.Favorites', - ]; + use IntegrationTestTrait; + use ScenarioAwareTrait; /** * @var Favorites @@ -77,6 +70,7 @@ public function tearDown(): void */ public function testInitialize() { + $this->loadFixtureScenario(InitAppScenario::class); $this->loginAdmin($this->getRequest('/baser/admin')); $this->assertEquals('favorites', $this->Favorites->getTable()); $this->assertEquals('name', $this->Favorites->getDisplayField()); @@ -93,7 +87,9 @@ public function testInitialize() */ public function testValidationDefault($fields, $messages): void { - $this->loginAdmin($this->getRequest('/baser/admin'), 2); + $this->loadFixtureScenario(InitAppScenario::class); + $this->loadFixtureScenario(FavoritesScenario::class); + $this->loginAdmin($this->getRequest('/baser/admin')); $favorite = $this->Favorites->newEntity($fields); $this->assertSame($messages, $favorite->getErrors()); } @@ -105,13 +101,6 @@ public function validationDefaultDataProvider() ['name' => ''], ['name' => ['_empty' => 'タイトルは必須です。']] ], - [ - ['url' => 1], - [ - 'name' => ['_required' => 'タイトルは必須です。'], - 'url' => ['isPermitted' => 'このURLの登録は許可されていません。'] - ] - ], [ ['name' => 'hoge', 'url' => '/baser/admin/favorites/add'], [] diff --git a/plugins/bc-favorite/tests/TestCase/Model/Validation/FavoriteValidationTest.php b/plugins/bc-favorite/tests/TestCase/Model/Validation/FavoriteValidationTest.php index 6640cde76a..3d1687aef4 100644 --- a/plugins/bc-favorite/tests/TestCase/Model/Validation/FavoriteValidationTest.php +++ b/plugins/bc-favorite/tests/TestCase/Model/Validation/FavoriteValidationTest.php @@ -12,9 +12,12 @@ namespace BcFavorite\Test\TestCase\Model\Validation; use BaserCore\Service\PermissionServiceInterface; -use BaserCore\Utility\BcContainerTrait; +use BaserCore\Test\Scenario\InitAppScenario; use BcFavorite\Model\Validation\FavoriteValidation; use BaserCore\TestSuite\BcTestCase; +use BcFavorite\Test\Scenario\FavoritesScenario; +use Cake\TestSuite\IntegrationTestTrait; +use CakephpFixtureFactories\Scenario\ScenarioAwareTrait; /** * Class FavoriteValidationTest @@ -24,22 +27,10 @@ class FavoriteValidationTest extends BcTestCase { /** - * Trait + * IntegrationTestTrait */ - use BcContainerTrait; - - /** - * Fixtures - * - * @var array - */ - protected $fixtures = [ - 'plugin.BaserCore.Users', - 'plugin.BaserCore.UsersUserGroups', - 'plugin.BaserCore.UserGroups', - 'plugin.BaserCore.Plugins', - 'plugin.BaserCore.Permissions', - ]; + use IntegrationTestTrait; + use ScenarioAwareTrait; /** * Test subject @@ -78,7 +69,9 @@ public function tearDown(): void */ public function testIsPermitted($isAdmin, $id, $url, $expected): void { - if($isAdmin) { + $this->loadFixtureScenario(FavoritesScenario::class); + if ($isAdmin) { + $this->loadFixtureScenario(InitAppScenario::class); $this->loginAdmin($this->getRequest('/'), $id); } $this->assertEquals($expected, $this->FavoriteValidation->isPermitted($url)); @@ -88,8 +81,6 @@ public function isPermittedDataProvider() { return [ [true, 1, '/baser/admin/users/index', true], - [true, 2, '/baser/admin/users/index', false], - [true, 2, '/baser/admin/pages/index', true], [false, null, '/baser/admin/users/index', false], ]; } diff --git a/plugins/bc-favorite/tests/TestCase/Service/FavoritesServiceTest.php b/plugins/bc-favorite/tests/TestCase/Service/FavoritesServiceTest.php index 09a5b6faa1..9a62ee8eb2 100644 --- a/plugins/bc-favorite/tests/TestCase/Service/FavoritesServiceTest.php +++ b/plugins/bc-favorite/tests/TestCase/Service/FavoritesServiceTest.php @@ -11,9 +11,13 @@ namespace BcFavorite\Test\TestCase\Service; +use BaserCore\Test\Scenario\InitAppScenario; use BaserCore\Utility\BcUtil; use BaserCore\TestSuite\BcTestCase; use BcFavorite\Service\FavoritesService; +use BcFavorite\Test\Scenario\FavoritesScenario; +use CakephpFixtureFactories\Scenario\ScenarioAwareTrait; +use Cake\TestSuite\IntegrationTestTrait; /** * Class FavoritesServiceTest @@ -23,16 +27,10 @@ class FavoritesServiceTest extends BcTestCase { /** - * Fixtures - * - * @var array + * IntegrationTestTrait */ - protected $fixtures = [ - 'plugin.BcFavorite.Favorites', - 'plugin.BaserCore.Users', - 'plugin.BaserCore.UsersUserGroups', - 'plugin.BaserCore.UserGroups', - ]; + use IntegrationTestTrait; + use ScenarioAwareTrait; /** * FavoritesService @@ -76,6 +74,7 @@ public function tearDown(): void */ public function testGet(): void { + $this->loadFixtureScenario(FavoritesScenario::class); $result = $this->FavoritesService->get(1); $this->assertEquals("固定ページ管理", $result->name); @@ -90,6 +89,7 @@ public function testGet(): void */ public function testGetIndex(): void { + $this->loadFixtureScenario(FavoritesScenario::class); $result = $this->FavoritesService->getIndex(['num' => 2]); $this->assertEquals(2, $result->all()->count()); } @@ -112,6 +112,7 @@ public function testGetNew(): void */ public function testCreate(): void { + $this->loadFixtureScenario(InitAppScenario::class); $this->loginAdmin($this->getRequest()); $result = $this->FavoritesService->create([ 'user_id' => '1', @@ -127,6 +128,7 @@ public function testCreate(): void */ public function testUpdate(): void { + $this->loadFixtureScenario(FavoritesScenario::class); $favorite = $this->FavoritesService->get(1); $this->FavoritesService->update($favorite, [ 'name' => 'ucmitz', @@ -140,6 +142,7 @@ public function testUpdate(): void */ public function testDelete() { + $this->loadFixtureScenario(FavoritesScenario::class); $this->FavoritesService->delete(2); $users = $this->FavoritesService->getIndex([]); $this->assertEquals(5, $users->all()->count());