diff --git a/core/modules/simpletest/src/TestFileCreationTrait.php b/core/modules/simpletest/src/TestFileCreationTrait.php index 932a26d..e552b90 100644 --- a/core/modules/simpletest/src/TestFileCreationTrait.php +++ b/core/modules/simpletest/src/TestFileCreationTrait.php @@ -15,7 +15,7 @@ * Gets a list of files that can be used in tests. * * The first time this method is called, it will call - * simpletest_generate_file() to generate binary and ASCII text files in the + * $this->generateFile() to generate binary and ASCII text files in the * public:// directory. It will also copy all files in * core/modules/simpletest/files to public://. These contain image, SQL, PHP, * JavaScript, and HTML files. @@ -50,14 +50,14 @@ protected function drupalGetTestFiles($type, $size = NULL) { $lines = array(64, 1024); $count = 0; foreach ($lines as $line) { - simpletest_generate_file('binary-' . $count++, 64, $line, 'binary'); + $this->generateFile('binary-' . $count++, 64, $line, 'binary'); } // Generate ASCII text test files. $lines = array(16, 256, 1024, 2048, 20480); $count = 0; foreach ($lines as $line) { - simpletest_generate_file('text-' . $count++, 64, $line, 'text'); + $this->generateFile('text-' . $count++, 64, $line, 'text'); } // Copy other test files from simpletest. @@ -103,4 +103,52 @@ protected function drupalCompareFiles($file1, $file2) { return strnatcmp($file1->name, $file2->name); } } + + /** + * Generates a test file. + * + * @param string $filename + * The name of the file, including the path. The suffix '.txt' is appended + * to the supplied file name and the file is put into the public:// files + * directory. + * @param int $width + * The number of characters on one line. + * @param int $lines + * The number of lines in the file. + * @param string $type + * (optional) The type, one of: + * - text: The generated file contains random ASCII characters. + * - binary: The generated file contains random characters whose codes are + * in the range of 0 to 31. + * - binary-text: The generated file contains random sequence of '0' and '1' + * values. + * + * @return string + * The name of the file, including the path. + */ + public static function generateFile($filename, $width, $lines, $type = 'binary-text') { + $text = ''; + for ($i = 0; $i < $lines; $i++) { + // Generate $width - 1 characters to leave space for the "\n" character. + for ($j = 0; $j < $width - 1; $j++) { + switch ($type) { + case 'text': + $text .= chr(rand(32, 126)); + break; + case 'binary': + $text .= chr(rand(0, 31)); + break; + case 'binary-text': + default: + $text .= rand(0, 1); + break; + } + } + $text .= "\n"; + } + + // Create filename. + file_put_contents('public://' . $filename . '.txt', $text); + return $filename; + } }