Completed
Push — master ( 6b80f3...f506fb )
by Franco
12s
created

DMSDocumentTest::testGetTitleOrFilenameWithoutId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
class DMSDocumentTest 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...
3
{
4
    protected static $fixture_file = 'dmstest.yml';
5
6 View Code Duplication
    public function tearDownOnce()
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...
7
    {
8
        self::$is_running_test = true;
9
10
        $d = DataObject::get('DMSDocument');
11
        foreach ($d as $d1) {
12
            $d1->delete();
13
        }
14
        $t = DataObject::get('DMSTag');
15
        foreach ($t as $t1) {
16
            $t1->delete();
17
        }
18
19
        self::$is_running_test = $this->originalIsRunningTest;
20
    }
21
22
    public function testDefaultDownloadBehabiourCMSFields()
23
    {
24
        $document = singleton('DMSDocument');
25
        Config::inst()->update('DMSDocument', 'default_download_behaviour', 'open');
26
        $cmsFields = $document->getCMSFields();
27
        $this->assertEquals('open', $cmsFields->dataFieldByName('DownloadBehavior')->Value());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentTest>.

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...
28
29
30
        Config::inst()->update('DMSDocument', 'default_download_behaviour', 'download');
31
        $cmsFields = $document->getCMSFields();
32
        $this->assertEquals('download', $cmsFields->dataFieldByName('DownloadBehavior')->Value());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentTest>.

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
    }
34
35
    /**
36
     * Ensure that related documents can be retrieved for a given DMS document
37
     */
38
    public function testRelatedDocuments()
39
    {
40
        $document = $this->objFromFixture('DMSDocument', 'document_with_relations');
41
        $this->assertGreaterThan(0, $document->RelatedDocuments()->count());
0 ignored issues
show
Bug introduced by
The method assertGreaterThan() does not seem to exist on object<DMSDocumentTest>.

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...
42
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentTest>.

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...
43
            array('test-file-file-doesnt-exist-1', 'test-file-file-doesnt-exist-2'),
44
            $document->getRelatedDocuments()->column('Filename')
45
        );
46
    }
47
48
    /**
49
     * Test the extensibility of getRelatedDocuments
50
     */
51 View Code Duplication
    public function testGetRelatedDocumentsIsExtensible()
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...
52
    {
53
        DMSDocument::add_extension('StubRelatedDocumentExtension');
54
55
        $emptyDocument = new DMSDocument;
56
        $relatedDocuments = $emptyDocument->getRelatedDocuments();
57
58
        $this->assertCount(1, $relatedDocuments);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DMSDocumentTest>.

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...
59
        $this->assertSame('Extended', $relatedDocuments->first()->Filename);
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSDocumentTest>.

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...
60
    }
61
62
    /**
63
     * Ensure that the DMS Document CMS actions contains a grid field for managing related documents
64
     */
65
    public function testDocumentHasCmsFieldForManagingRelatedDocuments()
66
    {
67
        $document = $this->objFromFixture('DMSDocument', 'document_with_relations');
68
        $gridField = $this->getGridFieldFromDocument($document);
0 ignored issues
show
Documentation introduced by
$document is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
        $gridFieldConfig = $gridField->getConfig();
70
71
        $this->assertNotNull(
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentTest>.

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
            'GridFieldAddExistingAutocompleter',
73
            $addExisting = $gridFieldConfig->getComponentByType('GridFieldAddExistingAutocompleter'),
74
            'Related documents GridField has an "add existing" autocompleter'
75
        );
76
77
        $this->assertNull(
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DMSDocumentTest>.

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...
78
            $gridFieldConfig->getComponentByType('GridFieldAddNewButton'),
79
            'Related documents GridField does not have an "add new" button'
80
        );
81
    }
82
83
    /**
84
     * Ensure that the related documents list does not include the current document itself
85
     */
86
    public function testGetRelatedDocumentsForAutocompleter()
87
    {
88
        $document = $this->objFromFixture('DMSDocument', 'd1');
89
        $gridField = $this->getGridFieldFromDocument($document);
0 ignored issues
show
Documentation introduced by
$document is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
91
        $config = $gridField->getConfig();
92
93
        $autocompleter = $config->getComponentByType('GridFieldAddExistingAutocompleter');
94
        $autocompleter->setResultsFormat('$Filename');
95
96
        $jsonResult = $autocompleter->doSearch(
97
            $gridField,
98
            new SS_HTTPRequest('GET', '/', array('gridfield_relationsearch' => 'test'))
99
        );
100
101
        $this->assertNotContains('test-file-file-doesnt-exist-1', $jsonResult);
102
        $this->assertContains('test-file-file-doesnt-exist-2', $jsonResult);
103
    }
104
105
    /**
106
     * @return GridField
107
     */
108
    protected function getGridFieldFromDocument(DMSDocument $document)
109
    {
110
        $documentFields = $document->getCMSFields();
111
        /** @var FieldGroup $actions */
112
        $actions = $documentFields->fieldByName('ActionsPanel');
113
114
        $gridField = null;
115
        foreach ($actions->getChildren() as $child) {
116
            /** @var FieldGroup $child */
117
            if ($gridField = $child->fieldByName('RelatedDocuments')) {
118
                break;
119
            }
120
        }
121
        $this->assertInstanceOf('GridField', $gridField);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DMSDocumentTest>.

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...
122
        return $gridField;
123
    }
124
125
    /**
126
     * Ensure that HTML is returned containing list items with action panel steps
127
     */
128
    public function testGetActionTaskHtml()
