function TestFileCreationTrait::generateFile
Same name in other branches
- 8.9.x core/tests/Drupal/Tests/TestFileCreationTrait.php \Drupal\Tests\TestFileCreationTrait::generateFile()
- 10 core/tests/Drupal/Tests/TestFileCreationTrait.php \Drupal\Tests\TestFileCreationTrait::generateFile()
- 11.x core/tests/Drupal/Tests/TestFileCreationTrait.php \Drupal\Tests\TestFileCreationTrait::generateFile()
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.
4 calls to TestFileCreationTrait::generateFile()
- FileFieldDisplayTest::testNodeDisplay in core/
modules/ file/ tests/ src/ Functional/ FileFieldDisplayTest.php - Tests normal formatter display on node display.
- MaximumFileSizeExceededUploadTest::testUploadFileExceedingMaximumFileSize in core/
modules/ file/ tests/ src/ FunctionalJavascript/ MaximumFileSizeExceededUploadTest.php - Tests that uploading files exceeding maximum size are handled correctly.
- QuickEditFileTest::testRemove in core/
modules/ quickedit/ tests/ src/ FunctionalJavascript/ QuickEditFileTest.php - Tests if a file can be in-place removed with Quick Edit.
- TestFileCreationTrait::getTestFiles in core/
tests/ Drupal/ Tests/ TestFileCreationTrait.php - Gets a list of files that can be used in tests.
File
-
core/
tests/ Drupal/ Tests/ TestFileCreationTrait.php, line 148
Class
- TestFileCreationTrait
- Provides methods to create test files from given values.
Namespace
Drupal\TestsCode
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.
$filename = 'public://' . $filename . '.txt';
file_put_contents($filename, $text);
return $filename;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.