testHistoryViewerFieldsAreRemovedFromFieldLists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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(): void
33
    {
34
        parent::setUp();
35
        $this->factory = new DataObjectVersionFormFactory();
36
        $this->controller = new HistoryViewerController();
37
    }
38
39
    public function testGetFormType()
40
    {
41
        $this->assertSame('custom', $this->factory->getFormType(['Type' => 'custom']));
42
        $this->assertSame('history', $this->factory->getFormType([]));
43
    }
44
45
    public function testIsReadonlyFormType()
46
    {
47
        $this->assertFalse(
48
            $this->factory->isReadonlyFormType(['Type' => 'custom']),
49
            'Custom, or non default form types are not registered automatically as readonly form types'
50
        );
51
52
        $this->assertTrue(
53
            $this->factory->isReadonlyFormType([]),
54
            'Default "history" form type is registered as a readonly form type'
55
        );
56
    }
57
58
    public function testGetFormThrowsExceptionOnMissingRequiredContext()
59
    {
60
        $this->expectException(InvalidArgumentException::class);
61
        $this->expectExceptionMessage('Missing required context Record');
62
        $this->factory->getForm();
63
    }
64
65
    public function testFormFieldFromDataObjectAreAddedToForm()
66
    {
67
        $context = [
68
            'Record' => $this->objFromFixture(ObjectWithFields::class, 'object_one'),
69
            'Type' => 'custom_type',
70
        ];
71
        $form = $this->factory->getForm($this->controller, 'some_form', $context);
72
        $this->assertInstanceOf(TextField::class, $form->Fields()->fieldByName('Root.Main.Title'));
73
    }
74
75
    public function testHistoryViewerFieldsAreRemovedFromFieldLists()
76
    {
77
        $context = [
78
            'Record' => $this->objFromFixture(ObjectWithFields::class, 'object_one'),
79
            'Type' => 'custom_type',
80
        ];
81
        $form = $this->factory->getForm($this->controller, 'some_form', $context);
82
        $this->assertNull($form->Fields()->fieldByName('Root.CustomHistoryTab.History'));
83
    }
84
85
    public function testEmptyTabsAreRemovedAfterRemovingHistoryViewerFields()
86
    {
87
        $context = [
88
            'Record' => $this->objFromFixture(ObjectWithFields::class, 'object_one'),
89
        ];
90
        $form = $this->factory->getForm($this->controller, 'some_form', $context);
91
        $this->assertNull($form->Fields()->fieldByName('Root.CustomHistoryTab'));
92
        $this->assertNotNull($form->Fields()->fieldByName('Root.Main'));
93
    }
94
95
    public function testFieldsAreMadeReadonlyInDefaultContext()
96
    {
97
        $context = [
98
            'Record' => $this->objFromFixture(ObjectWithFields::class, 'object_one'),
99
        ];
100
        $form = $this->factory->getForm($this->controller, 'some_form', $context);
101
        $this->assertInstanceOf(
102
            ReadonlyField::class,
103
            $form->Fields()->fieldByName('Root.Main.Title'),
104
            'FieldList has readonly transformation performed'
105
        );
106
    }
107
108
    public function testSiteTreeMetaFieldsHaveNoRightTitle()
109
    {
110
        if (!class_exists(SiteTree::class)) {
111
            $this->markTestSkipped('This test requires the cms module to be installed.');
112
        }
113
114
        $record = new SiteTree();
115
        $record->write();
116
        $context = [
117
            'Record' => $record,
118
        ];
119
        $form = $this->factory->getForm($this->controller, 'some_form', $context);
120
        $field = $form->Fields()->dataFieldByName('MetaDescription');
121
        $this->assertInstanceOf(
122
            ReadonlyField::class,
123
            $field,
124
            'FieldList has readonly transformation performed'
125
        );
126
        $this->assertEmpty($field->RightTitle());
127
    }
128
}
129