Passed
Push — int-types ( 949579...4b2be5 )
by Sam
05:58
created

testReadOnlyGetHTMLFragmentsWithoutTabSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms\Tests\GridField;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\Form;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
11
use SilverStripe\Forms\GridField\GridFieldLazyLoader;
12
use SilverStripe\Forms\Tab;
13
use SilverStripe\Forms\TabSet;
14
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
15
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
16
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
17
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player;
18
use SilverStripe\ORM\ArrayList;
19
use SilverStripe\ORM\DataList;
20
21
class GridFieldLazyLoaderTest extends SapphireTest
22
{
23
    /**
24
     * @var ArrayList
25
     */
26
    protected $list;
27
28
    /**
29
     * @var GridField
30
     */
31
    protected $gridField;
32
33
    /**
34
     * @var GridFieldLazyLoader
35
     */
36
    protected $component;
37
38
    protected static $fixture_file = 'GridFieldTest.yml';
39
40
    protected static $extra_dataobjects = [
41
        Permissions::class,
42
        Cheerleader::class,
43
        Player::class,
44
        Team::class,
45
    ];
46
47
    protected function setUp()
48
    {
49
        parent::setUp();
50
        $this->list = DataList::create(Team::class);
51
        $this->component = new GridFieldLazyLoader();
52
        $config = GridFieldConfig_RecordEditor::create()->addComponent($this->component);
53
        $this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
54
    }
55
56
    public function testGetManipulatedDataWithoutHeader()
57
    {
58
        $gridField = $this->getHeaderlessGridField();
59
        $this->assertCount(
60
            0,
61
            $this->component->getManipulatedData($gridField, $this->list)->toArray(),
62
            'GridFieldLazyLoader::getManipulatedData should return an empty list if the X-Pjax is unset'
63
        );
64
    }
65
66
    public function testGetManipulatedDataWithoutTabSet()
67
    {
68
        $gridField = $this->getOutOfTabSetGridField();
69
        $this->assertSameSize(
70
            $this->list,
71
            $this->component->getManipulatedData($gridField, $this->list)->toArray(),
72
            'GridFieldLazyLoader::getManipulatedData should return a proper list if GridField is not in a tab'
73
        );
74
    }
75
76
    public function testGetManipulatedDataNonLazy()
77
    {
78
        $gridField = $this->getNonLazyGridField();
79
        $this->assertSameSize(
80
            $this->list,
81
            $this->component->getManipulatedData($gridField, $this->list),
82
            'GridFieldLazyLoader::getManipulatedData should return a proper list if GridField'
83
            . ' is in a tab with the pajax header'
84
        );
85
    }
86
87
    public function testGetHTMLFragmentsWithoutHeader()
88
    {
89
        $gridField = $this->getHeaderlessGridField();
90
        $actual = $this->component->getHTMLFragments($gridField);
91
        $this->assertEmpty($actual, 'getHTMLFragments should always return an array');
92
        $this->assertContains('grid-field--lazy-loadable', $gridField->extraClass());
93
        $this->assertNotContains('grid-field--lazy-loaded', $gridField->extraClass());
94
    }
95
96
    public function testGetHTMLFragmentsWithoutTabSet()
97
    {
98
        $gridField = $this->getOutOfTabSetGridField();
99
        $actual = $this->component->getHTMLFragments($gridField);
100
        $this->assertEmpty($actual, 'getHTMLFragments should always return an array');
101
        $this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
102
        $this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
103
    }
104
105
    public function testGetHTMLFragmentsNonLazy()
106
    {
107
        $gridField = $this->getNonLazyGridField();
108
        $actual = $this->component->getHTMLFragments($gridField);
109
        $this->assertEmpty($actual, 'getHTMLFragments should always return an array');
110
        $this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
111
        $this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
112
    }
113
114
115
    public function testReadOnlyGetManipulatedDataWithoutHeader()
116
    {
117
        $gridField = $this->makeGridFieldReadonly($this->getHeaderlessGridField());
118
        $this->assertCount(
119
            0,
120
            $this->component->getManipulatedData($gridField, $this->list)->toArray(),
121
            'Readonly GridFieldLazyLoader::getManipulatedData should return an empty list if the X-Pjax'
122
            . ' is unset'
123
        );
124
    }
125
126
    public function testReadOnlyGetManipulatedDataWithoutTabSet()
127
    {
128
        $gridField = $this->makeGridFieldReadonly($this->getOutOfTabSetGridField());
129
        $this->assertSameSize(
130
            $this->list,
131
            $this->component->getManipulatedData($gridField, $this->list)->toArray(),
132
            'Readonly GridFieldLazyLoader::getManipulatedData should return a proper list if GridField is'
133
            . ' not in a tab'
134
        );
135
    }
136
137
    public function testReadOnlyGetManipulatedDataNonLazy()
138
    {
139
        $gridField = $this->makeGridFieldReadonly($this->getNonLazyGridField());
140
        $this->assertSameSize(
141
            $this->list,
142
            $this->component->getManipulatedData($gridField, $this->list),
143
            'Readonly GridFieldLazyLoader::getManipulatedData should return a proper list if GridField is in'
144
            . ' a tab with the pajax header'
145
        );
146
    }
147
148
    public function testReadOnlyGetHTMLFragmentsWithoutHeader()
149
    {
150
        $gridField = $this->makeGridFieldReadonly($this->getHeaderlessGridField());
151
        $actual = $this->component->getHTMLFragments($gridField);
152
        $this->assertEmpty($actual, 'getHTMLFragments should always return an array');
153
        $this->assertContains('grid-field--lazy-loadable', $gridField->extraClass());
154
        $this->assertNotContains('grid-field--lazy-loaded', $gridField->extraClass());
155
    }
156
157
    public function testReadOnlyGetHTMLFragmentsWithoutTabSet()
158
    {
159
        $gridField = $this->makeGridFieldReadonly($this->getOutOfTabSetGridField());
160
        $actual = $this->component->getHTMLFragments($gridField);
161
        $this->assertEmpty($actual, 'getHTMLFragments should always return an array');
162
        $this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
163
        $this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
164
    }
165
166
    public function testReadOnlyGetHTMLFragmentsNonLazy()
167
    {
168
        $gridField = $this->makeGridFieldReadonly($this->getNonLazyGridField());
169
        $actual = $this->component->getHTMLFragments($gridField);
170
        $this->assertEmpty($actual, 'getHTMLFragments should always return an array');
171
        $this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
172
        $this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
173
    }
174
175
    /**
176
     * This GridField will be lazy because it doesn't have a `X-Pjax` header.
177
     * @return GridField
178
     */
179
    private function getHeaderlessGridField()
180
    {
181
        $this->gridField->setRequest(new HTTPRequest('GET', 'admin/pages/edit/show/9999'));
182
        $fieldList = FieldList::create(new TabSet('Root', new Tab('Main')));
183
        $fieldList->addFieldToTab('Root.GridField', $this->gridField);
184
        Form::create(null, 'Form', $fieldList, FieldList::create());
185
        return $this->gridField;
186
    }
187
188
    /**
189
     * This GridField will not be lazy because it's in not in a tab set.
190
     * @return GridField
191
     */
192
    private function getOutOfTabSetGridField()
193
    {
194
        $r = new HTTPRequest('POST', 'admin/pages/edit/EditForm/9999/field/testfield');
195
        $r->addHeader('X-Pjax', 'CurrentField');
196
        $this->gridField->setRequest($r);
197
        $fieldList = new FieldList($this->gridField);
198
        Form::create(null, 'Form', $fieldList, FieldList::create());
199
        return $this->gridField;
200
    }
201
202
    /**
203
     * This gridfield will not be lazy, because it has `X-Pjax` header equal to `CurrentField`
204
     * @return GridField
205
     */
206
    private function getNonLazyGridField()
207
    {
208
        $r = new HTTPRequest('POST', 'admin/pages/edit/EditForm/9999/field/testfield');
209
        $r->addHeader('X-Pjax', 'CurrentField');
210
        $this->gridField->setRequest($r);
211
        $fieldList = new FieldList(new TabSet('Root', new Tab('Main')));
212
        $fieldList->addFieldToTab('Root', $this->gridField);
213
        Form::create(null, 'Form', $fieldList, FieldList::create());
214
        return $this->gridField;
215
    }
216
217
    /**
218
     * Perform a readonly transformation on our GridField's Form and return the ReadOnly GridField.
219
     *
220
     * We need to make sure the LazyLoader component still works after our GridField has been made readonly.
221
     *
222
     * @param GridField $gridField
223
     * @return GridField
224
     */
225
    private function makeGridFieldReadonly(GridField $gridField)
226
    {
227
        $form = $gridField->getForm()->makeReadonly();
228
        $fields = $form->Fields()->dataFields();
229
        foreach ($fields as $field) {
230
            if ($field->getName() === 'testfield') {
231
                return $field;
232
            }
233
        }
234
    }
235
}
236