Completed
Pull Request — master (#162)
by Franco
02:21
created

DMSDocumentSetTest::testAddQueryFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
class DMSDocumentSetTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected static $fixture_file = 'dmstest.yml';
6
7
    /**
8
     * Ensure that getDocuments is extensible
9
     */
10 View Code Duplication
    public function testGetDocumentsIsExtensible()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
    {
12
        DMSDocumentSet::add_extension('StubRelatedDocumentExtension');
13
14
        $set = new DMSDocumentSet;
15
        $documents = $set->getDocuments();
16
17
        $this->assertCount(1, $documents);
0 ignored issues
show
Bug introduced by
The method assertCount() 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...
18
        $this->assertSame('Extended', $documents->first()->Filename);
0 ignored issues
show
Bug introduced by
The method assertSame() 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...
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'));
0 ignored issues
show
Bug introduced by
The method assertNull() 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...
31
        $gridFieldNotice = $fields->fieldByName('Root.Main.GridFieldNotice');
32
        $this->assertNotNull($gridFieldNotice);
0 ignored issues
show
Bug introduced by
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...
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'));
0 ignored issues
show
Bug introduced by
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...
40
        $this->assertNull($fields->fieldByName('Root.Main.GridFieldNotice'));
0 ignored issues
show
Bug introduced by
The method assertNull() 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...
41
    }
42
43
    public function testRelations()
44
    {
45
        $s1 = $this->objFromFixture('SiteTree', 's1');
46
        $s2 = $this->objFromFixture('SiteTree', 's2');
0 ignored issues
show
Unused Code introduced by
$s2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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');
0 ignored issues
show
Unused Code introduced by
$ds3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
53
        $this->assertCount(0, $s4->getDocumentSets(), 'Page 4 has no document sets associated');
0 ignored issues
show
Bug introduced by
The method assertCount() 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...
54
        $this->assertCount(2, $s1->getDocumentSets(), 'Page 1 has 2 document sets');
0 ignored issues
show
Bug introduced by
The method assertCount() 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...
55
        $this->assertEquals(array($ds1->ID, $ds2->ID), $s1->getDocumentSets()->column('ID'));
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
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'));
0 ignored issues
show
Bug introduced by
The method assertTrue() 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...
67
68
        /** @var GridFieldConfig $config */
69
        $config = $gridField->getConfig();
70
71
        $this->assertNotNull($addNew = $config->getComponentByType('DMSGridFieldAddNewButton'));
0 ignored issues
show
Bug introduced by
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...
72
        $this->assertSame($set->ID, $addNew->getDocumentSetId());
0 ignored issues
show
Bug introduced by
The method assertSame() 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...
73
74
        if (class_exists('GridFieldPaginatorWithShowAll')) {
75
            $this->assertNotNull($config->getComponentByType('GridFieldPaginatorWithShowAll'));
0 ignored issues
show
Bug introduced by
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...
76
        } else {
77
            $paginator = $config->getComponentByType('GridFieldPaginator');
78
            $this->assertNotNull($paginator);
0 ignored issues
show
Bug introduced by
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...
79
            $this->assertSame(15, $paginator->getItemsPerPage());
0 ignored issues
show
Bug introduced by
The method assertSame() 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...
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());
0 ignored issues
show
Bug introduced by
The method assertInternalType() 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...
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);
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() 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...
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);
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() 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...
115
        $this->assertTrue(is_callable($fieldFormatting['ManuallyAdded']));
0 ignored issues
show
Bug introduced by
The method assertTrue() 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...
116
117
        $this->assertSame('Manually', $fieldFormatting['ManuallyAdded'](1));
0 ignored issues
show
Bug introduced by
The method assertSame() 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...
118
        $this->assertSame('Query Builder', $fieldFormatting['ManuallyAdded'](0));
0 ignored issues
show
Bug introduced by
The method assertSame() 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...
119
    }
120
121
    /**
122
     * Test that query fields can be added to the gridfield
123
     */
124
    public function testAddQueryFields()
125
    {
126
127
        /** @var DMSDocumentSet $set */
128
        $set = $this->objFromFixture('DMSDocumentSet', 'ds6');
129
        /** @var FieldList $fields */
130
        $fields = new FieldList(new TabSet('Root'));
131
        /** @var FieldList $fields */
132
        $set->addQueryFields($fields);
133
        $keyValuePairs = $fields->dataFieldByName('KeyValuePairs');
134
        $this->assertNotNull(
0 ignored issues
show
Bug introduced by
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...
135
            $keyValuePairs,
136
            'addQueryFields() includes KeyValuePairs composite field'
137
        );
138
        $this->assertNotNull(
0 ignored issues
show
Bug introduced by
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...
139
            $keyValuePairs->fieldByName('KeyValuePairs[Title]'),
140
            'addQueryFields() includes KeyValuePairs composite field'
141
        );
142
143
        // Test that the notification field exists
144
        $this->assertNotNull($fields->fieldByName('Root.QueryBuilder.GridFieldNotice'));
0 ignored issues
show
Bug introduced by
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...
145
146
        // Test that Tags__ID field exists
147
        $this->assertContains(
148
            'Tags can be set in the taxonomies model admin',
149
            $keyValuePairs->fieldByName('KeyValuePairs[Tags__ID]')->RightTitle()
150
        );
151
    }
