1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for the file usage in entity fields with the Media filter markup.
|
6
|
*/
|
7
|
|
8
|
class MediaFileUsageTest extends MediaTestHelper {
|
9
|
|
10
|
/**
|
11
|
* Provide test information.
|
12
|
*/
|
13
|
public static function getInfo() {
|
14
|
return array(
|
15
|
'name' => t('File usage tracking'),
|
16
|
'description' => t('Tests tracking of usage for files in text fields.'),
|
17
|
'group' => t('Media'),
|
18
|
);
|
19
|
}
|
20
|
|
21
|
/**
|
22
|
* Enable media and file entity modules for testing.
|
23
|
*/
|
24
|
public function setUp() {
|
25
|
parent::setUp(array('media', 'file_entity'));
|
26
|
|
27
|
// Create and log in a user.
|
28
|
$account = $this->drupalCreateUser(array('administer nodes', 'create article content'));
|
29
|
$this->drupalLogin($account);
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Generates markup to be inserted for a file.
|
34
|
*
|
35
|
* This is a PHP version of InsertMedia.insert() from js/wysiwyg-media.js.
|
36
|
*
|
37
|
* @param int $fid
|
38
|
* Drupal file id
|
39
|
* @param int $count
|
40
|
* Quantity of markup to insert
|
41
|
*
|
42
|
* @return string
|
43
|
* Filter markup.
|
44
|
*/
|
45
|
private function generateFileMarkup($fid, $count = 1) {
|
46
|
$file_usage_markup = '';
|
47
|
|
48
|
// Build the data that is used in a media tag.
|
49
|
$data = array(
|
50
|
'fid' => $fid,
|
51
|
'type' => 'media',
|
52
|
'view_mode' => 'preview',
|
53
|
'attributes' => array(
|
54
|
'height' => 100,
|
55
|
'width' => 100,
|
56
|
'classes' => 'media-element file_preview',
|
57
|
)
|
58
|
);
|
59
|
|
60
|
// Create the file usage markup.
|
61
|
for ($i = 1; $i <= $count; $i++) {
|
62
|
$file_usage_markup .= '<p>[[' . drupal_json_encode($data) . ']]</p>';
|
63
|
}
|
64
|
|
65
|
return $file_usage_markup;
|
66
|
}
|
67
|
|
68
|
|
69
|
/**
|
70
|
* Utility function to create a test node.
|
71
|
*
|
72
|
* @param int $fid
|
73
|
* Create the node with media markup in the body field
|
74
|
*
|
75
|
* @return int
|
76
|
* Returns the node id
|
77
|
*/
|
78
|
private function createNode($fid = FALSE) {
|
79
|
$markup = '';
|
80
|
if (! empty($fid)) {
|
81
|
$markup = $this->generateFileMarkup($fid);
|
82
|
}
|
83
|
|
84
|
// Create an article node with file markup in the body field.
|
85
|
$edit = array(
|
86
|
'title' => $this->randomName(8),
|
87
|
'body[und][0][value]' => $markup,
|
88
|
);
|
89
|
// Save the article node. First argument is the URL, then the value array
|
90
|
// and the third is the label the button that should be "clicked".
|
91
|
$this->drupalPost('node/add/article', $edit, t('Save'));
|
92
|
|
93
|
// Get the article node that was saved by the unique title.
|
94
|
$node = $this->drupalGetNodeByTitle($edit['title']);
|
95
|
return $node->nid;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Tests the tracking of file usages for files submitted via the WYSIWYG editor.
|
100
|
*/
|
101
|
public function testFileUsageIncrementing() {
|
102
|
// Create a file.
|
103
|
$files = $this->drupalGetTestFiles('image');
|
104
|
$file = file_save($files[0]);
|
105
|
$fid = $file->fid;
|
106
|
|
107
|
// There should be zero usages of this file prior to node creation,
|
108
|
$file_uses = file_usage_list($file);
|
109
|
$this->assertEqual(empty($file_uses), TRUE, t('Created a new file with zero uses.'));
|
110
|
|
111
|
// Create a node to test with.
|
112
|
$nid = $this->createNode($fid);
|
113
|
|
114
|
// Get the new file usage count.
|
115
|
$file_uses = file_usage_list($file);
|
116
|
|
117
|
$this->assertEqual($file_uses['media']['node'][$nid], 1, t('File usage increases when added to a new node.'));
|
118
|
|
119
|
// Create a new revision that has the file on it. File usage will be 2.
|
120
|
$node = node_load($nid);
|
121
|
$node->revision = TRUE;
|
122
|
node_save($node);
|
123
|
|
124
|
$node = node_load($nid);
|
125
|
$file_uses = file_usage_list($file);
|
126
|
$revisions = count(node_revision_list($node));
|
127
|
// Keep track of this VID to test deletion later on.
|
128
|
$delete_one = $node->vid;
|
129
|
|
130
|
$this->assertEqual($revisions, 2, t('Node save created a second revision'));
|
131
|
$this->assertEqual($file_uses['media']['node'][$nid], 2, t('File usage incremented with a new node revision.'));
|
132
|
|
133
|
// Create a new revision that has two instances of the file. File usage will
|
134
|
// be 4.
|
135
|
$node = node_load($nid);
|
136
|
$node->body[LANGUAGE_NONE][0]['value'] = $this->generateFileMarkup($fid, 2);
|
137
|
$node->revision = TRUE;
|
138
|
node_save($node);
|
139
|
|
140
|
$node = node_load($nid);
|
141
|
$file_uses = file_usage_list($file);
|
142
|
$revisions = count(node_revision_list($node));
|
143
|
// Keep track of this VID to test deletion later on.
|
144
|
$delete_two = $node->vid;
|
145
|
|
146
|
$this->assertEqual($revisions, 3, t('Node save created a third revision.'));
|
147
|
$this->assertEqual($file_uses['media']['node'][$nid], 4, t('File usage incremented with multiple files and a new node revision.'));
|
148
|
|
149
|
// Create a new revision that has no file on it. File usage will be 4.
|
150
|
$node = node_load($nid);
|
151
|
$node->body[LANGUAGE_NONE][0]['value'] = '';
|
152
|
$node->revision = TRUE;
|
153
|
node_save($node);
|
154
|
|
155
|
$node = node_load($nid);
|
156
|
$file_uses = file_usage_list($file);
|
157
|
$revisions = count(node_revision_list($node));
|
158
|
// Keep track of this VID to test deletion later on.
|
159
|
$delete_zero = $node->vid;
|
160
|
|
161
|
$this->assertEqual($revisions, 4, t('Node save created a fourth revision.'));
|
162
|
$this->assertEqual($file_uses['media']['node'][$nid], 4, t('File usage does not change with a new revision of the node without the file'));
|
163
|
|
164
|
// Create a new revision that has the file on it. File usage will be 5.
|
165
|
$node = node_load($nid);
|
166
|
$node->body[LANGUAGE_NONE][0]['value'] = $this->generateFileMarkup($fid, 1);
|
167
|
$node->revision = TRUE;
|
168
|
node_save($node);
|
169
|
|
170
|
$node = node_load($nid);
|
171
|
$file_uses = file_usage_list($file);
|
172
|
$revisions = count(node_revision_list($node));
|
173
|
|
174
|
$this->assertEqual($revisions, 5, t('Node save created a new revision.'));
|
175
|
$this->assertEqual($file_uses['media']['node'][$nid], 5, t('File usage incremented with a single file on a new node revision.'));
|
176
|
|
177
|
// Delete a revision that has the file on it once. File usage will be 4.
|
178
|
node_revision_delete($delete_one);
|
179
|
$node = node_load($nid);
|
180
|
$file_uses = file_usage_list($file);
|
181
|
$this->assertEqual($file_uses['media']['node'][$nid], 4, t('Deleting revision with file decreases file usage'));
|
182
|
|
183
|
// Delete a revision that has no file on it. File usage will be 4.
|
184
|
node_revision_delete($delete_zero);
|
185
|
$node = node_load($nid);
|
186
|
$file_uses = file_usage_list($file);
|
187
|
$this->assertEqual($file_uses['media']['node'][$nid], 4, t('Deleting revision without a file does not change file usage.'));
|
188
|
|
189
|
// Delete a revision that has the file on it twice. File usage will be 2.
|
190
|
node_revision_delete($delete_two);
|
191
|
$node = node_load($nid);
|
192
|
$file_uses = file_usage_list($file);
|
193
|
$this->assertEqual($file_uses['media']['node'][$nid], 2, t('Deleting revision with file decreases file usage'));
|
194
|
|
195
|
// Create a new revision with the file on it twice. File usage will be 4.
|
196
|
$node = node_load($nid);
|
197
|
$node->body[LANGUAGE_NONE][0]['value'] = $this->generateFileMarkup($fid, 2);
|
198
|
$node->revision = TRUE;
|
199
|
node_save($node);
|
200
|
|
201
|
$node = node_load($nid);
|
202
|
$file_uses = file_usage_list($file);
|
203
|
|
204
|
$this->assertEqual($file_uses['media']['node'][$nid], 4, t('File usage incremented with files on a new node revision.'));
|
205
|
|
206
|
// Re-save current revision with file on it once instead of twice. File
|
207
|
// usage will be 3.
|
208
|
$node = node_load($nid);
|
209
|
$node->body[LANGUAGE_NONE][0]['value'] = $this->generateFileMarkup($fid, 1);
|
210
|
$saved_vid = $node->vid;
|
211
|
node_save($node);
|
212
|
|
213
|
$node = node_load($nid);
|
214
|
$file_uses = file_usage_list($file);
|
215
|
|
216
|
$this->assertEqual($node->vid, $saved_vid, t('Resaved node revision does not create new revision.'));
|
217
|
$this->assertEqual($file_uses['media']['node'][$nid], 3, t('Resaved node revision with fewer files reduces file usage.'));
|
218
|
|
219
|
// Delete the node. File usage will be 0.
|
220
|
$node = node_load($nid);
|
221
|
node_delete($nid);
|
222
|
|
223
|
$node = node_load($nid);
|
224
|
$file_uses = file_usage_list($file);
|
225
|
|
226
|
$this->assertEqual(empty($node), TRUE, t('Node has been deleted.'));
|
227
|
$this->assertEqual(empty($file_uses), TRUE, t('Deleting the node removes all file uses.'));
|
228
|
}
|
229
|
|
230
|
|
231
|
/**
|
232
|
* Tests the behavior of node and file deletion.
|
233
|
*/
|
234
|
public function testFileUsageIncrementingDelete() {
|
235
|
// Create a node with file markup in the body field with a new file.
|
236
|
$files = $this->drupalGetTestFiles('image');
|
237
|
$file = file_save($files[1]);
|
238
|
$fid = $file->fid;
|
239
|
$file_uses = file_usage_list($file);
|
240
|
|
241
|
$this->assertEqual(empty($file_uses), TRUE, t('Created a new file with zero uses.'));
|
242
|
|
243
|
// Create a new node with file markup.
|
244
|
$nid = $this->createNode($fid);
|
245
|
$file_uses = file_usage_list($file);
|
246
|
|
247
|
$this->assertEqual($file_uses['media']['node'][$nid], 1, t('Incremented file usage on node save.'));
|
248
|
|
249
|
// Try to delete the file. file_delete() should return file_usage().
|
250
|
$deleted = file_delete($file);
|
251
|
$this->assertTrue(is_array($deleted), t('File cannot be deleted while in use by a node.'));
|
252
|
|
253
|
// Delete the node.
|
254
|
node_delete($nid);
|
255
|
$node = node_load($nid);
|
256
|
$file_uses = file_usage_list($file);
|
257
|
|
258
|
$this->assertEqual(empty($node), TRUE, t('Node has been deleted.'));
|
259
|
$this->assertEqual(empty($file_uses), TRUE, t('File has zero usage after node is deleted.'));
|
260
|
|
261
|
$deleted = file_delete($file);
|
262
|
$this->assertTrue($deleted, t('File can be deleted with no usage.'));
|
263
|
|
264
|
$file = file_load($fid);
|
265
|
$this->assertTrue(empty($file), t('File no longer exists after delete.'));
|
266
|
}
|
267
|
|
268
|
|
269
|
/**
|
270
|
* Tests if node still remains updatable if file was deleted.
|
271
|
*/
|
272
|
public function testFileUsageForcedDelete() {
|
273
|
// Create a node with file markup in the body field with a new file.
|
274
|
$files = $this->drupalGetTestFiles('image');
|
275
|
$file = file_save($files[1]);
|
276
|
$fid = $file->fid;
|
277
|
$file_uses = file_usage_list($file);
|
278
|
|
279
|
$this->assertEqual(empty($file_uses), TRUE, t('Created a new file with zero uses.'));
|
280
|
|
281
|
// Create a new node with file markup.
|
282
|
$nid = $this->createNode($fid);
|
283
|
$file_uses = file_usage_list($file);
|
284
|
|
285
|
$this->assertEqual($file_uses['media']['node'][$nid], 1, t('Incremented file usage on node save.'));
|
286
|
|
287
|
// Force the file to delete.
|
288
|
$deleted = file_delete($file, TRUE);
|
289
|
$this->assertTrue($deleted, t('File was deleted although in use sice we forced it.'));
|
290
|
|
291
|
// Try to update the node that uses broken file.
|
292
|
$account = $this->drupalCreateUser(array('edit any article content'));
|
293
|
$node = node_load($nid);
|
294
|
$this->drupalLogin($account);
|
295
|
$this->drupalGet('node/' . $nid . '/edit');
|
296
|
$this->assertRaw(check_plain($node->body['und'][0]['value']), t('Reference to deleted file found in node body.'));
|
297
|
$edit = array(
|
298
|
'body[und][0][value]' => '',
|
299
|
);
|
300
|
$this->drupalPost(NULL, $edit, t('Save'));
|
301
|
$type = node_type_load($node->type);
|
302
|
$this->assertRaw(t('@type %title has been updated.', array('@type' => $type->name, '%title' => $node->title)), t('Node without reference to deleted file saved successfully.'));
|
303
|
}
|
304
|
|
305
|
}
|