a2f119b65ca4a4b51f7dee2e44edcf1457ec4bd7
[yaffs-website] / web / core / tests / Drupal / Tests / TestFileCreationTrait.php
1 <?php
2
3 namespace Drupal\Tests;
4
5 use Drupal\Core\StreamWrapper\PublicStream;
6
7 /**
8  * Provides methods to create test files from given values.
9  *
10  * This trait is meant to be used only by test classes.
11  */
12 trait TestFileCreationTrait {
13
14   /**
15    * Whether the files were copied to the test files directory.
16    *
17    * @var bool
18    */
19   protected $generatedTestFiles = FALSE;
20
21   /**
22    * Gets a list of files that can be used in tests.
23    *
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.
29    *
30    * All filenames are prefixed with their type and have appropriate extensions:
31    * - text-*.txt
32    * - binary-*.txt
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
38    *
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.
43    *
44    * @param $type
45    *   File type, possible values: 'binary', 'html', 'image', 'javascript',
46    *   'php', 'sql', 'text'.
47    * @param $size
48    *   (optional) File size in bytes to match. Defaults to NULL, which will not
49    *   filter the returned list by size.
50    *
51    * @return array[]
52    *   List of files in public:// that match the filter(s).
53    */
54   protected function getTestFiles($type, $size = NULL) {
55     if (empty($this->generatedTestFiles)) {
56       // Generate binary test files.
57       $lines = [64, 1024];
58       $count = 0;
59       foreach ($lines as $line) {
60         $this->generateFile('binary-' . $count++, 64, $line, 'binary');
61       }
62
63       // Generate ASCII text test files.
64       $lines = [16, 256, 1024, 2048, 20480];
65       $count = 0;
66       foreach ($lines as $line) {
67         $this->generateFile('text-' . $count++, 64, $line, 'text');
68       }
69
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());
75       }
76
77       $this->generatedTestFiles = TRUE;
78     }
79
80     $files = [];
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 . '\-.*/');
84
85       // If size is set then remove any files that are not of that size.
86       if ($size !== NULL) {
87         foreach ($files as $file) {
88           $stats = stat($file->uri);
89           if ($stats['size'] != $size) {
90             unset($files[$file->uri]);
91           }
92         }
93       }
94     }
95     usort($files, [$this, 'compareFiles']);
96     return $files;
97   }
98
99   /**
100    * Compares two files based on size and file name.
101    *
102    * Callback for uasort() within \TestFileCreationTrait::getTestFiles().
103    *
104    * @param \stdClass $file1
105    *   The first file.
106    * @param \stdClass $file2
107    *   The second class.
108    *
109    * @return int
110    */
111   protected function compareFiles($file1, $file2) {
112     $compare_size = filesize($file1->uri) - filesize($file2->uri);
113     if ($compare_size) {
114       // Sort by file size.
115       return $compare_size;
116     }
117     else {
118       // The files were the same size, so sort alphabetically.
119       return strnatcmp($file1->name, $file2->name);
120     }
121   }
122
123   /**
124    * Generates a test file.
125    *
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
129    *   directory.
130    * @param int $width
131    *   The number of characters on one line.
132    * @param int $lines
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'
140    *     values.
141    *
142    * @return string
143    *   The name of the file, including the path.
144    */
145   public static function generateFile($filename, $width, $lines, $type = 'binary-text') {
146     $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++) {
150         switch ($type) {
151           case 'text':
152             $text .= chr(rand(32, 126));
153             break;
154           case 'binary':
155             $text .= chr(rand(0, 31));
156             break;
157           case 'binary-text':
158           default:
159             $text .= rand(0, 1);
160             break;
161         }
162       }
163       $text .= "\n";
164     }
165
166     // Create filename.
167     $filename = 'public://' . $filename . '.txt';
168     file_put_contents($filename, $text);
169     return $filename;
170   }
171
172 }