Completed
Push — master ( b4e5c2...6db701 )
by Daniel
05:37 queued 03:12
created

tests/DMSDocumentTest.php (2 issues)

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
class DMSDocumentTest extends SapphireTest
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());
12
13
14
        Config::inst()->update('DMSDocument', 'default_download_behaviour', 'download');
15
        $cmsFields = $document->getCMSFields();
16
        $this->assertEquals('download', $cmsFields->dataFieldByName('DownloadBehavior')->Value());
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());
26
        $this->assertEquals(
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()
36
    {
37
        DMSDocument::add_extension('StubRelatedDocumentExtension');
38
39
        $emptyDocument = new DMSDocument;
40
        $relatedDocuments = $emptyDocument->getRelatedDocuments();
41
42
        $this->assertCount(1, $relatedDocuments);
43
        $this->assertSame('Extended', $relatedDocuments->first()->Filename);
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
$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(
56
            'GridFieldAddExistingAutocompleter',
57
            $addExisting = $gridFieldConfig->getComponentByType('GridFieldAddExistingAutocompleter'),
58
            'Related documents GridField has an "add existing" autocompleter'
59
        );
60
61
        $this->assertNull(
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
$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());
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);
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);
134
        $this->assertNotNull($result->getChildren()->fieldByName('CanViewType'));
135
        $this->assertNotNull($result->getChildren()->fieldByName('ViewerGroups'));
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());
152
153
        // Logged in user test
154
        $adminID = $this->logInWithPermission();
155
        $admin = Member::get()->byID($adminID);
156
        $this->assertTrue($document->canView($admin));
157
        /** @var Member $member */
158
        $admin->logout();
159
160
        // Check anyone
161
        $document = $this->objFromFixture('DMSDocument', 'doc-anyone');
162
        $this->assertTrue($document->canView());
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));
170
        // Add reportAdmin to group
171
        $reportAdmin->addToGroupByCode('content-author');
172
        $this->assertTrue($document->canView($reportAdmin));
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());
191
192
        //Logged in user test
193
        $contentAuthor = $this->objFromFixture('Member', 'editor');
194
        $this->assertTrue($document1->canEdit($contentAuthor));
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));
202
203
        $cableGuy->addToGroupByCode('content-author');
204
        $this->assertTrue($document2->canEdit($cableGuy));
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());
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');
233
        $this->assertSame(array('s1', 's2', 's3'), $result->column('URLSegment'));
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());
243
244
        $d2 = $this->objFromFixture('DMSDocument', 'd2');
245
        $this->assertSame('File That Doesn\'t Exist (Title)', $d2->getTitle());
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));
260
        $document->delete();
261
        $this->assertFalse($document->exists());
262
        $this->assertFalse(file_exists($filename));
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());
278
        $this->assertSame($expected, $document->getLink());
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());
291
    }
292
}
293