Unverified Commit f795f242 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2353881 by berdir, sharique, rpayanm, smustgrave, mohit_aghera,...

Issue #2353881 by berdir, sharique, rpayanm, smustgrave, mohit_aghera, tien.xuan.vo: Add test coverage for file entity reference selection plugin
parent fd5cea79
Loading
Loading
Loading
Loading
Loading
+0 −348

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Changes for core/modules/field/tests/src/Traits/EntityReferenceFieldCreationTrait.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ trait EntityReferenceFieldCreationTrait {
   *
   * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
   */
  protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = [], $cardinality = 1) {
  protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = [], $cardinality = 1): void {
    // Look for or add the specified field to the requested entity bundle.
    if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
      FieldStorageConfig::create([
+89 −0
Changes for core/modules/file/tests/src/Kernel/Plugin/EntityReference/FileSelectionTest.php: 89 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\file\Kernel\Plugin\EntityReference;

use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait;

/**
 * Tests file selection plugin.
 *
 * @group file
 */
class FileSelectionTest extends KernelTestBase {

  use EntityReferenceFieldCreationTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['entity_test', 'file', 'user', 'field'];

  /**
   * The selection handler.
   *
   * @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
   */
  protected SelectionInterface $selectionHandler;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->installEntitySchema('entity_test');
    $this->installEntitySchema('file');
    $this->installEntitySchema('user');

    $field_name = $this->randomMachineName();
    $this->createEntityReferenceField('entity_test', 'entity_test', $field_name, $this->randomString(), 'file');
    $field_config = FieldConfig::loadByName('entity_test', 'entity_test', $field_name);
    $this->selectionHandler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field_config);
  }

  /**
   * Tests that entity reference should refer permanent files.
   */
  public function testCanReferToPermanentFiles(): void {
    $expected_result = [];
    \file_put_contents('public://file.png', str_repeat('a', 10));
    $file = File::create([
      'uri' => 'public://file.png',
      'filename' => 'file.png',
      'label' => $this->randomMachineName(),
      'status' => FileInterface::STATUS_PERMANENT,
    ]);
    $file->save();
    $expected_result[$file->id()] = Html::escape($file->label());

    // Assert the result.
    $result = $this->selectionHandler->getReferenceableEntities();
    self::assertEquals($expected_result, $result['file'], 'Permanent files can be referred by handler.');
  }

  /**
   * Tests that entity reference should avoid referring to temporary files.
   */
  public function testCanNotReferToTemporaryFiles(): void {
    \file_put_contents('public://test.png', str_repeat('b', 10));
    $file = File::create([
      'uri' => 'public://test.png',
      'filename' => 'test.png',
      'label' => $this->randomMachineName(),
    ]);
    $file->save();

    // Assert the result.
    $result = $this->selectionHandler->getReferenceableEntities();
    self::assertEmpty($result, 'Temporary files can not be referred by handler.');
  }

}