3 namespace Drupal\Tests\file\Functional;
5 use Drupal\node\Entity\Node;
6 use Drupal\user\RoleInterface;
7 use Drupal\file\Entity\File;
10 * Confirm that file field submissions work correctly for anonymous visitors.
14 class FileFieldAnonymousSubmissionTest extends FileFieldTestBase {
19 protected function setUp() {
21 // Set up permissions for anonymous attacker user.
22 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
23 'create article content' => TRUE,
24 'access content' => TRUE,
29 * Tests the basic node submission for an anonymous visitor.
31 public function testAnonymousNode() {
32 $bundle_label = 'Article';
33 $node_title = 'test page';
35 // Load the node form.
36 $this->drupalLogout();
37 $this->drupalGet('node/add/article');
38 $this->assertResponse(200, 'Loaded the article node form.');
39 $this->assertText(strip_tags(t('Create @name', ['@name' => $bundle_label])));
42 'title[0][value]' => $node_title,
43 'body[0][value]' => 'Test article',
45 $this->drupalPostForm(NULL, $edit, 'Save');
46 $this->assertResponse(200);
47 $t_args = ['@type' => $bundle_label, '%title' => $node_title];
48 $this->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
50 if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
52 $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
53 $node = Node::load($nid);
54 $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
59 * Tests file submission for an anonymous visitor.
61 public function testAnonymousNodeWithFile() {
62 $bundle_label = 'Article';
63 $node_title = 'Test page';
64 $this->createFileField('field_image', 'node', 'article', [], ['file_extensions' => 'txt png']);
66 // Load the node form.
67 $this->drupalLogout();
68 $this->drupalGet('node/add/article');
69 $this->assertResponse(200, 'Loaded the article node form.');
70 $this->assertText(strip_tags(t('Create @name', ['@name' => $bundle_label])));
72 // Generate an image file.
73 $image = $this->getTestFile('image');
77 'title[0][value]' => $node_title,
78 'body[0][value]' => 'Test article',
79 'files[field_image_0]' => $this->container->get('file_system')->realpath($image->getFileUri()),
81 $this->drupalPostForm(NULL, $edit, 'Save');
82 $this->assertResponse(200);
83 $t_args = ['@type' => $bundle_label, '%title' => $node_title];
84 $this->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
86 if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
88 $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
89 $node = Node::load($nid);
90 $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
91 $this->assertFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.');
96 * Tests file submission for an anonymous visitor with a missing node title.
98 public function testAnonymousNodeWithFileWithoutTitle() {
99 $this->drupalLogout();
100 $this->doTestNodeWithFileWithoutTitle();
104 * Tests file submission for an authenticated user with a missing node title.
106 public function testAuthenticatedNodeWithFileWithoutTitle() {
107 $admin_user = $this->drupalCreateUser([
108 'bypass node access',
109 'access content overview',
112 $this->drupalLogin($admin_user);
113 $this->doTestNodeWithFileWithoutTitle();
117 * Helper method to test file submissions with missing node titles.
119 protected function doTestNodeWithFileWithoutTitle() {
120 $bundle_label = 'Article';
121 $node_title = 'Test page';
122 $this->createFileField('field_image', 'node', 'article', [], ['file_extensions' => 'txt png']);
124 // Load the node form.
125 $this->drupalGet('node/add/article');
126 $this->assertResponse(200, 'Loaded the article node form.');
127 $this->assertText(strip_tags(t('Create @name', ['@name' => $bundle_label])));
129 // Generate an image file.
130 $image = $this->getTestFile('image');
132 // Submit the form but exclude the title field.
134 'body[0][value]' => 'Test article',
135 'files[field_image_0]' => $this->container->get('file_system')->realpath($image->getFileUri()),
137 if (!$this->loggedInUser) {
143 $this->drupalPostForm(NULL, $edit, $label);
144 $this->assertResponse(200);
145 $t_args = ['@type' => $bundle_label, '%title' => $node_title];
146 $this->assertNoText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
147 $this->assertText('Title field is required.');
149 // Submit the form again but this time with the missing title field. This
150 // should still work.
152 'title[0][value]' => $node_title,
154 $this->drupalPostForm(NULL, $edit, $label);
156 // Confirm the final submission actually worked.
157 $t_args = ['@type' => $bundle_label, '%title' => $node_title];
158 $this->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
160 if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
161 $nid = end($matches);
162 $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
163 $node = Node::load($nid);
164 $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
165 $this->assertFileExists(File::load($node->field_image->target_id), 'The image was uploaded successfully.');