1
|
|
|
<?php |
2
|
|
|
class DMSDocumentTest extends SapphireTest |
|
|
|
|
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 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()); |
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
Config::inst()->update('DMSDocument', 'default_download_behaviour', 'download'); |
31
|
|
|
$cmsFields = $document->getCMSFields(); |
32
|
|
|
$this->assertEquals('download', $cmsFields->dataFieldByName('DownloadBehavior')->Value()); |
|
|
|
|
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()); |
|
|
|
|
42
|
|
|
$this->assertEquals( |
|
|
|
|
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() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
DMSDocument::add_extension('StubRelatedDocumentExtension'); |
54
|
|
|
|
55
|
|
|
$emptyDocument = new DMSDocument; |
56
|
|
|
$relatedDocuments = $emptyDocument->getRelatedDocuments(); |
57
|
|
|
|
58
|
|
|
$this->assertCount(1, $relatedDocuments); |
|
|
|
|
59
|
|
|
$this->assertSame('Extended', $relatedDocuments->first()->Filename); |
|
|
|
|
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); |
|
|
|
|
69
|
|
|
$gridFieldConfig = $gridField->getConfig(); |
70
|
|
|
|
71
|
|
|
$this->assertNotNull( |
|
|
|
|
72
|
|
|
'GridFieldAddExistingAutocompleter', |
73
|
|
|
$addExisting = $gridFieldConfig->getComponentByType('GridFieldAddExistingAutocompleter'), |
74
|
|
|
'Related documents GridField has an "add existing" autocompleter' |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->assertNull( |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
149
|
|
|
$this->assertNotNull($result->getChildren()->fieldByName('CanViewType')); |
|
|
|
|
150
|
|
|
$this->assertNotNull($result->getChildren()->fieldByName('ViewerGroups')); |
|
|
|
|
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()); |
|
|
|
|
167
|
|
|
|
168
|
|
|
// Logged in user test |
169
|
|
|
$adminID = $this->logInWithPermission(); |
170
|
|
|
$admin = Member::get()->byID($adminID); |
171
|
|
|
$this->assertTrue($document->canView($admin)); |
|
|
|
|
172
|
|
|
/** @var Member $member */ |
173
|
|
|
$admin->logout(); |
174
|
|
|
|
175
|
|
|
// Check anyone |
176
|
|
|
$document = $this->objFromFixture('DMSDocument', 'doc-anyone'); |
177
|
|
|
$this->assertTrue($document->canView()); |
|
|
|
|
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)); |
|
|
|
|
185
|
|
|
// Add reportAdmin to group |
186
|
|
|
$reportAdmin->addToGroupByCode('content-author'); |
187
|
|
|
$this->assertTrue($document->canView($reportAdmin)); |
|
|
|
|
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()); |
|
|
|
|
206
|
|
|
|
207
|
|
|
//Logged in user test |
208
|
|
|
$contentAuthor = $this->objFromFixture('Member', 'editor'); |
209
|
|
|
$this->assertTrue($document1->canEdit($contentAuthor)); |
|
|
|
|
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)); |
|
|
|
|
217
|
|
|
|
218
|
|
|
$cableGuy->addToGroupByCode('content-author'); |
219
|
|
|
$this->assertTrue($document2->canEdit($cableGuy)); |
|
|
|
|
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()); |
|
|
|
|
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'); |
|
|
|
|
248
|
|
|
$this->assertSame(array('s1', 's2', 's3'), $result->column('URLSegment')); |
|
|
|
|
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()); |
|
|
|
|
258
|
|
|
|
259
|
|
|
$d2 = $this->objFromFixture('DMSDocument', 'd2'); |
260
|
|
|
$this->assertSame('File That Doesn\'t Exist (Title)', $d2->getTitle()); |
|
|
|
|
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.