/** * Tests the translation values when saving a forward revision. */ public function testTranslationValuesWhenSavingForwardRevisions() { $user = $this->createUser(); $storage = $this->entityManager->getStorage('entity_test_mulrev'); // Create a test entity and a translation for it. $entity = EntityTestMulRev::create(['name' => 'default revision - en', 'user_id' => $user->id(), 'language' => 'en']); $entity->addTranslation('de', ['name' => 'default revision - de']); $entity->save(); // Create a forward revision for the entity and change a field value for // both languages. $forward_revision = $this->reloadEntity($entity); $forward_revision->setNewRevision(); $forward_revision->isDefaultRevision(FALSE); $forward_revision->name = 'forward revision - en'; $forward_revision->save(); $forward_revision_translation = $forward_revision->getTranslation('de'); $forward_revision_translation->name = 'forward revision - de'; $forward_revision_translation->save(); $forward_revision_id = $forward_revision->getRevisionId(); $forward_revision = $storage->loadRevision($forward_revision_id); // Change the value of the field in the default language, save the forward // revision and check that the value of the field in the second language is // also taken from the forward revision, *not* from the default revision. $forward_revision->name = 'updated forward revision - en'; $forward_revision->save(); $forward_revision = $storage->loadRevision($forward_revision_id); $this->assertEquals($forward_revision->name->value, 'updated forward revision - en'); $this->assertEquals($forward_revision->getTranslation('de')->name->value, 'forward revision - de'); }
/** * Tests non-revisionable fields on revisionable and translatable entities. */ public function testMulNonRevisionableField() { $user1 = $this->createUser(); $user2 = $this->createUser(); // Create a test entity. $entity = EntityTestMulRev::create(array('name' => $this->randomString(), 'user_id' => $user1->id(), 'language' => 'en', 'non_rev_field' => 'Huron')); $entity->save(); // Create a test entity. $entity2 = EntityTestMulRev::create(array('name' => $this->randomString(), 'user_id' => $user1->id(), 'language' => 'en', 'non_rev_field' => 'Michigan')); $entity2->save(); $this->assertEquals('Huron', $entity->get('non_rev_field')->value, 'Huron found on entity 1'); $this->assertEquals('Michigan', $entity2->get('non_rev_field')->value, 'Michigan 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'); $entity->addTranslation('de'); $entity->save(); $entity2->addTranslation('de'); $entity2->save(); $expected_revision_ids = [4 => 2, 3 => 1, 2 => 2, 1 => 1]; $revision_ids = $this->mulRev->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->mulRev->getQuery()->allRevisions()->condition('non_rev_field', 'Huron')->sort('revision_id', 'DESC')->execute(); $this->assertEquals($expected_non_rev_field_revision_ids, $non_rev_field_revision_ids, 'Revision ids found'); }
/** * Tests if the translation object has the right revision id after new revision. */ public function testNewRevisionAfterTranslation() { $user = $this->createUser(); // Create a test entity. $entity = EntityTestMulRev::create(['name' => $this->randomString(), 'user_id' => $user->id(), 'language' => 'en']); $entity->save(); $old_rev_id = $entity->getRevisionId(); $translation = $entity->addTranslation('de'); $translation->setNewRevision(); $translation->save(); $this->assertTrue($translation->getRevisionId() > $old_rev_id, 'The saved translation in new revision has a newer revision id.'); $this->assertTrue($this->reloadEntity($entity)->getRevisionId() > $old_rev_id, 'The entity from the storage has a newer revision id.'); }
/** * Test that fields referencing UUIDs can be denormalized. */ function testUuidEntityResolver() { // Create an entity to get the UUID from. $entity = EntityTestMulRev::create(array('type' => 'entity_test_mulrev')); $entity->set('name', 'foobar'); $entity->set('field_test_entity_reference', array(array('target_id' => 1))); $entity->save(); $field_uri = Url::fromUri('base:rest/relation/entity_test_mulrev/entity_test_mulrev/field_test_entity_reference', array('absolute' => TRUE))->toString(); $data = array('_links' => array('type' => array('href' => Url::fromUri('base:rest/type/entity_test_mulrev/entity_test_mulrev', array('absolute' => TRUE))->toString()), $field_uri => array(array('href' => $entity->url()))), '_embedded' => array($field_uri => array(array('_links' => array('self' => $entity->url()), 'uuid' => array(array('value' => $entity->uuid())))))); $denormalized = $this->container->get('serializer')->denormalize($data, 'Drupal\\entity_test\\Entity\\EntityTestMulRev', $this->format); $field_value = $denormalized->get('field_test_entity_reference')->getValue(); $this->assertEqual($field_value[0]['target_id'], 1, 'Entity reference resolved using UUID.'); }
/** * Tests translating a non-revisionable field. */ function testTranslatingNonRevisionableField() { /** @var \Drupal\Core\Entity\ContentEntityBase $entity */ $entity = EntityTestMulRev::create(); $entity->set('non_rev_field', 'Hello'); $entity->save(); $translation = $entity->addTranslation('es'); $translation->set('non_rev_field', 'Hola'); $translation->save(); $reloaded = EntityTestMulRev::load($entity->id()); $this->assertEquals('Hello', $reloaded->getTranslation('en')->get('non_rev_field')->value); $this->assertEquals('Hola', $reloaded->getTranslation('es')->get('non_rev_field')->value); }
/** * Tests the field item save workflow. */ public function testSaveWorkflow() { $entity = EntityTestMulRev::create(['name' => $this->randomString(), 'field_test_item' => $this->randomString(), $this->fieldName => $this->randomString()]); // Save a new entity and verify that the initial field value is overwritten // with a value containing the entity id, which implies a resave. Check that // the entity data structure and the stored values match. $this->assertSavedFieldItemValue($entity, "field_test:{$this->fieldName}:1:1"); // Update the entity and verify that the field value is overwritten on // presave if it is not resaved. $this->assertSavedFieldItemValue($entity, 'overwritten'); // Flag the field value as needing to be resaved and verify it actually is. $entity->field_test_item->value = $entity->{$this->fieldName}->value = 'resave'; $this->assertSavedFieldItemValue($entity, "field_test:{$this->fieldName}:1:3"); }
protected function setUp() { parent::setUp(); // User create needs sequence table. $this->installSchema('system', array('sequences')); // Create a test user to use as the entity owner. $this->user = \Drupal::entityManager()->getStorage('user')->create(['name' => 'serialization_test_user', 'mail' => '*****@*****.**', 'pass' => '123456']); $this->user->save(); // Create a test entity to serialize. $this->values = array('name' => $this->randomMachineName(), 'user_id' => $this->user->id(), 'field_test_text' => array('value' => $this->randomMachineName(), 'format' => 'full_html')); $this->entity = EntityTestMulRev::create($this->values); $this->entity->save(); $this->serializer = $this->container->get('serializer'); $this->installConfig(array('field')); }
/** * Tests if the translation object has the right revision id after new revision. */ public function testRevertRevisionAfterTranslation() { $user = $this->createUser(); $storage = $this->entityManager->getStorage('entity_test_mulrev'); // Create a test entity. $entity = EntityTestMulRev::create(['name' => $this->randomString(), 'user_id' => $user->id(), 'language' => 'en']); $entity->save(); $old_rev_id = $entity->getRevisionId(); $translation = $entity->addTranslation('de'); $translation->setNewRevision(); $translation->save(); $entity = $this->reloadEntity($entity); $this->assertTrue($entity->hasTranslation('de')); $entity = $storage->loadRevision($old_rev_id); $entity->setNewRevision(); $entity->isDefaultRevision(TRUE); $entity->save(); $entity = $this->reloadEntity($entity); $this->assertFalse($entity->hasTranslation('de')); }
/** * Test case sensitive and in-sensitive query conditions. */ public function testCaseSensitivity() { $bundle = $this->randomMachineName(); $field_storage = FieldStorageConfig::create(array('field_name' => 'field_ci', 'entity_type' => 'entity_test_mulrev', 'type' => 'string', 'cardinality' => 1, 'translatable' => FALSE, 'settings' => array('case_sensitive' => FALSE))); $field_storage->save(); FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => $bundle))->save(); $field_storage = FieldStorageConfig::create(array('field_name' => 'field_cs', 'entity_type' => 'entity_test_mulrev', 'type' => 'string', 'cardinality' => 1, 'translatable' => FALSE, 'settings' => array('case_sensitive' => TRUE))); $field_storage->save(); FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => $bundle))->save(); $fixtures = array(); for ($i = 0; $i < 2; $i++) { // If the last 4 of the string are all numbers, then there is no // difference between upper and lowercase and the case sensitive CONTAINS // test will fail. Ensure that can not happen by appending a non-numeric // character. See https://www.drupal.org/node/2397297. $string = $this->randomMachineName(7) . 'a'; $fixtures[] = array('original' => $string, 'uppercase' => Unicode::strtoupper($string), 'lowercase' => Unicode::strtolower($string)); } EntityTestMulRev::create(array('type' => $bundle, 'name' => $this->randomMachineName(), 'langcode' => 'en', 'field_ci' => $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 'field_cs' => $fixtures[0]['uppercase'] . $fixtures[1]['lowercase']))->save(); // Check the case insensitive field, = operator. $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'])->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, lowercase'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'])->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, uppercase'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'])->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, mixed.'); // Check the case sensitive field, = operator. $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'])->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, lowercase.'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'])->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, uppercase.'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'])->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, exact match.'); // Check the case insensitive field, IN operator. $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', array($fixtures[0]['lowercase'] . $fixtures[1]['lowercase']), 'IN')->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, lowercase'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', array($fixtures[0]['uppercase'] . $fixtures[1]['uppercase']), 'IN')->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, uppercase'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', array($fixtures[0]['uppercase'] . $fixtures[1]['lowercase']), 'IN')->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, mixed'); // Check the case sensitive field, IN operator. $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', array($fixtures[0]['lowercase'] . $fixtures[1]['lowercase']), 'IN')->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, lowercase'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', array($fixtures[0]['uppercase'] . $fixtures[1]['uppercase']), 'IN')->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, uppercase'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', array($fixtures[0]['uppercase'] . $fixtures[1]['lowercase']), 'IN')->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, mixed'); // Check the case insensitive field, STARTS_WITH operator. $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', $fixtures[0]['lowercase'], 'STARTS_WITH')->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', $fixtures[0]['uppercase'], 'STARTS_WITH')->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, exact match.'); // Check the case sensitive field, STARTS_WITH operator. $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', $fixtures[0]['lowercase'], 'STARTS_WITH')->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, lowercase.'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', $fixtures[0]['uppercase'], 'STARTS_WITH')->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, exact match.'); // Check the case insensitive field, ENDS_WITH operator. $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', $fixtures[1]['lowercase'], 'ENDS_WITH')->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', $fixtures[1]['uppercase'], 'ENDS_WITH')->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, exact match.'); // Check the case sensitive field, ENDS_WITH operator. $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', $fixtures[1]['lowercase'], 'ENDS_WITH')->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', $fixtures[1]['uppercase'], 'ENDS_WITH')->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, exact match.'); // Check the case insensitive field, CONTAINS operator, use the inner 8 // characters of the uppercase and lowercase strings. $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS')->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_ci', Unicode::strtolower(Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS')->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, exact match.'); // Check the case sensitive field, CONTAINS operator. $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS')->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.'); $result = \Drupal::entityQuery('entity_test_mulrev')->condition('field_cs', Unicode::strtolower(Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS')->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, exact match.'); }
/** * Tests that an entity revision is upcasted in the correct language. */ public function testEntityRevisionParamConverter() { // Create a test entity with multiple revisions and translations for them. $entity = EntityTestMulRev::create(['name' => 'default revision - en', 'user_id' => $this->webUser, 'language' => 'en']); $entity->addTranslation('de', ['name' => 'default revision - de']); $entity->save(); $forward_revision = \Drupal::entityTypeManager()->getStorage('entity_test_mulrev')->loadUnchanged($entity->id()); $forward_revision->setNewRevision(); $forward_revision->isDefaultRevision(FALSE); $forward_revision->name = 'forward revision - en'; $forward_revision->save(); $forward_revision_translation = $forward_revision->getTranslation('de'); $forward_revision_translation->name = 'forward revision - de'; $forward_revision_translation->save(); // Check that the entity revision is upcasted in the correct language. $revision_url = 'entity_test_mulrev/' . $entity->id() . '/revision/' . $forward_revision->getRevisionId() . '/view'; $this->drupalGet($revision_url); $this->assertText('forward revision - en'); $this->assertNoText('forward revision - de'); $this->drupalGet('de/' . $revision_url); $this->assertText('forward revision - de'); $this->assertNoText('forward revision - en'); }
/** * Tests multi column non revisionable base field for revisionable entity. */ public function testMultiColumnNonRevisionableBaseField() { \Drupal::state()->set('entity_test.multi_column', TRUE); \Drupal::entityDefinitionUpdateManager()->applyUpdates(); // Refresh the storage. $this->mulRev = $this->entityManager->getStorage('entity_test_mulrev'); $user1 = $this->createUser(); // Create a test entity. $entity = EntityTestMulRev::create(['name' => $this->randomString(), 'user_id' => $user1->id(), 'language' => 'en', 'non_rev_field' => 'Huron', 'description' => ['shape' => 'shape', 'color' => 'color']]); $entity->save(); $entity = $this->mulRev->loadUnchanged($entity->id()); $expected = [['shape' => 'shape', 'color' => 'color']]; $this->assertEquals('Huron', $entity->get('non_rev_field')->value, 'Huron found on entity 1'); $this->assertEquals($expected, $entity->description->getValue()); }