function simpletest_generate_file in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/simpletest/simpletest.module \simpletest_generate_file()
Generates a test file.
Parameters
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.
int $width: The number of characters on one line.
int $lines: The number of lines in the file.
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 value
string The name of the file, including the path.
2 calls to simpletest_generate_file()
- FileFieldDisplayTest::testNodeDisplay in core/
modules/ file/ src/ Tests/ FileFieldDisplayTest.php - Tests normal formatter display on node display.
- WebTestBase::drupalGetTestFiles in core/
modules/ simpletest/ src/ WebTestBase.php - Gets a list of files that can be used in tests.
File
- core/
modules/ simpletest/ simpletest.module, line 542 - Provides testing functionality.
Code
function simpletest_generate_file($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;
}