Skip to content

Commit

Permalink
fix #3143 【5.1】URL形式以外の文字も入力できてしまいます【コンテンツ管理>リンク編集】 (#3146)
Browse files Browse the repository at this point in the history
Co-authored-by: Đỗ Văn Hùng <[email protected]>
  • Loading branch information
HungDV2022 and dovanhung authored Feb 25, 2024
1 parent 94b7dbb commit c0e17a5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
6 changes: 4 additions & 2 deletions plugins/bc-content-link/src/Model/Table/ContentLinksTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public function validationDefault(Validator $validator): Validator
->requirePresence('id', 'update');

$validator
->scalar('url')
->notEmptyString('url', __d('baser_core', 'リンク先URLを入力してください。'), 'update');
->scalar('url')
->notEmptyString('url', __d('baser_core', 'リンク先URLを入力してください。'), 'update')
->regex('url', '/^http|^\/.*/', __d('baser_core', 'リンク先URLはURLの形式を入力してください。'))
->maxLength('url', 255, __d('baser_core', 'リンク先URLは255文字以内で入力してください。'));

return $validator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,47 @@ public function testValidationDefault()
], $contentLink->getErrors());
}

/**
* Test testValidationURL
*/
public function testValidationURL()
{
$validator = $this->ContentLinks->getValidator('default');
//エラー場合、
//スペースだけ入力
$errors = $validator->validate([
'url' => ' '
]);
//戻り値を確認
$this->assertEquals('リンク先URLはURLの形式を入力してください。', current($errors['url']));
//スラッシュがない場合
$errors = $validator->validate([
'url' => 'あああああ'
]);
//戻り値を確認
$this->assertEquals('リンク先URLはURLの形式を入力してください。', current($errors['url']));

//長いURLを入力場合
$errors = $validator->validate([
'url' => '/' . str_repeat('a', 255)
]);
//戻り値を確認
$this->assertEquals('リンク先URLは255文字以内で入力してください。', current($errors['url']));

//正常場合、
$errors = $validator->validate([
'url' => '/test'
]);
//戻り値を確認
$this->assertCount(0, $errors);

$errors = $validator->validate([
'url' => 'https://basercms.net/'
]);
//戻り値を確認
$this->assertCount(0, $errors);
}

/**
* test beforeCopyEvent
*/
Expand All @@ -88,13 +129,13 @@ public function testBeforeCopyEvent()
//イベントをコル
$this->entryEventToMock(self::EVENT_LAYER_MODEL, 'BcContentLink.ContentLinks.beforeCopy', function (Event $event) {
$data = $event->getData('data');
$data['url'] = 'beforeCopy';
$data['url'] = '/beforeCopy';
$event->setData('data', $data);
});
$this->ContentLinks->copy(1, 1, 'new title', 1, 1);
//イベントに入るかどうか確認
$contentLinks = $this->getTableLocator()->get('BcContentLink.ContentLinks');
$query = $contentLinks->find()->where(['url' => 'beforeCopy']);
$query = $contentLinks->find()->where(['url' => '/beforeCopy']);
$this->assertEquals(1, $query->count());
}

Expand All @@ -108,13 +149,13 @@ public function testAfterCopyEvent()
$this->entryEventToMock(self::EVENT_LAYER_MODEL, 'BcContentLink.ContentLinks.afterCopy', function (Event $event) {
$data = $event->getData('data');
$contentLinks = TableRegistry::getTableLocator()->get('BcContentLink.ContentLinks');
$data->url = 'AfterCopy';
$data->url = '/AfterCopy';
$contentLinks->save($data);
});
$this->ContentLinks->copy(1, 1, 'new title', 1, 1);
//イベントに入るかどうか確認
$contentLinks = $this->getTableLocator()->get('BcContentLink.ContentLinks');
$query = $contentLinks->find()->where(['url' => 'AfterCopy']);
$query = $contentLinks->find()->where(['url' => '/AfterCopy']);
$this->assertEquals(1, $query->count());
}

Expand Down

0 comments on commit c0e17a5

Please sign in to comment.