ContentEntityNonRevisionableFieldTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\entity_test\Entity\EntityTestMulRev;
  4. use Drupal\entity_test\Entity\EntityTestRev;
  5. use Drupal\language\Entity\ConfigurableLanguage;
  6. use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
  7. /**
  8. * Tests non-revisionable fields on revisionable (and translatable) entities.
  9. *
  10. * @group Entity
  11. */
  12. class ContentEntityNonRevisionableFieldTest extends EntityKernelTestBase {
  13. use EntityDefinitionTestTrait;
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. public static $modules = ['language'];
  20. /**
  21. * The EntityTestMulRev entity type storage.
  22. *
  23. * @var \Drupal\Core\Entity\EntityStorageInterface
  24. */
  25. protected $mulRev;
  26. /**
  27. * The EntityTestRev entity type storage.
  28. *
  29. * @var \Drupal\Core\Entity\EntityStorageInterface
  30. */
  31. protected $rev;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function setUp() {
  36. parent::setUp();
  37. // Enable an additional language.
  38. ConfigurableLanguage::createFromLangcode('de')->save();
  39. $this->installEntitySchema('entity_test_mulrev');
  40. $this->installEntitySchema('entity_test_rev');
  41. $this->mulRev = $this->entityTypeManager->getStorage('entity_test_mulrev');
  42. $this->rev = $this->entityTypeManager->getStorage('entity_test_rev');
  43. }
  44. /**
  45. * Tests non-revisionable fields on revisionable and translatable entities.
  46. */
  47. public function testMulNonRevisionableField() {
  48. $user1 = $this->createUser();
  49. $user2 = $this->createUser();
  50. // Create a test entity.
  51. $entity = EntityTestMulRev::create([
  52. 'name' => $this->randomString(),
  53. 'user_id' => $user1->id(),
  54. 'language' => 'en',
  55. 'non_rev_field' => 'Huron',
  56. ]);
  57. $entity->save();
  58. // Create a test entity.
  59. $entity2 = EntityTestMulRev::create([
  60. 'name' => $this->randomString(),
  61. 'user_id' => $user1->id(),
  62. 'language' => 'en',
  63. 'non_rev_field' => 'Michigan',
  64. ]);
  65. $entity2->save();
  66. $this->assertEquals('Huron', $entity->get('non_rev_field')->value, 'Huron found on entity 1');
  67. $this->assertEquals('Michigan', $entity2->get('non_rev_field')->value, 'Michigan found on entity 2');
  68. $entity->setNewRevision();
  69. $entity->setOwner($user2);
  70. $entity->save();
  71. $entity2->setNewRevision();
  72. $entity2->setOwner($user2);
  73. $entity2->save();
  74. $this->assertEquals($user2->id(), $entity->getOwner()->id(), 'User 2 found on entity 1');
  75. $this->assertEquals($user2->id(), $entity2->getOwner()->id(), 'User 2 found on entity 2');
  76. $entity->addTranslation('de');
  77. $entity->save();
  78. $entity2->addTranslation('de');
  79. $entity2->save();
  80. $expected_revision_ids = [
  81. 4 => 2,
  82. 3 => 1,
  83. 2 => 2,
  84. 1 => 1,
  85. ];
  86. $revision_ids = $this->mulRev->getQuery()
  87. ->allRevisions()
  88. ->sort('revision_id', 'DESC')
  89. ->execute();
  90. $this->assertEquals($expected_revision_ids, $revision_ids, 'Revision ids found');
  91. $expected_non_rev_field_revision_ids = [
  92. 3 => 1,
  93. 1 => 1,
  94. ];
  95. $non_rev_field_revision_ids = $this->mulRev->getQuery()
  96. ->allRevisions()
  97. ->condition('non_rev_field', 'Huron')
  98. ->sort('revision_id', 'DESC')
  99. ->execute();
  100. $this->assertEquals($expected_non_rev_field_revision_ids, $non_rev_field_revision_ids, 'Revision ids found');
  101. }
  102. /**
  103. * Tests non-revisionable fields on revisionable entities.
  104. */
  105. public function testNonRevisionableField() {
  106. $user1 = $this->createUser();
  107. $user2 = $this->createUser();
  108. // Create a test entity.
  109. $entity = EntityTestRev::create([
  110. 'name' => $this->randomString(),
  111. 'user_id' => $user1->id(),
  112. 'non_rev_field' => 'Superior',
  113. ]);
  114. $entity->save();
  115. // Create a test entity.
  116. $entity2 = EntityTestRev::create([
  117. 'name' => $this->randomString(),
  118. 'user_id' => $user1->id(),
  119. 'non_rev_field' => 'Ontario',
  120. ]);
  121. $entity2->save();
  122. $this->assertEquals('Superior', $entity->get('non_rev_field')->value, 'Superior found on entity 1');
  123. $this->assertEquals('Ontario', $entity2->get('non_rev_field')->value, 'Ontario found on entity 2');
  124. $entity->setNewRevision();
  125. $entity->setOwner($user2);
  126. $entity->save();
  127. $entity2->setNewRevision();
  128. $entity2->setOwner($user2);
  129. $entity2->save();
  130. $this->assertEquals($user2->id(), $entity->getOwner()->id(), 'User 2 found on entity 1');
  131. $this->assertEquals($user2->id(), $entity2->getOwner()->id(), 'User 2 found on entity 2');
  132. $expected_revision_ids = [
  133. 4 => 2,
  134. 3 => 1,
  135. 2 => 2,
  136. 1 => 1,
  137. ];
  138. $revision_ids = $this->rev->getQuery()
  139. ->allRevisions()
  140. ->sort('revision_id', 'DESC')
  141. ->execute();
  142. $this->assertEquals($expected_revision_ids, $revision_ids, 'Revision ids found');
  143. $expected_non_rev_field_revision_ids = [
  144. 3 => 1,
  145. 1 => 1,
  146. ];
  147. $non_rev_field_revision_ids = $this->rev->getQuery()
  148. ->allRevisions()
  149. ->condition('non_rev_field', 'Superior')
  150. ->sort('revision_id', 'DESC')
  151. ->execute();
  152. $this->assertEquals($expected_non_rev_field_revision_ids, $non_rev_field_revision_ids, 'Revision ids found');
  153. }
  154. /**
  155. * Tests multi column non revisionable base field for revisionable entity.
  156. */
  157. public function testMultiColumnNonRevisionableBaseField() {
  158. \Drupal::state()->set('entity_test.multi_column', TRUE);
  159. $this->applyEntityUpdates('entity_test_mulrev');
  160. // Refresh the storage.
  161. $this->mulRev = $this->entityTypeManager->getStorage('entity_test_mulrev');
  162. $user1 = $this->createUser();
  163. // Create a test entity.
  164. $entity = EntityTestMulRev::create([
  165. 'name' => $this->randomString(),
  166. 'user_id' => $user1->id(),
  167. 'language' => 'en',
  168. 'non_rev_field' => 'Huron',
  169. 'description' => [
  170. 'shape' => 'shape',
  171. 'color' => 'color',
  172. ],
  173. ]);
  174. $entity->save();
  175. $entity = $this->mulRev->loadUnchanged($entity->id());
  176. $expected = [
  177. [
  178. 'shape' => 'shape',
  179. 'color' => 'color',
  180. ],
  181. ];
  182. $this->assertEquals('Huron', $entity->get('non_rev_field')->value, 'Huron found on entity 1');
  183. $this->assertEquals($expected, $entity->description->getValue());
  184. }
  185. }