Skip to content
Merged
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"silverstripe/recipe-testing": "^3",
"silverstripe/standards": "^1",
"silverstripe/documentation-lint": "^1",
"silverstripe/siteconfig": "^5",
"squizlabs/php_codesniffer": "^3",
"phpstan/extension-installer": "^1.3"
},
Expand Down
8 changes: 8 additions & 0 deletions src/Form/MultiLinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public function setValue(mixed $value, $data = null): static
return parent::setValue($ids, $data);
}

public function setSubmittedValue($value, $data = null)
{
if (is_string($value)) {
$value = rtrim(ltrim($value, '['), ']');
}
return $this->setValue($value, $data);
}

public function getSchemaDataDefaults(): array
{
$data = parent::getSchemaDataDefaults();
Expand Down
49 changes: 49 additions & 0 deletions tests/behat/features/linkfield-siteconfig.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@retry @job2
Feature: Create Links in LinkField and MultiLinkField on SiteConfig
As a content editor
I want to add links to SiteConfig which has a slightly different EditForm to SiteTree

Background:
Given I add an extension "SilverStripe\FrameworkTest\LinkField\Extensions\LinkPageExtension" to the "SilverStripe\SiteConfig\SiteConfig" class
And I go to "/dev/build?flush"
And I am logged in with "ADMIN" permissions
And I go to "/admin/settings"

Scenario: I can update and save single and multi link fields

Given I should see the "#Form_EditForm_HasOneLink" element
And I should see the "#Form_EditForm_HasManyLinks" element

# Saving empty fields works
When I press the "Save" button
And I wait for 2 seconds
Then I should see a "Saved" success toast

# Saving populated fields works
When I click on the "[data-field-id='Form_EditForm_HasOneLink'] button" element
Then I should see "Phone number" in the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(3)" element
When I click on the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(3)" element
And I wait for 5 seconds
Then I should see "Phone number" in the ".modal-header" element
Then I fill in "LinkText" with "Phone"
Then I fill in "Phone" with "12345678"
And I should not see "Open in new window" in the ".modal-content" element
And I press the "Create link" button
And I wait for 2 seconds

When I click on the "[data-field-id='Form_EditForm_HasManyLinks'] button" element
Then I should see "Phone number" in the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(5)" element
When I click on the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(5)" element
And I wait for 5 seconds
Then I should see "Phone number" in the ".modal-header" element
Then I fill in "LinkText" with "Phone"
Then I fill in "Phone" with "87654321"
And I should not see "Open in new window" in the ".modal-content" element
And I press the "Create link" button
And I wait for 2 seconds

When I press the "Save" button
And I wait for 2 seconds
Then I should see a "Saved" success toast
And I should see "12345678"
And I should see "87654321"
33 changes: 29 additions & 4 deletions tests/php/Form/MultiLinkFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

class MultiLinkFieldTest extends SapphireTest
{
public function provideConvertValueToArray(): array
private static function getDataForProvider(): array
{
return [
'empty string' => [
'value' => '',
'expected' => [],
],
'non-comma-separated numeric string' => [
'non-comma-separated string' => [
'value' => 'this is a string',
'expected' => ['this is a string'],
],
Expand All @@ -29,8 +29,8 @@ public function provideConvertValueToArray(): array
'value' => '1,2,3,4',
'expected' => [1, 2, 3, 4],
],
'comma-separated string with spaces' => [
'value' => ' 1,2 , 3, 4 ',
'comma-separated string with whitesapce' => [
'value' => " 1,2 , 3, 4 \n ",
'expected' => [1, 2, 3, 4],
],
'number' => [
Expand All @@ -56,6 +56,11 @@ public function provideConvertValueToArray(): array
];
}

public static function provideConvertValueToArray(): array
{
return MultiLinkFieldTest::getDataForProvider();
}

/**
* @dataProvider provideConvertValueToArray
*/
Expand All @@ -66,4 +71,24 @@ public function testConvertValueToArray(mixed $value, array $expected): void
$reflectionMethod->setAccessible(true);
$this->assertSame($expected, $reflectionMethod->invoke($field, $value));
}

public static function provideSetSubmittedValue(): array
{
return array_merge(MultiLinkFieldTest::getDataForProvider(), [
'comma-separated string with brackets' => [
'value' => '[1,2,3,4]',
'expected' => [1, 2, 3, 4],
],
]);
}

/**
* @dataProvider provideSetSubmittedValue
*/
public function testSetSubmittedValue(mixed $value, array $expected): void
{
$field = new MultiLinkField('');
$field->setSubmittedValue($value);
$this->assertSame($expected, $field->Value($value));
}
}