Commit 4d92ef40 authored by catch's avatar catch

Issue #2856808 by amateescu, catch: Break out the 'entity_test_update' entity...

Issue #2856808 by amateescu, catch: Break out the 'entity_test_update' entity type into its own module and add additional test db dumps
parent b4095524
......@@ -37,6 +37,7 @@ protected function updateEntityTypeToRevisionable() {
$keys = $entity_type->getKeys();
$keys['revision'] = 'revision_id';
$entity_type->set('entity_keys', $keys);
$entity_type->set('revision_table', 'entity_test_update_revision');
$this->state->set('entity_test_update.entity_type', $entity_type);
}
......@@ -50,6 +51,7 @@ protected function updateEntityTypeToNotRevisionable() {
$keys = $entity_type->getKeys();
unset($keys['revision']);
$entity_type->set('entity_keys', $keys);
$entity_type->set('revision_table', NULL);
$this->state->set('entity_test_update.entity_type', $entity_type);
}
......@@ -86,6 +88,24 @@ protected function updateEntityTypeToNotTranslatable() {
$this->state->set('entity_test_update.entity_type', $entity_type);
}
/**
* Updates the 'entity_test_update' entity type to revisionable and
* translatable.
*/
protected function updateEntityTypeToRevisionableAndTranslatable() {
$entity_type = clone $this->entityManager->getDefinition('entity_test_update');
$keys = $entity_type->getKeys();
$keys['revision'] = 'revision_id';
$entity_type->set('entity_keys', $keys);
$entity_type->set('translatable', TRUE);
$entity_type->set('data_table', 'entity_test_update_data');
$entity_type->set('revision_table', 'entity_test_update_revision');
$entity_type->set('revision_data_table', 'entity_test_update_revision_data');
$this->state->set('entity_test_update.entity_type', $entity_type);
}
/**
* Adds a new base field to the 'entity_test_update' entity type.
*
......@@ -203,7 +223,7 @@ protected function removeBundleField() {
*/
protected function addEntityIndex() {
$indexes = [
'entity_test_update__new_index' => ['name', 'user_id'],
'entity_test_update__new_index' => ['name', 'test_single_property'],
];
$this->state->set('entity_test_update.additional_entity_indexes', $indexes);
}
......
......@@ -93,9 +93,6 @@ function entity_test_entity_type_alter(array &$entity_types) {
}
}
// Allow entity_test_update tests to override the entity type definition.
$entity_types['entity_test_update'] = $state->get('entity_test_update.entity_type', $entity_types['entity_test_update']);
// Allow entity_test_with_bundle tests to override the entity type definition.
$entity_types['entity_test_with_bundle'] = $state->get('entity_test_with_bundle.entity_type', $entity_types['entity_test_with_bundle']);
......
langcode: en
status: true
dependencies:
config:
- field.storage.entity_test_update.field_test_configurable_field
module:
- entity_test_update
id: entity_test_update.entity_test_update.field_test_configurable_field
field_name: field_test_configurable_field
entity_type: entity_test_update
bundle: entity_test_update
label: 'Test configurable field'
description: ''
required: false
translatable: true
default_value:
-
value1: { }
default_value_callback: ''
settings: { }
field_type: multi_value_test
langcode: en
status: true
dependencies:
module:
- entity_test_update
id: entity_test_update.field_test_configurable_field
field_name: field_test_configurable_field
entity_type: entity_test_update
type: multi_value_test
settings: { }
module: entity_test_update
locked: false
cardinality: -1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
field.value.multi_value_test:
type: mapping
label: 'Default value'
mapping:
value1:
type: sequence
sequence:
type: string
label: 'First value'
name: 'Entity Update Test'
type: module
description: 'Provides an entity type for testing definition and schema updates.'
package: Testing
version: VERSION
core: 8.x
<?php
/**
* @file
* Provides an entity type for testing definition and schema updates.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity_test_update\Entity\EntityTestUpdate;
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Implements hook_entity_base_field_info().
*/
function entity_test_update_entity_base_field_info(EntityTypeInterface $entity_type) {
// Add a base field that will be used to test that fields added through
// hook_entity_base_field_info() are handled correctly during a schema
// conversion (e.g. from non-revisionable to revisionable).
if ($entity_type->id() == 'entity_test_update') {
$fields = [];
$fields['test_entity_base_field_info'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Field added by hook_entity_base_field_info()'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
return $fields;
}
}
/**
* Implements hook_entity_type_alter().
*/
function entity_test_update_entity_type_alter(array &$entity_types) {
// Allow entity_test_update tests to override the entity type definition.
$entity_types['entity_test_update'] = \Drupal::state()->get('entity_test_update.entity_type', $entity_types['entity_test_update']);
}
/**
* Implements hook_entity_presave().
*/
function entity_test_update_entity_presave(EntityInterface $entity) {
// Simulate an error during the save process of a test entity.
if ($entity->getEntityTypeId() === 'entity_test_update' && \Drupal::state()->get('entity_test_update.throw_exception', FALSE)) {
throw new \Exception('Peekaboo!');
}
}
/**
* Creates a given number of 'entity_test_update' entities.
*
* The 'drupal-8.0.0-rc1-filled.standard.entity_test_update.php.gz' db dump was
* created with the following characteristics:
* - Drupal 8.0.0-rc1 installed with the Standard profile;
* - The 'entity_test_update' module enabled;
* - 102 test entities created and saved.
*
* The 'drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz' db dump
* was created with the following characteristics:
* - Drupal 8.0.0-rc1 installed with the Standard profile;
* - Manually edited the annotation of the entity_test_update entity type in
* order to make it translatable;
* - The entity_test_update and Language module enabled;
* - Romanian language added;
* - 50 test entities created, translated and saved;
* - The Content Translation module enabled and configured for our test entity
* type. This was enabled after the first 50 entities were created in order
* to have NULL values for its translation metadata fields
* (e.g. content_translation_status);
* - 52 more test entities (with the IDs 51 - 102) crated, translated and saved.
*
* @param int $start
* (optional) The entity ID to start from. Defaults to 1.
* @param int $end
* (optional) The entity ID to end with. Defaults to 50.
* @param bool $add_translation
* (optional) Whether to create a translation for each entity. If the number
* of entities is greater than 50, the default translation (en) of the 51st
* entity and the 52nd translation (ro) are disabled through the
* 'content_moderation_status' field. Defaults to FALSE.
*
* @see drupal-8.0.0-rc1-filled.standard.entity_test_update.php.gz
* @see drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz
*/
function _entity_test_update_create_test_entities($start = 1, $end = 50, $add_translation = FALSE) {
for ($i = $start; $i <= $end; $i++) {
$entity = EntityTestUpdate::create([
'id' => $i,
'name' => $i,
'test_single_property' => $i . ' - test single property',
'test_multiple_properties' => [[
'value1' => $i . ' - test multiple properties - value1',
'value2' => $i . ' - test multiple properties - value2',
]],
'test_single_property_multiple_values' => [
['value' => $i . ' - test single property multiple values 0'],
['value' => $i . ' - test single property multiple values 1']
],
'test_multiple_properties_multiple_values' => [
[
'value1' => $i . ' - test multiple properties multiple values - value1 0',
'value2' => $i . ' - test multiple properties multiple values - value2 0',
],
[
'value1' => $i . ' - test multiple properties multiple values - value1 1',
'value2' => $i . ' - test multiple properties multiple values - value2 1',
]
],
'test_non_rev_field' => $i . ' - test non-revisionable field',
'test_non_mul_field' => $i . ' - test non-translatable field',
'test_non_mulrev_field' => $i . ' - test non-translatable and non-revisionable field',
'field_test_configurable_field' => [
[
'value1' => $i . ' - field test configurable field - value1 0',
'value2' => $i . ' - field test configurable field - value2 0',
],
[
'value1' => $i . ' - field test configurable field - value1 1',
'value2' => $i . ' - field test configurable field - value2 1',
]
],
'test_entity_base_field_info' => $i . ' - test entity base field info',
]);
if ($add_translation) {
$entity->addTranslation('ro', [
'name' => $i . ' - ro',
'test_single_property' => $i . ' - test single property - ro',
'test_multiple_properties' => [[
'value1' => $i . ' - test multiple properties - value1 - ro',
'value2' => $i . ' - test multiple properties - value2 - ro',
]],
'test_single_property_multiple_values' => [
['value' => $i . ' - test single property multiple values 0 - ro'],
['value' => $i . ' - test single property multiple values 1 - ro']
],
'test_multiple_properties_multiple_values' => [
[
'value1' => $i . ' - test multiple properties multiple values - value1 0 - ro',
'value2' => $i . ' - test multiple properties multiple values - value2 0 - ro',
],
[
'value1' => $i . ' - test multiple properties multiple values - value1 1 - ro',
'value2' => $i . ' - test multiple properties multiple values - value2 1 - ro',
]
],
'test_non_rev_field' => $i . ' - test non-revisionable field - ro',
'field_test_configurable_field' => [
[
'value1' => $i . ' - field test configurable field - value1 0 - ro',
'value2' => $i . ' - field test configurable field - value2 0 - ro',
],
[
'value1' => $i . ' - field test configurable field - value1 1 - ro',
'value2' => $i . ' - field test configurable field - value2 1 - ro',
]
],
'test_entity_base_field_info' => $i . ' - test entity base field info - ro',
]);
if ($i == 101) {
$entity->getTranslation('en')->set('content_translation_status', FALSE);
}
if ($i == 102) {
$entity->getTranslation('ro')->set('content_translation_status', FALSE);
}
}
$entity->save();
}
}
<?php
namespace Drupal\entity_test_update\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Defines the test entity class for testing definition and schema updates.
*
* This entity type starts out non-revisionable and non-translatable, but during
* an update test it can be made revisionable and translatable using the helper
* methods from \Drupal\system\Tests\Entity\EntityDefinitionTestTrait.
*
* @ContentEntityType(
* id = "entity_test_update",
* label = @Translation("Test entity update"),
* handlers = {
* "storage_schema" = "Drupal\entity_test_update\EntityTestUpdateStorageSchema",
* },
* base_table = "entity_test_update",
* persistent_cache = FALSE,
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type",
* "label" = "name",
* "langcode" = "langcode",
* },
* content_translation_ui_skip = TRUE,
* )
*/
class EntityTestUpdate extends ContentEntityBase {
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage, array &$values) {
parent::preCreate($storage, $values);
if (empty($values['type'])) {
$values['type'] = $storage->getEntityTypeId();
}
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
// This entity type is used for generating database dumps from Drupal
// 8.0.0-rc1, which didn't have the entity key base fields defined in
// the parent class (ContentEntityBase), so we have to duplicate them here.
$fields[$entity_type->getKey('id')] = BaseFieldDefinition::create('integer')
->setLabel(new TranslatableMarkup('ID'))
->setDescription(new TranslatableMarkup('The ID of the test entity.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields[$entity_type->getKey('uuid')] = BaseFieldDefinition::create('uuid')
->setLabel(new TranslatableMarkup('UUID'))
->setDescription(new TranslatableMarkup('The UUID of the test entity.'))
->setReadOnly(TRUE);
$fields[$entity_type->getKey('bundle')] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Type'))
->setDescription(new TranslatableMarkup('The bundle of the test entity.'))
->setRequired(TRUE);
if ($entity_type->hasKey('revision')) {
$fields[$entity_type->getKey('revision')] = BaseFieldDefinition::create('integer')
->setLabel(new TranslatableMarkup('Revision ID'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
}
$fields[$entity_type->getKey('langcode')] = BaseFieldDefinition::create('language')
->setLabel(new TranslatableMarkup('Language'));
if ($entity_type->isRevisionable()) {
$fields[$entity_type->getKey('langcode')]->setRevisionable(TRUE);
}
if ($entity_type->isTranslatable()) {
$fields[$entity_type->getKey('langcode')]->setTranslatable(TRUE);
}
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Name'))
->setDescription(new TranslatableMarkup('The name of the test entity.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setSetting('max_length', 32)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
]);
$fields['test_single_property'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Field with a single property'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['test_multiple_properties'] = BaseFieldDefinition::create('multi_value_test')
->setLabel(new TranslatableMarkup('Field with multiple properties'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['test_single_property_multiple_values'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Field with a single property and multiple values'))
->setCardinality(2)
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['test_multiple_properties_multiple_values'] = BaseFieldDefinition::create('multi_value_test')
->setLabel(new TranslatableMarkup('Field with multiple properties and multiple values'))
->setCardinality(2)
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['test_non_rev_field'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Non Revisionable Field'))
->setDescription(new TranslatableMarkup('A non-revisionable test field.'))
->setCardinality(1)
->setRevisionable(FALSE)
->setTranslatable(TRUE);
$fields['test_non_mul_field'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Non Translatable Field'))
->setDescription(new TranslatableMarkup('A non-translatable test field.'))
->setCardinality(1)
->setRevisionable(TRUE)
->setTranslatable(FALSE);
$fields['test_non_mulrev_field'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Non Revisionable and Translatable Field'))
->setDescription(new TranslatableMarkup('A non-revisionable and non-translatable test field.'))
->setCardinality(1)
->setRevisionable(FALSE)
->setTranslatable(FALSE);
$fields += \Drupal::state()->get('entity_test_update.additional_base_field_definitions', []);
return $fields;
}
/**
* {@inheritdoc}
*/
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
$fields = parent::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);
$fields += \Drupal::state()->get('entity_test_update.additional_bundle_field_definitions.' . $bundle, []);
return $fields;
}
}
<?php
namespace Drupal\entity_test;
namespace Drupal\entity_test_update;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
......@@ -9,7 +9,7 @@
/**
* Defines the entity_test_update storage_schema handler.
*/
class EntityTestStorageSchema extends SqlContentEntityStorageSchema {
class EntityTestUpdateStorageSchema extends SqlContentEntityStorageSchema {
/**
* {@inheritdoc}
......
<?php
namespace Drupal\entity_test_update\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\FieldItemBase;
/**
* Defines the 'multi_value_test' field type.
*
* @FieldType(
* id = "multi_value_test",
* label = @Translation("Multiple values test"),
* description = @Translation("Another dummy field type."),
* )
*/
class MultiValueTestItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value1'] = DataDefinition::create('string')
->setLabel(t('First value'));
$properties['value2'] = DataDefinition::create('string')
->setLabel(t('Second value'));
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value1' => [
'type' => 'varchar',
'length' => 64,
],
'value2' => [
'type' => 'varchar',
'length' => 64,
],
],
];
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$item = $this->getValue();
return empty($item['value1']) && empty($item['value2']);
}
/**
* {@inheritdoc}
*/
public static function mainPropertyName() {
return 'value1';
}
}
......@@ -26,7 +26,7 @@ class ViewsEntitySchemaSubscriberIntegrationTest extends ViewsKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['entity_test', 'user', 'text'];
public static $modules = ['entity_test', 'entity_test_update', 'user', 'text'];
/**
* Views used by this test.
......@@ -90,6 +90,10 @@ protected function setUp($import_test_views = TRUE) {
public function testDeleteEntityType() {
$entity_storage = $this->entityManager->getStorage('view');
// Make the test entity type revisionable.
$this->updateEntityTypeToRevisionable();
$this->entityDefinitionUpdateManager->applyUpdates();
$views = $entity_storage->loadMultiple();
// Ensure that all test views exists.
......
......@@ -13,7 +13,7 @@
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionEvents;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTestUpdate;
use Drupal\entity_test_update\Entity\EntityTestUpdate;
use Drupal\system\Tests\Entity\EntityDefinitionTestTrait;
/**
......@@ -39,6 +39,13 @@ class EntityDefinitionUpdateTest extends EntityKernelTestBase {
*/
protected $database;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['entity_test_update'];
/**
* {@inheritdoc}
*/
......
......@@ -59,7 +59,7 @@ class EntityReferenceFieldTest extends EntityKernelTestBase {
*
* @var array
*/
public static $modules = ['entity_reference_test'];
public static $modules = ['entity_reference_test', 'entity_test_update'];
/**
* {@inheritdoc}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment