Completed
Pull Request — master (#138)
by Robbie
02:17
created

DMSDocumentTest::testGetLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
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
    public function testDefaultDownloadBehabiourCMSFields()
7
    {
8
        $document = singleton('DMSDocument');
9
        Config::inst()->update('DMSDocument', 'default_download_behaviour', 'open');
10
        $cmsFields = $document->getCMSFields();
11
        $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...
12
13
14
        Config::inst()->update('DMSDocument', 'default_download_behaviour', 'download');
15
        $cmsFields = $document->getCMSFields();
16
        $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...
17
    }
18
19
    /**
20
     * Ensure that related documents can be retrieved for a given DMS document
21
     */
22
    public function testRelatedDocuments()
23
    {
24
        $document = $this->objFromFixture('DMSDocument', 'document_with_relations');
25
        $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...
26
        $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...
27
            array('test-file-file-doesnt-exist-1', 'test-file-file-doesnt-exist-2'),
28
            $document->getRelatedDocuments()->column('Filename')
29
        );
30
    }
31
32
    /**
33
     * Test the extensibility of getRelatedDocuments
34
     */
35 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...
36
    {
37
        DMSDocument::add_extension('StubRelatedDocumentExtension');
38
39
        $emptyDocument = new DMSDocument;
40
        $relatedDocuments = $emptyDocument->getRelatedDocuments();
41
42
        $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...
43
        $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...
44
    }
45
46
    /**
47
     * Ensure that the DMS Document CMS actions contains a grid field for managing related documents
48
     */
49
    public function testDocumentHasCmsFieldForManagingRelatedDocuments()
50
    {
51
        $document = $this->objFromFixture('DMSDocument', 'document_with_relations');
52
        $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...
53
        $gridFieldConfig = $gridField->getConfig();
54
55
        $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...
56
            'GridFieldAddExistingAutocompleter',
57
            $addExisting = $gridFieldConfig->getComponentByType('GridFieldAddExistingAutocompleter'),
58
            'Related documents GridField has an "add existing" autocompleter'
59
        );
60
61
        $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...
62
            $gridFieldConfig->getComponentByType('GridFieldAddNewButton'),
63
            'Related documents GridField does not have an "add new" button'
64
        );
65
    }
66
67
    /**
68
     * Ensure that the related documents list does not include the current document itself
69
     */
70
    public function testGetRelatedDocumentsForAutocompleter()
71
    {
72
        $document = $this->objFromFixture('DMSDocument', 'd1');
73
        $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...
74
75
        $config = $gridField->getConfig();
76
77
        $autocompleter = $config->getComponentByType('GridFieldAddExistingAutocompleter');
78
        $autocompleter->setResultsFormat('$Filename');
79
80
        $jsonResult = $autocompleter->doSearch(
81
            $gridField,
82
            new SS_HTTPRequest('GET', '/', array('gridfield_relationsearch' => 'test'))
83
        );
84
85
        $this->assertNotContains('test-file-file-doesnt-exist-1', $jsonResult);
86
        $this->assertContains('test-file-file-doesnt-exist-2', $jsonResult);
87
        $this->assertEquals(array('Title:PartialMatch', 'Filename:PartialMatch'), $autocompleter->getSearchFields());
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...
88
    }
89
90
    /**
91
     * @return GridField
92
     */
93
    protected function getGridFieldFromDocument(DMSDocument $document)
94
    {
95
        $documentFields = $document->getCMSFields();
96
        /** @var FieldGroup $actions */
97
        $actions = $documentFields->fieldByName('ActionsPanel');
98
99
        $gridField = null;
100
        foreach ($actions->getChildren() as $child) {
101
            /** @var FieldGroup $child */
102
            if ($gridField = $child->fieldByName('RelatedDocuments')) {
103
                break;
104
            }
105
        }
106
        $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...
107
        return $gridField;
108
    }
109
110
    /**
111
     * Ensure that HTML is returned containing list items with action panel steps
112
     */
113
    public function testGetActionTaskHtml()
114
    {
115
        $document = $this->objFromFixture('DMSDocument', 'd1');
116
        $document->addActionPanelTask('example', 'Example');
117
118
        $result = $document->getActionTaskHtml();
119
120
        $this->assertContains('<label class="left">Actions</label>', $result);
121
        $this->assertContains('<li class="ss-ui-button dmsdocument-action" data-panel="', $result);
122
        $this->assertContains('permission', $result);
123
        $this->assertContains('Example', $result);
124
    }
125
126
    /*
127
     * Tests whether the permissions fields are added
128
     */
129
    public function testGetPermissionsActionPanel()
130
    {
131
        $result = $this->objFromFixture('DMSDocument', 'd1')->getPermissionsActionPanel();
132
133
        $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...
134
        $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...
135
        $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...
136
    }
137
138
    /**
139
     * Test view permissions
140
     */
141
    public function testCanView()
