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