From 38a227f886c680809929afd8bfe53a581dc331c9 Mon Sep 17 00:00:00 2001 From: HungDV2022 <110375578+HungDV2022@users.noreply.github.com> Date: Thu, 22 Feb 2024 07:57:51 +0900 Subject: [PATCH] =?UTF-8?q?fix=20#3110=E3=80=905.1=E3=80=91=E3=83=95?= =?UTF-8?q?=E3=82=A3=E3=83=BC=E3=83=AB=E3=83=89=E7=B7=A8=E9=9B=86=E3=81=AB?= =?UTF-8?q?=E3=81=A6=E3=83=86=E3=82=AD=E3=82=B9=E3=83=88=E9=96=A2=E9=80=A3?= =?UTF-8?q?=E8=A8=AD=E5=AE=9A=E3=81=AB=E6=95=B0=E5=AD=97=E4=BB=A5=E5=A4=96?= =?UTF-8?q?=E3=81=AE=E6=96=87=E5=AD=97=E3=82=92=E5=85=A5=E5=8A=9B=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=82=82=E4=BF=9D=E5=AD=98=E3=81=A7=E3=81=8D=E3=81=A6?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=86=E3=80=90=E3=82=AB=E3=82=B9=E3=82=BF?= =?UTF-8?q?=E3=83=A0=E3=82=B3=E3=83=B3=E3=83=86=E3=83=B3=E3=83=84=EF=BC=9E?= =?UTF-8?q?=E3=83=95=E3=82=A3=E3=83=BC=E3=83=AB=E3=83=89=E7=B7=A8=E9=9B=86?= =?UTF-8?q?=E3=80=91=20(#3139)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Đỗ Văn Hùng --- .../src/Model/Table/CustomFieldsTable.php | 8 +++ .../Model/Table/CustomFieldsTableTest.php | 62 ++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/plugins/bc-custom-content/src/Model/Table/CustomFieldsTable.php b/plugins/bc-custom-content/src/Model/Table/CustomFieldsTable.php index 3c58277108..c525b61c02 100644 --- a/plugins/bc-custom-content/src/Model/Table/CustomFieldsTable.php +++ b/plugins/bc-custom-content/src/Model/Table/CustomFieldsTable.php @@ -74,6 +74,14 @@ public function validationDefault(Validator $validator): Validator $validator ->scalar('type') ->notEmptyString('type', __d('baser_core', 'タイプを入力してください。')); + + $validator + ->allowEmptyString('size') + ->integer('size', __d('baser_core', '横幅サイズは整数を入力してください。')); + $validator + ->allowEmptyString('max_length') + ->integer('max_length', __d('baser_core', '最大文字数は整数を入力してください。')); + $validator ->add('source', [ 'checkSelectList' => [ diff --git a/plugins/bc-custom-content/tests/TestCase/Model/Table/CustomFieldsTableTest.php b/plugins/bc-custom-content/tests/TestCase/Model/Table/CustomFieldsTableTest.php index 0e654b586f..883bd21e9e 100644 --- a/plugins/bc-custom-content/tests/TestCase/Model/Table/CustomFieldsTableTest.php +++ b/plugins/bc-custom-content/tests/TestCase/Model/Table/CustomFieldsTableTest.php @@ -12,9 +12,12 @@ namespace BcCustomContent\Test\TestCase\Model\Table; use BaserCore\TestSuite\BcTestCase; +use BcCustomContent\Model\Table\CustomFieldsTable; +use BcCustomContent\Test\Factory\CustomFieldFactory; /** * CustomFieldsTableTest + * @property CustomFieldsTable $customFieldsTable */ class CustomFieldsTableTest extends BcTestCase { @@ -25,6 +28,7 @@ class CustomFieldsTableTest extends BcTestCase public function setUp(): void { parent::setUp(); + $this->customFieldsTable = $this->getTableLocator()->get('BcCustomContent.CustomFields'); } /** @@ -32,6 +36,7 @@ public function setUp(): void */ public function tearDown(): void { + unset($this->customFieldsTable); parent::tearDown(); } @@ -48,7 +53,62 @@ public function test_initialize() */ public function test_validationDefault() { - $this->markTestIncomplete('このテストは、まだ実装されていません。'); + $validator = $this->customFieldsTable->getValidator('default'); + //バリディションを発生しない テスト + $errors = $validator->validate([ + 'name' => 'a', + 'title' => 'a', + 'type' => 'group', + 'size' => '', + 'max_length' => '', + 'source' => '', + ]); + $this->assertCount(0, $errors); + + //notEmptyString テスト + $errors = $validator->validate([ + 'name' => '', + 'title' => '', + 'type' => '', + 'size' => '', + 'max_length' => '', + 'source' => '', + ]); + $this->assertEquals('フィールド名を入力してください。', current($errors['name'])); + $this->assertEquals('項目見出しを入力してください。', current($errors['title'])); + $this->assertEquals('タイプを入力してください。', current($errors['type'])); + + //maxLength テスト + $errors = $validator->validate([ + 'name' => str_repeat('a', 256), + 'title' => str_repeat('a', 256) + ]); + $this->assertEquals('フィールド名は255文字以内で入力してください。', current($errors['name'])); + $this->assertEquals('項目見出しは255文字以内で入力してください。', current($errors['title'])); + + //フィールド名は半角英数字とアンダースコア & 横幅サイズと最大文字数は整数ではない テスト + $errors = $validator->validate([ + 'name' => 'あ', + 'size' => 'a', + 'max_length' => 'b', + ]); + $this->assertEquals('フィールド名は半角英数字とアンダースコアのみで入力してください。', current($errors['name'])); + $this->assertEquals('横幅サイズは整数を入力してください。', current($errors['size'])); + $this->assertEquals('最大文字数は整数を入力してください。', current($errors['max_length'])); + + //validateUnique テスト + CustomFieldFactory::make(['name' => 'test'])->persist(); + $errors = $validator->validate([ + 'name' => 'test', + ]); + $this->assertEquals('既に登録のあるフィールド名です。', current($errors['name'])); + + //validate checkSelectList テスト + CustomFieldFactory::make(['name' => 'test'])->persist(); + $errors = $validator->validate([ + 'source' => "あ\rべ\r\nあ\nべ\ntest", + ]); + $this->assertEquals('選択リストに同じ項目を複数登録できません。', current($errors['source'])); } /**