Completed
Push — master ( 0446fc...908d30 )
by
unknown
11s
created

testAllFieldsAreReadonly()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
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\ElementalBlocks\Form\BlockLinkField_Readonly;
9
use SilverStripe\Forms\CheckboxField_Readonly;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\Forms\ToggleCompositeField;
13
use SilverStripe\Forms\TreeDropdownField_Readonly;
14
15
class BlockLinkField_ReadonlyTest extends SapphireTest
16
{
17
    protected static $fixture_file = 'BlockLinkFieldTest.yml';
18
19
    /**
20
     * @var BlockLinkField_Readonly
21
     */
22
    protected $field;
23
24
    protected function setUp()
25
    {
26
        parent::setUp();
27
28
        $this->field = (new BlockLinkField('Foo'))
29
            ->setValue(json_encode([
30
                'PageID' => $this->idFromFixture(SiteTree::class, 'a_page'),
31
                'Text' => 'Some text here',
32
                'Description' => 'Do not touch the monkey',
33
                'TargetBlank' => true,
34
            ]))
35
            ->performReadonlyTransformation();
36
    }
37
38
    public function testReadonlyFieldIsReturnedFromTransformation()
39
    {
40
        $this->assertInstanceOf(BlockLinkField_Readonly::class, $this->field);
41
    }
42
43
    public function testFieldReturnsToggleComposite()
44
    {
45
        $result = $this->field->Field();
46
        $this->assertInstanceOf(ToggleCompositeField::class, $result, 'Composite fields are housed in a toggle field');
47
    }
48
49
    public function testFieldsByNameAreAccessibleAndCorrectType()
50
    {
51
        $readonlyField = $this->field->Field();
52
53
        $pageId = $readonlyField->fieldByName('Foo_PageID');
54
        $this->assertInstanceOf(TreeDropdownField_Readonly::class, $pageId, 'Page selector is a tree dropdown');
55
        $this->assertSame($this->idFromFixture(SiteTree::class, 'a_page'), $pageId->Value());
56
    }
57
58
    public function testAllFieldsAreReadonly()
59
    {
60
        // There is no spoon
61
        $fields = $this->field->Field()->FieldList();
62
63
        $this->assertInstanceOf(FieldList::class, $fields);
64
65
        foreach ($fields as $field) {
66
            $this->assertTrue($field->isReadonly());
67
        }
68
    }
69
}
70