Skip to content

Commit

Permalink
fix #2189 BcValidation::checkDateの調査 (#2902)
Browse files Browse the repository at this point in the history
  • Loading branch information
seto1 committed Dec 1, 2023
1 parent cde5aa4 commit fe66b59
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 107 deletions.
51 changes: 16 additions & 35 deletions plugins/baser-core/src/Model/Table/ContentsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,22 @@ public function validationDefault(Validator $validator): Validator
]
]);
$validator
->dateTime('self_publish_begin')
->allowEmptyDateTime('self_publish_begin')
->add('self_publish_begin', [
'checkDate' => [
'rule' => ['checkDate'],
'provider' => 'bc',
'dateTime' => [
'rule' => ['dateTime'],
'message' => __d('baser_core', '公開開始日に不正な文字列が入っています。')
]
]);
])
->allowEmptyDateTime('self_publish_begin');

$validator
->dateTime('self_publish_end')
->allowEmptyDateTime('self_publish_end')
->add('self_publish_end', [
'checkDate' => [
'rule' => ['checkDate'],
'provider' => 'bc',
'dateTime' => [
'rule' => ['dateTime'],
'message' => __d('baser_core', '公開終了日に不正な文字列が入っています。')
]
])
->allowEmptyDateTime('self_publish_end')
->add('self_publish_end', [
'checkDateAfterThan' => [
'rule' => ['checkDateAfterThan', 'self_publish_begin'],
Expand All @@ -221,26 +217,22 @@ public function validationDefault(Validator $validator): Validator
]
]);
$validator
->dateTime('created_date')
->requirePresence('created_date', 'create', __d('baser_core', '作成日がありません。'))
->notEmptyDateTime('created_date', __d('baser_core', '作成日が空になってます。'))
->add('created_date', [
'checkDate' => [
'rule' => ['checkDate'],
'provider' => 'bc',
'dateTime' => [
'rule' => ['dateTime'],
'message' => __d('baser_core', '作成日が正しくありません。')
]
]);
])
->requirePresence('created_date', 'create', __d('baser_core', '作成日がありません。'))
->notEmptyDateTime('created_date', __d('baser_core', '作成日が空になってます。'));
$validator
->datetime('modified_date')
->notEmptyDateTime('modified_date', __d('baser_core', '更新日が空になってます。'), 'update')
->add('modified_date', [
'checkDate' => [
'rule' => ['checkDate'],
'provider' => 'bc',
'dateTime' => [
'rule' => ['dateTime'],
'message' => __d('baser_core', '更新日が正しくありません。')
]
]);
])
->notEmptyDateTime('modified_date', __d('baser_core', '更新日が空になってます。'), 'update');
return $validator;
}

Expand Down Expand Up @@ -342,19 +334,8 @@ public function beforeMarshal(EventInterface $event, ArrayObject $content, Array
if (isset($content['name'])) {
$content['name'] = BcUtil::urlencode(mb_substr($content['name'], 0, 230, 'UTF-8'));
}
if (!empty($content['self_publish_begin'])) {
$content['self_publish_begin'] = new FrozenTime($content['self_publish_begin']);
}
if (!empty($content['self_publish_end'])) {
$content['self_publish_end'] = new FrozenTime($content['self_publish_end']);
}
if (empty($content['modified_date'])) {
$content['modified_date'] = FrozenTime::now();
} else {
$content['modified_date'] = new FrozenTime($content['modified_date']);
}
if (!empty($content['created_date'])) {
$content['created_date'] = new FrozenTime($content['created_date']);
}
}
// name の 重複チェック&リネーム
Expand Down
15 changes: 0 additions & 15 deletions plugins/baser-core/src/Model/Validation/BcValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,21 +422,6 @@ public static function halfText($value)
return true;
}

/**
* 日付の正当性チェック
*
* @param string $value 確認する値
* @return boolean
* @checked
* @noTodo
* @unitTest
*/
public static function checkDate($value)
{
if (!$value instanceof FrozenTime) return false;
return true;
}

