From 73b1a951391253a0c01428c4e7ac0813bad43bd8 Mon Sep 17 00:00:00 2001 From: thangnn Date: Fri, 26 Apr 2024 13:23:48 +0700 Subject: [PATCH] unitTest_CustomContentsTable_beforeSave --- .../src/Model/Table/CustomContentsTable.php | 1 + .../Model/Table/CustomContentsTableTest.php | 30 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/plugins/bc-custom-content/src/Model/Table/CustomContentsTable.php b/plugins/bc-custom-content/src/Model/Table/CustomContentsTable.php index c1d88cb522..6a882524c6 100644 --- a/plugins/bc-custom-content/src/Model/Table/CustomContentsTable.php +++ b/plugins/bc-custom-content/src/Model/Table/CustomContentsTable.php @@ -78,6 +78,7 @@ public function validationWithTable(Validator $validator): Validator * @return boolean * @checked * @noTodo + * @unitTest */ public function beforeSave(EventInterface $event, EntityInterface $entity, \ArrayObject $options) { diff --git a/plugins/bc-custom-content/tests/TestCase/Model/Table/CustomContentsTableTest.php b/plugins/bc-custom-content/tests/TestCase/Model/Table/CustomContentsTableTest.php index facd835119..44584cbb71 100644 --- a/plugins/bc-custom-content/tests/TestCase/Model/Table/CustomContentsTableTest.php +++ b/plugins/bc-custom-content/tests/TestCase/Model/Table/CustomContentsTableTest.php @@ -11,8 +11,14 @@ namespace BcCustomContent\Test\TestCase\Model\Table; +use ArrayObject; +use BaserCore\Service\PluginsServiceInterface; use BaserCore\TestSuite\BcTestCase; use BcCustomContent\Model\Table\CustomContentsTable; +use BcCustomContent\Service\CustomContentsServiceInterface; +use BcCustomContent\Test\Scenario\CustomContentsScenario; +use Cake\Event\Event; +use CakephpFixtureFactories\Scenario\ScenarioAwareTrait; /** * CustomContentsTableTest @@ -20,6 +26,10 @@ */ class CustomContentsTableTest extends BcTestCase { + /** + * ScenarioAwareTrait + */ + use ScenarioAwareTrait; /** * Set up @@ -64,7 +74,6 @@ public function test_validationWithTable() 'range' => '一覧表示件数は100までの数値で入力してください。', 'halfText' => '一覧表示件数は半角で入力してください。' ], $errors['list_count']); - //101を入力した場合 $validator = $this->CustomContentsTable->getValidator('withTable'); $errors = $validator->validate([ @@ -73,7 +82,6 @@ public function test_validationWithTable() $this->assertEquals([ 'range' => '一覧表示件数は100までの数値で入力してください。', ], $errors['list_count']); - //何も入力しない場合 $validator = $this->CustomContentsTable->getValidator('withTable'); $errors = $validator->validate([ @@ -83,4 +91,22 @@ public function test_validationWithTable() '_empty' => '一覧表示件数は必須項目です。', ], $errors['list_count']); } + + /** + * test beforeSave + */ + public function test_beforeSave() + { + //サービスクラス + $PluginsService = $this->getService(PluginsServiceInterface::class); + $customContentService = $this->getService(CustomContentsServiceInterface::class); + $PluginsService->attach('BcSearchIndex'); + + //set isExcluded true + $this->loadFixtureScenario(CustomContentsScenario::class); + $customContent = $customContentService->get(1); + $customContent->content->exclude_search = 1; + $this->CustomContentsTable->beforeSave(new Event("beforeSave"), $customContent, new ArrayObject()); + $this->assertTrue($this->CustomContentsTable->isExcluded()); + } }