1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ElementalBlocks\Tests\Form; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\ElementalBlocks\Form\BlockLinkField; |
8
|
|
|
use SilverStripe\View\ArrayData; |
9
|
|
|
|
10
|
|
|
class BlockLinkFieldTest extends SapphireTest |
11
|
|
|
{ |
12
|
|
|
protected static $fixture_file = 'BlockLinkFieldTest.yml'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var BlockLinkField |
16
|
|
|
*/ |
17
|
|
|
protected $field; |
18
|
|
|
|
19
|
|
|
protected function setUp() |
20
|
|
|
{ |
21
|
|
|
parent::setUp(); |
22
|
|
|
$this->field = new BlockLinkField('test'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testHasUniqueFormFieldSelector() |
26
|
|
|
{ |
27
|
|
|
$this->assertContains('block-link-field', $this->field->Type()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testGetParsedValue() |
31
|
|
|
{ |
32
|
|
|
$this->field->setValue(json_encode(['foo' => 'bar', 'bar' => 'baz'])); |
33
|
|
|
$result = $this->field->getParsedValue(); |
34
|
|
|
|
35
|
|
|
$this->assertInstanceOf(ArrayData::class, $result); |
36
|
|
|
$this->assertSame('bar', $result->foo); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testParsedValueIsResetWhenSettingNewValue() |
40
|
|
|
{ |
41
|
|
|
$this->field->setValue(json_encode(['foo' => 'bar'])); |
42
|
|
|
$result1 = $this->field->getParsedValue(); |
43
|
|
|
|
44
|
|
|
$this->field->setValue(json_encode(['foo' => 'baz'])); |
45
|
|
|
$result2 = $this->field->getParsedValue(); |
46
|
|
|
|
47
|
|
|
$this->assertNotSame($result1, $result2); |
48
|
|
|
$this->assertSame('baz', $result2->foo); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testGetLinkMethods() |
52
|
|
|
{ |
53
|
|
|
$this->assertFalse($this->field->getLinkDefined()); |
54
|
|
|
|
55
|
|
|
$this->field->setValue(json_encode([ |
56
|
|
|
'PageID' => $this->idFromFixture(SiteTree::class, 'a_page'), |
57
|
|
|
'Text' => 'My link', |
58
|
|
|
'Description' => 'Click here to see what happens next', |
59
|
|
|
'TargetBlank' => true, |
60
|
|
|
])); |
61
|
|
|
|
62
|
|
|
$this->assertTrue($this->field->getLinkDefined()); |
63
|
|
|
$this->assertSame('/my-page', $this->field->getLinkRelativeUrl()); |
64
|
|
|
$this->assertSame('My link', $this->field->getLinkText()); |
65
|
|
|
$this->assertSame('Click here to see what happens next', $this->field->getLinkDescription()); |
66
|
|
|
$this->assertTrue($this->field->getLinkTargetBlank()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testGetLinkRelativeUrlReturnsEmptyStringOnInvalidPage() |
70
|
|
|
{ |
71
|
|
|
$this->field->setValue(json_encode([ |
72
|
|
|
'PageID' => 12345678 |
73
|
|
|
])); |
74
|
|
|
|
75
|
|
|
$this->assertSame('', $this->field->getLinkRelativeUrl()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGetLinkTextIsTrimmed() |
79
|
|
|
{ |
80
|
|
|
$this->field->setValue(json_encode([ |
81
|
|
|
'Text' => ' My text ', |
82
|
|
|
])); |
83
|
|
|
|
84
|
|
|
$this->assertSame('My text', $this->field->getLinkText()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testGetSetShowLinkText() |
88
|
|
|
{ |
89
|
|
|
$this->assertTrue($this->field->getShowLinkText(), 'Default to showing the link text field'); |
90
|
|
|
|
91
|
|
|
$this->field->setShowLinkText(false); |
92
|
|
|
$this->assertFalse($this->field->getShowLinkText(), 'Link text field can be disabled'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|