ContentEntityFieldMethodInvocationOrderTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\language\Entity\ConfigurableLanguage;
  4. /**
  5. * Tests correct field method invocation order.
  6. *
  7. * @group Entity
  8. */
  9. class ContentEntityFieldMethodInvocationOrderTest extends EntityKernelTestBase {
  10. /**
  11. * Modules to enable.
  12. *
  13. * @var array
  14. */
  15. public static $modules = ['language', 'system', 'entity_test'];
  16. /**
  17. * The EntityTest entity type storage.
  18. *
  19. * @var \Drupal\Core\Entity\ContentEntityStorageInterface
  20. */
  21. protected $entityTestFieldMethodsStorage;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function setUp() {
  26. parent::setUp();
  27. // Enable an additional language.
  28. ConfigurableLanguage::createFromLangcode('de')->save();
  29. ConfigurableLanguage::createFromLangcode('fr')->save();
  30. $this->installEntitySchema('entity_test_field_methods');
  31. $this->entityTestFieldMethodsStorage = $this->entityManager->getStorage('entity_test_field_methods');
  32. }
  33. /**
  34. * Tests correct field method invocation order.
  35. */
  36. public function testFieldMethodInvocationOrder() {
  37. // Create a test entity.
  38. $entity = $this->entityTestFieldMethodsStorage->create([
  39. 'name' => $this->randomString(),
  40. 'langcode' => 'de',
  41. ]);
  42. $entity->save();
  43. $entity->addTranslation('fr')
  44. ->save();
  45. // Reset the current value of the test field.
  46. foreach (['de', 'fr'] as $langcode) {
  47. $entity->getTranslation($langcode)->test_invocation_order->value = 0;
  48. }
  49. $entity->getTranslation('de')
  50. ->save();
  51. $this->assertTrue($entity->getTranslation('fr')->test_invocation_order->value > $entity->getTranslation('de')->test_invocation_order->value, 'The field presave method has been invoked in the correct entity translation order.');
  52. // Reset the current value of the test field.
  53. foreach (['de', 'fr'] as $langcode) {
  54. $entity->getTranslation($langcode)->test_invocation_order->value = 0;
  55. }
  56. $entity->getTranslation('fr')
  57. ->save();
  58. $this->assertTrue($entity->getTranslation('de')->test_invocation_order->value > $entity->getTranslation('fr')->test_invocation_order->value, 'The field presave method has been invoked in the correct entity translation order.');
  59. }
  60. }