/**
  * ::onEntityTypeUpdate
  */
 public function testonEntityTypeUpdateWithNewIndex()
 {
     $this->entityType = $original_entity_type = new ContentEntityType(array('id' => 'entity_test', 'entity_keys' => array('id' => 'id')));
     // Add a field with a really long index.
     $this->setUpStorageDefinition('long_index_name', array('columns' => array('long_index_name' => array('type' => 'int')), 'indexes' => array('long_index_name_really_long_long_name' => array(array('long_index_name', 10)))));
     $expected = array('entity_test' => array('description' => 'The base table for entity_test entities.', 'fields' => array('id' => array('type' => 'serial', 'not null' => TRUE), 'long_index_name' => array('type' => 'int', 'not null' => FALSE)), 'indexes' => array('entity_test__b588603cb9' => array(array('long_index_name', 10)))));
     $this->setUpStorageSchema($expected);
     $table_mapping = new DefaultTableMapping($this->entityType, $this->storageDefinitions);
     $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions));
     $table_mapping->setExtraColumns('entity_test', array('default_langcode'));
     $this->storage->expects($this->any())->method('getTableMapping')->will($this->returnValue($table_mapping));
     $this->storageSchema->expects($this->any())->method('loadEntitySchemaData')->willReturn(['entity_test' => ['indexes' => ['entity_test__b588603cb9' => ['longer_index_name'], 'entity_test__removed_field' => ['removed_field']]]]);
     // The original indexes should be dropped before the new one is added.
     $this->dbSchemaHandler->expects($this->at(0))->method('dropIndex')->with('entity_test', 'entity_test__b588603cb9');
     $this->dbSchemaHandler->expects($this->at(1))->method('dropIndex')->with('entity_test', 'entity_test__removed_field');
     $this->dbSchemaHandler->expects($this->atLeastOnce())->method('fieldExists')->willReturn(TRUE);
     $this->dbSchemaHandler->expects($this->atLeastOnce())->method('addIndex')->with('entity_test', 'entity_test__b588603cb9', [['long_index_name', 10]], $this->callback(function ($actual_value) use($expected) {
         $this->assertEquals($expected['entity_test']['indexes'], $actual_value['indexes']);
         $this->assertEquals($expected['entity_test']['fields'], $actual_value['fields']);
         // If the parameters don't match, the assertions above will throw an
         // exception.
         return TRUE;
     }));
     $this->assertNull($this->storageSchema->onEntityTypeUpdate($this->entityType, $original_entity_type));
 }
 /**
  * Tests the schema for a field dedicated table for an entity with a string identifier.
  *
  * @covers ::getDedicatedTableSchema()
  * @covers ::createDedicatedTableSchema()
  */
 public function testDedicatedTableSchemaForEntityWithStringIdentifier()
 {
     $entity_type_id = 'entity_test';
     $this->entityType = new ContentEntityType(array('id' => 'entity_test', 'entity_keys' => array('id' => 'id')));
     // Setup a field having a dedicated schema.
     $field_name = $this->getRandomGenerator()->name();
     $this->setUpStorageDefinition($field_name, array('columns' => array('shape' => array('type' => 'varchar', 'length' => 32, 'not null' => FALSE), 'color' => array('type' => 'varchar', 'length' => 32, 'not null' => FALSE)), 'foreign keys' => array('color' => array('table' => 'color', 'columns' => array('color' => 'id'))), 'unique keys' => array(), 'indexes' => array()));
     $field_storage = $this->storageDefinitions[$field_name];
     $field_storage->expects($this->any())->method('getType')->will($this->returnValue('shape'));
     $field_storage->expects($this->any())->method('getTargetEntityTypeId')->will($this->returnValue($entity_type_id));
     $field_storage->expects($this->any())->method('isMultiple')->will($this->returnValue(TRUE));
     $this->storageDefinitions['id']->expects($this->any())->method('getType')->will($this->returnValue('string'));
     $expected = array($entity_type_id . '__' . $field_name => array('description' => "Data storage for {$entity_type_id} field {$field_name}.", 'fields' => array('bundle' => array('type' => 'varchar', 'length' => 128, 'not null' => true, 'default' => '', 'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance'), 'deleted' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 0, 'description' => 'A boolean indicating whether this data item has been deleted'), 'entity_id' => array('type' => 'varchar', 'length' => 128, 'not null' => true, 'description' => 'The entity id this data is attached to'), 'revision_id' => array('type' => 'varchar', 'length' => 128, 'not null' => true, 'description' => 'The entity revision id this data is attached to, which for an unversioned entity type is the same as the entity id'), 'langcode' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'The language code for this data item.'), 'delta' => array('type' => 'int', 'unsigned' => true, 'not null' => true, 'description' => 'The sequence number for this data item, used for multi-value fields'), $field_name . '_shape' => array('type' => 'varchar', 'length' => 32, 'not null' => false), $field_name . '_color' => array('type' => 'varchar', 'length' => 32, 'not null' => false)), 'primary key' => array('entity_id', 'deleted', 'delta', 'langcode'), 'indexes' => array('bundle' => array('bundle'), 'deleted' => array('deleted'), 'entity_id' => array('entity_id'), 'revision_id' => array('revision_id'), 'langcode' => array('langcode')), 'foreign keys' => array($field_name . '_color' => array('table' => 'color', 'columns' => array($field_name . '_color' => 'id')))));
     $this->setUpStorageSchema($expected);
     $table_mapping = new DefaultTableMapping($this->storageDefinitions);
     $table_mapping->setFieldNames($entity_type_id, array_keys($this->storageDefinitions));
     $table_mapping->setExtraColumns($entity_type_id, array('default_langcode'));
     $this->storage->expects($this->any())->method('getTableMapping')->will($this->returnValue($table_mapping));
     $this->storageSchema->onFieldStorageDefinitionCreate($field_storage);
 }
 /**
  * @covers ::requiresEntityStorageSchemaChanges
  *
  * @dataProvider providerTestRequiresEntityStorageSchemaChanges
  */
 public function testRequiresEntityStorageSchemaChanges(ContentEntityTypeInterface $updated, ContentEntityTypeInterface $original, $requires_change, $change_schema, $change_shared_table)
 {
     $this->entityType = new ContentEntityType(array('id' => 'entity_test', 'entity_keys' => array('id' => 'id')));
     $this->setUpStorageSchema();
     $table_mapping = new DefaultTableMapping($this->entityType, $this->storageDefinitions);
     $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions));
     $table_mapping->setExtraColumns('entity_test', array('default_langcode'));
     $this->storage->expects($this->any())->method('getTableMapping')->will($this->returnValue($table_mapping));
     // Setup storage schema.
     if ($change_schema) {
         $this->storageSchema->expects($this->once())->method('loadEntitySchemaData')->willReturn(array());
     } else {
         $expected = ['entity_test' => ['primary key' => ['id']]];
         $this->storageSchema->expects($this->any())->method('loadEntitySchemaData')->willReturn($expected);
     }
     if ($change_shared_table) {
         $this->storageSchema->expects($this->once())->method('hasSharedTableNameChanges')->willReturn(TRUE);
     }
     $this->assertEquals($requires_change, $this->storageSchema->requiresEntityStorageSchemaChanges($updated, $original));
 }
