66d87cd7e8b043a4d8dc57075436dce158466662
[yaffs-website] / web / core / modules / file / src / Tests / FileFieldAnonymousSubmissionTest.php
1 <?php
2
3 namespace Drupal\file\Tests;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\user\RoleInterface;
7 use Drupal\file\Entity\File;
8
9 /**
10  * Confirm that file field submissions work correctly for anonymous visitors.
11  *
12  * @group file
13  */
14 class FileFieldAnonymousSubmissionTest extends FileFieldTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   protected function setUp() {
20     parent::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,
25     ]);
26   }
27
28   /**
29    * Tests the basic node submission for an anonymous visitor.
30    */
31   public function testAnonymousNode() {
32     $bundle_label = 'Article';
33     $node_title = 'test page';
34
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])));
40
41     $edit = [
42       'title[0][value]' => $node_title,
43       'body[0][value]' => 'Test article',
44     ];
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.');
49     $matches = [];
50     if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
51       $nid = end($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.');
55     }
56   }
57
58   /**
59    * Tests file submission for an anonymous visitor.
60    */
61   public function testAnonymousNodeWithFile() {
62     $bundle_label = 'Article';
63     $node_title = 'Test page';
64     $this->createFileField('field_image', 'node', 'article', [], ['file_extensions' => 'txt png']);
65
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])));
71
72     // Generate an image file.
73     $image = $this->getTestFile('image');
74
75     // Submit the form.
76     $edit = [
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()),
80     ];
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.');
85     $matches = [];
86     if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
87       $nid = end($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.');
92     }
93   }
94
95   /**
96    * Tests file submission for an anonymous visitor with a missing node title.
97    */
98   public function testAnonymousNodeWithFileWithoutTitle() {
99     $this->drupalLogout();
100     $this->doTestNodeWithFileWithoutTitle();
101   }
102
103   /**
104    * Tests file submission for an authenticated user with a missing node title.
105    */
106   public function testAuthenticatedNodeWithFileWithoutTitle() {
107     $admin_user = $this->drupalCreateUser([
108       'bypass node access',
109       'access content overview',
110       'administer nodes',
111     ]);
112     $this->drupalLogin($admin_user);
113     $this->doTestNodeWithFileWithoutTitle();
114   }
115
116   /**
117    * Helper method to test file submissions with missing node titles.
118    */
119   protected function doTestNodeWithFileWithoutTitle() {
120     $bundle_label = 'Article';
121     $node_title = 'Test page';
122     $this->createFileField('field_image', 'node', 'article', [], ['file_extensions' => 'txt png']);
123
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])));
128
129     // Generate an image file.
130     $image = $this->getTestFile('image');
131
132     // Submit the form but exclude the title field.
133     $edit = [
134       'body[0][value]' => 'Test article',
135       'files[field_image_0]' => $this->container->get('file_system')->realpath($image->getFileUri()),
136     ];
137     if (!$this->loggedInUser) {
138       $label = 'Save';
139     }
140     else {
141       $label = 'Save';
142     }
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.');
148
149     // Submit the form again but this time with the missing title field. This
150     // should still work.
151     $edit = [
152       'title[0][value]' => $node_title,
153     ];
154     $this->drupalPostForm(NULL, $edit, $label);
155
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.');
159     $matches = [];
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.');
166     }
167   }
168
169 }