function TypedDataResolverTest::testGetContextFromProperty

Same name in other branches
  1. 4.0.x tests/src/Kernel/TypedDataResolverTest.php \Drupal\Tests\ctools\Kernel\TypedDataResolverTest::testGetContextFromProperty()

Tests context extraction from properties.

File

tests/src/Kernel/TypedDataResolverTest.php, line 47

Class

TypedDataResolverTest
@coversDefaultClass \Drupal\ctools\TypedDataResolver

Namespace

Drupal\Tests\ctools\Kernel

Code

public function testGetContextFromProperty() {
    // Create a user and test entity to extract context from.
    $user = User::create([
        'uid' => 2,
        'name' => 'username',
        'mail' => 'mail@example.org',
    ]);
    $user->enforceIsNew(TRUE);
    $user->save();
    $entity_test = EntityTest::create([
        'user_id' => $user->id(),
        'name' => 'Test name',
    ]);
    // Test the language property.
    $property_context = $this->assertPropertyPath($entity_test, 'langcode:language', 'language');
    $this->assertEquals('en', $property_context->getContextValue()
        ->getId());
    // Test the reference to the user.
    $property_context = $this->assertPropertyPath($entity_test, 'user_id:entity', 'entity:user');
    $this->assertEquals($user->id(), $property_context->getContextValue()
        ->id());
    // Test the reference to the name.
    $property_context = $this->assertPropertyPath($entity_test, 'name:value', 'string');
    $this->assertEquals('Test name', $property_context->getContextValue());
    // Test explicitly specifying the delta.
    $property_context = $this->assertPropertyPath($entity_test, 'name:0:value', 'string');
    $this->assertEquals('Test name', $property_context->getContextValue());
    // Test following the reference.
    $property_context = $this->assertPropertyPath($entity_test, 'user_id:entity:mail:value', 'email');
    $this->assertEquals('mail@example.org', $property_context->getContextValue());
}