You are here

trait EntityDefinitionTestTrait in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityDefinitionTestTrait.php \Drupal\system\Tests\Entity\EntityDefinitionTestTrait

Provides some test methods used to update existing entity definitions.

Hierarchy

1 file declares its use of EntityDefinitionTestTrait
ViewsEntitySchemaSubscriberIntegrationTest.php in core/modules/views/src/Tests/EventSubscriber/ViewsEntitySchemaSubscriberIntegrationTest.php
Contains \Drupal\views\Tests\EventSubscriber\ViewsEntitySchemaSubscriberIntegrationTest.

File

core/modules/system/src/Tests/Entity/EntityDefinitionTestTrait.php, line 16
Contains \Drupal\system\Tests\Entity\EntityDefinitionTestTrait.

Namespace

Drupal\system\Tests\Entity
View source
trait EntityDefinitionTestTrait {

  /**
   * Enables a new entity type definition.
   */
  protected function enableNewEntityType() {
    $this->state
      ->set('entity_test_new', TRUE);
    $this->entityManager
      ->clearCachedDefinitions();
    $this->entityDefinitionUpdateManager
      ->applyUpdates();
  }

  /**
   * Resets the entity type definition.
   */
  protected function resetEntityType() {
    $this->state
      ->set('entity_test_update.entity_type', NULL);
    $this->entityManager
      ->clearCachedDefinitions();
    $this->entityDefinitionUpdateManager
      ->applyUpdates();
  }

  /**
   * Updates the 'entity_test_update' entity type to revisionable.
   */
  protected function updateEntityTypeToRevisionable() {
    $entity_type = clone $this->entityManager
      ->getDefinition('entity_test_update');
    $keys = $entity_type
      ->getKeys();
    $keys['revision'] = 'revision_id';
    $entity_type
      ->set('entity_keys', $keys);
    $this->state
      ->set('entity_test_update.entity_type', $entity_type);
  }

  /**
   * Updates the 'entity_test_update' entity type not revisionable.
   */
  protected function updateEntityTypeToNotRevisionable() {
    $entity_type = clone $this->entityManager
      ->getDefinition('entity_test_update');
    $keys = $entity_type
      ->getKeys();
    unset($keys['revision']);
    $entity_type
      ->set('entity_keys', $keys);
    $this->state
      ->set('entity_test_update.entity_type', $entity_type);
  }

  /**
   * Updates the 'entity_test_update' entity type to translatable.
   */
  protected function updateEntityTypeToTranslatable() {
    $entity_type = clone $this->entityManager
      ->getDefinition('entity_test_update');
    $entity_type
      ->set('translatable', TRUE);
    $entity_type
      ->set('data_table', 'entity_test_update_data');
    if ($entity_type
      ->isRevisionable()) {
      $entity_type
        ->set('revision_data_table', 'entity_test_update_revision_data');
    }
    $this->state
      ->set('entity_test_update.entity_type', $entity_type);
  }

  /**
   * Updates the 'entity_test_update' entity type to not translatable.
   */
  protected function updateEntityTypeToNotTranslatable() {
    $entity_type = clone $this->entityManager
      ->getDefinition('entity_test_update');
    $entity_type
      ->set('translatable', FALSE);
    $entity_type
      ->set('data_table', NULL);
    if ($entity_type
      ->isRevisionable()) {
      $entity_type
        ->set('revision_data_table', NULL);
    }
    $this->state
      ->set('entity_test_update.entity_type', $entity_type);
  }

  /**
   * Adds a new base field to the 'entity_test_update' entity type.
   *
   * @param string $type
   *   (optional) The field type for the new field. Defaults to 'string'.
   */
  protected function addBaseField($type = 'string') {
    $definitions['new_base_field'] = BaseFieldDefinition::create($type)
      ->setName('new_base_field')
      ->setLabel(t('A new base field'));
    $this->state
      ->set('entity_test_update.additional_base_field_definitions', $definitions);
  }

