Completed
Pull Request — master (#110)
by Franco
02:08
created

tests/DMSDocumentTest.php (1 issue)

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
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()
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 testPageRelations()
23
    {
24
        $s1 = $this->objFromFixture('SiteTree', 's1');
25
        $s2 = $this->objFromFixture('SiteTree', 's2');
26
        $s3 = $this->objFromFixture('SiteTree', 's3');
27
        $s4 = $this->objFromFixture('SiteTree', 's4');
28
        $s5 = $this->objFromFixture('SiteTree', 's5');
29
        $s6 = $this->objFromFixture('SiteTree', 's6');
30
31
        $d1 = $this->objFromFixture('DMSDocument', 'd1');
32
33
        $pages = $d1->Pages();
34
        $pagesArray = $pages->toArray();
35
        $this->assertEquals($pagesArray[0]->ID, $s1->ID, 'Page 1 associated correctly');
36
        $this->assertEquals($pagesArray[1]->ID, $s2->ID, 'Page 2 associated correctly');
37
        $this->assertEquals($pagesArray[2]->ID, $s3->ID, 'Page 3 associated correctly');
38
        $this->assertEquals($pagesArray[3]->ID, $s4->ID, 'Page 4 associated correctly');
39
        $this->assertEquals($pagesArray[4]->ID, $s5->ID, 'Page 5 associated correctly');
40
        $this->assertEquals($pagesArray[5]->ID, $s6->ID, 'Page 6 associated correctly');
41
    }
42
43
    public function testAddPageRelation()
44
    {
45
        $s1 = $this->objFromFixture('SiteTree', 's1');
46
        $s2 = $this->objFromFixture('SiteTree', 's2');
47
        $s3 = $this->objFromFixture('SiteTree', 's3');
48
49
        $doc = new DMSDocument();
50
        $doc->Filename = 'test file';
51
        $doc->Folder = '0';
52
        $doc->write();
53
54
        $doc->addPage($s1);
55
        $doc->addPage($s2);
56
        $doc->addPage($s3);
57
58
        $pages = $doc->Pages();
59
        $pagesArray = $pages->toArray();
60
        $this->assertEquals($pagesArray[0]->ID, $s1->ID, 'Page 1 associated correctly');
61
        $this->assertEquals($pagesArray[1]->ID, $s2->ID, 'Page 2 associated correctly');
62
        $this->assertEquals($pagesArray[2]->ID, $s3->ID, 'Page 3 associated correctly');
63
64
        $doc->removePage($s1);
65
        $pages = $doc->Pages();
66
        $pagesArray = $pages->toArray();    // Page 1 is missing
67
        $this->assertEquals($pagesArray[0]->ID, $s2->ID, 'Page 2 still associated correctly');
68
        $this->assertEquals($pagesArray[1]->ID, $s3->ID, 'Page 3 still associated correctly');
69
70
        $documents = $s2->Documents();
71
        $documentsArray = $documents->toArray();
72
        $this->assertDOSContains(
73
            array(
74
                array('Filename' => $doc->Filename)
75
            ),
76
            $documentsArray,
77
            'Document associated with page'
78
        );
79
80
        $doc->removeAllPages();
81
        $pages = $doc->Pages();
82
        $this->assertEquals($pages->Count(), 0, 'All pages removed');
83
84
        $documents = $s2->Documents();
85
        $documentsArray = $documents->toArray();
86
        $this->assertNotContains($doc, $documentsArray, 'Document no longer associated with page');
87
    }
88
89
    public function testDeletingPageWithAssociatedDocuments()
90
    {
91
        $s1 = $this->objFromFixture('SiteTree', 's1');
92
        $s2 = $this->objFromFixture('SiteTree', 's2');
93
        $s2->publish('Stage', 'Live');
94
        $s2ID = $s2->ID;
95
96
        $doc = new DMSDocument();
97
        $doc->Filename = 'delete test file';
98
        $doc->Folder = '0';
99
        $doc->write();
100
101
        $doc->addPage($s1);
102
        $doc->addPage($s2);
103
104
        $s1->delete();
105
106
        $documents = DataObject::get("DMSDocument", "\"Filename\" = 'delete test file'", false);
107
        $this->assertEquals(
108
            $documents->Count(),
109
            '1',
110
            "Deleting one of the associated page doesn't affect the single document we created"
111
        );
112
113
        $s2->delete();
114
        $documents = DataObject::get("DMSDocument", "\"Filename\" = 'delete test file'");
115
        $this->assertEquals(
116
            $documents->Count(),
117
            '1',
118
            "Deleting a page from draft stage doesn't delete the associated docs,"
119
            . "even if it's the last page they're associated with"
120
        );
121
122
        $s2 = Versioned::get_one_by_stage('SiteTree', 'Live', sprintf('"SiteTree"."ID" = %d', $s2ID));
123
        $s2->doDeleteFromLive();
124
        $documents = DataObject::get("DMSDocument", "\"Filename\" = 'delete test file'");
125
        $this->assertEquals(
126
            $documents->Count(),
127
            '0',
128
            'However, deleting the live version of the last page that a document is '
129
            . 'associated with causes that document to be deleted as well'
130
        );
131
    }
132
133
    public function testUnpublishPageWithAssociatedDocuments()
134
    {
135
        $s2 = $this->objFromFixture('SiteTree', 's2');
136
        $s2->publish('Stage', 'Live');
137
        $s2ID = $s2->ID;
138
139
        $doc = new DMSDocument();
140
        $doc->Filename = 'delete test file';
141
        $doc->Folder = '0';
142
        $doc->write();
143
144
        $doc->addPage($s2);
145
146
        $s2->doDeleteFromLive();
147
        $documents = DataObject::get("DMSDocument", "\"Filename\" = 'delete test file'");
148
        $this->assertEquals(
149
            $documents->Count(),
150
            '1',
151
            "Deleting a page from live stage doesn't delete the associated docs,"
152
            . "even if it's the last page they're associated with"
153
        );
154
155
        $s2 = Versioned::get_one_by_stage('SiteTree', 'Stage', sprintf('"SiteTree"."ID" = %d', $s2ID));
156
        $s2->delete();
157
        $documents = DataObject::get("DMSDocument", "\"Filename\" = 'delete test file'");
158
        $this->assertEquals(
159
            $documents->Count(),
160
            '0',
161
            'However, deleting the draft version of the last page that a document is '
162
            . 'associated with causes that document to be deleted as well'
163
        );
164
    }
165
166
    public function testDefaultDownloadBehabiourCMSFields()
167
    {
168
        $document = singleton('DMSDocument');
169
        Config::inst()->update('DMSDocument', 'default_download_behaviour', 'open');
170
        $cmsFields = $document->getCMSFields();
171
        $this->assertEquals('open', $cmsFields->dataFieldByName('DownloadBehavior')->Value());
172
173
174
        Config::inst()->update('DMSDocument', 'default_download_behaviour', 'download');
175
        $cmsFields = $document->getCMSFields();
176
        $this->assertEquals('download', $cmsFields->dataFieldByName('DownloadBehavior')->Value());
177
    }
178
179
    /**
180
     * Ensure that related documents can be retrieved for a given DMS document
181
     */
182
    public function testRelatedDocuments()
183
    {
184
        $document = $this->objFromFixture('DMSDocument', 'document_with_relations');
185
        $this->assertGreaterThan(0, $document->RelatedDocuments()->count());
186
        $this->assertEquals(
187
            array('test-file-file-doesnt-exist-1', 'test-file-file-doesnt-exist-2'),
188
            $document->getRelatedDocuments()->column('Filename')
189
        );
190
    }
191
192
    /**
193
     * Test the extensibility of getRelatedDocuments
194
     */
195
    public function testGetRelatedDocumentsIsExtensible()
196
    {
197
        DMSDocument::add_extension('StubRelatedDocumentExtension');
198
199
        $emptyDocument = new DMSDocument;
200
        $relatedDocuments = $emptyDocument->getRelatedDocuments();
201
202
        $this->assertCount(1, $relatedDocuments);
203
        $this->assertSame('Extended', $relatedDocuments->first()->Filename);
204
    }
205
206
    /**
207
     * Ensure that the DMS Document CMS actions contains a grid field for managing related documents
208
     */
209
    public function testDocumentHasCmsFieldForManagingRelatedDocuments()
210
    {
211
        $document = $this->objFromFixture('DMSDocument', 'document_with_relations');
212
213
        $documentFields = $document->getCMSFields();
214
        /** @var FieldGroup $actions */
215
        $actions = $documentFields->fieldByName('ActionsPanel');
216
217
        $gridField = null;
218
        foreach ($actions->getChildren() as $child) {
219
            /** @var FieldGroup $child */
220
            if ($gridField = $child->fieldByName('RelatedDocuments')) {
221
                break;
222
            }
223
        }
224
        $this->assertInstanceOf('GridField', $gridField);
225
226
        /** @var GridFieldConfig $gridFieldConfig */
227
        $gridFieldConfig = $gridField->getConfig();
228
229
        $this->assertNotNull(
230
            'GridFieldAddExistingAutocompleter',
231
            $addExisting = $gridFieldConfig->getComponentByType('GridFieldAddExistingAutocompleter'),
232
            'Related documents GridField has an "add existing" autocompleter'
233
        );
234
235
        $this->assertNull(
236
            $gridFieldConfig->getComponentByType('GridFieldAddNewButton'),
237
            'Related documents GridField does not have an "add new" button'
238
        );
239
    }
240
241
    /*
242
     * Tests whether the permissions fields are added
243
     */
244
    public function testAddPermissionsFields()
245
    {
246
        $document = $this->objFromFixture('DMSDocument', 'd1');
247
        $fields = $document->getCMSFields();
248
249
        $this->assertNotNull($fields->fieldByName('CanViewType'));
250
        $this->assertNotNull($fields->fieldByName('ViewerGroups'));
251
    }
252
253
    /**
254
     * Test view permissions
255
     */
256
    public function testCanView()
257
    {
258
        /** @var DMSDocument $document */
259
        $document = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
260
        // Make sure user is logged out
261
        if ($member = Member::currentUser()) {
262
            $member->logOut();
263
        }
264
265
        // Logged out user test
266
        $this->assertFalse($document->canView());
267
268
        // Logged in user test
269
        $adminID = $this->logInWithPermission();
270
        $admin = Member::get()->byID($adminID);
271
        $this->assertTrue($document->canView($admin));
272
        /** @var Member $member */
273
        $admin->logout();
274
275
        // Check anyone
276
        $document = $this->objFromFixture('DMSDocument', 'doc-anyone');
277
        $this->assertTrue($document->canView());
278
279
        // Check OnlyTheseUsers
280
        $document = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
281
        $reportAdminID = $this->logInWithPermission('cable-guy');
282
        /** @var Member $reportAdmin */
283
        $reportAdmin = Member::get()->byID($reportAdminID);
284
        $this->assertFalse($document->canView($reportAdmin));
285
        // Add reportAdmin to group
286
        $reportAdmin->addToGroupByCode('content-author');
287
        $this->assertTrue($document->canView($reportAdmin));
288
        $reportAdmin->logout();
289
    }
290
291
    /**
292
     * Tests edit permissions
293
     */
294
    public function testCanEdit()
295
    {
296
        // Make sure user is logged out
297
        if ($member = Member::currentUser()) {
298
            $member->logOut();
299
        }
300
301
        /** @var DMSDocument $document1 */
302
        $document1 = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
303
304
        // Logged out user test
305
        $this->assertFalse($document1->canEdit());
306
307
        //Logged in user test
308
        $contentAuthor = $this->objFromFixture('Member', 'editor');
309
        $this->assertTrue($document1->canEdit($contentAuthor));
310
311
        // Check OnlyTheseUsers
312
        /** @var DMSDocument $document2 */
313
        $document2 = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
314
        /** @var Member $cableGuy */
315
        $cableGuy = $this->objFromFixture('Member', 'non-editor');
316
        $this->assertFalse($document2->canEdit($cableGuy));
317
318
        $cableGuy->addToGroupByCode('content-author');
319
        $this->assertTrue($document2->canEdit($cableGuy));
320
    }
321
322
    /**
323
     * Test permission denied reasons for documents
324
     */
325
    public function testGetPermissionDeniedReason()
326
    {
327
        /** @var DMSDocument $document1 */
328
        $doc1 = $this->objFromFixture('DMSDocument', 'doc-logged-in-users');
329
        $this->assertContains('Please log in to view this document', $doc1->getPermissionDeniedReason());
330
331
        /** @var DMSDocument $doc2 */
332
        $doc2 = $this->objFromFixture('DMSDocument', 'doc-only-these-users');
333
        $this->assertContains('You are not authorised to view this document', $doc2->getPermissionDeniedReason());
334
335
        /** @var DMSDocument $doc3 */
336
        $doc3 = $this->objFromFixture('DMSDocument', 'doc-anyone');
337
        $this->assertEquals('', $doc3->getPermissionDeniedReason());
338
    }
339
}
340