TestFileCreationTrait.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Drupal\Tests;
  3. use Drupal\Core\StreamWrapper\PublicStream;
  4. /**
  5. * Provides methods to create test files from given values.
  6. *
  7. * This trait is meant to be used only by test classes.
  8. */
  9. trait TestFileCreationTrait {
  10. /**
  11. * Whether the files were copied to the test files directory.
  12. *
  13. * @var bool
  14. */
  15. protected $generatedTestFiles = FALSE;
  16. /**
  17. * Gets a list of files that can be used in tests.
  18. *
  19. * The first time this method is called, it will call
  20. * $this->generateFile() to generate binary and ASCII text files in the
  21. * public:// directory. It will also copy all files in
  22. * core/tests/fixtures/files to public://. These contain image, SQL, PHP,
  23. * JavaScript, and HTML files.
  24. *
  25. * All filenames are prefixed with their type and have appropriate extensions:
  26. * - text-*.txt
  27. * - binary-*.txt
  28. * - html-*.html and html-*.txt
  29. * - image-*.png, image-*.jpg, and image-*.gif
  30. * - javascript-*.txt and javascript-*.script
  31. * - php-*.txt and php-*.php
  32. * - sql-*.txt and sql-*.sql
  33. *
  34. * Any subsequent calls will not generate any new files, or copy the files
  35. * over again. However, if a test class adds a new file to public:// that
  36. * is prefixed with one of the above types, it will get returned as well, even
  37. * on subsequent calls.
  38. *
  39. * @param $type
  40. * File type, possible values: 'binary', 'html', 'image', 'javascript',
  41. * 'php', 'sql', 'text'.
  42. * @param $size
  43. * (optional) File size in bytes to match. Defaults to NULL, which will not
  44. * filter the returned list by size.
  45. *
  46. * @return array[]
  47. * List of files in public:// that match the filter(s).
  48. */
  49. protected function getTestFiles($type, $size = NULL) {
  50. /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  51. $file_system = \Drupal::service('file_system');
  52. if (empty($this->generatedTestFiles)) {
  53. // Generate binary test files.
  54. $lines = [64, 1024];
  55. $count = 0;
  56. foreach ($lines as $line) {
  57. $this->generateFile('binary-' . $count++, 64, $line, 'binary');
  58. }
  59. // Generate ASCII text test files.
  60. $lines = [16, 256, 1024, 2048, 20480];
  61. $count = 0;
  62. foreach ($lines as $line) {
  63. $this->generateFile('text-' . $count++, 64, $line, 'text');
  64. }
  65. // Copy other test files from fixtures.
  66. $original = \Drupal::service('app.root') . '/core/tests/fixtures/files';
  67. $files = $file_system->scanDirectory($original, '/(html|image|javascript|php|sql)-.*/');
  68. foreach ($files as $file) {
  69. $file_system->copy($file->uri, PublicStream::basePath());
  70. }
  71. $this->generatedTestFiles = TRUE;
  72. }
  73. $files = [];
  74. // Make sure type is valid.
  75. if (in_array($type, ['binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'])) {
  76. $files = $file_system->scanDirectory('public://', '/' . $type . '\-.*/');
  77. // If size is set then remove any files that are not of that size.
  78. if ($size !== NULL) {
  79. foreach ($files as $file) {
  80. $stats = stat($file->uri);
  81. if ($stats['size'] != $size) {
  82. unset($files[$file->uri]);
  83. }
  84. }
  85. }
  86. }
  87. usort($files, [$this, 'compareFiles']);
  88. return $files;
  89. }
  90. /**
  91. * Compares two files based on size and file name.
  92. *
  93. * Callback for uasort() within \TestFileCreationTrait::getTestFiles().
  94. *
  95. * @param object $file1
  96. * The first file.
  97. * @param object $file2
  98. * The second class.
  99. *
  100. * @return int
  101. */
  102. protected function compareFiles($file1, $file2) {
  103. $compare_size = filesize($file1->uri) - filesize($file2->uri);
  104. if ($compare_size) {
  105. // Sort by file size.
  106. return $compare_size;
  107. }
  108. else {
  109. // The files were the same size, so sort alphabetically.
  110. return strnatcmp($file1->name, $file2->name);
  111. }
  112. }
  113. /**
  114. * Generates a test file.
  115. *
  116. * @param string $filename
  117. * The name of the file, including the path. The suffix '.txt' is appended
  118. * to the supplied file name and the file is put into the public:// files
  119. * directory.
  120. * @param int $width
  121. * The number of characters on one line.
  122. * @param int $lines
  123. * The number of lines in the file.
  124. * @param string $type
  125. * (optional) The type, one of:
  126. * - text: The generated file contains random ASCII characters.
  127. * - binary: The generated file contains random characters whose codes are
  128. * in the range of 0 to 31.
  129. * - binary-text: The generated file contains random sequence of '0' and '1'
  130. * values.
  131. *
  132. * @return string
  133. * The name of the file, including the path.
  134. */
  135. public static function generateFile($filename, $width, $lines, $type = 'binary-text') {
  136. $text = '';
  137. for ($i = 0; $i < $lines; $i++) {
  138. // Generate $width - 1 characters to leave space for the "\n" character.
  139. for ($j = 0; $j < $width - 1; $j++) {
  140. switch ($type) {
  141. case 'text':
  142. $text .= chr(rand(32, 126));
  143. break;
  144. case 'binary':
  145. $text .= chr(rand(0, 31));
  146. break;
  147. case 'binary-text':
  148. default:
  149. $text .= rand(0, 1);
  150. break;
  151. }
  152. }
  153. $text .= "\n";
  154. }
  155. // Create filename.
  156. $filename = 'public://' . $filename . '.txt';
  157. file_put_contents($filename, $text);
  158. return $filename;
  159. }
  160. }