3 namespace Drupal\KernelTests\Core\File;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\KernelTests\KernelTestBase;
9 * Base class for file tests that adds some additional file specific
10 * assertions and helper functions.
12 abstract class FileTestBase extends KernelTestBase {
19 public static $modules = ['system'];
22 * A stream wrapper scheme to register for the test.
29 * A fully-qualified stream wrapper class name to register for the test.
38 protected function 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();
51 public function register(ContainerBuilder $container) {
52 parent::register($container);
54 $container->register('stream_wrapper.private', 'Drupal\Core\StreamWrapper\PrivateStream')
55 ->addTag('stream_wrapper', ['scheme' => 'private']);
57 if (isset($this->scheme)) {
58 $container->register('stream_wrapper.' . $this->scheme, $this->classname)
59 ->addTag('stream_wrapper', ['scheme' => $this->scheme]);
66 protected function setUpFilesystem() {
67 $public_file_directory = $this->siteDirectory . '/files';
69 require_once 'core/includes/file.inc';
71 mkdir($this->siteDirectory, 0775);
72 mkdir($this->siteDirectory . '/files', 0775);
73 mkdir($this->siteDirectory . '/files/config/' . CONFIG_SYNC_DIRECTORY, 0775, TRUE);
75 $this->setSetting('file_public_path', $public_file_directory);
77 $GLOBALS['config_directories'] = [
78 CONFIG_SYNC_DIRECTORY => $this->siteDirectory . '/files/config/sync',
83 * Helper function to test the permissions of a file.
87 * @param $expected_mode
88 * Octal integer like 0664 or 0777.
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);
96 // Mask out all but the last three octets.
97 $actual_mode = fileperms($filepath) & 0777;
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;
111 if (!isset($message)) {
112 $message = t('Expected file permission to be %expected, actually were %actual.', ['%actual' => decoct($actual_mode), '%expected' => decoct($expected_mode)]);
114 $this->assertEqual($actual_mode, $expected_mode, $message);
118 * Helper function to test the permissions of a directory.
121 * String directory path.
122 * @param $expected_mode
123 * Octal integer like 0664 or 0777.
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);
131 // Mask out all but the last three octets.
132 $actual_mode = fileperms($directory) & 0777;
133 $expected_mode = $expected_mode & 0777;
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;
147 if (!isset($message)) {
148 $message = t('Expected directory permission to be %expected, actually were %actual.', ['%actual' => decoct($actual_mode), '%expected' => decoct($expected_mode)]);
150 $this->assertEqual($actual_mode, $expected_mode, $message);
154 * Create a directory and assert it exists.
157 * Optional string with a directory path. If none is provided, a random
158 * name in the site's files directory will be used.
160 * The path to the directory.
162 public function createDirectory($path = NULL) {
163 // A directory to operate on.
165 $path = file_default_scheme() . '://' . $this->randomMachineName();
167 $this->assertTrue(drupal_mkdir($path) && is_dir($path), 'Directory was created successfully.');
172 * Create a file and return the URI of it.
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.
178 * Optional contents to save into the file. If a NULL value is provided an
179 * arbitrary string will be used.
181 * Optional string indicating the stream scheme to use. Drupal core includes
182 * public, private, and temporary. The public wrapper is the default.
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();
192 if (!isset($scheme)) {
193 $scheme = file_default_scheme();
195 $filepath = $scheme . '://' . $filepath;
197 if (!isset($contents)) {
198 $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
201 file_put_contents($filepath, $contents);
202 $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file');