function FileManagedTestBase::createUri

Same name in other branches
  1. 8.9.x core/modules/file/src/Tests/FileManagedTestBase.php \Drupal\file\Tests\FileManagedTestBase::createUri()
  2. 8.9.x core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::createUri()
  3. 10 core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::createUri()
  4. 11.x core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::createUri()

Creates a file and returns its URI.

Parameters

string $filepath: Optional string specifying the file path. If none is provided then a randomly named file will be created in the site's files directory.

string $contents: Optional contents to save into the file. If a NULL value is provided an arbitrary string will be used.

string $scheme: Optional string indicating the stream scheme to use. Drupal core includes public, private, and temporary. The public wrapper is the default.

Return value

string File URI.

1 call to FileManagedTestBase::createUri()
FileManagedTestBase::createFile in core/modules/file/tests/src/Functional/FileManagedTestBase.php
Creates and saves a file, asserting that it was saved.

File

core/modules/file/tests/src/Functional/FileManagedTestBase.php, line 183

Class

FileManagedTestBase
Provides a base class for testing files with the file_test module.

Namespace

Drupal\Tests\file\Functional

Code

public function createUri($filepath = NULL, $contents = NULL, $scheme = NULL) {
    if (!isset($filepath)) {
        // Prefix with non-latin characters to ensure that all file-related
        // tests work with international filenames.
        // cSpell:disable-next-line
        $filepath = 'Файл для тестирования ' . $this->randomMachineName();
    }
    if (!isset($scheme)) {
        $scheme = 'public';
    }
    $filepath = $scheme . '://' . $filepath;
    if (!isset($contents)) {
        $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
    }
    file_put_contents($filepath, $contents);
    $this->assertFileExists($filepath);
    return $filepath;
}

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