You are here

function simpletest_generate_file in Drupal 8

Same name and namespace in other branches
  1. 7 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.

Deprecated

in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Tests\TestFileCreationTrait::generateFile() instead.

See also

https://www.drupal.org/node/3077768

1 call to simpletest_generate_file()
SimpletestDeprecationTest::testDeprecatedSimpletestGenerateFile in core/modules/simpletest/tests/src/Kernel/SimpletestDeprecationTest.php
@expectedDeprecation simpletest_generate_file() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Tests\TestFileCreationTrait::generateFile() instead. See https://www.drupal.org/node/3077768

File

core/modules/simpletest/simpletest.module, line 576
Provides testing functionality.

Code

function simpletest_generate_file($filename, $width, $lines, $type = 'binary-text') {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \\Drupal\\Tests\\TestFileCreationTrait::generateFile() instead. See https://www.drupal.org/node/3077768', E_USER_DEPRECATED);
  $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;
}