3 namespace Drupal\Tests;
5 use Drupal\Core\StreamWrapper\PublicStream;
8 * Provides methods to create test files from given values.
10 * This trait is meant to be used only by test classes.
12 trait TestFileCreationTrait {
15 * Whether the files were copied to the test files directory.
19 protected $generatedTestFiles = FALSE;
22 * Gets a list of files that can be used in tests.
24 * The first time this method is called, it will call
25 * $this->generateFile() to generate binary and ASCII text files in the
26 * public:// directory. It will also copy all files in
27 * core/modules/simpletest/files to public://. These contain image, SQL, PHP,
28 * JavaScript, and HTML files.
30 * All filenames are prefixed with their type and have appropriate extensions:
33 * - html-*.html and html-*.txt
34 * - image-*.png, image-*.jpg, and image-*.gif
35 * - javascript-*.txt and javascript-*.script
36 * - php-*.txt and php-*.php
37 * - sql-*.txt and sql-*.sql
39 * Any subsequent calls will not generate any new files, or copy the files
40 * over again. However, if a test class adds a new file to public:// that
41 * is prefixed with one of the above types, it will get returned as well, even
42 * on subsequent calls.
45 * File type, possible values: 'binary', 'html', 'image', 'javascript',
46 * 'php', 'sql', 'text'.
48 * (optional) File size in bytes to match. Defaults to NULL, which will not
49 * filter the returned list by size.
52 * List of files in public:// that match the filter(s).
54 protected function getTestFiles($type, $size = NULL) {
55 if (empty($this->generatedTestFiles)) {
56 // Generate binary test files.
59 foreach ($lines as $line) {
60 $this->generateFile('binary-' . $count++, 64, $line, 'binary');
63 // Generate ASCII text test files.
64 $lines = [16, 256, 1024, 2048, 20480];
66 foreach ($lines as $line) {
67 $this->generateFile('text-' . $count++, 64, $line, 'text');
70 // Copy other test files from simpletest.
71 $original = drupal_get_path('module', 'simpletest') . '/files';
72 $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
73 foreach ($files as $file) {
74 file_unmanaged_copy($file->uri, PublicStream::basePath());
77 $this->generatedTestFiles = TRUE;
81 // Make sure type is valid.
82 if (in_array($type, ['binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'])) {
83 $files = file_scan_directory('public://', '/' . $type . '\-.*/');
85 // If size is set then remove any files that are not of that size.
87 foreach ($files as $file) {
88 $stats = stat($file->uri);
89 if ($stats['size'] != $size) {
90 unset($files[$file->uri]);
95 usort($files, [$this, 'compareFiles']);
100 * Compares two files based on size and file name.
102 * Callback for uasort() within \TestFileCreationTrait::getTestFiles().
104 * @param \stdClass $file1
106 * @param \stdClass $file2
111 protected function compareFiles($file1, $file2) {
112 $compare_size = filesize($file1->uri) - filesize($file2->uri);
114 // Sort by file size.
115 return $compare_size;
118 // The files were the same size, so sort alphabetically.
119 return strnatcmp($file1->name, $file2->name);
124 * Generates a test file.
126 * @param string $filename
127 * The name of the file, including the path. The suffix '.txt' is appended
128 * to the supplied file name and the file is put into the public:// files
131 * The number of characters on one line.
133 * The number of lines in the file.
134 * @param string $type
135 * (optional) The type, one of:
136 * - text: The generated file contains random ASCII characters.
137 * - binary: The generated file contains random characters whose codes are
138 * in the range of 0 to 31.
139 * - binary-text: The generated file contains random sequence of '0' and '1'
143 * The name of the file, including the path.
145 public static function generateFile($filename, $width, $lines, $type = 'binary-text') {
147 for ($i = 0; $i < $lines; $i++) {
148 // Generate $width - 1 characters to leave space for the "\n" character.
149 for ($j = 0; $j < $width - 1; $j++) {
152 $text .= chr(rand(32, 126));
155 $text .= chr(rand(0, 31));
167 file_put_contents('public://' . $filename . '.txt', $text);