Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / file / tests / src / Functional / FileFieldRevisionTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Functional;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Tests creating and deleting revisions with files attached.
9  *
10  * @group file
11  */
12 class FileFieldRevisionTest extends FileFieldTestBase {
13
14   /**
15    * Tests creating multiple revisions of a node and managing attached files.
16    *
17    * Expected behaviors:
18    *  - Adding a new revision will make another entry in the field table, but
19    *    the original file will not be duplicated.
20    *  - Deleting a revision should not delete the original file if the file
21    *    is in use by another revision.
22    *  - When the last revision that uses a file is deleted, the original file
23    *    should be deleted also.
24    */
25   public function testRevisions() {
26     // This test expects unused managed files to be marked as a temporary file
27     // and then deleted up by file_cron().
28     $this->config('file.settings')
29       ->set('make_unused_managed_files_temporary', TRUE)
30       ->save();
31     $node_storage = $this->container->get('entity.manager')->getStorage('node');
32     $type_name = 'article';
33     $field_name = strtolower($this->randomMachineName());
34     $this->createFileField($field_name, 'node', $type_name);
35     // Create the same fields for users.
36     $this->createFileField($field_name, 'user', 'user');
37
38     $test_file = $this->getTestFile('text');
39
40     // Create a new node with the uploaded file.
41     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
42
43     // Check that the file exists on disk and in the database.
44     $node_storage->resetCache([$nid]);
45     $node = $node_storage->load($nid);
46     $node_file_r1 = File::load($node->{$field_name}->target_id);
47     $node_vid_r1 = $node->getRevisionId();
48     $this->assertFileExists($node_file_r1, 'New file saved to disk on node creation.');
49     $this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.');
50     $this->assertFileIsPermanent($node_file_r1, 'File is permanent.');
51
52     // Upload another file to the same node in a new revision.
53     $this->replaceNodeFile($test_file, $field_name, $nid);
54     $node_storage->resetCache([$nid]);
55     $node = $node_storage->load($nid);
56     $node_file_r2 = File::load($node->{$field_name}->target_id);
57     $node_vid_r2 = $node->getRevisionId();
58     $this->assertFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.');
59     $this->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.');
60     $this->assertFileIsPermanent($node_file_r2, 'Replacement file is permanent.');
61
62     // Check that the original file is still in place on the first revision.
63     $node = node_revision_load($node_vid_r1);
64     $current_file = File::load($node->{$field_name}->target_id);
65     $this->assertEqual($node_file_r1->id(), $current_file->id(), 'Original file still in place after replacing file in new revision.');
66     $this->assertFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.');
67     $this->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision');
68     $this->assertFileIsPermanent($node_file_r1, 'Original file is still permanent.');
69
70     // Save a new version of the node without any changes.
71     // Check that the file is still the same as the previous revision.
72     $this->drupalPostForm('node/' . $nid . '/edit', ['revision' => '1'], t('Save'));
73     $node_storage->resetCache([$nid]);
74     $node = $node_storage->load($nid);
75     $node_file_r3 = File::load($node->{$field_name}->target_id);
76     $node_vid_r3 = $node->getRevisionId();
77     $this->assertEqual($node_file_r2->id(), $node_file_r3->id(), 'Previous revision file still in place after creating a new revision without a new file.');
78     $this->assertFileIsPermanent($node_file_r3, 'New revision file is permanent.');
79
80     // Revert to the first revision and check that the original file is active.
81     $this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r1 . '/revert', [], t('Revert'));
82     $node_storage->resetCache([$nid]);
83     $node = $node_storage->load($nid);
84     $node_file_r4 = File::load($node->{$field_name}->target_id);
85     $this->assertEqual($node_file_r1->id(), $node_file_r4->id(), 'Original revision file still in place after reverting to the original revision.');
86     $this->assertFileIsPermanent($node_file_r4, 'Original revision file still permanent after reverting to the original revision.');
87
88     // Delete the second revision and check that the file is kept (since it is
89     // still being used by the third revision).
90     $this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', [], t('Delete'));
91     $this->assertFileExists($node_file_r3, 'Second file is still available after deleting second revision, since it is being used by the third revision.');
92     $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting second revision, since it is being used by the third revision.');
93     $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting second revision, since it is being used by the third revision.');
94
95     // Attach the second file to a user.
96     $user = $this->drupalCreateUser();
97     $user->$field_name->target_id = $node_file_r3->id();
98     $user->$field_name->display = 1;
99     $user->save();
100     $this->drupalGet('user/' . $user->id() . '/edit');
101
102     // Delete the third revision and check that the file is not deleted yet.
103     $this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', [], t('Delete'));
104     $this->assertFileExists($node_file_r3, 'Second file is still available after deleting third revision, since it is being used by the user.');
105     $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting third revision, since it is being used by the user.');
106     $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting third revision, since it is being used by the user.');
107
108     // Delete the user and check that the file is also deleted.
109     $user->delete();
110     // TODO: This seems like a bug in File API. Clearing the stat cache should
111     // not be necessary here. The file really is deleted, but stream wrappers
112     // doesn't seem to think so unless we clear the PHP file stat() cache.
113     clearstatcache($node_file_r1->getFileUri());
114     clearstatcache($node_file_r2->getFileUri());
115     clearstatcache($node_file_r3->getFileUri());
116     clearstatcache($node_file_r4->getFileUri());
117
118     // Call file_cron() to clean up the file. Make sure the changed timestamp
119     // of the file is older than the system.file.temporary_maximum_age
120     // configuration value.
121     db_update('file_managed')
122       ->fields([
123         'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
124       ])
125       ->condition('fid', $node_file_r3->id())
126       ->execute();
127     \Drupal::service('cron')->run();
128
129     $this->assertFileNotExists($node_file_r3, 'Second file is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
130     $this->assertFileEntryNotExists($node_file_r3, 'Second file entry is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
131
132     // Delete the entire node and check that the original file is deleted.
133     $this->drupalPostForm('node/' . $nid . '/delete', [], t('Delete'));
134     // Call file_cron() to clean up the file. Make sure the changed timestamp
135     // of the file is older than the system.file.temporary_maximum_age
136     // configuration value.
137     db_update('file_managed')
138       ->fields([
139         'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1),
140       ])
141       ->condition('fid', $node_file_r1->id())
142       ->execute();
143     \Drupal::service('cron')->run();
144     $this->assertFileNotExists($node_file_r1, 'Original file is deleted after deleting the entire node with two revisions remaining.');
145     $this->assertFileEntryNotExists($node_file_r1, 'Original file entry is deleted after deleting the entire node with two revisions remaining.');
146   }
147
148 }