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 #3701 【ブログ】APIから記事登録時に必須項目のバリデーションが効いていない #3708

Merged
merged 9 commits into from
Sep 20, 2024
4 changes: 3 additions & 1 deletion plugins/bc-blog/src/Model/Table/BlogPostsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function validationDefault(Validator $validator): Validator
$validator
->scalar('title')
->maxLength('title', 255, __d('baser_core', 'タイトルは255文字以内で入力してください。'))
->requirePresence('title', 'update', __d('baser_core', 'タイトルを入力してください。'))
->requirePresence('title', 'create', __d('baser_core', 'タイトルを入力してください。'))
->notEmptyString('title', __d('baser_core', 'タイトルを入力してください。'));
$validator
->scalar('content')
Expand Down Expand Up @@ -196,9 +196,11 @@ public function validationDefault(Validator $validator): Validator
'message' => __d('baser_core', '投稿日の形式が不正です。')
]
])
->requirePresence('posted', 'create', __d('baser_core', '投稿日を入力してください。'))
->notEmptyDateTime('posted', __d('baser_core', '投稿日を入力してください。'));;
$validator
->integer('user_id')
->requirePresence('user_id', 'create', __d('baser_core', '投稿者を選択してください。'))
->notEmptyString('user_id', __d('baser_core', '投稿者を選択してください。'));
$validator
->allowEmptyString('eye_catch')
Expand Down
3 changes: 3 additions & 0 deletions plugins/bc-blog/src/Service/BlogPostsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,13 @@ public function createYearMonthDayCondition($conditions, $year, $month, $day)
public function getNew(int $blogContentId, int $userId)
{
return $this->BlogPosts->newEntity([
'title' => '',
'user_id' => $userId,
'posted' => \Cake\I18n\DateTime::now(),
'status' => false,
'blog_content_id' => $blogContentId
], [
'validate' => false,
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function testAdd()
$this->enableSecurityToken();
$this->enableCsrfToken();
$this->loadFixtureScenario(BlogContentScenario::class, 1, 1, null, 'test', '/');
$this->post('/baser/admin/bc-blog/blog_posts/add/1', ['blog_content_id' => 2, 'title' => 'test']);
$this->post('/baser/admin/bc-blog/blog_posts/add/1', ['blog_content_id' => 2, 'title' => 'test', 'user_id' => 1, 'posted' => '2022-12-01 00:00:00']);
$this->assertResponseCode(302);
$this->assertFlashMessage('記事「test」を追加しました。');
$this->assertRedirect(['action' => 'edit/1/1']);
Expand Down Expand Up @@ -208,8 +208,8 @@ public function testEdit()
$this->assertEquals('blog post edit', $blogPost['title']);
$this->assertEquals('ホームページをオープンしました', $blogPost['name']);

//dataは空にする場合を確認
$this->post('/baser/admin/bc-blog/blog_posts/edit/1/1', []);
//エラーを発生した場合を確認
$this->post('/baser/admin/bc-blog/blog_posts/edit/1/1', ['name' => str_repeat('a', 256)]);
// ステータスを確認
$this->assertResponseCode(200);
// メッセージを確認
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public function test_add()
{
// postデータを生成
$postData = [
'user_id' => 1,
'posted' => '2022-12-01 00:00:00',
'blog_content_id' => 1,
'title' => 'baserCMS inc. [デモ] の新しい記事',
'content' => '記事の概要',
Expand Down Expand Up @@ -161,15 +163,15 @@ public function test_edit()
$this->assertEquals('blog post edit', $result->blogPost->title);
$this->assertEquals('記事「blog post edit」を更新しました。', $result->message);

//dataは空にする場合を確認
//エラーを発生した場合を確認
//APIをコル
$this->post('/baser/api/admin/bc-blog/blog_posts/edit/1.json?token=' . $this->accessToken, []);
$this->post('/baser/api/admin/bc-blog/blog_posts/edit/1.json?token=' . $this->accessToken, ['name' => str_repeat('a', 256)]);
//ステータスを確認
$this->assertResponseCode(400);
//戻る値を確認
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals('入力エラーです。内容を修正してください。', $result->message);
$this->assertEquals('タイトルを入力してください。', $result->errors->title->_required);
$this->assertEquals('スラッグは255文字以内で入力してください。', $result->errors->name->maxLength);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions plugins/bc-blog/tests/TestCase/Model/BlogPostsTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public function test_validationDefault_testIsNull()
$this->assertEquals('タイトルを入力してください。', current($errors['title']));
$this->assertEquals('投稿日を入力してください。', current($errors['posted']));
$this->assertEquals('投稿者を選択してください。', current($errors['user_id']));

$errors = $validator->validate([]);

//戻り値を確認
$this->assertEquals('タイトルを入力してください。', current($errors['title']));
$this->assertEquals('投稿日を入力してください。', current($errors['posted']));
$this->assertEquals('投稿者を選択してください。', current($errors['user_id']));
}

/*
Expand Down
59 changes: 31 additions & 28 deletions plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ public function testCreate()
{
// パラメータを生成
$postData = [
'title' => 'new blog',
'user_id' => 1,
'blog_content_id' => 1,
'posted' => '2022-12-01 00:00:00',
'publish_begin' => '2022-12-01 00:00:00',
Expand All @@ -649,6 +651,28 @@ public function testCreate()
$this->BlogPostsService->create([]);
}

/**
* 新規登録
* BlogPostsService::create
*/
public function testCreateExceptionTitle()
{
// パラメータを生成
$postData = [
'user_id' => 1,
'blog_content_id' => 1,
'posted' => '2022-12-01 00:00:00',
'publish_begin' => '2022-12-01 00:00:00',
'publish_end' => '2022-12-31 23:59:59',
];

// title を指定しなかった場合はエラーとなること
$this->expectException('Cake\ORM\Exception\PersistenceFailedException');
$this->expectExceptionMessage('Entity save failure. Found the following errors (title._required: "タイトルを入力してください。"');
// サービスメソッドを呼ぶ
$this->BlogPostsService->create($postData);
}

/**
* 新規登録
* BlogPostsService::create
Expand All @@ -658,6 +682,8 @@ public function testCreateExceptionPosted()
{
// パラメータを生成
$postData = [
'title' => 'new blog',
'user_id' => 1,
'blog_content_id' => 1,
'posted' => '',
'publish_begin' => '',
Expand Down Expand Up @@ -697,41 +723,18 @@ public function testCreateExceptionPosted()
public function testUpdate()
{
//データ生成
BlogPostFactory::make([
'id' => 1,
'title' => 'post title 1',
'posted' => '2022-12-01 00:00:00',
'publish_begin' => '2022-12-01 00:00:00',
'publish_end' => '9999-12-31 23:59:59'
])->persist();
BlogPostFactory::make(['id' => 1, 'title' => 'post title 1'])->persist();

//データ生成
$postData = [
'title' => 'title of post 3',
'posted' => '2021-11-01 00:00:00',
'publish_begin' => '2021-10-01 00:00:00',
'publish_end' => '2100-11-30 23:59:59'
];
// サービスメソッドを呼ぶ
$result = $this->BlogPostsService->update(BlogPostFactory::get(1), $postData);
$result = $this->BlogPostsService->update(BlogPostFactory::get(1), ['title' => 'title of post 3']);
// 戻り値を確認
$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("2100-11-30 23:59:59", $result["publish_end"]->i18nFormat('yyyy-MM-dd HH:mm:ss'));

// titleがない時を確認すること
$postData = [
'posted' => '2021-11-01 00:00:00',
'publish_begin' => '2021-11-01 00:00:00',
'publish_end' => '9999-11-31 23:59:59'
];

// title を指定しなかった場合はエラーとなること
// titleの長さは256文字を指定しなかった場合はエラーとなること
$this->expectException("Cake\ORM\Exception\PersistenceFailedException");
$this->expectExceptionMessage("タイトルを入力してください。");
$this->expectExceptionMessage("タイトルは255文字以内で入力してください。");
// サービスメソッドを呼ぶ
$this->BlogPostsService->update(BlogPostFactory::get(1), $postData);
$this->BlogPostsService->update(BlogPostFactory::get(1), ['title' => str_repeat('a', 256)]);
}

/**
Expand Down
Loading