function FileItemTest::validateFileUriForDirectory

Tests file URIs generated for a given file directory.

Parameters

string $file_directory: The file directory to test (e.g., empty or 'custom_directory/subdir').

string $expected_start: The expected starting string of the file URI (e.g., 'public://').

1 call to FileItemTest::validateFileUriForDirectory()
FileItemTest::testFileItem in core/modules/file/tests/src/Kernel/FileItemTest.php
Tests using entity fields of the file field type.

File

core/modules/file/tests/src/Kernel/FileItemTest.php, line 178

Class

FileItemTest
Tests using entity fields of the file field type.

Namespace

Drupal\Tests\file\Kernel

Code

private function validateFileUriForDirectory(string $file_directory, string $expected_start) : void {
  // Mock the field definition with the specified file directory.
  $definition = $this->createMock(FieldDefinitionInterface::class);
  $definition->expects($this->any())
    ->method('getSettings')
    ->willReturn([
    'file_extensions' => 'txt',
    'file_directory' => $file_directory,
    'uri_scheme' => 'public',
    'display_default' => TRUE,
  ]);
  // Generate a sample file value.
  $value = FileItem::generateSampleValue($definition);
  $this->assertNotEmpty($value);
  // Load the file entity and get its URI.
  $fid = $value['target_id'];
  $file = File::load($fid);
  $fileUri = $file->getFileUri();
  // Verify the file URI starts with the expected protocol and structure.
  $this->assertStringStartsWith($expected_start, $fileUri);
  $this->assertMatchesRegularExpression('#^' . preg_quote($expected_start, '#') . '[^/]+#', $fileUri);
}

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