Skip to content
Snippets Groups Projects
Unverified Commit 08080669 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3471194 by shalini_jha, kim.pepper, smustgrave, quietone, longwave,...

Issue #3471194 by shalini_jha, kim.pepper, smustgrave, quietone, longwave, larowlan: ImageItem and FileItem generateSampleValue methods makes bad Uris

(cherry picked from commit ec48de10)
parent 7a6e5906
No related branches found
No related tags found
10 merge requests!12812Issue #3527142 by andypost, catch, longwave, xjm: Update Composer and...,!12811Issue #3527142 by andypost, catch, longwave, xjm: Update Composer and...,!12686Draft: Issue #3535330: Assets paths in CSS no longer rewritten when aggregation is enabled,!12661Issue #3255804 by godotislate, longwave, berdir, alexpott, catch, andypost,...,!12660Issue #3255804 by godotislate, longwave, berdir, alexpott, catch, andypost,...,!12618Issue #3522970 by longwave, smustgrave: Remove unused BrowserTestBase::$originalContainer,!12486Resolve #3532360 "Check for session",!9103Issue #3150540 by kriboogh: Configuration langcode is forced to site default language,!8811Issue #3129179: Provide some way to rebuild the persistent bundle field map,!7916Remove taxonomy dependency on node module - 11.x
Pipeline #531766 passed
Pipeline: drupal

#531767

    ......@@ -360,8 +360,11 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
    $dirname = static::doGetUploadLocation($settings);
    \Drupal::service('file_system')->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY);
    // Ensure directory ends with a slash.
    $dirname .= str_ends_with($dirname, '/') ? '' : '/';
    // Generate a file entity.
    $destination = $dirname . '/' . $random->name(10, TRUE) . '.txt';
    $destination = $dirname . $random->name(10) . '.txt';
    $data = $random->paragraphs(3);
    /** @var \Drupal\file\FileRepositoryInterface $file_repository */
    $file_repository = \Drupal::service('file.repository');
    ......
    ......@@ -6,12 +6,14 @@
    use Drupal\Core\Field\FieldItemInterface;
    use Drupal\Core\Field\FieldItemListInterface;
    use Drupal\Core\Field\FieldDefinitionInterface;
    use Drupal\Core\Field\FieldStorageDefinitionInterface;
    use Drupal\entity_test\Entity\EntityTest;
    use Drupal\field\Entity\FieldConfig;
    use Drupal\Tests\field\Kernel\FieldKernelTestBase;
    use Drupal\field\Entity\FieldStorageConfig;
    use Drupal\file\Entity\File;
    use Drupal\file\Plugin\Field\FieldType\FileItem;
    use Drupal\user\Entity\Role;
    /**
    ......@@ -155,6 +157,48 @@ public function testFileItem(): void {
    \Drupal::service('renderer')->renderRoot($output);
    $this->assertTrue(!empty($entity->file_test->entity));
    $this->assertEquals($uri, $entity->file_test->entity->getFileUri());
    // Test file URIs with empty and custom directories.
    $this->validateFileUriForDirectory(
    '', 'public://'
    );
    $this->validateFileUriForDirectory(
    'custom_directory/subdir', 'public://custom_directory/subdir/'
    );
    }
    /**
    * Tests file URIs generated for a given file directory.
    *
    * @param string $file_directory
    * The file directory to test (e.g., empty or 'custom_directory/subdir').
    * @param string $expected_start
    * The expected starting string of the file URI (e.g., 'public://').
    */
    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);
    }
    }
    ......@@ -389,7 +389,9 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
    $image->setFileName($file_system->basename($path));
    $destination_dir = static::doGetUploadLocation($settings);
    $file_system->prepareDirectory($destination_dir, FileSystemInterface::CREATE_DIRECTORY);
    $destination = $destination_dir . '/' . basename($path);
    // Ensure directory ends with a slash.
    $destination_dir .= str_ends_with($destination_dir, '/') ? '' : '/';
    $destination = $destination_dir . basename($path);
    $file = \Drupal::service('file.repository')->move($image, $destination);
    $images[$extension][$min_resolution][$max_resolution][$file->id()] = $file;
    }
    ......
    ......@@ -9,6 +9,7 @@
    use Drupal\Core\Entity\Entity\EntityFormDisplay;
    use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
    use Drupal\Core\Entity\EntityTypeManagerInterface;
    use Drupal\Core\Field\FieldDefinitionInterface;
    use Drupal\Core\Field\FieldItemInterface;
    use Drupal\Core\Field\FieldItemListInterface;
    use Drupal\Core\Field\FieldStorageDefinitionInterface;
    ......@@ -19,6 +20,7 @@
    use Drupal\field\Entity\FieldStorageConfig;
    use Drupal\file\Entity\File;
    use Drupal\Tests\field\Kernel\FieldKernelTestBase;
    use Drupal\image\Plugin\Field\FieldType\ImageItem;
    use Drupal\user\Entity\Role;
    /**
    ......@@ -207,6 +209,14 @@ public function testImageItemMalformed(): void {
    $this->assertEmpty($entity->image_test->height);
    }
    /**
    * Tests image URIs for empty and custom directories.
    */
    public function testImageUriDirectories(): void {
    $this->validateImageUriForDirectory('', 'public://');
    $this->validateImageUriForDirectory('custom_directory/subdir', 'public://custom_directory/subdir/');
    }
    /**
    * Tests display_default.
    */
    ......@@ -225,4 +235,36 @@ public function testDisplayDefaultValue(): void {
    self::assertEquals(1, $form_state->getValue(['image_test', 0, 'display']));
    }
    /**
    * Validates the image file URI generated for a given file directory.
    *
    * @param string $file_directory
    * The file directory to test (e.g., empty or 'custom_directory/subdir').
    * @param string $expected_start
    * The expected starting string of the file URI (e.g., 'public://').
    */
    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);
    }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment