Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / File / NameMungingTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\File;
4
5 /**
6  * Tests filename munging and unmunging.
7  *
8  * @group File
9  */
10 class NameMungingTest extends FileTestBase {
11
12   /**
13    * @var string
14    */
15   protected $badExtension;
16
17   /**
18    * @var string
19    */
20   protected $name;
21
22   /**
23    * @var string
24    */
25   protected $nameWithUcExt;
26
27   protected function setUp() {
28     parent::setUp();
29     $this->badExtension = 'php';
30     $this->name = $this->randomMachineName() . '.' . $this->badExtension . '.txt';
31     $this->nameWithUcExt = $this->randomMachineName() . '.' . strtoupper($this->badExtension) . '.txt';
32   }
33
34   /**
35    * Create a file and munge/unmunge the name.
36    */
37   public function testMunging() {
38     // Disable insecure uploads.
39     $this->config('system.file')->set('allow_insecure_uploads', 0)->save();
40     $munged_name = file_munge_filename($this->name, '', TRUE);
41     $messages = drupal_get_messages();
42     $this->assertTrue(in_array(strtr('For security reasons, your upload has been renamed to <em class="placeholder">%filename</em>.', ['%filename' => $munged_name]), $messages['status']), 'Alert properly set when a file is renamed.');
43     $this->assertNotEqual($munged_name, $this->name, format_string('The new filename (%munged) has been modified from the original (%original)', ['%munged' => $munged_name, '%original' => $this->name]));
44   }
45
46   /**
47    * Tests munging with a null byte in the filename.
48    */
49   public function testMungeNullByte() {
50     $prefix = $this->randomMachineName();
51     $filename = $prefix . '.' . $this->badExtension . "\0.txt";
52     $this->assertEqual(file_munge_filename($filename, ''), $prefix . '.' . $this->badExtension . '_.txt', 'A filename with a null byte is correctly munged to remove the null byte.');
53   }
54
55   /**
56    * If the system.file.allow_insecure_uploads setting evaluates to true, the file should
57    * come out untouched, no matter how evil the filename.
58    */
59   public function testMungeIgnoreInsecure() {
60     $this->config('system.file')->set('allow_insecure_uploads', 1)->save();
61     $munged_name = file_munge_filename($this->name, '');
62     $this->assertIdentical($munged_name, $this->name, format_string('The original filename (%original) matches the munged filename (%munged) when insecure uploads are enabled.', ['%munged' => $munged_name, '%original' => $this->name]));
63   }
64
65   /**
66    * White listed extensions are ignored by file_munge_filename().
67    */
68   public function testMungeIgnoreWhitelisted() {
69     // Declare our extension as whitelisted. The declared extensions should
70     // be case insensitive so test using one with a different case.
71     $munged_name = file_munge_filename($this->nameWithUcExt, $this->badExtension);
72     $this->assertIdentical($munged_name, $this->nameWithUcExt, format_string('The new filename (%munged) matches the original (%original) once the extension has been whitelisted.', ['%munged' => $munged_name, '%original' => $this->nameWithUcExt]));
73     // The allowed extensions should also be normalized.
74     $munged_name = file_munge_filename($this->name, strtoupper($this->badExtension));
75     $this->assertIdentical($munged_name, $this->name, format_string('The new filename (%munged) matches the original (%original) also when the whitelisted extension is in uppercase.', ['%munged' => $munged_name, '%original' => $this->name]));
76   }
77
78   /**
79    * Ensure that unmunge gets your name back.
80    */
81   public function testUnMunge() {
82     $munged_name = file_munge_filename($this->name, '', FALSE);
83     $unmunged_name = file_unmunge_filename($munged_name);
84     $this->assertIdentical($unmunged_name, $this->name, format_string('The unmunged (%unmunged) filename matches the original (%original)', ['%unmunged' => $unmunged_name, '%original' => $this->name]));
85   }
86
87 }