public function testRecord() { $entity = EntityTestRev::create(); // We don't want to save the entity and trigger the hooks in the storage // controller. We just want to test the sequence storage here, so we mock // entity IDs here. $expected = ['entity_type_id' => 'entity_test_rev', 'entity_id' => 1, 'entity_uuid' => $entity->uuid(), 'revision_id' => 1, 'deleted' => FALSE, 'rev' => FALSE, 'local' => (bool) $entity->getEntityType()->get('local'), 'is_stub' => FALSE]; $entity->id->value = $expected['entity_id']; $entity->revision_id->value = $expected['revision_id']; $entity->_deleted->value = $expected['deleted']; $entity->_rev->value = $expected['rev']; $values = $this->sequenceIndex->getRange(2); $this->assertEqual(2, count($values), 'There are two index entries'); $this->sequenceIndex->add($entity); $expected['seq'] = $this->multiversionManager->lastSequenceId(); // We should have 2 entities of user entity type (anonymous and root user) // and one entity_test_rev. $values = $this->sequenceIndex->getRange(3); $this->assertEqual(3, count($values), 'One new index entry was added.'); foreach ($expected as $key => $value) { $this->assertIdentical($value, $values[2][$key], "Index entry key {$key} have value {$value}"); } $entity = EntityTestRev::create(); $workspace_name = $this->randomMachineName(); /** @var \Drupal\Core\Entity\EntityStorageInterface $workspace_storage */ $workspace_storage = $this->container->get('entity.manager')->getStorage('workspace'); $workspace_storage->create(['name' => $workspace_name]); // Generate a new sequence ID. $this->sequenceIndex->useWorkspace($workspace_name)->add($entity); $values = $this->sequenceIndex->getRange(3); $this->assertEqual(1, count($values), 'One index entry was added to the new workspace.'); }
/** * {@inheritdoc} */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields = parent::baseFieldDefinitions($entity_type); $fields['parent_id'] = BaseFieldDefinition::create('string')->setLabel(t('Parent ID'))->setDescription(t('The ID of the parent entity of which this entity is referenced.')); $fields['parent_type'] = BaseFieldDefinition::create('string')->setLabel(t('Parent type'))->setDescription(t('The entity parent type to which this entity is referenced.')); $fields['parent_field_name'] = BaseFieldDefinition::create('string')->setLabel(t('Parent field name'))->setDescription(t('The entity parent field name to which this entity is referenced.')); return $fields; }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('user'); $field_name = 'test'; $this->fieldStorage = FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test_rev', 'type' => 'string', 'cardinality' => 1]); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test_rev', 'bundle' => 'entity_test_rev', 'required' => TRUE]); $this->field->save(); // Create an entity with field data. $this->entity = EntityTestRev::create(['user_id' => mt_rand(1, 10), 'name' => $this->randomMachineName(), $field_name => $this->randomString()]); $this->entity->save(); }
protected function setUp() { parent::setUp(); $field_name = 'test_options'; $this->fieldStorage = FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test_rev', 'type' => 'list_string', 'cardinality' => 1, 'settings' => ['allowed_values_function' => 'options_test_dynamic_values_callback']]); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test_rev', 'bundle' => 'entity_test_rev', 'required' => TRUE])->save(); entity_get_form_display('entity_test_rev', 'entity_test_rev', 'default')->setComponent($field_name, ['type' => 'options_select'])->save(); // Create an entity and prepare test data that will be used by // options_test_dynamic_values_callback(). $values = ['user_id' => mt_rand(1, 10), 'name' => $this->randomMachineName()]; $this->entity = EntityTestRev::create($values); $this->entity->save(); $this->test = ['label' => $this->entity->label(), 'uuid' => $this->entity->uuid(), 'bundle' => $this->entity->bundle(), 'uri' => $this->entity->url()]; }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema('entity_test'); $this->installEntitySchema('entity_test_rev'); // Setup a field storage and field, but also change the views data for the // entity_test entity type. $field_storage = FieldStorageConfig::create(['field_name' => 'field_test', 'type' => 'integer', 'entity_type' => 'entity_test']); $field_storage->save(); $field = FieldConfig::create(['field_name' => 'field_test', 'entity_type' => 'entity_test', 'bundle' => 'entity_test']); $field->save(); $random_number = (string) 30856; for ($i = 0; $i < 5; $i++) { $this->entities[$i] = $entity = EntityTest::create(['bundle' => 'entity_test', 'field_test' => $random_number[$i]]); $entity->save(); } // Setup some test data for entities with revisions. // We are testing both base field revisions and field config revisions. $field_storage = FieldStorageConfig::create(['field_name' => 'field_test', 'type' => 'integer', 'entity_type' => 'entity_test_rev']); $field_storage->save(); $field = FieldConfig::create(['field_name' => 'field_test', 'entity_type' => 'entity_test_rev', 'bundle' => 'entity_test_rev']); $field->save(); $this->entityRevision = []; $this->entityRevision[0] = $entity = EntityTestRev::create(['name' => 'base value', 'field_test' => 1]); $entity->save(); $entity = clone $entity; $entity->setNewRevision(TRUE); $entity->name->value = 'revision value1'; $entity->field_test->value = 2; $entity->save(); $this->entityRevision[1] = $entity; $entity = clone $entity; $entity->setNewRevision(TRUE); $entity->name->value = 'revision value2'; $entity->field_test->value = 3; $entity->save(); $this->entityRevision[2] = $entity; $this->entityRevision[0] = $entity = EntityTestRev::create(['name' => 'next entity value', 'field_test' => 4]); $entity->save(); \Drupal::state()->set('entity_test.views_data', ['entity_test' => ['id' => ['field' => ['id' => 'field']]], 'entity_test_rev_revision' => ['id' => ['field' => ['id' => 'field']]]]); Views::viewsData()->clear(); }
public function testRevisionIdGeneration() { $entity = EntityTestRev::create(); $first_rev = $this->multiversionManager->newRevisionId($entity, 0); $this->assertRevisionId(1, $first_rev, 'First revision ID was generated correctly.'); $new_rev = $this->multiversionManager->newRevisionId($entity, 0); $this->assertEqual($first_rev, $new_rev, 'Identical revision IDs with same input parameters.'); $second_rev = $this->multiversionManager->newRevisionId($entity, 1); $this->assertRevisionId(2, $second_rev, 'Second revision ID was generated correctly.'); $this->assertEqual($this->extractRevisionHash($first_rev), $this->extractRevisionHash($second_rev), 'First and second revision hashes was identical (entity did not change).'); $revs = [$first_rev]; $test_entity = clone $entity; $test_entity->_rev->value = $first_rev; $revs[] = $this->multiversionManager->newRevisionId($test_entity, 0); $this->assertTrue(count($revs) == count(array_unique($revs)), 'Revision ID varies on old revision.'); $test_entity = clone $entity; $test_entity->name = $this->randomMachineName(); $revs[] = $this->multiversionManager->newRevisionId($test_entity, 0); $this->assertTrue(count($revs) == count(array_unique($revs)), 'Revision ID varies on entity fields.'); $test_entity = clone $entity; $test_entity->_deleted->value = TRUE; $revs[] = $this->multiversionManager->newRevisionId($test_entity, 0); $this->assertTrue(count($revs) == count(array_unique($revs)), 'Revision ID varies on deleted flag.'); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema('entity_test'); $this->installEntitySchema('user'); $this->installEntitySchema('entity_test_rev'); // Bypass any field access. $this->adminUser = User::create(['name' => $this->randomString()]); $this->adminUser->save(); $this->container->get('current_user')->setAccount($this->adminUser); $this->testUsers = []; for ($i = 0; $i < 5; $i++) { $this->testUsers[$i] = User::create(['name' => 'test ' . $i, 'timezone' => User::getAllowedTimezones()[$i]]); $this->testUsers[$i]->save(); } // Setup a field storage and field, but also change the views data for the // entity_test entity type. $field_storage = FieldStorageConfig::create(['field_name' => 'field_test', 'type' => 'integer', 'entity_type' => 'entity_test']); $field_storage->save(); $field = FieldConfig::create(['field_name' => 'field_test', 'entity_type' => 'entity_test', 'bundle' => 'entity_test']); $field->save(); $field_storage_multiple = FieldStorageConfig::create(['field_name' => 'field_test_multiple', 'type' => 'integer', 'entity_type' => 'entity_test', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED]); $field_storage_multiple->save(); $field_multiple = FieldConfig::create(['field_name' => 'field_test_multiple', 'entity_type' => 'entity_test', 'bundle' => 'entity_test']); $field_multiple->save(); $random_number = (string) 30856; $random_number_multiple = (string) 1370359990; for ($i = 0; $i < 5; $i++) { $this->entities[$i] = $entity = EntityTest::create(['bundle' => 'entity_test', 'name' => 'test ' . $i, 'field_test' => $random_number[$i], 'field_test_multiple' => [$random_number_multiple[$i * 2], $random_number_multiple[$i * 2 + 1]], 'user_id' => $this->testUsers[$i]->id()]); $entity->save(); } // Setup some test data for entities with revisions. // We are testing both base field revisions and field config revisions. $field_storage = FieldStorageConfig::create(['field_name' => 'field_test', 'type' => 'integer', 'entity_type' => 'entity_test_rev']); $field_storage->save(); $field = FieldConfig::create(['field_name' => 'field_test', 'entity_type' => 'entity_test_rev', 'bundle' => 'entity_test_rev']); $field->save(); $field_storage_multiple = FieldStorageConfig::create(['field_name' => 'field_test_multiple', 'type' => 'integer', 'entity_type' => 'entity_test_rev', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED]); $field_storage_multiple->save(); $field_multiple = FieldConfig::create(['field_name' => 'field_test_multiple', 'entity_type' => 'entity_test_rev', 'bundle' => 'entity_test_rev']); $field_multiple->save(); $this->entityRevision = []; $this->entityRevision[0] = $entity = EntityTestRev::create(['name' => 'base value', 'field_test' => 1, 'field_test_multiple' => [1, 3, 7], 'user_id' => $this->testUsers[0]->id()]); $entity->save(); $original_entity = clone $entity; $entity = clone $original_entity; $entity->setNewRevision(TRUE); $entity->name->value = 'revision value1'; $entity->field_test->value = 2; $entity->field_test_multiple[0]->value = 0; $entity->field_test_multiple[1]->value = 3; $entity->field_test_multiple[2]->value = 5; $entity->user_id->target_id = $this->testUsers[1]->id(); $entity->save(); $this->entityRevision[1] = $entity; $entity = clone $original_entity; $entity->setNewRevision(TRUE); $entity->name->value = 'revision value2'; $entity->field_test->value = 3; $entity->field_test_multiple[0]->value = 9; $entity->field_test_multiple[1]->value = 9; $entity->field_test_multiple[2]->value = 9; $entity->user_id->target_id = $this->testUsers[2]->id(); $entity->save(); $this->entityRevision[2] = $entity; $this->entityRevision[3] = $entity = EntityTestRev::create(['name' => 'next entity value', 'field_test' => 4, 'field_test_multiple' => [2, 9, 9], 'user_id' => $this->testUsers[3]->id()]); $entity->save(); \Drupal::state()->set('entity_test.views_data', ['entity_test' => ['id' => ['field' => ['id' => 'field']]], 'entity_test_rev_revision' => ['id' => ['field' => ['id' => 'field']]]]); Views::viewsData()->clear(); }
/** * {@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, array()); return $fields; }
/** * Tests string formatter output. */ public function testStringFormatter() { $value = $this->randomString(); $value .= "\n\n<strong>" . $this->randomString() . '</strong>'; $value .= "\n\n" . $this->randomString(); $entity = EntityTestRev::create(array()); $entity->{$this->fieldName}->value = $value; // Verify that all HTML is escaped and newlines are retained. $this->renderEntityFields($entity, $this->display); $this->assertNoRaw($value); $this->assertRaw(nl2br(SafeMarkup::checkPlain($value))); // Verify the cache tags. $build = $entity->{$this->fieldName}->view(); $this->assertTrue(!isset($build[0]['#cache']), 'The string formatter has no cache tags.'); $value = $this->randomMachineName(); $entity->{$this->fieldName}->value = $value; $entity->save(); // Set the formatter to link to the entity. $this->display->setComponent($this->fieldName, ['type' => 'string', 'settings' => ['link_to_entity' => TRUE]]); $this->display->save(); $this->renderEntityFields($entity, $this->display); $this->assertLink($value, 0); $this->assertLinkByHref($entity->url()); // $entity->url('revision') falls back to the canonical URL if this is no // revision. $this->assertLinkByHref($entity->url('revision')); // Make the entity a new revision. $old_revision_id = $entity->getRevisionId(); $entity->setNewRevision(TRUE); $value2 = $this->randomMachineName(); $entity->{$this->fieldName}->value = $value2; $entity->save(); $entity_new_revision = \Drupal::entityManager()->getStorage('entity_test_rev')->loadRevision($old_revision_id); $this->renderEntityFields($entity, $this->display); $this->assertLink($value2, 0); $this->assertLinkByHref($entity->url('revision')); $this->renderEntityFields($entity_new_revision, $this->display); $this->assertLink($value, 0); $this->assertLinkByHref('/entity_test_rev/' . $entity_new_revision->id() . '/revision/' . $entity_new_revision->getRevisionId() . '/view'); }
/** * Tests non-revisionable fields on revisionable entities. */ public function testNonRevisionableField() { $user1 = $this->createUser(); $user2 = $this->createUser(); // Create a test entity. $entity = EntityTestRev::create(array('name' => $this->randomString(), 'user_id' => $user1->id(), 'non_rev_field' => 'Superior')); $entity->save(); // Create a test entity. $entity2 = EntityTestRev::create(array('name' => $this->randomString(), 'user_id' => $user1->id(), 'non_rev_field' => 'Ontario')); $entity2->save(); $this->assertEquals('Superior', $entity->get('non_rev_field')->value, 'Superior found on entity 1'); $this->assertEquals('Ontario', $entity2->get('non_rev_field')->value, 'Ontario found on entity 2'); $entity->setNewRevision(); $entity->setOwner($user2); $entity->save(); $entity2->setNewRevision(); $entity2->setOwner($user2); $entity2->save(); $this->assertEquals($user2->id(), $entity->getOwner()->id(), 'User 2 found on entity 1'); $this->assertEquals($user2->id(), $entity2->getOwner()->id(), 'User 2 found on entity 2'); $expected_revision_ids = [4 => 2, 3 => 1, 2 => 2, 1 => 1]; $revision_ids = $this->rev->getQuery()->allRevisions()->sort('revision_id', 'DESC')->execute(); $this->assertEquals($expected_revision_ids, $revision_ids, 'Revision ids found'); $expected_non_rev_field_revision_ids = [3 => 1, 1 => 1]; $non_rev_field_revision_ids = $this->rev->getQuery()->allRevisions()->condition('non_rev_field', 'Superior')->sort('revision_id', 'DESC')->execute(); $this->assertEquals($expected_non_rev_field_revision_ids, $non_rev_field_revision_ids, 'Revision ids found'); }