function ImageItemTest::validateImageUriForDirectory

Validates the image file URI 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 ImageItemTest::validateImageUriForDirectory()
ImageItemTest::testImageUriDirectories in core/modules/image/tests/src/Kernel/ImageItemTest.php
Tests image URIs for empty and custom directories.

File

core/modules/image/tests/src/Kernel/ImageItemTest.php, line 246

Class

ImageItemTest
Tests using entity fields of the image field type.

Namespace

Drupal\Tests\image\Kernel

Code

private function validateImageUriForDirectory(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' => 'jpg',
    'file_directory' => $file_directory,
    'uri_scheme' => 'public',
  ]);
  // Generate sample value and check the URI format.
  $value = ImageItem::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.