Completed
Pull Request — master (#146)
by Franco
02:39
created

tests/DMSDocumentSetTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class DMSDocumentSetTest extends SapphireTest
4
{
5
    protected static $fixture_file = 'dmstest.yml';
6
7
    /**
8
     * Ensure that getDocuments is extensible
9
     */
10 View Code Duplication
    public function testGetDocumentsIsExtensible()
11
    {
12
        DMSDocumentSet::add_extension('StubRelatedDocumentExtension');
13
14
        $set = new DMSDocumentSet;
15
        $documents = $set->getDocuments();
16
17
        $this->assertCount(1, $documents);
18
        $this->assertSame('Extended', $documents->first()->Filename);
19
    }
20
21
    /**
22
     * Test that the GridField for documents isn't shown until you've saved the set
23
     */
24
    public function testGridFieldShowsWhenSetIsSaved()
25
    {
26
        $set = DMSDocumentSet::create();
27
28
        // Not in database yet
29
        $fields = $set->getCMSFields();
30
        $this->assertNull($fields->fieldByName('Root.Main.Documents'));
31
        $gridFieldNotice = $fields->fieldByName('Root.Main.GridFieldNotice');
32
        $this->assertNotNull($gridFieldNotice);
33
        $this->assertContains('Managing documents will be available', $gridFieldNotice->getContent());
34
35
        // In the database
36
        $set->Title = 'Testing';
37
        $set->write();
38
        $fields = $set->getCMSFields();
39
        $this->assertNotNull($fields->fieldByName('Root.Main.Documents'));
40
        $this->assertNull($fields->fieldByName('Root.Main.GridFieldNotice'));
41
    }
42
43
    public function testRelations()
44
    {
45
        $s1 = $this->objFromFixture('SiteTree', 's1');
46
        $s2 = $this->objFromFixture('SiteTree', 's2');
47
        $s4 = $this->objFromFixture('SiteTree', 's4');
48
49
        $ds1 = $this->objFromFixture('DMSDocumentSet', 'ds1');
50
        $ds2 = $this->objFromFixture('DMSDocumentSet', 'ds2');
51
        $ds3 = $this->objFromFixture('DMSDocumentSet', 'ds3');
52
53
        $this->assertCount(0, $s4->getDocumentSets(), 'Page 4 has no document sets associated');
54
        $this->assertCount(2, $s1->getDocumentSets(), 'Page 1 has 2 document sets');
55
        $this->assertEquals(array($ds1->ID, $ds2->ID), $s1->getDocumentSets()->column('ID'));
56
    }
57
58
    /**
59
     * Test that various components exist in the GridField config. See {@link DMSDocumentSet::getCMSFields} for context.
60
     */
61
    public function testDocumentGridFieldConfig()
62
    {
63
        $set = $this->objFromFixture('DMSDocumentSet', 'ds1');
64
        $fields = $set->getCMSFields();
65
        $gridField = $fields->fieldByName('Root.Main.Documents');
66
        $this->assertTrue((bool) $gridField->hasClass('documents'));
67
68
        /** @var GridFieldConfig $config */
69
        $config = $gridField->getConfig();
70
71
        $this->assertNotNull($addNew = $config->getComponentByType('DMSGridFieldAddNewButton'));
72
        $this->assertSame($set->ID, $addNew->getDocumentSetId());
73
74
        if (class_exists('GridFieldPaginatorWithShowAll')) {
75
            $this->assertNotNull($config->getComponentByType('GridFieldPaginatorWithShowAll'));
76
        } else {
77
            $paginator = $config->getComponentByType('GridFieldPaginator');
78
            $this->assertNotNull($paginator);
79
            $this->assertSame(15, $paginator->getItemsPerPage());
80
        }
81
82
        $sortableAssertion = class_exists('GridFieldSortableRows') ? 'assertNotNull' : 'assertNull';
83
        $this->$sortableAssertion($config->getComponentByType('GridFieldSortableRows'));
84
    }
85
86
    /**
87
     * Ensure that the display fields for the documents GridField can be returned
88
     */
89
    public function testGetDocumentDisplayFields()
90
    {
91
        $document = $this->objFromFixture('DMSDocumentSet', 'ds1');
92
        $this->assertInternalType('array', $document->getDocumentDisplayFields());
93
94
        Config::inst()->update('DMSDocument', 'display_fields', array('apple' => 'Apple', 'orange' => 'Orange'));
95
        $displayFields = $document->getDocumentDisplayFields();
96
        $this->assertContains('Apple', $displayFields);
97
        $this->assertContains('Orange', $displayFields);
98
        $this->assertArrayHasKey('ManuallyAdded', $displayFields);
99
        $this->assertContains('Added', $displayFields);
100
    }
101
102
    /**
103
     * Tests to ensure that the callback for formatting ManuallyAdded will return a nice label for the user
104
     */
105
    public function testNiceFormattingForManuallyAddedInGridField()
106
    {
107
        $fieldFormatting = $this->objFromFixture('DMSDocumentSet', 'ds1')
108
            ->getCMSFields()
109
            ->fieldByName('Root.Main.Documents')
110
            ->getConfig()
111
            ->getComponentByType('GridFieldDataColumns')
112
            ->getFieldFormatting();
113
114
        $this->assertArrayHasKey('ManuallyAdded', $fieldFormatting);
115
        $this->assertTrue(is_callable($fieldFormatting['ManuallyAdded']));
116
117
        $this->assertSame('Manually', $fieldFormatting['ManuallyAdded'](1));
118
        $this->assertSame('Query Builder', $fieldFormatting['ManuallyAdded'](0));
119
    }
120
121
    /**
122
     * Test that query fields can be added to the gridfield
123
     */
124
    public function testAddQueryFields()
125
    {
126
        /** @var DMSDocumentSet $set */
127
        $set = $this->objFromFixture('DMSDocumentSet', 'ds6');
128
        /** @var FieldList $fields */
129
        $fields = new FieldList(new TabSet('Root'));
130
        /** @var FieldList $fields */
131
        $set->addQueryFields($fields);
132
        $keyValuePairs = $fields->dataFieldByName('KeyValuePairs');
133
        $this->assertNotNull(
134
            $keyValuePairs,
135
            'addQueryFields() includes KeyValuePairs composite field'
136
        );
137
        $this->assertNotNull(
138
            $keyValuePairs->fieldByName('KeyValuePairs[Title]'),
139
            'addQueryFields() includes KeyValuePairs composite field'
140
        );
141
142
        // Test that the notification field exists
143
        $this->assertNotNull($fields->fieldByName('Root.QueryBuilder.GridFieldNotice'));
0 ignored issues
show
The method assertNotNull() does not seem to exist on object<DMSDocumentSetTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
145
        // Test that Tags__ID field exists
146
        $this->assertContains(
147
            'Tags can be set in the taxonomy area,',
148
            $keyValuePairs->fieldByName('KeyValuePairs[Tags__ID]')->RightTitle()
149
        );
150
    }
151
152
    /**
153
     * Ensure that the "direction" dropdown field has user friendly field labels
154
     */
155
    public function testQueryBuilderDirectionFieldHasFriendlyLabels()
156
    {
157
        $fields = $this->objFromFixture('DMSDocumentSet', 'ds1')->getCMSFields();
158
159
        $dropdown = $fields->fieldByName('Root.QueryBuilder')->FieldList()->filterByCallback(function ($field) {
160
            return $field instanceof FieldGroup;
161
        })->first()->fieldByName('SortByDirection');
162
163
        $this->assertInstanceOf('DropdownField', $dropdown);
164
        $source = $dropdown->getSource();
165
        $this->assertContains('Ascending', $source);
166
        $this->assertContains('Descending', $source);
167
    }
168
169
    /**
170
     * Ensure that the configurable shortcode handler key is a hidden field in the CMS
171
     */
172
    public function testShortcodeHandlerKeyFieldExists()
173
    {
174
        Config::inst()->update('DMS', 'shortcode_handler_key', 'unit-test');
175
176
        $set = DMSDocumentSet::create(array('Title' => 'TestSet'));
177
        $set->write();
178
179
        $fields = $set->getCMSFields();
180
        $field = $fields->fieldByName('Root.Main.DMSShortcodeHandlerKey');
181
182
        $this->assertInstanceOf('HiddenField', $field);
183
        $this->assertSame('unit-test', $field->Value());
184
    }
185
186
    /**
187
     * Ensure that if the module is available, the orderable rows GridField component is added
188
     */
189
    public function testDocumentsAreOrderable()
190
    {
191
        if (!class_exists('GridFieldSortableRows')) {
192
            $this->markTestSkipped('Test requires undefinedoffset/sortablegridfield installed.');
193
        }
194
195
        $fields = $this->objFromFixture('DMSDocumentSet', 'ds1')->getCMSFields();
196
197
        $gridField = $fields->fieldByName('Root.Main.Documents');
198
        $this->assertInstanceOf('GridField', $gridField);
199
200
        $this->assertInstanceOf(
201
            'GridFieldSortableRows',
202
            $gridField->getConfig()->getComponentByType('GridFieldSortableRows')
203
        );
204
    }
205
206
    /**
207
     * Test that extra documents are added after write
208
     */
209
    public function testSaveLinkedDocuments()
210
    {
211
        /** @var DMSDocumentSet $set */
212
        $set = $this->objFromFixture('DMSDocumentSet', 'dsSaveLinkedDocuments');
213
        // Assert initially docs
214
        $this->assertEquals(1, $set->getDocuments()->count(), 'Set has 1 document');
215
        // Now apply the query and see if 2 extras were added with CreatedByID filter
216
        $set->KeyValuePairs = '{"Filename":"extradoc3"}';
217
        $set->saveLinkedDocuments();
218
        $this->assertEquals(2, $set->getDocuments()->count(), 'Set has 2 documents');
219
    }
220
221
    /**
222
     * Tests that an exception is thrown if no title entered for a DMSDocumentSet.
223
     * @expectedException ValidationException
224
     */
225
    public function testExceptionOnNoTitleGiven()
226
    {
227
        DMSDocumentSet::create(array('Title' => ''))->write();
228
    }
229
230
    /**
231
     * Ensure that when editing in a page context that the "page" field is removed, or is labelled "Show on page"
232
     * otherwise
233
     */
234
    public function testPageFieldRemovedWhenEditingInPageContext()
235
    {
236
        $set = $this->objFromFixture('DMSDocumentSet', 'ds1');
237
238
        $fields = $set->getCMSFields();
239
        $this->assertInstanceOf('DropdownField', $fields->fieldByName('Root.Main.PageID'));
240
241
        $pageController = new CMSPageEditController;
242
        $pageController->pushCurrent();
243
244
        $fields = $set->getCMSFields();
245
        $this->assertNull($fields->fieldByName('Root.Main.PageID'));
246
    }
247
}
248