Passed
Pull Request — master (#8)
by Robbie
02:56
created

testSiteTreeMetaFieldsHaveNoRightTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\VersionedAdmin\Tests\Forms;
4
5
use InvalidArgumentException;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Forms\ReadonlyField;
9
use SilverStripe\Forms\TextField;
10
use SilverStripe\VersionedAdmin\Controllers\HistoryViewerController;
11
use SilverStripe\VersionedAdmin\Forms\DataObjectVersionFormFactory;
12
use SilverStripe\VersionedAdmin\Tests\Forms\DataObjectVersionFormFactoryTest\ObjectWithFields;
13
14
class DataObjectVersionFormFactoryTest extends SapphireTest
15
{
16
    protected static $fixture_file = 'DataObjectVersionFormFactoryTest.yml';
17
18
    protected static $extra_dataobjects = [
19
        ObjectWithFields::class,
20
    ];
21
22
    /**
23
     * @var DataObjectVersionFormFactory
24
     */
25
    protected $factory;
26
27
    /**
28
     * @var HistoryViewerController
29
     */
30
    protected $controller;
31
32
    protected function setUp()
33
    {
34
        parent::setUp();
35
36
        $this->factory = new DataObjectVersionFormFactory();
37
        $this->controller = new HistoryViewerController();
38
    }
39
40
    public function testGetFormType()
41
    {
42
        $this->assertSame('custom', $this->factory->getFormType(['Type' => 'custom']));
43
        $this->assertSame('history', $this->factory->getFormType([]));
44
    }
45
46
    public function testIsReadonlyFormType()
47
    {
48
        $this->assertFalse(
49
            $this->factory->isReadonlyFormType(['Type' => 'custom']),
50
            'Custom, or non default form types are not registered automatically as readonly form types'
51
        );
52
53
        $this->assertTrue(
54
            $this->factory->isReadonlyFormType([]),
55
            'Default "history" form type is registered as a readonly form type'
56
        );
57
    }
58
59
    /**
60
     * @expectedException InvalidArgumentException
61
     * @expectedExceptionMessage Missing required context Record
62
     */
63
    public function testGetFormThrowsExceptionOnMissingRequiredContext()
64
    {
65
        $this->factory->getForm();
66
    }
67
68
    public function testFormFieldFromDataObjectAreAddedToForm()
69
    {
70
        $context = [
71
            'Record' => $this->objFromFixture(ObjectWithFields::class, 'object_one'),
72
            'Type' => 'custom_type',
73
        ];
74
        $form = $this->factory->getForm($this->controller, 'some_form', $context);
75
        $this->assertInstanceOf(TextField::class, $form->Fields()->fieldByName('Root.Main.Title'));
76
    }
77
78
    public function testHistoryViewerFieldsAreRemovedFromFieldLists()
79
    {
80
        $context = [
81
            'Record' => $this->objFromFixture(ObjectWithFields::class, 'object_one'),
82
            'Type' => 'custom_type',
83
        ];
84
        $form = $this->factory->getForm($this->controller, 'some_form', $context);
85
        $this->assertNull($form->Fields()->fieldByName('Root.CustomHistoryTab.History'));
86
    }
87
88
    public function testEmptyTabsAreRemovedAfterRemovingHistoryViewerFields()
89
    {
90
        $context = [
91
            'Record' => $this->objFromFixture(ObjectWithFields::class, 'object_one'),
92
        ];
93
        $form = $this->factory->getForm($this->controller, 'some_form', $context);
94
        $this->assertNull($form->Fields()->fieldByName('Root.CustomHistoryTab'));
95
        $this->assertNotNull($form->Fields()->fieldByName('Root.Main'));
96
    }
97
98
    public function testFieldsAreMadeReadonlyInDefaultContext()
99
    {
100
        $context = [
101
            'Record' => $this->objFromFixture(ObjectWithFields::class, 'object_one'),
102
        ];
103
        $form = $this->factory->getForm($this->controller, 'some_form', $context);
104
        $this->assertInstanceOf(
105
            ReadonlyField::class,
106
            $form->Fields()->fieldByName('Root.Main.Title'),
107
            'FieldList has readonly transformation performed'
108
        );
109
    }
110
111
    public function testSiteTreeMetaFieldsHaveNoRightTitle()
112
    {
113
        if (!class_exists(SiteTree::class)) {
114
            $this->markTestSkipped('This test requires the cms module to be installed.');
115
        }
116
117
        $context = [
118
            'Record' => new SiteTree(),
119
        ];
120
        $form = $this->factory->getForm($this->controller, 'some_form', $context);
121
        $field = $form->Fields()->dataFieldByName('MetaDescription');
122
        $this->assertInstanceOf(
123
            ReadonlyField::class,
124
            $field,
125
            'FieldList has readonly transformation performed'
126
        );
127
        $this->assertEmpty($field->RightTitle());
128
    }
129
}
130