|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\ElementalBannerBlock\Tests\Form; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
6
|
|
|
use SilverStripe\ElementalBannerBlock\Form\BlockLinkField; |
|
7
|
|
|
use SilverStripe\View\ArrayData; |
|
8
|
|
|
|
|
9
|
|
|
class BlockLinkFieldTest extends SapphireTest |
|
10
|
|
|
{ |
|
11
|
|
|
protected static $fixture_file = 'BlockLinkFieldTest.yml'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var BlockLinkField |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $field; |
|
17
|
|
|
|
|
18
|
|
|
protected function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
parent::setUp(); |
|
21
|
|
|
$this->field = new BlockLinkField('test'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testHasUniqueFormFieldSelector() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->assertContains('block-link-field', $this->field->Type()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testGetParsedValue() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->field->setValue(json_encode(['foo' => 'bar', 'bar' => 'baz'])); |
|
32
|
|
|
$result = $this->field->getParsedValue(); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertInstanceOf(ArrayData::class, $result); |
|
35
|
|
|
$this->assertSame('bar', $result->foo); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testParsedValueIsResetWhenSettingNewValue() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->field->setValue(json_encode(['foo' => 'bar'])); |
|
41
|
|
|
$result1 = $this->field->getParsedValue(); |
|
42
|
|
|
|
|
43
|
|
|
$this->field->setValue(json_encode(['foo' => 'baz'])); |
|
44
|
|
|
$result2 = $this->field->getParsedValue(); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertNotSame($result1, $result2); |
|
47
|
|
|
$this->assertSame('baz', $result2->foo); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testGetLinkRelativeUrlReturnsEmptyStringOnInvalidPage() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->field->setValue(json_encode([ |
|
53
|
|
|
'PageID' => 12345678 |
|
54
|
|
|
])); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertSame('', $this->field->getLinkRelativeUrl()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testGetSetShowLinkText() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->assertTrue($this->field->getShowLinkText(), 'Default to showing the link text field'); |
|
62
|
|
|
|
|
63
|
|
|
$this->field->setShowLinkText(false); |
|
64
|
|
|
$this->assertFalse($this->field->getShowLinkText(), 'Link text field can be disabled'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testGetAttributes() |
|
68
|
|
|
{ |
|
69
|
|
|
$attributes = $this->field->getAttributes(); |
|
70
|
|
|
$this->assertArrayHasKey('data-schema', $attributes); |
|
71
|
|
|
$this->assertArrayHasKey('data-state', $attributes); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testGetSchemaDataDefault() |
|
75
|
|
|
{ |
|
76
|
|
|
$schemaDataDefaults = $this->field->getSchemaDataDefaults(); |
|
77
|
|
|
$this->assertArrayHasKey('showLinkText', $schemaDataDefaults['data']); |
|
78
|
|
|
$this->assertArrayHasKey('linkedPage', $schemaDataDefaults['data']); |
|
79
|
|
|
$this->assertArrayHasKey('title', $schemaDataDefaults['data']); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|