diff --git a/plugins/bc-content-link/src/Model/Table/ContentLinksTable.php b/plugins/bc-content-link/src/Model/Table/ContentLinksTable.php index 77b0761081..c8ebbacf21 100644 --- a/plugins/bc-content-link/src/Model/Table/ContentLinksTable.php +++ b/plugins/bc-content-link/src/Model/Table/ContentLinksTable.php @@ -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; } diff --git a/plugins/bc-content-link/tests/TestCase/Model/Table/ContentLinksTableTest.php b/plugins/bc-content-link/tests/TestCase/Model/Table/ContentLinksTableTest.php index d957639513..0c4d79a475 100644 --- a/plugins/bc-content-link/tests/TestCase/Model/Table/ContentLinksTableTest.php +++ b/plugins/bc-content-link/tests/TestCase/Model/Table/ContentLinksTableTest.php @@ -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 */ @@ -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()); } @@ -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()); }