Skip to content

Commit 6b76021

Browse files
committed
FIX Ensure value is in the correct format after save
1 parent 9f19bae commit 6b76021

4 files changed

Lines changed: 64 additions & 4 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"silverstripe/recipe-testing": "^3",
2727
"silverstripe/standards": "^1",
2828
"silverstripe/documentation-lint": "^1",
29+
"silverstripe/siteconfig": "^5",
2930
"squizlabs/php_codesniffer": "^3",
3031
"phpstan/extension-installer": "^1.3"
3132
},

src/Form/MultiLinkField.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ private function convertValueToArray(mixed $value): array
6565
// A comma separated list of IDs will be turned into an array of IDs
6666
// Anything else will either get caught in the empty check or the !is_iterable check
6767
if (is_string($value)) {
68-
$value = $this->convertCommaSeparatedString(trim($value));
68+
// trim whitespace as well as any surronding brackets e.g. "[1,2,3]" => "1,2,3"
69+
$value = trim($value, " \n\r\t\v\0[]");
70+
$value = $this->convertCommaSeparatedString($value);
6971
}
7072
if (empty($value)) {
7173
return [];
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@retry @job2
2+
Feature: Create Links in LinkField and MultiLinkField on SiteConfig
3+
As a content editor
4+
I want to add links to SiteConfig which has a slightly different EditForm to SiteTree
5+
6+
Background:
7+
Given I add an extension "SilverStripe\FrameworkTest\LinkField\Extensions\LinkPageExtension" to the "SilverStripe\SiteConfig\SiteConfig" class
8+
And I go to "/dev/build?flush"
9+
And I am logged in with "ADMIN" permissions
10+
And I go to "/admin/settings"
11+
12+
Scenario: I can update and save single and multi link fields
13+
14+
Given I should see the "#Form_EditForm_HasOneLink" element
15+
And I should see the "#Form_EditForm_HasManyLinks" element
16+
17+
# Saving empty fields works
18+
When I press the "Save" button
19+
And I wait for 2 seconds
20+
Then I should see a "Saved" success toast
21+
22+
# Saving populated fields works
23+
When I click on the "[data-field-id='Form_EditForm_HasOneLink'] button" element
24+
Then I should see "Phone number" in the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(3)" element
25+
When I click on the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(3)" element
26+
And I wait for 5 seconds
27+
Then I should see "Phone number" in the ".modal-header" element
28+
Then I fill in "LinkText" with "Phone"
29+
Then I fill in "Phone" with "12345678"
30+
And I should not see "Open in new window" in the ".modal-content" element
31+
And I press the "Create link" button
32+
And I wait for 2 seconds
33+
34+
When I click on the "[data-field-id='Form_EditForm_HasManyLinks'] button" element
35+
Then I should see "Phone number" in the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(5)" element
36+
When I click on the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(5)" element
37+
And I wait for 5 seconds
38+
Then I should see "Phone number" in the ".modal-header" element
39+
Then I fill in "LinkText" with "Phone"
40+
Then I fill in "Phone" with "87654321"
41+
And I should not see "Open in new window" in the ".modal-content" element
42+
And I press the "Create link" button
43+
And I wait for 2 seconds
44+
45+
When I press the "Save" button
46+
And I wait for 2 seconds
47+
Then I should see a "Saved" success toast
48+
And I should see "12345678"
49+
And I should see "87654321"

tests/php/Form/MultiLinkFieldTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function provideConvertValueToArray(): array
1717
'value' => '',
1818
'expected' => [],
1919
],
20-
'non-comma-separated numeric string' => [
20+
'non-comma-separated string' => [
2121
'value' => 'this is a string',
2222
'expected' => ['this is a string'],
2323
],
@@ -29,8 +29,16 @@ public function provideConvertValueToArray(): array
2929
'value' => '1,2,3,4',
3030
'expected' => [1, 2, 3, 4],
3131
],
32-
'comma-separated string with spaces' => [
33-
'value' => ' 1,2 , 3, 4 ',
32+
'comma-separated string with whitesapce' => [
33+
'value' => " 1,2 , 3, 4 \n ",
34+
'expected' => [1, 2, 3, 4],
35+
],
36+
'comma-separated string with brackets' => [
37+
'value' => '[1,2,3,4]',
38+
'expected' => [1, 2, 3, 4],
39+
],
40+
'comma-separated string with brackets and whitespace' => [
41+
'value' => "\t [1,\n2, 3,4] ",
3442
'expected' => [1, 2, 3, 4],
3543
],
3644
'number' => [

0 commit comments

Comments
 (0)