Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 21 additions & 5 deletions plugins/bc-mail/src/Model/Table/MailFieldsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,17 @@ public function validationDefault(Validator $validator): Validator
->scalar('group_field')
->maxLength('group_field', 255, __d('baser_core', 'グループ名は255文字以内で入力してください。'))
->add('group_field', [
'halfTextMailField' => [
'rule' => 'halfTextMailField',
'halfTextMailGroupField' => [
'rule' => 'halfTextMailGroupField',
'provider' => 'table',
'message' => __d('baser_core', 'グループ名は半角英数字、ハイフン、アンダースコアで入力してください。')
]]);
$validator
->scalar('group_valid')
->maxLength('group_valid', 255, __d('baser_core', 'グループ入力チェックは255文字以内で入力してください。'))
->add('group_valid', [
'halfTextMailField' => [
'rule' => 'halfTextMailField',
'halfTextMailGroupField' => [
'rule' => 'halfTextMailGroupField',
'provider' => 'table',
'message' => __d('baser_core', 'グループ入力チェックは半角英数字、ハイフン、アンダースコアで入力してください。')
]]);
Expand Down Expand Up @@ -233,7 +233,7 @@ public function duplicateMailField(string $value, array $context)

/**
* メールフィールドの値として正しい文字列か検証する
* 半角小文字英数-_
* 半角小文字英数字とアンダースコアを許容
* メールフィールドは、DBテーブルのフィールドとして利用されるため、
* 大文字を利用した場合にクォートを入れないとエラーとなってしまう
*
Expand Down Expand Up @@ -354,4 +354,20 @@ public function formatSource($source)
return implode("\n", $sourceList);
}

/**
* グループ系フィールドの値として正しい文字列か検証する
* 半角英数字、ハイフン、アンダースコアを許容
*
* @param string $value
* @return bool
* @checked
* @noTodo
* @unitTest
*/
public function halfTextMailGroupField(string $value): bool
{
$pattern = "/^[a-zA-Z0-9_-]*$/";
return preg_match($pattern, $value) === 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ public function test_validationDefaultHankakuCheck()
$this->assertEquals('グループ入力チェックは半角英数字、ハイフン、アンダースコアで入力してください。', current($errors['group_valid']));
}

public function test_validationDefaultAllowHyphenOnlyForGroupFields()
{
$validator = $this->MailFieldsTable->getValidator('default');
$errors = $validator->validate([
'name' => 'test',
'mail_content_id' => 999,
'type' => 'text',
'field_name' => 'field-name',
'group_field' => 'Group-Name',
'group_valid' => 'Group-Valid'
]);

$this->assertEquals('フィールド名は小文字の半角英数字、アンダースコアのみで入力してください。', current($errors['field_name']));
$this->assertArrayNotHasKey('group_field', $errors);
$this->assertArrayNotHasKey('group_valid', $errors);
}

public function test_validationDefaultDuplicate()
{
MailFieldsFactory::make(['mail_content_id' => 1, 'field_name' => 'field_1'])->persist();
Expand Down Expand Up @@ -212,7 +229,7 @@ public function testDuplicateMailField()

/**
* メールフィールドの値として正しい文字列か検証する
* 半角英数-_
* 半角小文字英数字とアンダースコア
*/
public function testHalfTextMailField()
{
Expand All @@ -225,6 +242,27 @@ public function testHalfTextMailField()
$string = 'abcABC123_';
$result = $this->MailFieldsTable->halfTextMailField($string);
$this->assertFalse($result);

//case false
$string = 'abc123-_';
$result = $this->MailFieldsTable->halfTextMailField($string);
$this->assertFalse($result);
}

/**
* グループ系フィールドの値として正しい文字列か検証する
*/
public function testHalfTextMailGroupField()
{
//case true
$string = 'abcABC123-_';
$result = $this->MailFieldsTable->halfTextMailGroupField($string);
$this->assertTrue($result);

//case false
$string = 'abcABC123-_:!';
$result = $this->MailFieldsTable->halfTextMailGroupField($string);
$this->assertFalse($result);
}

/**
Expand Down
Loading