3 namespace Drupal\Tests\ctools\Kernel;
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;
13 * @coversDefaultClass \Drupal\ctools\TypedDataResolver
17 class TypedDataResolverTest extends KernelTestBase {
24 public static $modules = ['user', 'system', 'entity_test', 'ctools'];
27 * @var \Drupal\ctools\TypedDataResolver
29 protected $typedDataResolver;
34 protected function setUp() {
37 $this->installSchema('system', 'sequences');
38 $this->installEntitySchema('user');
40 $this->typedDataResolver = \Drupal::service('ctools.typed_data.resolver');
44 * Tests context extraction from properties.
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);
51 $entity_test = EntityTest::create(['user_id' => $user->id(), 'name' => 'Test name']);
53 // Test the language property.
54 $property_context = $this->assertPropertyPath($entity_test, 'langcode:language', 'language');
55 $this->assertEquals('en', $property_context->getContextValue()->getId());
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());
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());
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());
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());
75 * Asserts that a context for the given property path can be derived.
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.
84 * @return \Drupal\Core\Plugin\Context\ContextInterface
85 * The context with a value.
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);
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());
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());
101 // Return the context with value so it can be asserted.
102 return $property_context;