Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / File / FileTestBase.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\File;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Base class for file tests that adds some additional file specific
10  * assertions and helper functions.
11  */
12 abstract class FileTestBase extends KernelTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['system'];
20
21   /**
22    * A stream wrapper scheme to register for the test.
23    *
24    * @var string
25    */
26   protected $scheme;
27
28   /**
29    * A fully-qualified stream wrapper class name to register for the test.
30    *
31    * @var string
32    */
33   protected $classname;
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp() {
39     parent::setUp();
40     // \Drupal\KernelTests\KernelTestBase::bootKernel() sets a global override
41     // for the default scheme because core relies on it in
42     // file_default_scheme(). As we are creating the configuration here remove
43     // the global override.
44     unset($GLOBALS['config']['system.file']);
45     \Drupal::configFactory()->getEditable('system.file')->set('default_scheme', 'public')->save();
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function register(ContainerBuilder $container) {
52     parent::register($container);
53
54     $container->register('stream_wrapper.private', 'Drupal\Core\StreamWrapper\PrivateStream')
55       ->addTag('stream_wrapper', ['scheme' => 'private']);
56
57     if (isset($this->scheme)) {
58       $container->register('stream_wrapper.' . $this->scheme, $this->classname)
59         ->addTag('stream_wrapper', ['scheme' => $this->scheme]);
60     }
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   protected function setUpFilesystem() {
67     $public_file_directory = $this->siteDirectory . '/files';
68
69     require_once 'core/includes/file.inc';
70
71     mkdir($this->siteDirectory, 0775);
72     mkdir($this->siteDirectory . '/files', 0775);
73     mkdir($this->siteDirectory . '/files/config/' . CONFIG_SYNC_DIRECTORY, 0775, TRUE);
74
75     $this->setSetting('file_public_path', $public_file_directory);
76
77     $GLOBALS['config_directories'] = [
78       CONFIG_SYNC_DIRECTORY => $this->siteDirectory . '/files/config/sync',
79     ];
80   }
81
82   /**
83    * Helper function to test the permissions of a file.
84    *
85    * @param $filepath
86    *   String file path.
87    * @param $expected_mode
88    *   Octal integer like 0664 or 0777.
89    * @param $message
90    *   Optional message.
91    */
92   public function assertFilePermissions($filepath, $expected_mode, $message = NULL) {
93     // Clear out PHP's file stat cache to be sure we see the current value.
94     clearstatcache(TRUE, $filepath);
95
96     // Mask out all but the last three octets.
97     $actual_mode = fileperms($filepath) & 0777;
98
99     // PHP on Windows has limited support for file permissions. Usually each of
100     // "user", "group" and "other" use one octal digit (3 bits) to represent the
101     // read/write/execute bits. On Windows, chmod() ignores the "group" and
102     // "other" bits, and fileperms() returns the "user" bits in all three
103     // positions. $expected_mode is updated to reflect this.
104     if (substr(PHP_OS, 0, 3) == 'WIN') {
105       // Reset the "group" and "other" bits.
106       $expected_mode = $expected_mode & 0700;
107       // Shift the "user" bits to the "group" and "other" positions also.
108       $expected_mode = $expected_mode | $expected_mode >> 3 | $expected_mode >> 6;
109     }
110
111     if (!isset($message)) {
112       $message = t('Expected file permission to be %expected, actually were %actual.', ['%actual' => decoct($actual_mode), '%expected' => decoct($expected_mode)]);
113     }
114     $this->assertEqual($actual_mode, $expected_mode, $message);
115   }
116
117   /**
118    * Helper function to test the permissions of a directory.
119    *
120    * @param $directory
121    *   String directory path.
122    * @param $expected_mode
123    *   Octal integer like 0664 or 0777.
124    * @param $message
125    *   Optional message.
126    */
127   public function assertDirectoryPermissions($directory, $expected_mode, $message = NULL) {
128     // Clear out PHP's file stat cache to be sure we see the current value.
129     clearstatcache(TRUE, $directory);
130
131     // Mask out all but the last three octets.
132     $actual_mode = fileperms($directory) & 0777;
133     $expected_mode = $expected_mode & 0777;
134
135     // PHP on Windows has limited support for file permissions. Usually each of
136     // "user", "group" and "other" use one octal digit (3 bits) to represent the
137     // read/write/execute bits. On Windows, chmod() ignores the "group" and
138     // "other" bits, and fileperms() returns the "user" bits in all three
139     // positions. $expected_mode is updated to reflect this.
140     if (substr(PHP_OS, 0, 3) == 'WIN') {
141       // Reset the "group" and "other" bits.
142       $expected_mode = $expected_mode & 0700;
143       // Shift the "user" bits to the "group" and "other" positions also.
144       $expected_mode = $expected_mode | $expected_mode >> 3 | $expected_mode >> 6;
145     }
146
147     if (!isset($message)) {
148       $message = t('Expected directory permission to be %expected, actually were %actual.', ['%actual' => decoct($actual_mode), '%expected' => decoct($expected_mode)]);
149     }
150     $this->assertEqual($actual_mode, $expected_mode, $message);
151   }
152
153   /**
154    * Create a directory and assert it exists.
155    *
156    * @param $path
157    *   Optional string with a directory path. If none is provided, a random
158    *   name in the site's files directory will be used.
159    * @return
160    *   The path to the directory.
161    */
162   public function createDirectory($path = NULL) {
163     // A directory to operate on.
164     if (!isset($path)) {
165       $path = file_default_scheme() . '://' . $this->randomMachineName();
166     }
167     $this->assertTrue(drupal_mkdir($path) && is_dir($path), 'Directory was created successfully.');
168     return $path;
169   }
170
171   /**
172    * Create a file and return the URI of it.
173    *
174    * @param $filepath
175    *   Optional string specifying the file path. If none is provided then a
176    *   randomly named file will be created in the site's files directory.
177    * @param $contents
178    *   Optional contents to save into the file. If a NULL value is provided an
179    *   arbitrary string will be used.
180    * @param $scheme
181    *   Optional string indicating the stream scheme to use. Drupal core includes
182    *   public, private, and temporary. The public wrapper is the default.
183    * @return
184    *   File URI.
185    */
186   public function createUri($filepath = NULL, $contents = NULL, $scheme = NULL) {
187     if (!isset($filepath)) {
188       // Prefix with non-latin characters to ensure that all file-related
189       // tests work with international filenames.
190       $filepath = 'Файл для тестирования ' . $this->randomMachineName();
191     }
192     if (!isset($scheme)) {
193       $scheme = file_default_scheme();
194     }
195     $filepath = $scheme . '://' . $filepath;
196
197     if (!isset($contents)) {
198       $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
199     }
200
201     file_put_contents($filepath, $contents);
202     $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file');
203     return $filepath;
204   }
205
206 }