  /**
   * Adds a long-named base field to the 'entity_test_update' entity type.
   */
  protected function addLongNameBaseField() {
    $key = 'entity_test_update.additional_base_field_definitions';
    $definitions = $this->state
      ->get($key, []);
    $definitions['new_long_named_entity_reference_base_field'] = BaseFieldDefinition::create('entity_reference')
      ->setName('new_long_named_entity_reference_base_field')
      ->setLabel(t('A new long-named base field'))
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default');
    $this->state
      ->set($key, $definitions);
  }

  /**
   * Adds a new revisionable base field to the 'entity_test_update' entity type.
   *
   * @param string $type
   *   (optional) The field type for the new field. Defaults to 'string'.
   */
  protected function addRevisionableBaseField($type = 'string') {
    $definitions['new_base_field'] = BaseFieldDefinition::create($type)
      ->setName('new_base_field')
      ->setLabel(t('A new revisionable base field'))
      ->setRevisionable(TRUE);
    $this->state
      ->set('entity_test_update.additional_base_field_definitions', $definitions);
  }

  /**
   * Modifies the new base field from 'string' to 'text'.
   */
  protected function modifyBaseField() {
    $this
      ->addBaseField('text');
  }

  /**
   * Promotes a field to an entity key.
   */
  protected function makeBaseFieldEntityKey() {
    $entity_type = clone $this->entityManager
      ->getDefinition('entity_test_update');
    $entity_keys = $entity_type
      ->getKeys();
    $entity_keys['new_base_field'] = 'new_base_field';
    $entity_type
      ->set('entity_keys', $entity_keys);
    $this->state
      ->set('entity_test_update.entity_type', $entity_type);
  }

  /**
   * Removes the new base field from the 'entity_test_update' entity type.
   */
  protected function removeBaseField() {
    $this->state
      ->delete('entity_test_update.additional_base_field_definitions');
  }

  /**
   * Adds a single-field index to the base field.
   */
  protected function addBaseFieldIndex() {
    $this->state
      ->set('entity_test_update.additional_field_index.entity_test_update.new_base_field', TRUE);
  }

  /**
   * Removes the index added in addBaseFieldIndex().
   */
  protected function removeBaseFieldIndex() {
    $this->state
      ->delete('entity_test_update.additional_field_index.entity_test_update.new_base_field');
  }

  /**
   * Adds a new bundle field to the 'entity_test_update' entity type.
   *
   * @param string $type
   *   (optional) The field type for the new field. Defaults to 'string'.
   */
  protected function addBundleField($type = 'string') {
    $definitions['new_bundle_field'] = FieldStorageDefinition::create($type)
      ->setName('new_bundle_field')
      ->setLabel(t('A new bundle field'))
      ->setTargetEntityTypeId('entity_test_update');
    $this->state
      ->set('entity_test_update.additional_field_storage_definitions', $definitions);
    $this->state
      ->set('entity_test_update.additional_bundle_field_definitions.test_bundle', $definitions);
  }

  /**
   * Modifies the new bundle field from 'string' to 'text'.
   */
  protected function modifyBundleField() {
    $this
      ->addBundleField('text');
  }

  /**
   * Removes the new bundle field from the 'entity_test_update' entity type.
   */
  protected function removeBundleField() {
    $this->state
      ->delete('entity_test_update.additional_field_storage_definitions');
    $this->state
      ->delete('entity_test_update.additional_bundle_field_definitions.test_bundle');
  }

  /**
   * Adds an index to the 'entity_test_update' entity type's base table.
   *
   * @see \Drupal\entity_test\EntityTestStorageSchema::getEntitySchema().
   */
  protected function addEntityIndex() {
    $indexes = array(
      'entity_test_update__new_index' => array(
        'name',
        'user_id',
      ),
    );
    $this->state
      ->set('entity_test_update.additional_entity_indexes', $indexes);
  }

  /**
   * Removes the index added in addEntityIndex().
   */
  protected function removeEntityIndex() {
    $this->state
      ->delete('entity_test_update.additional_entity_indexes');
  }

  /**
   * Renames the base table to 'entity_test_update_new'.
   */
  protected function renameBaseTable() {
    $entity_type = clone $this->entityManager
      ->getDefinition('entity_test_update');
    $entity_type
      ->set('base_table', 'entity_test_update_new');
    $this->state
      ->set('entity_test_update.entity_type', $entity_type);
  }

  /**
   * Renames the data table to 'entity_test_update_data_new'.
   */
  protected function renameDataTable() {
    $entity_type = clone $this->entityManager
      ->getDefinition('entity_test_update');
    $entity_type
      ->set('data_table', 'entity_test_update_data_new');
    $this->state
      ->set('entity_test_update.entity_type', $entity_type);
  }

  /**
   * Renames the revision table to 'entity_test_update_revision_new'.
   */
  protected function renameRevisionBaseTable() {
    $entity_type = clone $this->entityManager
      ->getDefinition('entity_test_update');
    $entity_type
      ->set('revision_table', 'entity_test_update_revision_new');
    $this->state
      ->set('entity_test_update.entity_type', $entity_type);
  }

  /**
   * Renames the revision data table to 'entity_test_update_revision_data_new'.
   */
  protected function renameRevisionDataTable() {
    $entity_type = clone $this->entityManager
      ->getDefinition('entity_test_update');
    $entity_type
      ->set('revision_data_table', 'entity_test_update_revision_data_new');
    $this->state
      ->set('entity_test_update.entity_type', $entity_type);
  }

  /**
   * Removes the entity type.
   */
  protected function deleteEntityType() {
    $this->state
      ->set('entity_test_update.entity_type', 'null');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityDefinitionTestTrait::addBaseField protected function Adds a new base field to the 'entity_test_update' entity type.
EntityDefinitionTestTrait::addBaseFieldIndex protected function Adds a single-field index to the base field.
EntityDefinitionTestTrait::addBundleField protected function Adds a new bundle field to the 'entity_test_update' entity type.
EntityDefinitionTestTrait::addEntityIndex protected function Adds an index to the 'entity_test_update' entity type's base table.
EntityDefinitionTestTrait::addLongNameBaseField protected function Adds a long-named base field to the 'entity_test_update' entity type.
EntityDefinitionTestTrait::addRevisionableBaseField protected function Adds a new revisionable base field to the 'entity_test_update' entity type.
EntityDefinitionTestTrait::deleteEntityType protected function Removes the entity type.
EntityDefinitionTestTrait::enableNewEntityType protected function Enables a new entity type definition.
EntityDefinitionTestTrait::makeBaseFieldEntityKey protected function Promotes a field to an entity key.
EntityDefinitionTestTrait::modifyBaseField protected function Modifies the new base field from 'string' to 'text'.
EntityDefinitionTestTrait::modifyBundleField protected function Modifies the new bundle field from 'string' to 'text'.
EntityDefinitionTestTrait::removeBaseField protected function Removes the new base field from the 'entity_test_update' entity type.
EntityDefinitionTestTrait::removeBaseFieldIndex protected function Removes the index added in addBaseFieldIndex().
EntityDefinitionTestTrait::removeBundleField protected function Removes the new bundle field from the 'entity_test_update' entity type.
EntityDefinitionTestTrait::removeEntityIndex protected function Removes the index added in addEntityIndex().
EntityDefinitionTestTrait::renameBaseTable protected function Renames the base table to 'entity_test_update_new'.
EntityDefinitionTestTrait::renameDataTable protected function Renames the data table to 'entity_test_update_data_new'.
EntityDefinitionTestTrait::renameRevisionBaseTable protected function Renames the revision table to 'entity_test_update_revision_new'.
EntityDefinitionTestTrait::renameRevisionDataTable protected function Renames the revision data table to 'entity_test_update_revision_data_new'.
EntityDefinitionTestTrait::resetEntityType protected function Resets the entity type definition.
EntityDefinitionTestTrait::updateEntityTypeToNotRevisionable protected function Updates the 'entity_test_update' entity type not revisionable.
EntityDefinitionTestTrait::updateEntityTypeToNotTranslatable protected function Updates the 'entity_test_update' entity type to not translatable.
EntityDefinitionTestTrait::updateEntityTypeToRevisionable protected function Updates the 'entity_test_update' entity type to revisionable.
EntityDefinitionTestTrait::updateEntityTypeToTranslatable protected function Updates the 'entity_test_update' entity type to translatable.