function TestFileCreationTrait::getTestFiles

Same name in other branches
  1. 9 core/tests/Drupal/Tests/TestFileCreationTrait.php \Drupal\Tests\TestFileCreationTrait::getTestFiles()
  2. 8.9.x core/tests/Drupal/Tests/TestFileCreationTrait.php \Drupal\Tests\TestFileCreationTrait::getTestFiles()
  3. 10 core/tests/Drupal/Tests/TestFileCreationTrait.php \Drupal\Tests\TestFileCreationTrait::getTestFiles()

Gets a list of files that can be used in tests.

The first time this method is called, it will call $this->generateFile() to generate binary and ASCII text files in the public:// directory. It will also copy all files in core/tests/fixtures/files to public://. These contain image, SQL, PHP, JavaScript, and HTML files.

All filenames are prefixed with their type and have appropriate extensions:

  • text-*.txt
  • binary-*.txt
  • html-*.html and html-*.txt
  • image-*.png, image-*.jpg, and image-*.gif
  • javascript-*.txt and javascript-*.script
  • php-*.txt and php-*.php
  • sql-*.txt and sql-*.sql

Any subsequent calls will not generate any new files, or copy the files over again. However, if a test class adds a new file to public:// that is prefixed with one of the above types, it will get returned as well, even on subsequent calls.

Parameters

$type: File type, possible values: 'binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'.

$size: (optional) File size in bytes to match. Defaults to NULL, which will not filter the returned list by size.

Return value

object[] List of files in public:// that match the filter(s). Each file is an object with 'uri', 'filename', and 'name' properties.

73 calls to TestFileCreationTrait::getTestFiles()
AccessTest::testFileAccess in core/modules/file/tests/src/Kernel/AccessTest.php
Tests 'update' and 'delete' access to file entities.
AjaxFileManagedMultipleTest::testMultipleFilesUpload in core/modules/file/tests/src/FunctionalJavascript/AjaxFileManagedMultipleTest.php
Tests if managed file form element works well with multiple files upload.
ClaroModalDisplayTest::testModalAddAnother in core/tests/Drupal/FunctionalJavascriptTests/Theme/ClaroModalDisplayTest.php
Tests the position f "add another" button in dialogs.
CommentPreviewTest::testCommentPreview in core/modules/comment/tests/src/Functional/CommentPreviewTest.php
Tests comment preview.
ContentEntityFormFieldValidationFilteringTest::testFieldWidgetsWithLimitedValidationErrors in core/tests/Drupal/FunctionalTests/Entity/ContentEntityFormFieldValidationFilteringTest.php
Tests field widgets with #limit_validation_errors.

... See full list

File

core/tests/Drupal/Tests/TestFileCreationTrait.php, line 57

Class

TestFileCreationTrait
Provides methods to create test files from given values.

Namespace

Drupal\Tests

Code

protected function getTestFiles($type, $size = NULL) {
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    if (empty($this->generatedTestFiles)) {
        // Generate binary test files.
        $lines = [
            64,
            1024,
        ];
        $count = 0;
        foreach ($lines as $line) {
            $this->generateFile('binary-' . $count++, 64, $line, 'binary');
        }
        // Generate ASCII text test files.
        $lines = [
            16,
            256,
            1024,
            2048,
            20480,
        ];
        $count = 0;
        foreach ($lines as $line) {
            $this->generateFile('text-' . $count++, 64, $line, 'text');
        }
        // Copy other test files from fixtures.
        $original = \Drupal::root() . '/core/tests/fixtures/files';
        $files = $file_system->scanDirectory($original, '/(html|image|javascript|php|sql)-.*/');
        foreach ($files as $file) {
            $file_system->copy($file->uri, PublicStream::basePath());
        }
        $this->generatedTestFiles = TRUE;
    }
    $files = [];
    // Make sure type is valid.
    if (in_array($type, [
        'binary',
        'html',
        'image',
        'javascript',
        'php',
        'sql',
        'text',
    ])) {
        $files = $file_system->scanDirectory('public://', '/' . $type . '\\-.*/');
        // If size is set then remove any files that are not of that size.
        if ($size !== NULL) {
            foreach ($files as $file) {
                $stats = stat($file->uri);
                if ($stats['size'] != $size) {
                    unset($files[$file->uri]);
                }
            }
        }
    }
    usort($files, [
        $this,
        'compareFiles',
    ]);
    return $files;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.