Chris@0: generateFile() to generate binary and ASCII text files in the Chris@0: * public:// directory. It will also copy all files in Chris@0: * core/modules/simpletest/files to public://. These contain image, SQL, PHP, Chris@0: * JavaScript, and HTML files. Chris@0: * Chris@0: * All filenames are prefixed with their type and have appropriate extensions: Chris@0: * - text-*.txt Chris@0: * - binary-*.txt Chris@0: * - html-*.html and html-*.txt Chris@0: * - image-*.png, image-*.jpg, and image-*.gif Chris@0: * - javascript-*.txt and javascript-*.script Chris@0: * - php-*.txt and php-*.php Chris@0: * - sql-*.txt and sql-*.sql Chris@0: * Chris@0: * Any subsequent calls will not generate any new files, or copy the files Chris@0: * over again. However, if a test class adds a new file to public:// that Chris@0: * is prefixed with one of the above types, it will get returned as well, even Chris@0: * on subsequent calls. Chris@0: * Chris@0: * @param $type Chris@0: * File type, possible values: 'binary', 'html', 'image', 'javascript', Chris@0: * 'php', 'sql', 'text'. Chris@0: * @param $size Chris@0: * (optional) File size in bytes to match. Defaults to NULL, which will not Chris@0: * filter the returned list by size. Chris@0: * Chris@0: * @return array[] Chris@0: * List of files in public:// that match the filter(s). Chris@0: */ Chris@0: protected function getTestFiles($type, $size = NULL) { Chris@0: if (empty($this->generatedTestFiles)) { Chris@0: // Generate binary test files. Chris@0: $lines = [64, 1024]; Chris@0: $count = 0; Chris@0: foreach ($lines as $line) { Chris@0: $this->generateFile('binary-' . $count++, 64, $line, 'binary'); Chris@0: } Chris@0: Chris@0: // Generate ASCII text test files. Chris@0: $lines = [16, 256, 1024, 2048, 20480]; Chris@0: $count = 0; Chris@0: foreach ($lines as $line) { Chris@0: $this->generateFile('text-' . $count++, 64, $line, 'text'); Chris@0: } Chris@0: Chris@0: // Copy other test files from simpletest. Chris@0: $original = drupal_get_path('module', 'simpletest') . '/files'; Chris@0: $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/'); Chris@0: foreach ($files as $file) { Chris@18: \Drupal::service('file_system')->copy($file->uri, PublicStream::basePath()); Chris@0: } Chris@0: Chris@0: $this->generatedTestFiles = TRUE; Chris@0: } Chris@0: Chris@0: $files = []; Chris@0: // Make sure type is valid. Chris@0: if (in_array($type, ['binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'])) { Chris@0: $files = file_scan_directory('public://', '/' . $type . '\-.*/'); Chris@0: Chris@0: // If size is set then remove any files that are not of that size. Chris@0: if ($size !== NULL) { Chris@0: foreach ($files as $file) { Chris@0: $stats = stat($file->uri); Chris@0: if ($stats['size'] != $size) { Chris@0: unset($files[$file->uri]); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: usort($files, [$this, 'compareFiles']); Chris@0: return $files; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Compares two files based on size and file name. Chris@0: * Chris@0: * Callback for uasort() within \TestFileCreationTrait::getTestFiles(). Chris@0: * Chris@0: * @param \stdClass $file1 Chris@0: * The first file. Chris@0: * @param \stdClass $file2 Chris@0: * The second class. Chris@0: * Chris@0: * @return int Chris@0: */ Chris@0: protected function compareFiles($file1, $file2) { Chris@0: $compare_size = filesize($file1->uri) - filesize($file2->uri); Chris@0: if ($compare_size) { Chris@0: // Sort by file size. Chris@0: return $compare_size; Chris@0: } Chris@0: else { Chris@0: // The files were the same size, so sort alphabetically. Chris@0: return strnatcmp($file1->name, $file2->name); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a test file. Chris@0: * Chris@0: * @param string $filename Chris@0: * The name of the file, including the path. The suffix '.txt' is appended Chris@0: * to the supplied file name and the file is put into the public:// files Chris@0: * directory. Chris@0: * @param int $width Chris@0: * The number of characters on one line. Chris@0: * @param int $lines Chris@0: * The number of lines in the file. Chris@0: * @param string $type Chris@0: * (optional) The type, one of: Chris@0: * - text: The generated file contains random ASCII characters. Chris@0: * - binary: The generated file contains random characters whose codes are Chris@0: * in the range of 0 to 31. Chris@0: * - binary-text: The generated file contains random sequence of '0' and '1' Chris@0: * values. Chris@0: * Chris@0: * @return string Chris@0: * The name of the file, including the path. Chris@0: */ Chris@0: public static function generateFile($filename, $width, $lines, $type = 'binary-text') { Chris@0: $text = ''; Chris@0: for ($i = 0; $i < $lines; $i++) { Chris@0: // Generate $width - 1 characters to leave space for the "\n" character. Chris@0: for ($j = 0; $j < $width - 1; $j++) { Chris@0: switch ($type) { Chris@0: case 'text': Chris@0: $text .= chr(rand(32, 126)); Chris@0: break; Chris@0: case 'binary': Chris@0: $text .= chr(rand(0, 31)); Chris@0: break; Chris@0: case 'binary-text': Chris@0: default: Chris@0: $text .= rand(0, 1); Chris@0: break; Chris@0: } Chris@0: } Chris@0: $text .= "\n"; Chris@0: } Chris@0: Chris@0: // Create filename. Chris@12: $filename = 'public://' . $filename . '.txt'; Chris@12: file_put_contents($filename, $text); Chris@0: return $filename; Chris@0: } Chris@0: Chris@0: }