From c0e17a5be4f8e558e85e873eeae73f94c3c68921 Mon Sep 17 00:00:00 2001 From: HungDV2022 <110375578+HungDV2022@users.noreply.github.com> Date: Sun, 25 Feb 2024 11:37:28 +0900 Subject: [PATCH] =?UTF-8?q?fix=20#3143=20=E3=80=905.1=E3=80=91URL=E5=BD=A2?= =?UTF-8?q?=E5=BC=8F=E4=BB=A5=E5=A4=96=E3=81=AE=E6=96=87=E5=AD=97=E3=82=82?= =?UTF-8?q?=E5=85=A5=E5=8A=9B=E3=81=A7=E3=81=8D=E3=81=A6=E3=81=97=E3=81=BE?= =?UTF-8?q?=E3=81=84=E3=81=BE=E3=81=99=E3=80=90=E3=82=B3=E3=83=B3=E3=83=86?= =?UTF-8?q?=E3=83=B3=E3=83=84=E7=AE=A1=E7=90=86=EF=BC=9E=E3=83=AA=E3=83=B3?= =?UTF-8?q?=E3=82=AF=E7=B7=A8=E9=9B=86=E3=80=91=20(#3146)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Đỗ Văn Hùng --- .../src/Model/Table/ContentLinksTable.php | 6 ++- .../Model/Table/ContentLinksTableTest.php | 49 +++++++++++++++++-- 2 files changed, 49 insertions(+), 6 deletions(-) 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()); }