TestFileCreationTrait.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/modules/simpletest/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. if (empty($this->generatedTestFiles)) {
  51. // Generate binary test files.
  52. $lines = [64, 1024];
  53. $count = 0;
  54. foreach ($lines as $line) {
  55. $this->generateFile('binary-' . $count++, 64, $line, 'binary');
  56. }
  57. // Generate ASCII text test files.
  58. $lines = [16, 256, 1024, 2048, 20480];
  59. $count = 0;
  60. foreach ($lines as $line) {
  61. $this->generateFile('text-' . $count++, 64, $line, 'text');
  62. }
  63. // Copy other test files from simpletest.
  64. $original = drupal_get_path('module', 'simpletest') . '/files';
  65. $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
  66. foreach ($files as $file) {
  67. file_unmanaged_copy($file->uri, PublicStream::basePath());
  68. }
  69. $this->generatedTestFiles = TRUE;
  70. }
  71. $files = [];
  72. // Make sure type is valid.
  73. if (in_array($type, ['binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'])) {
  74. $files = file_scan_directory('public://', '/' . $type . '\-.*/');
  75. // If size is set then remove any files that are not of that size.
  76. if ($size !== NULL) {
  77. foreach ($files as $file) {
  78. $stats = stat($file->uri);
  79. if ($stats['size'] != $size) {
  80. unset($files[$file->uri]);
  81. }
  82. }
  83. }
  84. }
  85. usort($files, [$this, 'compareFiles']);
  86. return $files;
  87. }
  88. /**
  89. * Compares two files based on size and file name.
  90. *
  91. * Callback for uasort() within \TestFileCreationTrait::getTestFiles().
  92. *
  93. * @param \stdClass $file1
  94. * The first file.
  95. * @param \stdClass $file2
  96. * The second class.
  97. *
  98. * @return int
  99. */
  100. protected function compareFiles($file1, $file2) {
  101. $compare_size = filesize($file1->uri) - filesize($file2->uri);
  102. if ($compare_size) {
  103. // Sort by file size.
  104. return $compare_size;
  105. }
  106. else {
  107. // The files were the same size, so sort alphabetically.
  108. return strnatcmp($file1->name, $file2->name);
  109. }
  110. }
  111. /**
  112. * Generates a test file.
  113. *
  114. * @param string $filename
  115. * The name of the file, including the path. The suffix '.txt' is appended
  116. * to the supplied file name and the file is put into the public:// files
  117. * directory.
  118. * @param int $width
  119. * The number of characters on one line.
  120. * @param int $lines
  121. * The number of lines in the file.
  122. * @param string $type
  123. * (optional) The type, one of:
  124. * - text: The generated file contains random ASCII characters.
  125. * - binary: The generated file contains random characters whose codes are
  126. * in the range of 0 to 31.
  127. * - binary-text: The generated file contains random sequence of '0' and '1'
  128. * values.
  129. *
  130. * @return string
  131. * The name of the file, including the path.
  132. */
  133. public static function generateFile($filename, $width, $lines, $type = 'binary-text') {
  134. $text = '';
  135. for ($i = 0; $i < $lines; $i++) {
  136. // Generate $width - 1 characters to leave space for the "\n" character.
  137. for ($j = 0; $j < $width - 1; $j++) {
  138. switch ($type) {
  139. case 'text':
  140. $text .= chr(rand(32, 126));
  141. break;
  142. case 'binary':
  143. $text .= chr(rand(0, 31));
  144. break;
  145. case 'binary-text':
  146. default:
  147. $text .= rand(0, 1);
  148. break;
  149. }
  150. }
  151. $text .= "\n";
  152. }
  153. // Create filename.
  154. $filename = 'public://' . $filename . '.txt';
  155. file_put_contents($filename, $text);
  156. return $filename;
  157. }
  158. }