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

ENH Use symfony/validation logic #3009

Open
wants to merge 1 commit into
base: 6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions code/Model/RedirectorPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\UrlField;
use SilverStripe\Versioned\Versioned;

/**
Expand Down Expand Up @@ -47,6 +48,9 @@ class RedirectorPage extends Page
'RedirectionType',
'Content',
],
'fieldClasses' => [
'ExternalURL' => UrlField::class,
],
];

private static $table_name = 'RedirectorPage';
Expand Down Expand Up @@ -171,35 +175,12 @@ public function syncLinkTracking()
}
}

protected function onBeforeWrite()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to keep this is as you can programatically save things without it going via a UrlField

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That felt very much like it was being done instead of validation. I feel like validating the value in the better option.

I can add the URL validation in the validate() method for this class if you like? But I don't think it makes sense to alter the value that's being set, especially programatic values.

{
parent::onBeforeWrite();

if ($this->ExternalURL && substr($this->ExternalURL ?? '', 0, 2) !== '//') {
$urlParts = parse_url($this->ExternalURL ?? '');
if ($urlParts) {
if (empty($urlParts['scheme'])) {
// no scheme, assume http
$this->ExternalURL = 'http://' . $this->ExternalURL;
} elseif (!in_array($urlParts['scheme'], [
'http',
'https',
])) {
// we only allow http(s) urls
$this->ExternalURL = '';
}
} else {
// malformed URL to reject
$this->ExternalURL = '';
}
}
}
Comment on lines -174 to -196
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to set false if it's invalid, because the URLField validation won't allow an invalid value in the first place.
The trade off is we're not assuming http by default anymore, users will have to add that themselves.
The URLField validation has been updated to make it clear when that needs to be added.


public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
// Remove all metadata fields, does not apply for redirector pages
$fields->removeByName('Metadata');
$fields->dataFieldByName('ExternalURL')?->setAllowRelativeProtocol(true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the first condition in the now-removed onBeforeWrite().


$fields->addFieldsToTab(
'Root.Main',
Expand Down
38 changes: 0 additions & 38 deletions tests/php/Model/RedirectorPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,33 +121,6 @@ public function testReflexiveAndTransitiveInternalRedirectors()
$this->assertEquals(Director::absoluteURL('/redirection-dest'), $response->getHeader("Location"));
}

public function testExternalURLGetsPrefixIfNotSet()
{
$page = $this->objFromFixture(RedirectorPage::class, 'externalnoprefix');
$this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite has prefixed with http');
$page->write();
$this->assertEquals(
$page->ExternalURL,
'http://google.com',
'onBeforeWrite will not double prefix if written again!'
);
}

public function testAllowsProtocolRelative()
{
$noProtocol = new RedirectorPage(['ExternalURL' => 'mydomain.com']);
$noProtocol->write();
$this->assertEquals('http://mydomain.com', $noProtocol->ExternalURL);

$protocolAbsolute = new RedirectorPage(['ExternalURL' => 'http://mydomain.com']);
$protocolAbsolute->write();
$this->assertEquals('http://mydomain.com', $protocolAbsolute->ExternalURL);

$protocolRelative = new RedirectorPage(['ExternalURL' => '//mydomain.com']);
$protocolRelative->write();
$this->assertEquals('//mydomain.com', $protocolRelative->ExternalURL);
}

/**
* Test that we can trigger a redirection before RedirectorPageController::init() is called
*/
Expand All @@ -163,17 +136,6 @@ public function testRedirectRespectsFinishedResponse()
RedirectorPageController::remove_extension(RedirectorPageTest_RedirectExtension::class);
}

public function testNoJSLinksAllowed()
{
$page = new RedirectorPage();
$js = 'javascript:alert("hello world")';
$page->ExternalURL = $js;
$this->assertEquals($js, $page->ExternalURL);

$page->write();
$this->assertEmpty($page->ExternalURL);
}

public function testFileRedirector()
{
$page = $this->objFromFixture(RedirectorPage::class, 'file');
Expand Down
Loading