|
1
|
|
|
<?php |
|
2
|
|
|
class DMSTest extends FunctionalTest |
|
|
|
|
|
|
3
|
|
|
{ |
|
4
|
|
|
protected static $fixture_file = 'dmstest.yml'; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Stub PDF files for testing |
|
8
|
|
|
* @var string |
|
9
|
|
|
*/ |
|
10
|
|
|
public static $testFile = 'dms/tests/DMS-test-lorum-file.pdf'; |
|
11
|
|
|
public static $testFile2 = 'dms/tests/DMS-test-document-2.pdf'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Store values to reset back to after this test runs |
|
15
|
|
|
*/ |
|
16
|
|
|
public static $dmsFolderOld; |
|
17
|
|
|
public static $dmsFolderSizeOld; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var DMS |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $dms; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp() |
|
25
|
|
|
{ |
|
26
|
|
|
parent::setUp(); |
|
27
|
|
|
|
|
28
|
|
|
self::$dmsFolderOld = DMS::$dmsFolder; |
|
29
|
|
|
self::$dmsFolderSizeOld = DMS::$dmsFolderSize; |
|
30
|
|
|
|
|
31
|
|
|
//use a test DMS folder, so we don't overwrite the live one |
|
32
|
|
|
DMS::$dmsFolder = 'dms-assets-test-1234'; |
|
33
|
|
|
|
|
34
|
|
|
//clear out the test folder (in case a broken test doesn't delete it) |
|
35
|
|
|
$this->delete(BASE_PATH . DIRECTORY_SEPARATOR . 'dms-assets-test-1234'); |
|
36
|
|
|
|
|
37
|
|
|
$this->dms = DMS::inst(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
View Code Duplication |
public function tearDown() |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
parent::tearDown(); |
|
43
|
|
|
|
|
44
|
|
|
self::$is_running_test = true; |
|
45
|
|
|
|
|
46
|
|
|
$d = DataObject::get("DMSDocument"); |
|
47
|
|
|
foreach ($d as $d1) { |
|
48
|
|
|
$d1->delete(); |
|
49
|
|
|
} |
|
50
|
|
|
$t = DataObject::get("DMSTag"); |
|
51
|
|
|
foreach ($t as $t1) { |
|
52
|
|
|
$t1->delete(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
//delete the test folder after the test runs |
|
56
|
|
|
$this->delete(BASE_PATH . DIRECTORY_SEPARATOR . 'dms-assets-test-1234'); |
|
57
|
|
|
|
|
58
|
|
|
//set the old DMS folder back again |
|
59
|
|
|
DMS::$dmsFolder = self::$dmsFolderOld; |
|
60
|
|
|
DMS::$dmsFolderSize = self::$dmsFolderSizeOld; |
|
61
|
|
|
|
|
62
|
|
|
self::$is_running_test = $this->originalIsRunningTest; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Delete a file that was created during a unit test |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $path |
|
69
|
|
|
*/ |
|
70
|
|
View Code Duplication |
public function delete($path) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
if (file_exists($path) || is_dir($path)) { |
|
73
|
|
|
$it = new RecursiveIteratorIterator( |
|
74
|
|
|
new RecursiveDirectoryIterator($path), |
|
75
|
|
|
RecursiveIteratorIterator::CHILD_FIRST |
|
76
|
|
|
); |
|
77
|
|
|
foreach ($it as $file) { |
|
78
|
|
|
if (in_array($file->getBasename(), array('.', '..'))) { |
|
79
|
|
|
continue; |
|
80
|
|
|
} elseif ($file->isDir()) { |
|
81
|
|
|
rmdir($file->getPathname()); |
|
82
|
|
|
} elseif ($file->isFile() || $file->isLink()) { |
|
83
|
|
|
unlink($file->getPathname()); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
rmdir($path); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testDMSStorage() |
|
91
|
|
|
{ |
|
92
|
|
|
$file = self::$testFile; |
|
93
|
|
|
$document = $this->dms->storeDocument($file); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertNotNull($document, "Document object created"); |
|
|
|
|
|
|
96
|
|
|
$this->assertTrue( |
|
|
|
|
|
|
97
|
|
|
file_exists( |
|
98
|
|
|
DMS::get_dms_path() . DIRECTORY_SEPARATOR . $document->Folder |
|
99
|
|
|
. DIRECTORY_SEPARATOR . $document->Filename |
|
100
|
|
|
), |
|
101
|
|
|
"Document file copied into DMS folder" |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testDMSFolderSpanning() |
|
106
|
|
|
{ |
|
107
|
|
|
DMS::$dmsFolderSize = 5; |
|
108
|
|
|
$file = self::$testFile; |
|
109
|
|
|
|
|
110
|
|
|
$documents = array(); |
|
111
|
|
|
for ($i = 0; $i <= 16; $i++) { |
|
112
|
|
|
$document = $this->dms->storeDocument($file); |
|
113
|
|
|
$this->assertNotNull($document, "Document object created on run number: $i"); |
|
|
|
|
|
|
114
|
|
|
$this->assertTrue(file_exists($document->getFullPath())); |
|
|
|
|
|
|
115
|
|
|
$documents[] = $document; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Test document objects have their folders set |
|
119
|
|
|
$folders = array(); |
|
120
|
|
|
for ($i = 0; $i <= 16; $i++) { |
|
121
|
|
|
$folderName = $documents[$i]->Folder; |
|
122
|
|
|
$this->assertTrue( |
|
|
|
|
|
|
123
|
|
|
strpos($documents[$i]->getFullPath(), DIRECTORY_SEPARATOR . $folderName . DIRECTORY_SEPARATOR) !== false, |
|
124
|
|
|
"Correct folder name for the documents. Document path contains reference to folder name '$folderName'" |
|
125
|
|
|
); |
|
126
|
|
|
$folders[] = $folderName; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// Test we created 4 folder to contain the 17 files |
|
130
|
|
|
foreach ($folders as $f) { |
|
131
|
|
|
$this->assertTrue(is_dir(DMS::get_dms_path() . DIRECTORY_SEPARATOR . $f), "Document folder '$f' exists"); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testReplaceDocument() |
|
136
|
|
|
{ |
|
137
|
|
|
// Store the first document |
|
138
|
|
|
$document = $this->dms->storeDocument(self::$testFile); |
|
139
|
|
|
$document->Title = "My custom title"; |
|
140
|
|
|
$document->Description = "My custom description"; |
|
141
|
|
|
$document->write(); |
|
142
|
|
|
|
|
143
|
|
|
// Then overwrite with a second document |
|
144
|
|
|
$document = $document->replaceDocument(self::$testFile2); |
|
145
|
|
|
|
|
146
|
|
|
$this->assertNotNull($document, "Document object created"); |
|
|
|
|
|
|
147
|
|
|
$this->assertTrue( |
|
|
|
|
|
|
148
|
|
|
file_exists( |
|
149
|
|
|
DMS::get_dms_path() . DIRECTORY_SEPARATOR . $document->Folder |
|
150
|
|
|
. DIRECTORY_SEPARATOR . $document->Filename |
|
151
|
|
|
), |
|
152
|
|
|
"Document file copied into DMS folder" |
|
153
|
|
|
); |
|
154
|
|
|
$this->assertContains( |
|
155
|
|
|
"DMS-test-document-2", |
|
156
|
|
|
$document->Filename, |
|
157
|
|
|
"Original document filename is contain in the new filename" |
|
158
|
|
|
); |
|
159
|
|
|
$this->assertEquals("My custom title", $document->Title, "Custom title not modified"); |
|
|
|
|
|
|
160
|
|
|
$this->assertEquals("My custom description", $document->Description, "Custom description not modified"); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Test that documents can be returned by a given page |
|
165
|
|
|
*/ |
|
166
|
|
View Code Duplication |
public function testGetByPageWithoutEmbargoes() |
|
|
|
|
|
|
167
|
|
|
{ |
|
168
|
|
|
$pageWithEmbargoes = $this->objFromFixture('SiteTree', 's3'); |
|
169
|
|
|
$documents = $this->dms->getByPage($pageWithEmbargoes); |
|
|
|
|
|
|
170
|
|
|
// Fixture: 6 documents in set, 1 is embargoed |
|
171
|
|
|
$this->assertCount(5, $documents, 'Embargoed documents are excluded by default'); |
|
|
|
|
|
|
172
|
|
|
$this->assertContainsOnlyInstancesOf('DMSDocument', $documents); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Test that embargoed documents are excluded from getByPage |
|
177
|
|
|
*/ |
|
178
|
|
View Code Duplication |
public function testGetByPageWithEmbargoedDocuments() |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
$pageWithEmbargoes = $this->objFromFixture('SiteTree', 's3'); |
|
181
|
|
|
$documents = $this->dms->getByPage($pageWithEmbargoes, true); |
|
|
|
|
|
|
182
|
|
|
// Fixture: 6 documents in set, 1 is embargoed |
|
183
|
|
|
$this->assertCount(6, $documents, 'Embargoed documents can be included'); |
|
|
|
|
|
|
184
|
|
|
$this->assertContainsOnlyInstancesOf('DMSDocument', $documents); |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Ensure the shortcode handler key is configurable |
|
189
|
|
|
*/ |
|
190
|
|
|
public function testShortcodeHandlerKeyIsConfigurable() |
|
191
|
|
|
{ |
|
192
|
|
|
Config::inst()->update('DMS', 'shortcode_handler_key', 'testing'); |
|
193
|
|
|
$this->assertSame('testing', DMS::inst()->getShortcodeHandlerKey()); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Test that document sets can be retrieved for a given page |
|
198
|
|
|
*/ |
|
199
|
|
|
public function testGetDocumentSetsByPage() |
|
200
|
|
|
{ |
|
201
|
|
|
$page = $this->objFromFixture('SiteTree', 's1'); |
|
202
|
|
|
$sets = $this->dms->getDocumentSetsByPage($page); |
|
|
|
|
|
|
203
|
|
|
$this->assertCount(2, $sets); |
|
|
|
|
|
|
204
|
|
|
$this->assertContainsOnlyInstancesOf('DMSDocumentSet', $sets); |
|
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
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.