88574cb652b48b2222cc3e48a242c32837eca452
[yaffs-website] / web / core / modules / file / src / Tests / FilePrivateTest.php
1 <?php
2
3 namespace Drupal\file\Tests;
4
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;
10
11 /**
12  * Uploads a test to a private node and checks access.
13  *
14  * @group file
15  */
16 class FilePrivateTest extends FileFieldTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['node_access_test', 'field_test'];
24
25   protected function setUp() {
26     parent::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)
33       ->save();
34   }
35
36   /**
37    * Tests file access for file uploaded to a private node.
38    */
39   public function testPrivateFile() {
40     $node_storage = $this->container->get('entity.manager')->getStorage('node');
41     /** @var \Drupal\Core\File\FileSystemInterface $file_system */
42     $file_system = \Drupal::service('file_system');
43     $type_name = 'article';
44     $field_name = strtolower($this->randomMachineName());
45     $this->createFileField($field_name, 'node', $type_name, ['uri_scheme' => 'private']);
46
47     $test_file = $this->getTestFile('text');
48     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name, TRUE, ['private' => TRUE]);
49     \Drupal::entityManager()->getStorage('node')->resetCache([$nid]);
50     /* @var \Drupal\node\NodeInterface $node */
51     $node = $node_storage->load($nid);
52     $node_file = File::load($node->{$field_name}->target_id);
53     // Ensure the file can be viewed.
54     $this->drupalGet('node/' . $node->id());
55     $this->assertRaw($node_file->getFilename(), 'File reference is displayed after attaching it');
56     // Ensure the file can be downloaded.
57     $this->drupalGet(file_create_url($node_file->getFileUri()));
58     $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
59     $this->drupalLogOut();
60     $this->drupalGet(file_create_url($node_file->getFileUri()));
61     $this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
62
63     // Create a field with no view access. See
64     // field_test_entity_field_access().
65     $no_access_field_name = 'field_no_view_access';
66     $this->createFileField($no_access_field_name, 'node', $type_name, ['uri_scheme' => 'private']);
67     // Test with the field that should deny access through field access.
68     $this->drupalLogin($this->adminUser);
69     $nid = $this->uploadNodeFile($test_file, $no_access_field_name, $type_name, TRUE, ['private' => TRUE]);
70     \Drupal::entityManager()->getStorage('node')->resetCache([$nid]);
71     $node = $node_storage->load($nid);
72     $node_file = File::load($node->{$no_access_field_name}->target_id);
73
74     // Ensure the file cannot be downloaded.
75     $file_url = file_create_url($node_file->getFileUri());
76     $this->drupalGet($file_url);
77     $this->assertResponse(403, 'Confirmed that access is denied for the file without view field access permission.');
78
79     // Attempt to reuse the file when editing a node.
80     $edit = [];
81     $edit['title[0][value]'] = $this->randomMachineName();
82     $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
83     $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
84     $edit[$field_name . '[0][fids]'] = $node_file->id();
85     $this->drupalPostForm('node/' . $new_node->id() . '/edit', $edit, t('Save'));
86     // Make sure the form submit failed - we stayed on the edit form.
87     $this->assertUrl('node/' . $new_node->id() . '/edit');
88     // Check that we got the expected constraint form error.
89     $constraint = new ReferenceAccessConstraint();
90     $this->assertRaw(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()]));
91     // Attempt to reuse the existing file when creating a new node, and confirm
92     // that access is still denied.
93     $edit = [];
94     $edit['title[0][value]'] = $this->randomMachineName();
95     $edit[$field_name . '[0][fids]'] = $node_file->id();
96     $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
97     $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
98     $this->assertTrue(empty($new_node), 'Node was not created.');
99     $this->assertUrl('node/add/' . $type_name);
100     $this->assertRaw(SafeMarkup::format($constraint->message, ['%type' => 'file', '%id' => $node_file->id()]));
101
102     // Now make file_test_file_download() return everything.
103     \Drupal::state()->set('file_test.allow_all', TRUE);
104     // Delete the node.
105     $node->delete();
106     // Ensure the file can still be downloaded by the owner.
107     $this->drupalGet($file_url);
108     $this->assertResponse(200, 'Confirmed that the owner still has access to the temporary file.');
109
110     // Ensure the file cannot be downloaded by an anonymous user.
111     $this->drupalLogout();
112     $this->drupalGet($file_url);
113     $this->assertResponse(403, 'Confirmed that access is denied for an anonymous user to the temporary file.');
114
115     // Ensure the file cannot be downloaded by another user.
116     $account = $this->drupalCreateUser();
117     $this->drupalLogin($account);
118     $this->drupalGet($file_url);
119     $this->assertResponse(403, 'Confirmed that access is denied for another user to the temporary file.');
120
121     // As an anonymous user, create a temporary file with no references and
122     // confirm that only the session that uploaded it may view it.
123     $this->drupalLogout();
124     user_role_change_permissions(
125       RoleInterface::ANONYMOUS_ID,
126       [
127         "create $type_name content" => TRUE,
128         'access content' => TRUE,
129       ]
130     );
131     $test_file = $this->getTestFile('text');
132     $this->drupalGet('node/add/' . $type_name);
133     $edit = ['files[' . $field_name . '_0]' => $file_system->realpath($test_file->getFileUri())];
134     $this->drupalPostForm(NULL, $edit, t('Upload'));
135     /** @var \Drupal\file\FileStorageInterface $file_storage */
136     $file_storage = $this->container->get('entity.manager')->getStorage('file');
137     $files = $file_storage->loadByProperties(['uid' => 0]);
138     $this->assertEqual(1, count($files), 'Loaded one anonymous file.');
139     $file = end($files);
140     $this->assertTrue($file->isTemporary(), 'File is temporary.');
141     $usage = $this->container->get('file.usage')->listUsage($file);
142     $this->assertFalse($usage, 'No file usage found.');
143     $file_url = file_create_url($file->getFileUri());
144     $this->drupalGet($file_url);
145     $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the temporary file.');
146     // Close the prior connection and remove the session cookie.
147     $this->curlClose();
148     $this->curlCookies = [];
149     $this->cookies = [];
150     $this->drupalGet($file_url);
151     $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the temporary file.');
152
153     // As an anonymous user, create a permanent file, then remove all
154     // references to the file (so that it becomes temporary again) and confirm
155     // that only the session that uploaded it may view it.
156     $test_file = $this->getTestFile('text');
157     $this->drupalGet('node/add/' . $type_name);
158     $edit = [];
159     $edit['title[0][value]'] = $this->randomMachineName();
160     $edit['files[' . $field_name . '_0]'] = $file_system->realpath($test_file->getFileUri());
161     $this->drupalPostForm(NULL, $edit, t('Save'));
162     $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
163     $file_id = $new_node->{$field_name}->target_id;
164     $file = File::load($file_id);
165     $this->assertTrue($file->isPermanent(), 'File is permanent.');
166     // Remove the reference to this file.
167     $new_node->{$field_name} = [];
168     $new_node->save();
169     $file = File::load($file_id);
170     $this->assertTrue($file->isTemporary(), 'File is temporary.');
171     $usage = $this->container->get('file.usage')->listUsage($file);
172     $this->assertFalse($usage, 'No file usage found.');
173     $file_url = file_create_url($file->getFileUri());
174     $this->drupalGet($file_url);
175     $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the file whose references were removed.');
176     // Close the prior connection and remove the session cookie.
177     $this->curlClose();
178     $this->curlCookies = [];
179     $this->cookies = [];
180     $this->drupalGet($file_url);
181     $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the file whose references were removed.');
182
183     // As an anonymous user, create a permanent file that is referenced by a
184     // published node and confirm that all anonymous users may view it.
185     $test_file = $this->getTestFile('text');
186     $this->drupalGet('node/add/' . $type_name);
187     $edit = [];
188     $edit['title[0][value]'] = $this->randomMachineName();
189     $edit['files[' . $field_name . '_0]'] = $file_system->realpath($test_file->getFileUri());
190     $this->drupalPostForm(NULL, $edit, t('Save'));
191     $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
192     $file = File::load($new_node->{$field_name}->target_id);
193     $this->assertTrue($file->isPermanent(), 'File is permanent.');
194     $usage = $this->container->get('file.usage')->listUsage($file);
195     $this->assertTrue($usage, 'File usage found.');
196     $file_url = file_create_url($file->getFileUri());
197     $this->drupalGet($file_url);
198     $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the permanent file that is referenced by a published node.');
199     // Close the prior connection and remove the session cookie.
200     $this->curlClose();
201     $this->curlCookies = [];
202     $this->cookies = [];
203     $this->drupalGet($file_url);
204     $this->assertResponse(200, 'Confirmed that another anonymous user also has access to the permanent file that is referenced by a published node.');
205
206     // As an anonymous user, create a permanent file that is referenced by an
207     // unpublished node and confirm that no anonymous users may view it (even
208     // the session that uploaded the file) because they cannot view the
209     // unpublished node.
210     $test_file = $this->getTestFile('text');
211     $this->drupalGet('node/add/' . $type_name);
212     $edit = [];
213     $edit['title[0][value]'] = $this->randomMachineName();
214     $edit['files[' . $field_name . '_0]'] = $file_system->realpath($test_file->getFileUri());
215     $this->drupalPostForm(NULL, $edit, t('Save'));
216     $new_node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
217     $new_node->setPublished(FALSE);
218     $new_node->save();
219     $file = File::load($new_node->{$field_name}->target_id);
220     $this->assertTrue($file->isPermanent(), 'File is permanent.');
221     $usage = $this->container->get('file.usage')->listUsage($file);
222     $this->assertTrue($usage, 'File usage found.');
223     $file_url = file_create_url($file->getFileUri());
224     $this->drupalGet($file_url);
225     $this->assertResponse(403, 'Confirmed that the anonymous uploader cannot access the permanent file when it is referenced by an unpublished node.');
226     // Close the prior connection and remove the session cookie.
227     $this->curlClose();
228     $this->curlCookies = [];
229     $this->cookies = [];
230     $this->drupalGet($file_url);
231     $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the permanent file when it is referenced by an unpublished node.');
232   }
233
234 }