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

BbsSettingにバリデーション追加 #87

Merged
merged 1 commit into from
Jun 29, 2016
Merged
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
1 change: 0 additions & 1 deletion Model/BbsFrameSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public function saveBbsFrameSetting($data) {
//バリデーション
$this->set($data);
if (! $this->validates()) {
$this->rollback();
return false;
}

Expand Down
43 changes: 42 additions & 1 deletion Model/BbsSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,48 @@ class BbsSetting extends BbsesAppModel {
'Blocks.BlockRolePermission',
);

/**
* beforeValidate
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
* @link http://book.cakephp.org/2.0/ja/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->validate = Hash::merge($this->validate, array(
'bbs_key' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => __d('net_commons', 'Invalid request.'),
'allowEmpty' => false,
'required' => true,
'on' => 'update', // Limit validation to 'create' or 'update' operations
),
),
'use_workflow' => array(
'boolean' => array(
'rule' => array('boolean'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'use_like' => array(
'boolean' => array(
'rule' => array('boolean'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'use_unlike' => array(
'boolean' => array(
'rule' => array('boolean'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
));

return parent::beforeValidate($options);
}

/**
* Get bbs setting data
*
Expand Down Expand Up @@ -75,7 +117,6 @@ public function saveBbsSetting($data) {
//バリデーション
$this->set($data);
if (! $this->validates()) {
$this->rollback();
return false;
}

Expand Down
35 changes: 5 additions & 30 deletions Test/Case/Model/BbsSetting/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,38 +148,13 @@ public function dataProviderSaveOnValidationError() {
*/
public function dataProviderValidationError() {
return array(
array($this->__getData(), 'use_workflow', 'a', //PENDING バリデーションなし?
array($this->__getData(), 'use_workflow', 'a',
__d('net_commons', 'Invalid request.')),
array($this->__getData(), 'use_like', 'a',
__d('net_commons', 'Invalid request.')),
array($this->__getData(), 'use_unlike', 'a',
__d('net_commons', 'Invalid request.')),
);
}

/**
* Validatesのテスト
*
* @param array $data 登録データ
* @param string $field フィールド名
* @param string $value セットする値
* @param string $message エラーメッセージ
* @param array $overwrite 上書きするデータ
* @dataProvider dataProviderValidationError
* @return void
*/
public function testValidationError($data, $field, $value, $message, $overwrite = array()) {
$model = $this->_modelName;

if (is_null($value)) {
unset($data[$model][$field]);
} else {
$data[$model][$field] = $value;
}
$data = Hash::merge($data, $overwrite);

//validate処理実行
$this->$model->set($data);
$result = $this->$model->validates();
$this->assertTrue($result); //PENDING
//$this->assertFalse($result); PENDING バリデーションなしの場合?
//$this->assertEquals($this->$model->validationErrors[$field][0], $message);
}

}