142
    {
143
        /** @var DMSDocument $document */
144
        $document = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
145
        // Make sure user is logged out
146
        if ($member = Member::currentUser()) {
147
            $member->logOut();
148
        }
149
150
        // Logged out user test
151
        $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...
152
153
        // Logged in user test
154
        $adminID = $this->logInWithPermission();
155
        $admin = Member::get()->byID($adminID);
156
        $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...
157
        /** @var Member $member */
158
        $admin->logout();
159
160
        // Check anyone
161
        $document = $this->objFromFixture('DMSDocument', 'doc-anyone');
162
        $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...
163
164
        // Check OnlyTheseUsers
165
        $document = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
166
        $reportAdminID = $this->logInWithPermission('cable-guy');
167
        /** @var Member $reportAdmin */
168
        $reportAdmin = Member::get()->byID($reportAdminID);
169
        $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...
170
        // Add reportAdmin to group
171
        $reportAdmin->addToGroupByCode('content-author');
172
        $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...
173
        $reportAdmin->logout();
174
    }
175
176
    /**
177
     * Tests edit permissions
178
     */
179
    public function testCanEdit()
180
    {
181
        // Make sure user is logged out
182
        if ($member = Member::currentUser()) {
183
            $member->logOut();
184
        }
185
186
        /** @var DMSDocument $document1 */
187
        $document1 = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
188
189
        // Logged out user test
190
        $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...
191
192
        //Logged in user test
193
        $contentAuthor = $this->objFromFixture('Member', 'editor');
194
        $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...
195
196
        // Check OnlyTheseUsers
197
        /** @var DMSDocument $document2 */
198
        $document2 = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
199
        /** @var Member $cableGuy */
200
        $cableGuy = $this->objFromFixture('Member', 'non-editor');
201
        $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...
202
203
        $cableGuy->addToGroupByCode('content-author');
204
        $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...
205
    }
206
207
    /**
208
     * Test permission denied reasons for documents
209
     */
210
    public function testGetPermissionDeniedReason()
211
    {
212
        /** @var DMSDocument $document1 */
213
        $doc1 = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
214
        $this->assertContains('Please log in to view this document', $doc1->getPermissionDeniedReason());
215
216
        /** @var DMSDocument $doc2 */
217
        $doc2 = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
218
        $this->assertContains('You are not authorised to view this document', $doc2->getPermissionDeniedReason());
219
220
        /** @var DMSDocument $doc3 */
221
        $doc3 = $this->objFromFixture('DMSDocument', 'doc-anyone');
222
        $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...
223
    }
224
225
    /**
226
     * Ensure that all pages that a document belongs to (via many document sets) can be retrieved in one list
227
     */
228
    public function testGetRelatedPages()
229
    {
230
        $document = $this->objFromFixture('DMSDocument', 'd1');
231
        $result = $document->getRelatedPages();
232
        $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...
233
        $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...
234
    }
235
236
    /**
237
     * Test that the title is returned if it is set, otherwise the filename without ID
238
     */
239
    public function testGetTitleOrFilenameWithoutId()
240
    {
241
        $d1 = $this->objFromFixture('DMSDocument', 'd1');
242
        $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...
243
244
        $d2 = $this->objFromFixture('DMSDocument', 'd2');
245
        $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...
246
    }
247
248
    /**
249
     * Ensure that the folder a document's file is stored in can be retrieved, and that delete() will also delete
250
     * the file and the record
251
     */
252
    public function testGetStorageFolderThenDelete()
253
    {
254
        Config::inst()->update('DMS', 'folder_name', 'assets/_unit-tests');
255
256
        $document = DMS::inst()->storeDocument('dms/tests/DMS-test-lorum-file.pdf');
257
        $filename = $document->getStorageFolder() . '/' . $document->getFileName();
258
259
        $this->assertTrue(file_exists($filename));
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...
260
        $document->delete();
261
        $this->assertFalse($document->exists());
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...
262
        $this->assertFalse(file_exists($filename));
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...
263
264
        DMSFilesystemTestHelper::delete('assets/_unit-tests');
265
    }
266
267
    /**
268
     * Test that the link contains an ID and URL slug
269
     */
270
    public function testGetLink()
271
    {
272
        Config::inst()->update('DMS', 'folder_name', 'assets/_unit-tests');
273
274
        $document = DMS::inst()->storeDocument('dms/tests/DMS-test-lorum-file.pdf');
275
276
        $expected = '/dmsdocument/' . $document->ID . '-dms-test-lorum-file-pdf';
277
        $this->assertSame($expected, $document->Link());
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...
278
        $this->assertSame($expected, $document->getLink());
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...
279
    }
280
281
    /**
282
     * Ensure that the description can be returned in HTML format
283
     */
284
    public function testGetDescriptionWithLineBreak()
285
    {
286
        $document = DMSDocument::create();
287
        $document->Description = "Line 1\nLine 2\nLine 3";
288
        $document->write();
289
290
        $this->assertSame("Line 1<br />\nLine 2<br />\nLine 3", $document->getDescriptionWithLineBreak());
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...
291
    }
292
}
293