/**
* 日時チェック
* - 開始日時が終了日時より過去の場合、true を返す
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testView()
$page->contents = "<p>test</p>";
$page->title = "testView title";
$page->content['title'] = "testView title";
$page->content['created_date'] = date('Y-m-d');
$page->content['created_date'] = date('Y-m-d H:i:s');

$this->enableCsrfToken();
$this->post('/baser/admin/baser-core/preview/view?url=https://localhost/&preview=default', $page->toArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,30 +420,6 @@ public function halfTextDataProvider()
];
}

/**
* Test CheckDate
*
* @param string $value
* @param boolean $expect
* @return void
* @dataProvider checkDateDataProvider
*/
public function testCheckDate($value, $expect)
{
$result = $this->BcValidation->checkDate($value);
$this->assertEquals($expect, $result);
}

public function checkDateDataProvider()
{
return [
[FrozenTime::now(), true],
[new FrozenTime('2015-01-01'), true],
['', false],
['2015-01-01 00:00:00', false],
];
}

/**
* Test checkDateRange
*
Expand Down
28 changes: 11 additions & 17 deletions plugins/bc-blog/src/Model/Table/BlogPostsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,13 @@ public function validationDefault(Validator $validator): Validator
]
]);
$validator
->dateTime('publish_begin')
->allowEmptyDateTime('publish_begin')
->add('publish_begin', [
'checkDate' => [
'rule' => ['checkDate'],
'provider' => 'bc',
'dateTime' => [
'rule' => ['dateTime'],
'message' => __d('baser_core', '公開開始日の形式が不正です。')
]
])
->allowEmptyDateTime('publish_begin')
->add('publish_begin', [
'checkDateRange' => [
'rule' => ['checkDateRange', ['publish_begin', 'publish_end']],
Expand All @@ -183,25 +181,21 @@ public function validationDefault(Validator $validator): Validator
]
]);
$validator
->dateTime('publish_end')
->allowEmptyDateTime('publish_end')
->add('publish_end', [
'checkDate' => [
'rule' => ['checkDate'],
'provider' => 'bc',
'dateTime' => [
'rule' => ['dateTime'],
'message' => __d('baser_core', '公開終了日の形式が不正です。')
]
]);
])
->allowEmptyDateTime('publish_end');
$validator
->dateTime('posted')
->notEmptyString('posted', __d('baser_core', '投稿日を入力してください。'))
->add('posted', [
'checkDate' => [
'rule' => ['checkDate'],
'provider' => 'bc',
'dateTime' => [
'rule' => ['dateTime'],
'message' => __d('baser_core', '投稿日の形式が不正です。')
]
]);
])
->notEmptyDateTime('posted', __d('baser_core', '投稿日を入力してください。'));;
$validator
->integer('user_id')
->notEmptyString('user_id', __d('baser_core', '投稿者を選択してください。'));
Expand Down
6 changes: 0 additions & 6 deletions plugins/bc-blog/src/Service/BlogPostsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,6 @@ public function create(array $postData)
));
}
$postData['no'] = $this->BlogPosts->getMax('no', ['BlogPosts.blog_content_id' => $postData['blog_content_id']]) + 1;
if (!empty($postData['posted'])) $postData['posted'] = new FrozenTime($postData['posted']);
if (!empty($postData['publish_begin'])) $postData['publish_begin'] = new FrozenTime($postData['publish_begin']);
if (!empty($postData['publish_end'])) $postData['publish_end'] = new FrozenTime($postData['publish_end']);
$blogPost = $this->BlogPosts->patchEntity($this->BlogPosts->newEmptyEntity(), $postData);
return $this->BlogPosts->saveOrFail($blogPost);
}
Expand All @@ -566,9 +563,6 @@ public function update(EntityInterface $post, array $postData)
ini_get('post_max_size')
));
}
if (!empty($postData['posted'])) $postData['posted'] = new FrozenTime($postData['posted']);
if (!empty($postData['publish_begin'])) $postData['publish_begin'] = new FrozenTime($postData['publish_begin']);
if (!empty($postData['publish_end'])) $postData['publish_end'] = new FrozenTime($postData['publish_end']);
$blogPost = $this->BlogPosts->patchEntity($post, $postData);
return $this->BlogPosts->saveOrFail($blogPost);
}
Expand Down
4 changes: 0 additions & 4 deletions plugins/bc-blog/src/Service/Front/BlogFrontService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use BaserCore\Annotation\Checked;
use Cake\ORM\ResultSet;
use Cake\ORM\TableRegistry;
use Cake\I18n\FrozenTime;

/**
* BlogFrontService
Expand Down Expand Up @@ -397,9 +396,6 @@ public function setupPreviewForArchives(Controller $controller): void
if ($request->getQuery('preview') === 'draft') {
$postArray['detail'] = $postArray['detail_draft'];
}
if (!empty($postArray['posted'])) $postArray['posted'] = new FrozenTime($postArray['posted']);
if (!empty($postArray['publish_begin'])) $postArray['publish_begin'] = new FrozenTime($postArray['publish_begin']);
if (!empty($postArray['publish_end'])) $postArray['publish_end'] = new FrozenTime($postArray['publish_end']);

$vars['post'] = $this->BlogPostsService->BlogPosts->patchEntity(
$vars['post'] ?? $this->BlogPostsService->BlogPosts->newEmptyEntity(),
Expand Down
6 changes: 3 additions & 3 deletions plugins/bc-blog/tests/TestCase/Model/BlogPostsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ public function test_validationDefault_testNotAllow()
//草稿欄
$this->assertEquals('草稿欄でスクリプトの入力は許可されていません。', current($errors['detail_draft']));
//公開開始日
$this->assertEquals('公開開始日の形式が不正です。', $errors['publish_begin']['checkDate']);
$this->assertEquals('公開開始日の形式が不正です。', $errors['publish_begin']['dateTime']);
$this->assertEquals('公開期間が不正です。', $errors['publish_begin']['checkDateRange']);
//公開終了日
$this->assertEquals('公開終了日の形式が不正です。', $errors['publish_end']['checkDate']);
$this->assertEquals('公開終了日の形式が不正です。', $errors['publish_end']['dateTime']);
//投稿日
$this->assertEquals('投稿日の形式が不正です。', $errors['posted']['checkDate']);
$this->assertEquals('投稿日の形式が不正です。', $errors['posted']['dateTime']);
//アイキャッチ画像
$this->assertEquals('許可されていないファイルです。', $errors['eyecatch']['fileExt']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,15 +708,15 @@ public function testUpdate()
'title' => 'title of post 3',
'posted' => '2021-11-01 00:00:00',
'publish_begin' => '2021-10-01 00:00:00',
'publish_end' => '9999-11-30 23:59:59'
'publish_end' => '2100-11-30 23:59:59'
];
// サービスメソッドを呼ぶ
$result = $this->BlogPostsService->update(BlogPostFactory::get(1), $postData);
// 戻り値を確認
$this->assertEquals("title of post 3", $result["title"]);
$this->assertEquals("2021-11-01 00:00:00", $result["posted"]->i18nFormat('yyyy-MM-dd HH:mm:ss'));
$this->assertEquals("2021-10-01 00:00:00", $result["publish_begin"]->i18nFormat('yyyy-MM-dd HH:mm:ss'));
$this->assertEquals("9999-11-30 23:59:59", $result["publish_end"]->i18nFormat('yyyy-MM-dd HH:mm:ss'));
$this->assertEquals("2100-11-30 23:59:59", $result["publish_end"]->i18nFormat('yyyy-MM-dd HH:mm:ss'));

// titleがない時を確認すること
$postData = [
Expand Down

0 comments on commit fe66b59

Please sign in to comment.