Skip to content

Commit

Permalink
FIX Get array values, not keys (#11414)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Oct 2, 2024
1 parent 7f11bf3 commit 33929e2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Forms/UrlField.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public function validate($validator)
}

/**
* Set which protocols valid URLs are allowed to have
* Set which protocols valid URLs are allowed to have.
* Passing an empty array will result in using configured defaults.
*/
public function setAllowedProtocols(array $protocols): static
{
// Ensure the array isn't associative so we can use 0 index in validate().
$this->protocols = array_keys($protocols);
$this->protocols = $protocols;
return $this;
}

Expand All @@ -67,10 +67,12 @@ public function setAllowedProtocols(array $protocols): static
*/
public function getAllowedProtocols(): array
{
if (empty($this->protocols)) {
return static::config()->get('default_protocols');
$protocols = $this->protocols;
if (empty($protocols)) {
$protocols = static::config()->get('default_protocols');
}
return $this->protocols;
// Ensure the array isn't associative so we can use 0 index in validate().
return array_values($protocols);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/php/Forms/UrlFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,23 @@ public function testValidate(string $url, bool $valid)
$expectedCount = $valid ? 0 : 1;
$this->assertEquals($expectedCount, count($validator->getErrors()));
}

public function testAllowedProtocols(): void
{
$field = new UrlField('MyUrl');
// Defaults should be http and https
$this->assertSame(['https', 'http'], $field->getAllowedProtocols());

// Defaults change with config, and ignore keys
UrlField::config()->set('default_protocols', ['my-key' => 'ftp']);
$this->assertSame(['ftp'], $field->getAllowedProtocols());

// Can set explicit protocols - again keys are ignored
$field->setAllowedProtocols(['http', 'key' => 'irc', 'nntp']);
$this->assertSame(['http', 'irc', 'nntp'], $field->getAllowedProtocols());

// Can reset back to config defaults
$field->setAllowedProtocols([]);
$this->assertSame(['ftp'], $field->getAllowedProtocols());
}
}

0 comments on commit 33929e2

Please sign in to comment.