public function FieldTest::testEntityReferenceTokens in Token 8
File
- tests/
src/ Kernel/ FieldTest.php, line 465
Class
- FieldTest
- Tests field tokens.
Namespace
Drupal\Tests\token\KernelCode
public function testEntityReferenceTokens() {
$reference = Node::create([
'title' => 'Test node to reference',
'type' => 'article',
'test_field' => [
'value' => 'foo',
'format' => $this->testFormat
->id(),
],
]);
$reference
->save();
$term_reference_field_value = $this
->randomString();
$term_reference = $this
->createTerm($this->vocabulary, [
'name' => 'Term to reference',
'term_field' => [
'value' => $term_reference_field_value,
'format' => $this->testFormat
->id(),
],
]);
$entity = Node::create([
'title' => 'Test entity reference',
'type' => 'article',
'test_reference' => [
'target_id' => $reference
->id(),
],
'test_term_reference' => [
'target_id' => $term_reference
->id(),
],
]);
$entity
->save();
$this
->assertTokens('node', [
'node' => $entity,
], [
'test_reference:entity:title' => Markup::create('Test node to reference'),
'test_reference:entity:test_field' => Markup::create('foo'),
'test_term_reference:entity:term_field' => Html::escape($term_reference_field_value),
'test_reference:target_id' => $reference
->id(),
'test_term_reference:target_id' => $term_reference
->id(),
'test_term_reference:entity:url:path' => '/' . $term_reference
->toUrl('canonical')
->getInternalPath(),
// Expects the entity's label to be returned for :entity tokens.
'test_reference:entity' => $reference
->label(),
'test_term_reference:entity' => $term_reference
->label(),
]);
// Test some non existent tokens.
$this
->assertNoTokens('node', [
'node' => $entity,
], [
'test_reference:1:title',
'test_reference:entity:does_not_exist',
'test_reference:does_not:exist',
'test_term_reference:does_not_exist',
'test_term_reference:does:not:exist',
'test_term_reference:does_not_exist:0',
'non_existing_field:entity:title',
]);
/** @var \Drupal\token\Token $token_service */
$token_service = \Drupal::service('token');
$token_info = $token_service
->getTokenInfo('node', 'test_reference');
$this
->assertEquals('Test reference', $token_info['name']);
$this
->assertEquals('Entity reference field.', (string) $token_info['description']);
$this
->assertEquals('token', $token_info['module']);
$this
->assertEquals('node-test_reference', $token_info['type']);
// Test target_id field property token info.
$token_info = $token_service
->getTokenInfo('node-test_reference', 'target_id');
$this
->assertEquals('Content ID', $token_info['name']);
$this
->assertEquals('token', $token_info['module']);
$this
->assertEquals('token', $token_info['module']);
// Test entity field property token info.
$token_info = $token_service
->getTokenInfo('node-test_reference', 'entity');
$this
->assertEquals('Content', $token_info['name']);
$this
->assertEquals('The referenced entity', $token_info['description']);
$this
->assertEquals('token', $token_info['module']);
$this
->assertEquals('node', $token_info['type']);
// Test entity field property token info of the term reference.
$token_info = $token_service
->getTokenInfo('node-test_term_reference', 'entity');
$this
->assertEquals('Taxonomy term', $token_info['name']);
$this
->assertEquals('The referenced entity', $token_info['description']);
$this
->assertEquals('token', $token_info['module']);
$this
->assertEquals('term', $token_info['type']);
}