PageRenderTime 45ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Tests/File/DirectoryTest.php

https://gitlab.com/reasonat/test8
PHP | 163 lines | 79 code | 26 blank | 58 comment | 2 complexity | df18e3413414d79c535fa0c25b9affa1 MD5 | raw file
  1. <?php
  2. namespace Drupal\system\Tests\File;
  3. use Drupal\Component\PhpStorage\FileStorage;
  4. /**
  5. * Tests operations dealing with directories.
  6. *
  7. * @group File
  8. */
  9. class DirectoryTest extends FileTestBase {
  10. /**
  11. * Test local directory handling functions.
  12. */
  13. function testFileCheckLocalDirectoryHandling() {
  14. $site_path = $this->container->get('site.path');
  15. $directory = $site_path . '/files';
  16. // Check a new recursively created local directory for correct file system
  17. // permissions.
  18. $parent = $this->randomMachineName();
  19. $child = $this->randomMachineName();
  20. // Files directory already exists.
  21. $this->assertTrue(is_dir($directory), t('Files directory already exists.'), 'File');
  22. // Make files directory writable only.
  23. $old_mode = fileperms($directory);
  24. // Create the directories.
  25. $parent_path = $directory . DIRECTORY_SEPARATOR . $parent;
  26. $child_path = $parent_path . DIRECTORY_SEPARATOR . $child;
  27. $this->assertTrue(drupal_mkdir($child_path, 0775, TRUE), t('No error reported when creating new local directories.'), 'File');
  28. // Ensure new directories also exist.
  29. $this->assertTrue(is_dir($parent_path), t('New parent directory actually exists.'), 'File');
  30. $this->assertTrue(is_dir($child_path), t('New child directory actually exists.'), 'File');
  31. // Check that new directory permissions were set properly.
  32. $this->assertDirectoryPermissions($parent_path, 0775);
  33. $this->assertDirectoryPermissions($child_path, 0775);
  34. // Check that existing directory permissions were not modified.
  35. $this->assertDirectoryPermissions($directory, $old_mode);
  36. // Check creating a directory using an absolute path.
  37. $absolute_path = drupal_realpath($directory) . DIRECTORY_SEPARATOR . $this->randomMachineName() . DIRECTORY_SEPARATOR . $this->randomMachineName();
  38. $this->assertTrue(drupal_mkdir($absolute_path, 0775, TRUE), 'No error reported when creating new absolute directories.', 'File');
  39. $this->assertDirectoryPermissions($absolute_path, 0775);
  40. }
  41. /**
  42. * Test directory handling functions.
  43. */
  44. function testFileCheckDirectoryHandling() {
  45. // A directory to operate on.
  46. $directory = file_default_scheme() . '://' . $this->randomMachineName() . '/' . $this->randomMachineName();
  47. $this->assertFalse(is_dir($directory), 'Directory does not exist prior to testing.');
  48. // Non-existent directory.
  49. $this->assertFalse(file_prepare_directory($directory, 0), 'Error reported for non-existing directory.', 'File');
  50. // Make a directory.
  51. $this->assertTrue(file_prepare_directory($directory, FILE_CREATE_DIRECTORY), 'No error reported when creating a new directory.', 'File');
  52. // Make sure directory actually exists.
  53. $this->assertTrue(is_dir($directory), 'Directory actually exists.', 'File');
  54. if (substr(PHP_OS, 0, 3) != 'WIN') {
  55. // PHP on Windows doesn't support any kind of useful read-only mode for
  56. // directories. When executing a chmod() on a directory, PHP only sets the
  57. // read-only flag, which doesn't prevent files to actually be written
  58. // in the directory on any recent version of Windows.
  59. // Make directory read only.
  60. @drupal_chmod($directory, 0444);
  61. $this->assertFalse(file_prepare_directory($directory, 0), 'Error reported for a non-writeable directory.', 'File');
  62. // Test directory permission modification.
  63. $this->settingsSet('file_chmod_directory', 0777);
  64. $this->assertTrue(file_prepare_directory($directory, FILE_MODIFY_PERMISSIONS), 'No error reported when making directory writeable.', 'File');
  65. }
  66. // Test that the directory has the correct permissions.
  67. $this->assertDirectoryPermissions($directory, 0777, 'file_chmod_directory setting is respected.');
  68. // Remove .htaccess file to then test that it gets re-created.
  69. @drupal_unlink(file_default_scheme() . '://.htaccess');
  70. $this->assertFalse(is_file(file_default_scheme() . '://.htaccess'), 'Successfully removed the .htaccess file in the files directory.', 'File');
  71. file_ensure_htaccess();
  72. $this->assertTrue(is_file(file_default_scheme() . '://.htaccess'), 'Successfully re-created the .htaccess file in the files directory.', 'File');
  73. // Verify contents of .htaccess file.
  74. $file = file_get_contents(file_default_scheme() . '://.htaccess');
  75. $this->assertEqual($file, FileStorage::htaccessLines(FALSE), 'The .htaccess file contains the proper content.', 'File');
  76. }
  77. /**
  78. * This will take a directory and path, and find a valid filepath that is not
  79. * taken by another file.
  80. */
  81. function testFileCreateNewFilepath() {
  82. // First we test against an imaginary file that does not exist in a
  83. // directory.
  84. $basename = 'xyz.txt';
  85. $directory = 'core/misc';
  86. $original = $directory . '/' . $basename;
  87. $path = file_create_filename($basename, $directory);
  88. $this->assertEqual($path, $original, format_string('New filepath %new equals %original.', array('%new' => $path, '%original' => $original)), 'File');
  89. // Then we test against a file that already exists within that directory.
  90. $basename = 'druplicon.png';
  91. $original = $directory . '/' . $basename;
  92. $expected = $directory . '/druplicon_0.png';
  93. $path = file_create_filename($basename, $directory);
  94. $this->assertEqual($path, $expected, format_string('Creating a new filepath from %original equals %new (expected %expected).', array('%new' => $path, '%original' => $original, '%expected' => $expected)), 'File');
  95. // @TODO: Finally we copy a file into a directory several times, to ensure a properly iterating filename suffix.
  96. }
  97. /**
  98. * This will test the filepath for a destination based on passed flags and
  99. * whether or not the file exists.
  100. *
  101. * If a file exists, file_destination($destination, $replace) will either
  102. * return:
  103. * - the existing filepath, if $replace is FILE_EXISTS_REPLACE
  104. * - a new filepath if FILE_EXISTS_RENAME
  105. * - an error (returning FALSE) if FILE_EXISTS_ERROR.
  106. * If the file doesn't currently exist, then it will simply return the
  107. * filepath.
  108. */
  109. function testFileDestination() {
  110. // First test for non-existent file.
  111. $destination = 'core/misc/xyz.txt';
  112. $path = file_destination($destination, FILE_EXISTS_REPLACE);
  113. $this->assertEqual($path, $destination, 'Non-existing filepath destination is correct with FILE_EXISTS_REPLACE.', 'File');
  114. $path = file_destination($destination, FILE_EXISTS_RENAME);
  115. $this->assertEqual($path, $destination, 'Non-existing filepath destination is correct with FILE_EXISTS_RENAME.', 'File');
  116. $path = file_destination($destination, FILE_EXISTS_ERROR);
  117. $this->assertEqual($path, $destination, 'Non-existing filepath destination is correct with FILE_EXISTS_ERROR.', 'File');
  118. $destination = 'core/misc/druplicon.png';
  119. $path = file_destination($destination, FILE_EXISTS_REPLACE);
  120. $this->assertEqual($path, $destination, 'Existing filepath destination remains the same with FILE_EXISTS_REPLACE.', 'File');
  121. $path = file_destination($destination, FILE_EXISTS_RENAME);
  122. $this->assertNotEqual($path, $destination, 'A new filepath destination is created when filepath destination already exists with FILE_EXISTS_RENAME.', 'File');
  123. $path = file_destination($destination, FILE_EXISTS_ERROR);
  124. $this->assertEqual($path, FALSE, 'An error is returned when filepath destination already exists with FILE_EXISTS_ERROR.', 'File');
  125. }
  126. /**
  127. * Ensure that the file_directory_temp() function always returns a value.
  128. */
  129. function testFileDirectoryTemp() {
  130. // Start with an empty variable to ensure we have a clean slate.
  131. $config = $this->config('system.file');
  132. $config->set('path.temporary', '')->save();
  133. $tmp_directory = file_directory_temp();
  134. $this->assertEqual(empty($tmp_directory), FALSE, 'file_directory_temp() returned a non-empty value.');
  135. $this->assertEqual($config->get('path.temporary'), $tmp_directory);
  136. }
  137. }