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

fix #3249 カスタムコンテンツ:フィールド バリデーション不具合 #3309

Merged
merged 16 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
25 changes: 25 additions & 0 deletions plugins/baser-core/src/Model/Validation/BcValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,4 +614,29 @@ public static function hexColorPlus($value): bool
return preg_match('/\A([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})\z/i', $value);
}

/**
* Jsonをバリデーション
* 半角小文字英数字とアンダースコアを許容
* @param $string
* @param $key
* @return bool
* @checked
* @noTodo
* @unitTest
*/
public static function checkAlphaNumericWithJson($string, $key)
{
$value = json_decode($string, true);
$keys = explode('.', $key);

foreach ($keys as $k) {
$value = $value[$k];
}

if (empty($value) || preg_match("/^[a-z0-9_]+$/", $value)) {
return true;
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,32 @@ public function test_notBlankOnlyString()
$result = $this->BcValidation->notBlankOnlyString($str);
$this->assertTrue($result);
}
/**
* test checkAlphaNumericWithJson
*/
public function test_checkAlphaNumericWithJson()
{
//戻り=falseケース:全角文字
$key = 'BcCustomContent.email_confirm';
$str = '{"BcCustomContent":{"email_confirm":"ああ","max_file_size":"","file_ext":""}}';
$result = $this->BcValidation->checkAlphaNumericWithJson($str, $key);
$this->assertFalse($result);

//戻り=falseケース:半角スペース
$str = '{"BcCustomContent":{"email_confirm":" ","max_file_size":"","file_ext":""}}';
$result = $this->BcValidation->checkAlphaNumericWithJson($str, $key);
$this->assertFalse($result);

//戻り=falseケース:半角・全角
$str = '{"BcCustomContent":{"email_confirm":"ああaaa","max_file_size":"","file_ext":""}}';
$result = $this->BcValidation->checkAlphaNumericWithJson($str, $key);
$this->assertFalse($result);

//戻り=trueケース
$str = '{"BcCustomContent":{"email_confirm":"aaaa_bbb","max_file_size":"","file_ext":""}}';
$result = $this->BcValidation->checkAlphaNumericWithJson($str, $key);
$this->assertTrue($result);
}

/**
* test hexColorPlus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,20 @@
</ul>
</div>

<?php
$metaErrors = $entity->getError('meta');
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 別方法を判断しました。いかがでしょうか。

$emailConfirmClass = 'bca-textbox__input';
if (isset($metaErrors['checkAlphaNumericWithJson'])) {
$emailConfirmClass = 'bca-textbox__input form-error';
}
?>

<span v-show="showControlEmailConfirm" style="display: block">
<?php echo $this->BcAdminForm->label('meta.BcCustomContent.email_confirm', __d('baser_core', 'Eメール比較先フィールド名')) ?>&nbsp;
<?php echo $this->BcAdminForm->control('meta.BcCustomContent.email_confirm', [
'type' => 'text',
'size' => 20,
'class' => $emailConfirmClass
]) ?>
<i class="bca-icon--question-circle bca-help"></i>
<div class="bca-helptext">
Expand Down Expand Up @@ -170,6 +179,7 @@
</span>

<?php echo $this->BcAdminForm->error('validate') ?>
<?php echo $this->BcAdminForm->error('meta') ?>
</td>
</tr>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function initialize(array $config): void
* @return Validator
* @checked
* @noTodo
* @unitTest
*/
public function validationDefault(Validator $validator): Validator
{
Expand Down Expand Up @@ -89,6 +90,14 @@ public function validationDefault(Validator $validator): Validator
'message' => __d('baser_core', '選択リストに同じ項目を複数登録できません。')
]
]);
$validator
->add('meta', [
'checkAlphaNumericWithJson' => [
'rule' => ['checkAlphaNumericWithJson', 'BcCustomContent.email_confirm'],
'provider' => 'bc',
'message' => __d('baser_core', 'Eメール比較先フィールド名は半角小文字英数字とアンダースコアのみで入力してください。')
]
]);
return $validator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function test_edit()
$this->loadFixtureScenario(CustomFieldsScenario::class);
$data = CustomFieldFactory::get(1);
$data['title'] = 'test edit title';
$data['meta'] = ['BcCustomContent' => ['email_confirm' => 'aa']];
//APIを呼ぶ
$this->post('/baser/api/admin/bc-custom-content/custom_fields/edit/1.json?token=' . $this->accessToken, $data->toArray());
//ステータスを確認
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public function testEdit()
//Postデータを生成
$data = CustomFieldFactory::get(1);
$data['title'] = 'test edit title';
$data['meta'] = ['BcCustomContent' => ['email_confirm' => 'aa']];
//対象URLをコル
$this->post('/baser/admin/bc-custom-content/custom_fields/edit/1', $data->toArray());
$this->assertResponseCode(302);
Expand All @@ -191,7 +192,7 @@ public function testEdit()
$this->assertEquals(1, $query->count());

