You are here

class FieldDefinitionListenerTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Field/FieldDefinitionListenerTest.php \Drupal\Tests\Core\Field\FieldDefinitionListenerTest
  2. 9 core/tests/Drupal/Tests/Core/Field/FieldDefinitionListenerTest.php \Drupal\Tests\Core\Field\FieldDefinitionListenerTest

@coversDefaultClass \Drupal\Core\Field\FieldDefinitionListener @group Field

Hierarchy

Expanded class hierarchy of FieldDefinitionListenerTest

File

core/tests/Drupal/Tests/Core/Field/FieldDefinitionListenerTest.php, line 23

Namespace

Drupal\Tests\Core\Field
View source
class FieldDefinitionListenerTest extends UnitTestCase {

  /**
   * The key-value factory.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $keyValueFactory;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $entityTypeManager;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $entityFieldManager;

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $cacheBackend;

  /**
   * The field definition listener under test.
   *
   * @var \Drupal\Core\Field\FieldDefinitionListener
   */
  protected $fieldDefinitionListener;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->keyValueFactory = $this
      ->prophesize(KeyValueFactoryInterface::class);
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->entityFieldManager = $this
      ->prophesize(EntityFieldManagerInterface::class);
    $this->cacheBackend = $this
      ->prophesize(CacheBackendInterface::class);
    $this->fieldDefinitionListener = new FieldDefinitionListener($this->entityTypeManager
      ->reveal(), $this->entityFieldManager
      ->reveal(), $this->keyValueFactory
      ->reveal(), $this->cacheBackend
      ->reveal());
  }

  /**
   * Sets up the entity type manager to be tested.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions
   *   (optional) An array of entity type definitions.
   */
  protected function setUpEntityTypeManager($definitions = []) {
    $class = $this
      ->getMockClass(EntityInterface::class);
    foreach ($definitions as $key => $entity_type) {

      // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called
      // by \Drupal\Core\Entity\EntityTypeManager::processDefinition() so it must
      // always be mocked.
      $entity_type
        ->getLinkTemplates()
        ->willReturn([]);

      // Give the entity type a legitimate class to return.
      $entity_type
        ->getClass()
        ->willReturn($class);
      $definitions[$key] = $entity_type
        ->reveal();
    }
    $this->entityTypeManager
      ->getDefinition(Argument::cetera())
      ->will(function ($args) use ($definitions) {
      $entity_type_id = $args[0];
      $exception_on_invalid = $args[1];
      if (isset($definitions[$entity_type_id])) {
        return $definitions[$entity_type_id];
      }
      elseif (!$exception_on_invalid) {
        return NULL;
      }
      else {
        throw new PluginNotFoundException($entity_type_id);
      }
    });
    $this->entityTypeManager
      ->getDefinitions()
      ->willReturn($definitions);
  }

  /**
   * @covers ::onFieldDefinitionCreate
   */
  public function testOnFieldDefinitionCreateNewField() {
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);
    $field_definition
      ->getTargetEntityTypeId()
      ->willReturn('test_entity_type');
    $field_definition
      ->getTargetBundle()
      ->willReturn('test_bundle');
    $field_definition
      ->getName()
      ->willReturn('test_field');
    $field_definition
      ->getType()
      ->willReturn('test_type');
    $storage = $this
      ->prophesize(DynamicallyFieldableEntityStorageInterface::class);
    $storage
      ->onFieldDefinitionCreate($field_definition
      ->reveal())
      ->shouldBeCalledTimes(1);
    $this->entityTypeManager
      ->getStorage('test_entity_type')
      ->willReturn($storage
      ->reveal());
    $entity = $this
      ->prophesize(EntityTypeInterface::class);
    $this
      ->setUpEntityTypeManager([
      'test_entity_type' => $entity,
    ]);

    // Set up the stored bundle field map.
    $key_value_store = $this
      ->prophesize(KeyValueStoreInterface::class);
    $this->keyValueFactory
      ->get('entity.definitions.bundle_field_map')
      ->willReturn($key_value_store
      ->reveal());
    $key_value_store
      ->get('test_entity_type')
      ->willReturn([]);
    $key_value_store
      ->set('test_entity_type', [
      'test_field' => [
        'type' => 'test_type',
        'bundles' => [
          'test_bundle' => 'test_bundle',
        ],
      ],
    ])
      ->shouldBeCalled();
    $this->fieldDefinitionListener
      ->onFieldDefinitionCreate($field_definition
      ->reveal());
  }

  /**
   * @covers ::onFieldDefinitionCreate
   */
  public function testOnFieldDefinitionCreateExistingField() {
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);
    $field_definition
      ->getTargetEntityTypeId()
      ->willReturn('test_entity_type');
    $field_definition
      ->getTargetBundle()
      ->willReturn('test_bundle');
    $field_definition
      ->getName()
      ->willReturn('test_field');
    $storage = $this
      ->prophesize(DynamicallyFieldableEntityStorageInterface::class);
    $storage
      ->onFieldDefinitionCreate($field_definition
      ->reveal())
      ->shouldBeCalledTimes(1);
    $this->entityTypeManager
      ->getStorage('test_entity_type')
      ->willReturn($storage
      ->reveal());
    $entity = $this
      ->prophesize(EntityTypeInterface::class);
    $this
      ->setUpEntityTypeManager([
      'test_entity_type' => $entity,
    ]);

    // Set up the stored bundle field map.
    $key_value_store = $this
      ->prophesize(KeyValueStoreInterface::class);
    $this->keyValueFactory
      ->get('entity.definitions.bundle_field_map')
      ->willReturn($key_value_store
      ->reveal());
    $key_value_store
      ->get('test_entity_type')
      ->willReturn([
      'test_field' => [
        'type' => 'test_type',
        'bundles' => [
          'existing_bundle' => 'existing_bundle',
        ],
      ],
    ]);
    $key_value_store
      ->set('test_entity_type', [
      'test_field' => [
        'type' => 'test_type',
        'bundles' => [
          'existing_bundle' => 'existing_bundle',
          'test_bundle' => 'test_bundle',
        ],
      ],
    ])
      ->shouldBeCalled();
    $this->fieldDefinitionListener
      ->onFieldDefinitionCreate($field_definition
      ->reveal());
  }

  /**
   * @covers ::onFieldDefinitionUpdate
   */
  public function testOnFieldDefinitionUpdate() {
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);
    $field_definition
      ->getTargetEntityTypeId()
      ->willReturn('test_entity_type');
    $storage = $this
      ->prophesize(DynamicallyFieldableEntityStorageInterface::class);
    $storage
      ->onFieldDefinitionUpdate($field_definition
      ->reveal(), $field_definition
      ->reveal())
      ->shouldBeCalledTimes(1);
    $this->entityTypeManager
      ->getStorage('test_entity_type')
      ->willReturn($storage
      ->reveal());
    $entity = $this
      ->prophesize(EntityTypeInterface::class);
    $this
      ->setUpEntityTypeManager([
      'test_entity_type' => $entity,
    ]);
    $this->fieldDefinitionListener
      ->onFieldDefinitionUpdate($field_definition
      ->reveal(), $field_definition
      ->reveal());
  }

  /**
   * @covers ::onFieldDefinitionDelete
   */
  public function testOnFieldDefinitionDeleteMultipleBundles() {
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);
    $field_definition
      ->getTargetEntityTypeId()
      ->willReturn('test_entity_type');
    $field_definition
      ->getTargetBundle()
      ->willReturn('test_bundle');
    $field_definition
      ->getName()
      ->willReturn('test_field');
    $storage = $this
      ->prophesize(DynamicallyFieldableEntityStorageInterface::class);
    $storage
      ->onFieldDefinitionDelete($field_definition
      ->reveal())
      ->shouldBeCalledTimes(1);
    $this->entityTypeManager
      ->getStorage('test_entity_type')
      ->willReturn($storage
      ->reveal());
    $entity = $this
      ->prophesize(EntityTypeInterface::class);
    $this
      ->setUpEntityTypeManager([
      'test_entity_type' => $entity,
    ]);

    // Set up the stored bundle field map.
    $key_value_store = $this
      ->prophesize(KeyValueStoreInterface::class);
    $this->keyValueFactory
      ->get('entity.definitions.bundle_field_map')
      ->willReturn($key_value_store
      ->reveal());
    $key_value_store
      ->get('test_entity_type')
      ->willReturn([
      'test_field' => [
        'type' => 'test_type',
        'bundles' => [
          'test_bundle' => 'test_bundle',
        ],
      ],
      'second_field' => [
        'type' => 'test_type',
        'bundles' => [
          'test_bundle' => 'test_bundle',
        ],
      ],
    ]);
    $key_value_store
      ->set('test_entity_type', [
      'second_field' => [
        'type' => 'test_type',
        'bundles' => [
          'test_bundle' => 'test_bundle',
        ],
      ],
    ])
      ->shouldBeCalled();
    $this->fieldDefinitionListener
      ->onFieldDefinitionDelete($field_definition
      ->reveal());
  }

  /**
   * @covers ::onFieldDefinitionDelete
   */
  public function testOnFieldDefinitionDeleteSingleBundles() {
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);
    $field_definition
      ->getTargetEntityTypeId()
      ->willReturn('test_entity_type');
    $field_definition
      ->getTargetBundle()
      ->willReturn('test_bundle');
    $field_definition
      ->getName()
      ->willReturn('test_field');
    $storage = $this
      ->prophesize(DynamicallyFieldableEntityStorageInterface::class);
    $storage
      ->onFieldDefinitionDelete($field_definition
      ->reveal())
      ->shouldBeCalledTimes(1);
    $this->entityTypeManager
      ->getStorage('test_entity_type')
      ->willReturn($storage
      ->reveal());
    $entity = $this
      ->prophesize(EntityTypeInterface::class);
    $this
      ->setUpEntityTypeManager([
      'test_entity_type' => $entity,
    ]);

    // Set up the stored bundle field map.
    $key_value_store = $this
      ->prophesize(KeyValueStoreInterface::class);
    $this->keyValueFactory
      ->get('entity.definitions.bundle_field_map')
      ->willReturn($key_value_store
      ->reveal());
    $key_value_store
      ->get('test_entity_type')
      ->willReturn([
      'test_field' => [
        'type' => 'test_type',
        'bundles' => [
          'test_bundle' => 'test_bundle',
          'second_bundle' => 'second_bundle',
        ],
      ],
    ]);
    $key_value_store
      ->set('test_entity_type', [
      'test_field' => [
        'type' => 'test_type',
        'bundles' => [
          'second_bundle' => 'second_bundle',
        ],
      ],
    ])
      ->shouldBeCalled();
    $this->fieldDefinitionListener
      ->onFieldDefinitionDelete($field_definition
      ->reveal());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldDefinitionListenerTest::$cacheBackend protected property The cache backend.
FieldDefinitionListenerTest::$entityFieldManager protected property The entity field manager.
FieldDefinitionListenerTest::$entityTypeManager protected property The entity type manager.
FieldDefinitionListenerTest::$fieldDefinitionListener protected property The field definition listener under test.
FieldDefinitionListenerTest::$keyValueFactory protected property The key-value factory.
FieldDefinitionListenerTest::setUp protected function Overrides UnitTestCase::setUp
FieldDefinitionListenerTest::setUpEntityTypeManager protected function Sets up the entity type manager to be tested.
FieldDefinitionListenerTest::testOnFieldDefinitionCreateExistingField public function @covers ::onFieldDefinitionCreate
FieldDefinitionListenerTest::testOnFieldDefinitionCreateNewField public function @covers ::onFieldDefinitionCreate
FieldDefinitionListenerTest::testOnFieldDefinitionDeleteMultipleBundles public function @covers ::onFieldDefinitionDelete
FieldDefinitionListenerTest::testOnFieldDefinitionDeleteSingleBundles public function @covers ::onFieldDefinitionDelete
FieldDefinitionListenerTest::testOnFieldDefinitionUpdate public function @covers ::onFieldDefinitionUpdate
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function