Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / file / tests / src / FunctionalJavascript / MaximumFileSizeExceededUploadTest.php
1 <?php
2
3 namespace Drupal\Tests\file\FunctionalJavascript;
4
5 use Drupal\Component\Utility\Bytes;
6 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
7 use Drupal\Tests\TestFileCreationTrait;
8 use Drupal\Tests\file\Functional\FileFieldCreationTrait;
9
10 /**
11  * Tests uploading a file that exceeds the maximum file size.
12  *
13  * @group file
14  */
15 class MaximumFileSizeExceededUploadTest extends JavascriptTestBase {
16
17   use FileFieldCreationTrait;
18   use TestFileCreationTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = ['node', 'file'];
24
25   /**
26    * The file system service.
27    *
28    * @var \Drupal\Core\File\FileSystemInterface
29    */
30   protected $fileSystem;
31
32   /**
33    * A test user.
34    *
35    * @var \Drupal\user\UserInterface
36    */
37   protected $user;
38
39   /**
40    * The original value of the 'display_errors' PHP configuration option.
41    *
42    * @todo Remove this when issue #2905597 is fixed.
43    * @see https://www.drupal.org/node/2905597
44    *
45    * @var string
46    */
47   protected $originalDisplayErrorsValue;
48
49   /**
50    * {@inheritdoc}
51    */
52   protected function setUp() {
53     parent::setUp();
54
55     $this->fileSystem = $this->container->get('file_system');
56
57     // Create the Article node type.
58     $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
59
60     // Attach a file field to the node type.
61     $field_settings = ['file_extensions' => 'txt'];
62     $this->createFileField('field_file', 'node', 'article', [], $field_settings);
63
64     // Log in as a content author who can create Articles.
65     $this->user = $this->drupalCreateUser([
66       'access content',
67       'create article content',
68     ]);
69     $this->drupalLogin($this->user);
70
71     // Disable the displaying of errors, so that the AJAX responses are not
72     // contaminated with error messages about exceeding the maximum POST size.
73     // @todo Remove this when issue #2905597 is fixed.
74     // @see https://www.drupal.org/node/2905597
75     $this->originalDisplayErrorsValue = ini_set('display_errors', '0');
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   protected function tearDown() {
82     // Restore the displaying of errors to the original value.
83     // @todo Remove this when issue #2905597 is fixed.
84     // @see https://www.drupal.org/node/2905597
85     ini_set('display_errors', $this->originalDisplayErrorsValue);
86
87     parent::tearDown();
88   }
89
90   /**
91    * Tests that uploading files exceeding maximum size are handled correctly.
92    */
93   public function testUploadFileExceedingMaximumFileSize() {
94     $session = $this->getSession();
95
96     // Create a test file that exceeds the maximum POST size with 1 kilobyte.
97     $post_max_size = Bytes::toInt(ini_get('post_max_size'));
98     $invalid_file = $this->generateFile('exceeding_post_max_size', ceil(($post_max_size + 1024) / 1024), 1024);
99
100     // Go to the node creation form and try to upload the test file.
101     $this->drupalGet('node/add/article');
102     $page = $session->getPage();
103     $page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($invalid_file));
104
105     // An error message should appear informing the user that the file exceeded
106     // the maximum file size.
107     $this->assertSession()->waitForElement('css', '.messages--error');
108     // The error message includes the actual file size limit which depends on
109     // the current environment, so we check for a part of the message.
110     $this->assertSession()->pageTextContains('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size');
111
112     // Now upload a valid file and check that the error message disappears.
113     $valid_file = $this->generateFile('not_exceeding_post_max_size', 8, 8);
114     $page->attachFileToField("files[field_file_0]", $this->fileSystem->realpath($valid_file));
115     $this->assertSession()->waitForElement('named', ['id_or_name', 'field_file_0_remove_button']);
116     $this->assertSession()->elementNotExists('css', '.messages--error');
117   }
118
119 }