//タイトルを指定しない場合、
$this->post('/baser/admin/bc-custom-content/custom_fields/edit/1', ['title' => '']);
$this->post('/baser/admin/bc-custom-content/custom_fields/edit/1', ['title' => '', ]);
$this->assertResponseCode(200);
//エラーを確認
$vars = $this->_controller->viewBuilder()->getVars();
Expand Down Expand Up @@ -219,6 +220,7 @@ public function testBeforeEditEvent()
//Postデータを生成
$data = CustomFieldFactory::get(1);
$data['title'] = 'test edit title';
$data['meta'] = ['BcCustomContent' => ['email_confirm' => 'aa']];
//対象URLをコル
$this->post('/baser/admin/bc-custom-content/custom_fields/edit/1', $data->toArray());
//イベントに入るかどうか確認
Expand Down Expand Up @@ -246,6 +248,7 @@ public function testAfterEditEvent()
//Postデータを生成
$data = CustomFieldFactory::get(1);
$data['title'] = 'test edit title';
$data['meta'] = ['BcCustomContent' => ['email_confirm' => 'aa']];
//対象URLをコル
$this->post('/baser/admin/bc-custom-content/custom_fields/edit/1', $data->toArray());
//イベントに入るかどうか確認
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,92 @@
namespace BcCustomContent\Test\TestCase\Model\Table;

use BaserCore\TestSuite\BcTestCase;
use BcCustomContent\Model\Table\CustomFieldsTable;

/**
* CustomFieldsTableTest
*/
class CustomFieldsTableTest extends BcTestCase
{

/**
* @var CustomFieldsTable
*/
public $CustomFieldsTable;

/**
* Set up
*/
public function setUp(): void
{
parent::setUp();
$this->CustomFieldsTable = $this->getTableLocator()->get('BcCustomContent.CustomFields');
}

/**
* Tear down
*/
public function tearDown(): void
{
unset($this->CustomFieldsTable);
parent::tearDown();
}

public function test_validationDefault()
{
$validator = $this->CustomFieldsTable->getValidator('default');
//入力フィールドのデータが超えた場合、
$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']));

//入力フィールドのデータがNULL場合、
$errors = $validator->validate([
'name' => '',
'title' => '',
'type' => '',
]);
//戻り値を確認
$this->assertEquals('フィールド名を入力してください。', current($errors['name']));
$this->assertEquals('項目見出しを入力してください。', current($errors['title']));
$this->assertEquals('タイプを入力してください。', current($errors['type']));

//フィールド名は半角小文字英数字とアンダースコアのみ利用可能
$errors = $validator->validate([
'name' => 'test sss',
]);
//戻り値を確認
$this->assertEquals('フィールド名は半角小文字英数字とアンダースコアのみで入力してください。', current($errors['name']));
$errors = $validator->validate([
'name' => 'ひらがな',
]);
//戻り値を確認
$this->assertEquals('フィールド名は半角小文字英数字とアンダースコアのみで入力してください。', current($errors['name']));

//trueを返す
$errors = $validator->validate([
'name' => 'test_test',
]);
//戻り値を確認
$this->assertArrayNotHasKey('name', $errors);

//Eメール比較先フィールド名のバリデーション
//trueを返す
$errors = $validator->validate([
'meta' => '{"BcCustomContent":{"email_confirm":"aaaa_bbb","max_file_size":"","file_ext":""}}'
]);
//戻り値を確認
$this->assertArrayNotHasKey('meta', $errors);

//全角文字
$errors = $validator->validate([
'meta' => '{"BcCustomContent":{"email_confirm":"ああ","max_file_size":"","file_ext":""}}'
]);
//戻り値を確認
$this->assertEquals('Eメール比較先フィールド名は半角小文字英数字とアンダースコアのみで入力してください。', current($errors['meta']));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,15 @@ public function test_update()
$this->loadFixtureScenario(CustomFieldsScenario::class);
$customField = $this->CustomFieldsService->get(1);
$customField->title = 'test edit title';
$customField->meta = ['BcCustomContent' => ['email_confirm' => 'aa']];
//正常系をテスト
$rs = $this->CustomFieldsService->update($customField, $customField->toArray());
//戻る値を確認
$this->assertEquals($rs->title, 'test edit title');

//異常系をテスト
$customField->title = null;
$customField->meta = ['BcCustomContent' => ['email_confirm' => 'aa']];
$this->expectException(PersistenceFailedException::class);
$this->expectExceptionMessage('Entity save failure. Found the following errors (title._empty: "項目見出しを入力してください。")');
$this->CustomFieldsService->update($customField, $customField->toArray());
Expand Down
Loading