c53c9c1f279435bb12077e37c606ff4bee820026
[yaffs-website] / web / modules / contrib / ctools / tests / src / Kernel / TypedDataResolverTest.php
1 <?php
2
3 namespace Drupal\Tests\ctools\Kernel;
4
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Plugin\Context\Context;
7 use Drupal\Core\Plugin\Context\ContextDefinition;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\KernelTests\KernelTestBase;
10 use Drupal\user\Entity\User;
11
12 /**
13  * @coversDefaultClass \Drupal\ctools\TypedDataResolver
14  *
15  * @group CTools
16  */
17 class TypedDataResolverTest extends KernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['user', 'system', 'entity_test', 'ctools'];
25
26   /**
27    * @var \Drupal\ctools\TypedDataResolver
28    */
29   protected $typedDataResolver;
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36
37     $this->installSchema('system', 'sequences');
38     $this->installEntitySchema('user');
39
40     $this->typedDataResolver = \Drupal::service('ctools.typed_data.resolver');
41   }
42
43   /**
44    * Tests context extraction from properties.
45    */
46   public function testGetContextFromProperty() {
47     // Create a user and test entity to extract context from.
48     $user = User::create(['uid' => 2, 'name' => 'username', 'mail' => 'mail@example.org']);
49     $user->enforceIsNew(TRUE);
50     $user->save();
51     $entity_test = EntityTest::create(['user_id' => $user->id(), 'name' => 'Test name']);
52
53     // Test the language property.
54     $property_context = $this->assertPropertyPath($entity_test, 'langcode:language', 'language');
55     $this->assertEquals('en', $property_context->getContextValue()->getId());
56
57     // Test the reference to the user.
58     $property_context = $this->assertPropertyPath($entity_test, 'user_id:entity', 'entity:user');
59     $this->assertEquals($user->id(), $property_context->getContextValue()->id());
60
61     // Test the reference to the name.
62     $property_context = $this->assertPropertyPath($entity_test, 'name:value', 'string');
63     $this->assertEquals('Test name', $property_context->getContextValue());
64
65     // Test explicitly specifying the delta.
66     $property_context = $this->assertPropertyPath($entity_test, 'name:0:value', 'string');
67     $this->assertEquals('Test name', $property_context->getContextValue());
68
69     // Test following the reference.
70     $property_context = $this->assertPropertyPath($entity_test, 'user_id:entity:mail:value', 'email');
71     $this->assertEquals('mail@example.org', $property_context->getContextValue());
72   }
73
74   /**
75    * Asserts that a context for the given property path can be derived.
76    *
77    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
78    *   The entity to test with.
79    * @param $property_path
80    *   The property path to look for.
81    * @param $expected_data_type
82    *   The expected data type.
83    *
84    * @return \Drupal\Core\Plugin\Context\ContextInterface
85    *   The context with a value.
86    */
87   protected function assertPropertyPath(ContentEntityInterface $entity, $property_path, $expected_data_type)  {
88     $typed_data_entity = $entity->getTypedData();
89     $context_definition = new ContextDefinition($typed_data_entity->getDataDefinition()->getDataType());
90     $context_with_value = new Context($context_definition, $typed_data_entity);
91     $context_without_value = new Context($context_definition);
92
93     // Test the context without value.
94     $property_context = $this->typedDataResolver->getContextFromProperty($property_path, $context_without_value);
95     $this->assertEquals($expected_data_type, $property_context->getContextDefinition()->getDataType());
96
97     // Test the context with value.
98     $property_context = $this->typedDataResolver->getContextFromProperty($property_path, $context_with_value);
99     $this->assertEquals($expected_data_type, $property_context->getContextDefinition()->getDataType());
100
101     // Return the context with value so it can be asserted.
102     return $property_context;
103   }
104
105 }