3 namespace Drupal\file\Tests;
5 use Drupal\Core\Entity\Plugin\Validation\Constraint\ReferenceAccessConstraint;
6 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\file\Entity\File;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\user\RoleInterface;
12 * Uploads a test to a private node and checks access.
16 class FilePrivateTest extends FileFieldTestBase {
23 public static $modules = ['node_access_test', 'field_test'];
25 protected function setUp() {
27 node_access_test_add_field(NodeType::load('article'));
28 node_access_rebuild();
29 \Drupal::state()->set('node_access_test.private', TRUE);
30 // This test expects unused managed files to be marked as a temporary file.
31 $this->config('file.settings')
32 ->set('make_unused_managed_files_temporary', TRUE)
37 * Tests file access for file uploaded to a private node.
39 public function testPrivateFile() {
40 $node_storage = $this->container->get('entity.manager')->getStorage('node');
41 $type_name = 'article';
42 $field_name = strtolower($this->randomMachineName());
43 $this->createFileField($field_name, 'node', $type_name, ['uri_scheme' => 'private']);
45 $test_file = $this->getTestFile('text');
46 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name, TRUE, ['private' => TRUE]);
47 \Drupal::entityManager()->getStorage('node')->resetCache([$nid]);
48 /* @var \Drupal\node\NodeInterface $node */
49 $node = $node_storage->load($nid);
50 $node_file = File::load($node->{$field_name}->target_id);
51 // Ensure the file can be viewed.
52 $this->drupalGet('node/' . $node->id());
53 $this->assertRaw($node_file->getFilename(), 'File reference is displayed after attaching it');
54 // Ensure the file can be downloaded.
55 $this->drupalGet(file_create_url($node_file->getFileUri()));
56 $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
57 $this->drupalLogOut();
58 $this->drupalGet(file_create_url($node_file->getFileUri()));
59 $this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
61 // Create a field with no view access. See
62 // field_test_entity_field_access().
63 $no_access_field_name = 'field_no_view_access';
64 $this->createFileField($no_access_field_name, 'node', $type_name, ['uri_scheme' => 'private']);
65 // Test with the field that should deny access through field access.
66 $this->drupalLogin($this->adminUser);
67 $nid = $this->uploadNodeFile($test_file, $no_access_field_name, $type_name, TRUE, ['private' => TRUE]);
68 \Drupal::entityManager()->getStorage('node')->resetCache([$nid]);
69 $node = $node_storage->load($nid);
70 $node_file = File::load($node->{$no_access_field_name}->target_id);
72 // Ensure the file cannot be downloaded.
73 $file_url = file_create_url($node_file->getFileUri());
74 $this->drupalGet($file_url);
75 $this->assertResponse(403, 'Confirmed that access is denied for the file without view field access permission.');
77 // Attempt to reuse the file when editing a node.
79 $edit['title[0][value]'] = $this->randomMachineName();
80 $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
81 $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
82 $edit[$field_name . '[0][fids]'] = $node_file->id();
83 $this->drupalPostForm('node/' . $new_node->id() . '/edit', $edit, t('Save'));
84 // Make sure the form submit failed - we stayed on the edit form.
85 $this->assertUrl('node/' . $new_node->id() . '/edit');
86 // Check that we got the expected constraint form error.
87 $constraint = new ReferenceAccessConstraint();
88 $this->assertRaw(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()]));
89 // Attempt to reuse the existing file when creating a new node, and confirm
90 // that access is still denied.
92 $edit['title[0][value]'] = $this->randomMachineName();
93 $edit[$field_name . '[0][fids]'] = $node_file->id();
94 $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
95 $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
96 $this->assertTrue(empty($new_node), 'Node was not created.');
97 $this->assertUrl('node/add/' . $type_name);
98 $this->assertRaw(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()]));
100 // Now make file_test_file_download() return everything.
101 \Drupal::state()->set('file_test.allow_all', TRUE);
104 // Ensure the file can still be downloaded by the owner.
105 $this->drupalGet($file_url);
106 $this->assertResponse(200, 'Confirmed that the owner still has access to the temporary file.');
108 // Ensure the file cannot be downloaded by an anonymous user.
109 $this->drupalLogout();
110 $this->drupalGet($file_url);
111 $this->assertResponse(403, 'Confirmed that access is denied for an anonymous user to the temporary file.');
113 // Ensure the file cannot be downloaded by another user.
114 $account = $this->drupalCreateUser();
115 $this->drupalLogin($account);
116 $this->drupalGet($file_url);
117 $this->assertResponse(403, 'Confirmed that access is denied for another user to the temporary file.');
119 // As an anonymous user, create a temporary file with no references and
120 // confirm that only the session that uploaded it may view it.
121 $this->drupalLogout();
122 user_role_change_permissions(
123 RoleInterface::ANONYMOUS_ID,
125 "create $type_name content" => TRUE,
126 'access content' => TRUE,
129 $test_file = $this->getTestFile('text');
130 $this->drupalGet('node/add/' . $type_name);
131 $edit = ['files[' . $field_name . '_0]' => drupal_realpath($test_file->getFileUri())];
132 $this->drupalPostForm(NULL, $edit, t('Upload'));
133 /** @var \Drupal\file\FileStorageInterface $file_storage */
134 $file_storage = $this->container->get('entity.manager')->getStorage('file');
135 $files = $file_storage->loadByProperties(['uid' => 0]);
136 $this->assertEqual(1, count($files), 'Loaded one anonymous file.');
138 $this->assertTrue($file->isTemporary(), 'File is temporary.');
139 $usage = $this->container->get('file.usage')->listUsage($file);
140 $this->assertFalse($usage, 'No file usage found.');
141 $file_url = file_create_url($file->getFileUri());
142 $this->drupalGet($file_url);
143 $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the temporary file.');
144 // Close the prior connection and remove the session cookie.
146 $this->curlCookies = [];
148 $this->drupalGet($file_url);
149 $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the temporary file.');
151 // As an anonymous user, create a permanent file, then remove all
152 // references to the file (so that it becomes temporary again) and confirm
153 // that only the session that uploaded it may view it.
154 $test_file = $this->getTestFile('text');
155 $this->drupalGet('node/add/' . $type_name);
157 $edit['title[0][value]'] = $this->randomMachineName();
158 $edit['files[' . $field_name . '_0]'] = drupal_realpath($test_file->getFileUri());
159 $this->drupalPostForm(NULL, $edit, t('Save'));
160 $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
161 $file_id = $new_node->{$field_name}->target_id;
162 $file = File::load($file_id);
163 $this->assertTrue($file->isPermanent(), 'File is permanent.');
164 // Remove the reference to this file.
165 $new_node->{$field_name} = [];
167 $file = File::load($file_id);
168 $this->assertTrue($file->isTemporary(), 'File is temporary.');
169 $usage = $this->container->get('file.usage')->listUsage($file);
170 $this->assertFalse($usage, 'No file usage found.');
171 $file_url = file_create_url($file->getFileUri());
172 $this->drupalGet($file_url);
173 $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the file whose references were removed.');
174 // Close the prior connection and remove the session cookie.
176 $this->curlCookies = [];
178 $this->drupalGet($file_url);
179 $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the file whose references were removed.');
181 // As an anonymous user, create a permanent file that is referenced by a
182 // published node and confirm that all anonymous users may view it.
183 $test_file = $this->getTestFile('text');
184 $this->drupalGet('node/add/' . $type_name);
186 $edit['title[0][value]'] = $this->randomMachineName();
187 $edit['files[' . $field_name . '_0]'] = drupal_realpath($test_file->getFileUri());
188 $this->drupalPostForm(NULL, $edit, t('Save'));
189 $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
190 $file = File::load($new_node->{$field_name}->target_id);
191 $this->assertTrue($file->isPermanent(), 'File is permanent.');
192 $usage = $this->container->get('file.usage')->listUsage($file);
193 $this->assertTrue($usage, 'File usage found.');
194 $file_url = file_create_url($file->getFileUri());
195 $this->drupalGet($file_url);
196 $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the permanent file that is referenced by a published node.');
197 // Close the prior connection and remove the session cookie.
199 $this->curlCookies = [];
201 $this->drupalGet($file_url);
202 $this->assertResponse(200, 'Confirmed that another anonymous user also has access to the permanent file that is referenced by a published node.');
204 // As an anonymous user, create a permanent file that is referenced by an
205 // unpublished node and confirm that no anonymous users may view it (even
206 // the session that uploaded the file) because they cannot view the
208 $test_file = $this->getTestFile('text');
209 $this->drupalGet('node/add/' . $type_name);
211 $edit['title[0][value]'] = $this->randomMachineName();
212 $edit['files[' . $field_name . '_0]'] = drupal_realpath($test_file->getFileUri());
213 $this->drupalPostForm(NULL, $edit, t('Save'));
214 $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
215 $new_node->setPublished(FALSE);
217 $file = File::load($new_node->{$field_name}->target_id);
218 $this->assertTrue($file->isPermanent(), 'File is permanent.');
219 $usage = $this->container->get('file.usage')->listUsage($file);
220 $this->assertTrue($usage, 'File usage found.');
221 $file_url = file_create_url($file->getFileUri());
222 $this->drupalGet($file_url);
223 $this->assertResponse(403, 'Confirmed that the anonymous uploader cannot access the permanent file when it is referenced by an unpublished node.');
224 // Close the prior connection and remove the session cookie.
226 $this->curlCookies = [];
228 $this->drupalGet($file_url);
229 $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the permanent file when it is referenced by an unpublished node.');