129
    {
130
        $document = $this->objFromFixture('DMSDocument', 'd1');
131
        $document->addActionPanelTask('example', 'Example');
132
133
        $result = $document->getActionTaskHtml();
134
135
        $this->assertContains('<label class="left">Actions</label>', $result);
136
        $this->assertContains('<li class="ss-ui-button" data-panel="', $result);
137
        $this->assertContains('permission', $result);
138
        $this->assertContains('Example', $result);
139
    }
140
141
    /*
142
     * Tests whether the permissions fields are added
143
     */
144
    public function testGetPermissionsActionPanel()
145
    {
146
        $result = $this->objFromFixture('DMSDocument', 'd1')->getPermissionsActionPanel();
147
148
        $this->assertInstanceOf('CompositeField', $result);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DMSDocumentTest>.

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...
149
        $this->assertNotNull($result->getChildren()->fieldByName('CanViewType'));
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentTest>.

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...
150
        $this->assertNotNull($result->getChildren()->fieldByName('ViewerGroups'));
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentTest>.

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...
151
    }
152
153
    /**
154
     * Test view permissions
155
     */
156
    public function testCanView()
157
    {
158
        /** @var DMSDocument $document */
159
        $document = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
160
        // Make sure user is logged out
161
        if ($member = Member::currentUser()) {
162
            $member->logOut();
163
        }
164
165
        // Logged out user test
166
        $this->assertFalse($document->canView());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentTest>.

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...
167
168
        // Logged in user test
169
        $adminID = $this->logInWithPermission();
170
        $admin = Member::get()->byID($adminID);
171
        $this->assertTrue($document->canView($admin));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentTest>.

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...
172
        /** @var Member $member */
173
        $admin->logout();
174
175
        // Check anyone
176
        $document = $this->objFromFixture('DMSDocument', 'doc-anyone');
177
        $this->assertTrue($document->canView());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentTest>.

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...
178
179
        // Check OnlyTheseUsers
180
        $document = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
181
        $reportAdminID = $this->logInWithPermission('cable-guy');
182
        /** @var Member $reportAdmin */
183
        $reportAdmin = Member::get()->byID($reportAdminID);
184
        $this->assertFalse($document->canView($reportAdmin));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentTest>.

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...
185
        // Add reportAdmin to group
186
        $reportAdmin->addToGroupByCode('content-author');
187
        $this->assertTrue($document->canView($reportAdmin));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentTest>.

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...
188
        $reportAdmin->logout();
189
    }
190
191
    /**
192
     * Tests edit permissions
193
     */
194
    public function testCanEdit()
195
    {
196
        // Make sure user is logged out
197
        if ($member = Member::currentUser()) {
198
            $member->logOut();
199
        }
200
201
        /** @var DMSDocument $document1 */
202
        $document1 = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
203
204
        // Logged out user test
205
        $this->assertFalse($document1->canEdit());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentTest>.

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...
206
207
        //Logged in user test
208
        $contentAuthor = $this->objFromFixture('Member', 'editor');
209
        $this->assertTrue($document1->canEdit($contentAuthor));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentTest>.

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...
210
211
        // Check OnlyTheseUsers
212
        /** @var DMSDocument $document2 */
213
        $document2 = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
214
        /** @var Member $cableGuy */
215
        $cableGuy = $this->objFromFixture('Member', 'non-editor');
216
        $this->assertFalse($document2->canEdit($cableGuy));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentTest>.

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...
217
218
        $cableGuy->addToGroupByCode('content-author');
219
        $this->assertTrue($document2->canEdit($cableGuy));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentTest>.

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...
220
    }
221
222
    /**
223
     * Test permission denied reasons for documents
224
     */
225
    public function testGetPermissionDeniedReason()
226
    {
227
        /** @var DMSDocument $document1 */
228
        $doc1 = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
229
        $this->assertContains('Please log in to view this document', $doc1->getPermissionDeniedReason());
230
231
        /** @var DMSDocument $doc2 */
232
        $doc2 = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
233
        $this->assertContains('You are not authorised to view this document', $doc2->getPermissionDeniedReason());
234
235
        /** @var DMSDocument $doc3 */
236
        $doc3 = $this->objFromFixture('DMSDocument', 'doc-anyone');
237
        $this->assertEquals('', $doc3->getPermissionDeniedReason());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentTest>.

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...
238
    }
239
240
    /**
241
     * Ensure that all pages that a document belongs to (via many document sets) can be retrieved in one list
242
     */
243
    public function testGetRelatedPages()
244
    {
245
        $document = $this->objFromFixture('DMSDocument', 'd1');
246
        $result = $document->getRelatedPages();
247
        $this->assertCount(3, $result, 'Document 1 is related to 3 Pages');
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DMSDocumentTest>.

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...
248
        $this->assertSame(array('s1', 's2', 's3'), $result->column('URLSegment'));
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSDocumentTest>.

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...
249
    }
250
251
    /**
252
     * Test that the title is returned if it is set, otherwise the filename without ID
253
     */
254
    public function testGetTitleOrFilenameWithoutId()
255
    {
256
        $d1 = $this->objFromFixture('DMSDocument', 'd1');
257
        $this->assertSame('test-file-file-doesnt-exist-1', $d1->getTitle());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSDocumentTest>.

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...
258
259
        $d2 = $this->objFromFixture('DMSDocument', 'd2');
260
        $this->assertSame('File That Doesn\'t Exist (Title)', $d2->getTitle());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSDocumentTest>.

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...
261
    }
262
}
263