33099623cd322f00369e0bf0aa881474a4c0b981
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityFieldDefaultValueTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
5 use Drupal\Component\Uuid\Uuid;
6 use Drupal\Component\Render\FormattableMarkup;
7
8 /**
9  * Tests default values for entity fields.
10  *
11  * @group Entity
12  */
13 class EntityFieldDefaultValueTest extends EntityKernelTestBase {
14
15   /**
16    * The UUID object to be used for generating UUIDs.
17    *
18    * @var \Drupal\Component\Uuid\UuidInterface
19    */
20   protected $uuid;
21
22   protected function setUp() {
23     parent::setUp();
24     // Initiate the generator object.
25     $this->uuid = $this->container->get('uuid');
26   }
27
28   /**
29    * Tests default values on entities and fields.
30    */
31   public function testDefaultValues() {
32     // All entity variations have to have the same results.
33     foreach (entity_test_entity_types() as $entity_type) {
34       $this->assertDefaultValues($entity_type);
35     }
36   }
37
38   /**
39    * Executes a test set for a defined entity type.
40    *
41    * @param string $entity_type_id
42    *   The entity type to run the tests with.
43    */
44   protected function assertDefaultValues($entity_type_id) {
45     $entity = $this->container->get('entity_type.manager')
46       ->getStorage($entity_type_id)
47       ->create();
48     $definition = $this->entityManager->getDefinition($entity_type_id);
49     $langcode_key = $definition->getKey('langcode');
50     $this->assertEqual($entity->{$langcode_key}->value, 'en', new FormattableMarkup('%entity_type: Default language', ['%entity_type' => $entity_type_id]));
51     $this->assertTrue(Uuid::isValid($entity->uuid->value), new FormattableMarkup('%entity_type: Default UUID', ['%entity_type' => $entity_type_id]));
52     $this->assertEqual($entity->name->getValue(), [], 'Field has one empty value by default.');
53   }
54
55   /**
56    * Tests custom default value callbacks.
57    */
58   public function testDefaultValueCallback() {
59     $entity = $this->entityManager->getStorage('entity_test_default_value')->create();
60     // The description field has a default value callback for testing, see
61     // entity_test_field_default_value().
62     $string = 'description_' . $entity->language()->getId();
63     $expected = [
64       [
65         'shape' => "shape:0:$string",
66         'color' => "color:0:$string",
67       ],
68       [
69         'shape' => "shape:1:$string",
70         'color' => "color:1:$string",
71       ],
72     ];
73     $this->assertEqual($entity->description->getValue(), $expected);
74   }
75
76 }