FileTestBase.php 6.8 KB

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