Esempio n. 4
0
 /**
  * Tests fields on the revision table.
  */
 public function testRevisionTableFields()
 {
     $entity_type = $this->baseEntityType->set('base_table', 'entity_test_mulrev')->set('revision_table', 'entity_test_mulrev_revision')->set('data_table', 'entity_test_mulrev_property_data')->set('revision_data_table', 'entity_test_mulrev_property_revision')->set('id', 'entity_test_mulrev')->set('translatable', TRUE);
     $base_field_definitions = $this->setupBaseFields(EntityTestMulRev::baseFieldDefinitions($this->baseEntityType));
     $user_base_field_definitions = ['uid' => BaseFieldDefinition::create('integer')->setLabel('ID')->setDescription('The ID of the user entity.')->setReadOnly(TRUE)->setSetting('unsigned', TRUE)];
     $this->entityManager->expects($this->any())->method('getBaseFieldDefinitions')->will($this->returnValueMap([['user', $user_base_field_definitions], ['entity_test_mulrev', $base_field_definitions]]));
     $this->viewsData->setEntityType($entity_type);
     // Setup the table mapping.
     $table_mapping = $this->getMock('Drupal\\Core\\Entity\\Sql\\TableMappingInterface');
     $table_mapping->expects($this->any())->method('getTableNames')->willReturn(['entity_test_mulrev', 'entity_test_mulrev_revision', 'entity_test_mulrev_property_data', 'entity_test_mulrev_property_revision']);
     $table_mapping->expects($this->any())->method('getColumnNames')->willReturnMap([['id', ['value' => 'id']], ['uuid', ['value' => 'uuid']], ['type', ['value' => 'type']], ['langcode', ['value' => 'langcode']], ['name', ['value' => 'name']], ['description', ['value' => 'description__value', 'format' => 'description__format']], ['homepage', ['value' => 'homepage']], ['user_id', ['target_id' => 'user_id']], ['revision_id', ['value' => 'id']]]);
     $table_mapping->expects($this->any())->method('getFieldNames')->willReturnMap([['entity_test_mulrev', ['id', 'revision_id', 'uuid', 'type']], ['entity_test_mulrev_revision', ['id', 'revision_id', 'langcode']], ['entity_test_mulrev_property_data', ['id', 'revision_id', 'langcode', 'name', 'description', 'homepage', 'user_id']], ['entity_test_mulrev_property_revision', ['id', 'revision_id', 'langcode', 'name', 'description', 'homepage', 'user_id']]]);
     $table_mapping->expects($this->any())->method('getFieldTableName')->willReturnCallback(function ($field) {
         if ($field == 'uuid') {
             return 'entity_test_mulrev';
         }
         return 'entity_test_mulrev_property_data';
     });
     $this->entityStorage->expects($this->once())->method('getTableMapping')->willReturn($table_mapping);
     $this->setupFieldStorageDefinition();
     $data = $this->viewsData->getViewsData();
     // Check the base fields.
     $this->assertFalse(isset($data['entity_test_mulrev']['id']));
     $this->assertFalse(isset($data['entity_test_mulrev']['type']));
     $this->assertFalse(isset($data['entity_test_mulrev']['revision_id']));
     $this->assertUuidField($data['entity_test_mulrev']['uuid']);
     $this->assertField($data['entity_test_mulrev']['uuid'], 'uuid');
     // Also ensure that field_data only fields don't appear on the base table.
     $this->assertFalse(isset($data['entity_test_mulrev']['name']));
     $this->assertFalse(isset($data['entity_test_mul']['description']));
     $this->assertFalse(isset($data['entity_test_mul']['description__value']));
     $this->assertFalse(isset($data['entity_test_mul']['description__format']));
     $this->assertFalse(isset($data['entity_test_mul']['homepage']));
     $this->assertFalse(isset($data['entity_test_mulrev']['langcode']));
     $this->assertFalse(isset($data['entity_test_mulrev']['user_id']));
     // Check the revision fields. The revision ID should only appear in the data
     // table.
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['revision_id']));
     // Also ensure that field_data only fields don't appear on the revision table.
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['id']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['name']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['description']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['description__value']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['description__format']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['homepage']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['user_id']));
     // Check the data fields.
     $this->assertNumericField($data['entity_test_mulrev_property_data']['id']);
     $this->assertField($data['entity_test_mulrev_property_data']['id'], 'id');
     $this->assertNumericField($data['entity_test_mulrev_property_data']['revision_id']);
     $this->assertField($data['entity_test_mulrev_property_data']['revision_id'], 'revision_id');
     $this->assertLanguageField($data['entity_test_mulrev_property_data']['langcode']);
     $this->assertField($data['entity_test_mulrev_property_data']['langcode'], 'langcode');
     $this->assertStringField($data['entity_test_mulrev_property_data']['name']);
     $this->assertField($data['entity_test_mulrev_property_data']['name'], 'name');
     $this->assertLongTextField($data['entity_test_mulrev_property_data'], 'description');
     $this->assertField($data['entity_test_mulrev_property_data']['description__value'], 'description');
     $this->assertField($data['entity_test_mulrev_property_data']['description__format'], 'description');
     $this->assertUriField($data['entity_test_mulrev_property_data']['homepage']);
     $this->assertField($data['entity_test_mulrev_property_data']['homepage'], 'homepage');
     $this->assertEntityReferenceField($data['entity_test_mulrev_property_data']['user_id']);
     $this->assertField($data['entity_test_mulrev_property_data']['user_id'], 'user_id');
     $relationship = $data['entity_test_mulrev_property_data']['user_id']['relationship'];
     $this->assertEquals('users_field_data', $relationship['base']);
     $this->assertEquals('uid', $relationship['base field']);
     // Check the property data fields.
     $this->assertNumericField($data['entity_test_mulrev_property_revision']['id']);
     $this->assertField($data['entity_test_mulrev_property_revision']['id'], 'id');
     $this->assertLanguageField($data['entity_test_mulrev_property_revision']['langcode']);
     $this->assertField($data['entity_test_mulrev_property_revision']['langcode'], 'langcode');
     $this->assertEquals('Translation language', $data['entity_test_mulrev_property_revision']['langcode']['title']);
     $this->assertStringField($data['entity_test_mulrev_property_revision']['name']);
     $this->assertField($data['entity_test_mulrev_property_revision']['name'], 'name');
     $this->assertLongTextField($data['entity_test_mulrev_property_revision'], 'description');
     $this->assertField($data['entity_test_mulrev_property_revision']['description__value'], 'description');
     $this->assertField($data['entity_test_mulrev_property_revision']['description__format'], 'description');
     $this->assertUriField($data['entity_test_mulrev_property_revision']['homepage']);
     $this->assertField($data['entity_test_mulrev_property_revision']['homepage'], 'homepage');
     $this->assertEntityReferenceField($data['entity_test_mulrev_property_revision']['user_id']);
     $this->assertField($data['entity_test_mulrev_property_revision']['user_id'], 'user_id');
     $relationship = $data['entity_test_mulrev_property_revision']['user_id']['relationship'];
     $this->assertEquals('users_field_data', $relationship['base']);
     $this->assertEquals('uid', $relationship['base field']);
 }
 /**
  * Tests fields on the revision table.
  */
 public function testRevisionTableFields()
 {
     $entity_type = $this->baseEntityType->set('base_table', 'entity_test_mulrev')->set('revision_table', 'entity_test_mulrev_revision')->set('data_table', 'entity_test_mulrev_property_data')->set('revision_data_table', 'entity_test_mulrev_property_revision')->set('id', 'entity_test_mulrev');
     $base_field_definitions = $this->setupBaseFields(EntityTestMulRev::baseFieldDefinitions($this->baseEntityType));
     $this->entityManager->expects($this->once())->method('getBaseFieldDefinitions')->with('entity_test_mulrev')->willReturn($base_field_definitions);
     $this->viewsData->setEntityType($entity_type);
     // Setup the table mapping.
     $table_mapping = $this->getMock('Drupal\\Core\\Entity\\Sql\\TableMappingInterface');
     $table_mapping->expects($this->any())->method('getTableNames')->willReturn(['entity_test_mulrev', 'entity_test_mulrev_revision', 'entity_test_mulrev_property_data', 'entity_test_mulrev_property_revision']);
     $table_mapping->expects($this->any())->method('getColumnNames')->willReturnMap([['id', ['value' => 'id']], ['uuid', ['value' => 'uuid']], ['type', ['value' => 'type']], ['langcode', ['value' => 'langcode']], ['name', ['value' => 'name']], ['description', ['value' => 'description__value', 'format' => 'description__format']], ['homepage', ['value' => 'homepage']], ['user_id', ['target_id' => 'user_id']], ['revision_id', ['value' => 'id']]]);
     $table_mapping->expects($this->any())->method('getFieldNames')->willReturnMap([['entity_test_mulrev', ['id', 'revision_id', 'uuid', 'type']], ['entity_test_mulrev_revision', ['id', 'revision_id', 'langcode']], ['entity_test_mulrev_property_data', ['id', 'revision_id', 'langcode', 'name', 'description', 'homepage', 'user_id']], ['entity_test_mulrev_property_revision', ['id', 'revision_id', 'langcode', 'name', 'description', 'homepage', 'user_id']]]);
     $this->entityStorage->expects($this->once())->method('getTableMapping')->willReturn($table_mapping);
     $this->setupFieldStorageDefinition();
     $data = $this->viewsData->getViewsData();
     // Check the base fields.
     $this->assertNumericField($data['entity_test_mulrev']['id']);
     $this->assertNumericField($data['entity_test_mulrev']['revision_id']);
     $this->assertUuidField($data['entity_test_mulrev']['uuid']);
     $this->assertStringField($data['entity_test_mulrev']['type']);
     // Also ensure that field_data only fields don't appear on the base table.
     $this->assertFalse(isset($data['entity_test_mulrev']['name']));
     $this->assertFalse(isset($data['entity_test_mul']['description']));
     $this->assertFalse(isset($data['entity_test_mul']['description__value']));
     $this->assertFalse(isset($data['entity_test_mul']['description__format']));
     $this->assertFalse(isset($data['entity_test_mul']['homepage']));
     $this->assertFalse(isset($data['entity_test_mulrev']['langcode']));
     $this->assertFalse(isset($data['entity_test_mulrev']['user_id']));
     // Check the revision fields.
     $this->assertNumericField($data['entity_test_mulrev_revision']['id']);
     $this->assertNumericField($data['entity_test_mulrev_revision']['revision_id']);
     $this->assertLanguageField($data['entity_test_mulrev_revision']['langcode']);
     $this->assertEquals('Original language', $data['entity_test_mulrev_revision']['langcode']['title']);
     // Also ensure that field_data only fields don't appear on the revision table.
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['name']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['description']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['description__value']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['description__format']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['homepage']));
     $this->assertFalse(isset($data['entity_test_mulrev_revision']['user_id']));
     // Check the data fields.
     $this->assertNumericField($data['entity_test_mulrev_property_data']['id']);
     $this->assertLanguageField($data['entity_test_mulrev_property_data']['langcode']);
     $this->assertStringField($data['entity_test_mulrev_property_data']['name']);
     $this->assertLongTextField($data['entity_test_mulrev_property_data'], 'description');
     $this->assertUriField($data['entity_test_mulrev_property_data']['homepage']);
     $this->assertEntityReferenceField($data['entity_test_mulrev_property_data']['user_id']);
     $relationship = $data['entity_test_mulrev_property_data']['user_id']['relationship'];
     $this->assertEquals('users', $relationship['base']);
     $this->assertEquals('uid', $relationship['base field']);
     // Check the property data fields.
     $this->assertNumericField($data['entity_test_mulrev_property_revision']['id']);
     $this->assertLanguageField($data['entity_test_mulrev_property_revision']['langcode']);
     $this->assertEquals('Translation language', $data['entity_test_mulrev_property_revision']['langcode']['title']);
     $this->assertStringField($data['entity_test_mulrev_property_revision']['name']);
     $this->assertLongTextField($data['entity_test_mulrev_property_revision'], 'description');
     $this->assertUriField($data['entity_test_mulrev_property_revision']['homepage']);
     $this->assertEntityReferenceField($data['entity_test_mulrev_property_revision']['user_id']);
     $relationship = $data['entity_test_mulrev_property_revision']['user_id']['relationship'];
     $this->assertEquals('users', $relationship['base']);
     $this->assertEquals('uid', $relationship['base field']);
 }