7ac9d89852681024205f8d264c6132eefb099206
[yaffs-website] / web / core / modules / system / tests / modules / entity_test / tests / src / Functional / Rest / EntityTestJsonInternalPropertyNormalizerTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_test\Functional\Rest;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
9
10 /**
11  * Test that internal properties are not exposed in the 'json' format.
12  *
13  * @group rest
14  */
15 class EntityTestJsonInternalPropertyNormalizerTest extends EntityTestResourceTestBase {
16
17   use AnonResourceTestTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   protected static $format = 'json';
23
24   /**
25    * {@inheritdoc}
26    */
27   protected static $mimeType = 'application/json';
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function getExpectedNormalizedEntity() {
33     $expected = parent::getExpectedNormalizedEntity();
34     // The 'internal_value' property in test field type is not exposed in the
35     // normalization because setInternal(FALSE) was not called for this
36     // property.
37     // @see \Drupal\entity_test\Plugin\Field\FieldType\InternalPropertyTestFieldItem::propertyDefinitions
38     $expected['field_test_internal'] = [
39       [
40         'value' => 'This value shall not be internal!',
41         'non_internal_value' => 'Computed! This value shall not be internal!',
42       ],
43     ];
44     return $expected;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function createEntity() {
51     if (!FieldStorageConfig::loadByName('entity_test', 'field_test_internal')) {
52       FieldStorageConfig::create([
53         'entity_type' => 'entity_test',
54         'field_name' => 'field_test_internal',
55         'type' => 'internal_property_test',
56         'cardinality' => 1,
57         'translatable' => FALSE,
58       ])->save();
59       FieldConfig::create([
60         'entity_type' => 'entity_test',
61         'field_name' => 'field_test_internal',
62         'bundle' => 'entity_test',
63         'label' => 'Test field with internal and non-internal properties',
64       ])->save();
65     }
66
67     $entity = parent::createEntity();
68     $entity->field_test_internal = [
69       'value' => 'This value shall not be internal!',
70     ];
71     $entity->save();
72     return $entity;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   protected function getNormalizedPostEntity() {
79     return parent::getNormalizedPostEntity() + [
80       'field_test_internal' => [
81         [
82           'value' => 'This value shall not be internal!',
83         ],
84       ],
85     ];
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   protected function getExpectedCacheContexts() {
92     return Cache::mergeContexts(parent::getExpectedCacheContexts(), ['request_format']);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   protected function getExpectedCacheTags() {
99     return Cache::mergeTags(parent::getExpectedCacheTags(), ['you_are_it', 'no_tag_backs']);
100   }
101
102 }