152
153
    public function testAddQueryFieldsIsExtensible()
154
    {
155
156
        DMSDocumentSet::add_extension('StubDocumentSetMockExtension');
157
158
        $fields = new FieldList(new TabSet('Root'));
159
        $set = new DMSDocumentSet;
160
        $set->addQueryFields($fields);
161
162
        $this->assertNotNull(
0 ignored issues
show
Bug introduced by
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...
163
            $fields->dataFieldByName('ExtendedField'),
164
            'addQueryFields() is extendible as it included the field from the extension'
165
        );
166
    }
167
168
    /**
169
     * Ensure that the configurable shortcode handler key is a hidden field in the CMS
170
     */
171
    public function testShortcodeHandlerKeyFieldExists()
172
    {
173
        Config::inst()->update('DMS', 'shortcode_handler_key', 'unit-test');
174
175
        $set = DMSDocumentSet::create(array('Title' => 'TestSet'));
176
        $set->write();
177
178
        $fields = $set->getCMSFields();
179
        $field = $fields->fieldByName('Root.Main.DMSShortcodeHandlerKey');
180
181
        $this->assertInstanceOf('HiddenField', $field);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() 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...
182
        $this->assertSame('unit-test', $field->Value());
0 ignored issues
show
Bug introduced by
The method assertSame() 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...
183
    }
184
185
    /**
186
     * Ensure that if the module is available, the orderable rows GridField component is added
187
     */
188
    public function testDocumentsAreOrderable()
189
    {
190
        if (!class_exists('GridFieldSortableRows')) {
191
            $this->markTestSkipped('Test requires undefinedoffset/sortablegridfield installed.');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() 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...
192
        }
193
194
        $fields = $this->objFromFixture('DMSDocumentSet', 'ds1')->getCMSFields();
195
196
        $gridField = $fields->fieldByName('Root.Main.Documents');
197
        $this->assertInstanceOf('GridField', $gridField);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() 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...
198
199
        $this->assertInstanceOf(
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() 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...
200
            'GridFieldSortableRows',
201
            $gridField->getConfig()->getComponentByType('GridFieldSortableRows')
202
        );
203
    }
204
205
    /**
206
     * Test that extra documents are added after write
207
     */
208
    public function testSaveLinkedDocuments()
209
    {
210
        /** @var DMSDocumentSet $set */
211
        $set = $this->objFromFixture('DMSDocumentSet', 'dsSaveLinkedDocuments');
212
        // Assert initially docs
213
        $this->assertEquals(1, $set->getDocuments()->count(), 'Set has 1 document');
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
214
        // Now apply the query and see if 2 extras were added with CreatedByID filter
215
        $set->KeyValuePairs = '{"Filename":"extradoc3"}';
216
        $set->saveLinkedDocuments();
217
        $this->assertEquals(2, $set->getDocuments()->count(), 'Set has 2 documents');
0 ignored issues
show
Bug introduced by
The method assertEquals() 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...
218
    }
219
220
    /**
221
     * Tests that an exception is thrown if no title entered for a DMSDocumentSet.
222
     * @expectedException ValidationException
223
     */
224
    public function testExceptionOnNoTitleGiven()
225
    {
226
        DMSDocumentSet::create(array('Title' => ''))->write();
227
    }
228
229
    /**
230
     * Ensure that when editing in a page context that the "page" field is removed, or is labelled "Show on page"
231
     * otherwise
232
     */
233
    public function testPageFieldRemovedWhenEditingInPageContext()
234
    {
235
        $set = $this->objFromFixture('DMSDocumentSet', 'ds1');
236
237
        $fields = $set->getCMSFields();
238
        $this->assertInstanceOf('DropdownField', $fields->fieldByName('Root.Main.PageID'));
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() 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...
239
240
        $pageController = new CMSPageEditController;
241
        $pageController->pushCurrent();
242
243
        $fields = $set->getCMSFields();
244
        $this->assertNull($fields->fieldByName('Root.Main.PageID'));
0 ignored issues
show
Bug introduced by
The method assertNull() 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...
245
    }
246
}
247