EntityRevisionTranslationTest.php 4.09 KB
Newer Older
1 2
<?php

3
namespace Drupal\KernelTests\Core\Entity;
4 5 6 7 8 9 10 11 12

use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\language\Entity\ConfigurableLanguage;

/**
 * Tests proper revision propagation of entities.
 *
 * @group Entity
 */
13
class EntityRevisionTranslationTest extends EntityKernelTestBase {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

  /**
   * {@inheritdoc}
   */
  public static $modules = ['language'];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Enable an additional language.
    ConfigurableLanguage::createFromLangcode('de')->save();

    $this->installEntitySchema('entity_test_mulrev');
  }

  /**
   * 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.');
  }

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  /**
   * 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'));
  }

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  /**
   * 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');
  }

135
}