Skip to content

Commit 1de6548

Browse files
Merge pull request #386 from creative-commoners/pulls/4.2/value-string-array
FIX Ensure value is in the correct format after save
2 parents 9f19bae + f2b7fdc commit 1de6548

4 files changed

Lines changed: 87 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public function setValue(mixed $value, $data = null): static
2626
return parent::setValue($ids, $data);
2727
}
2828

29+
public function setSubmittedValue($value, $data = null)
30+
{
31+
if (is_string($value)) {
32+
$value = rtrim(ltrim($value, '['), ']');
33+
}
34+
return $this->setValue($value, $data);
35+
}
36+
2937
public function getSchemaDataDefaults(): array
3038
{
3139
$data = parent::getSchemaDataDefaults();
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: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
class MultiLinkFieldTest extends SapphireTest
1212
{
13-
public function provideConvertValueToArray(): array
13+
private static function getDataForProvider(): array
1414
{
1515
return [
1616
'empty string' => [
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,8 @@ 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 ",
3434
'expected' => [1, 2, 3, 4],
3535
],
3636
'number' => [
@@ -56,6 +56,11 @@ public function provideConvertValueToArray(): array
5656
];
5757
}
5858

59+
public static function provideConvertValueToArray(): array
60+
{
61+
return MultiLinkFieldTest::getDataForProvider();
62+
}
63+
5964
/**
6065
* @dataProvider provideConvertValueToArray
6166
*/
@@ -66,4 +71,24 @@ public function testConvertValueToArray(mixed $value, array $expected): void
6671
$reflectionMethod->setAccessible(true);
6772
$this->assertSame($expected, $reflectionMethod->invoke($field, $value));
6873
}
74+
75+
public static function provideSetSubmittedValue(): array
76+
{
77+
return array_merge(MultiLinkFieldTest::getDataForProvider(), [
78+
'comma-separated string with brackets' => [
79+
'value' => '[1,2,3,4]',
80+
'expected' => [1, 2, 3, 4],
81+
],
82+
]);
83+
}
84+
85+
/**
86+
* @dataProvider provideSetSubmittedValue
87+
*/
88+
public function testSetSubmittedValue(mixed $value, array $expected): void
89+
{
90+
$field = new MultiLinkField('');
91+
$field->setSubmittedValue($value);
92+
$this->assertSame($expected, $field->Value($value));
93+
}
6994
}

0 commit comments

Comments
 (0)