3 namespace Drupal\Tests\rdf\Kernel\Field;
5 use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
6 use Drupal\user\Entity\Role;
7 use Drupal\user\RoleInterface;
10 * Tests the RDFa output of the entity reference field formatter.
14 class EntityReferenceRdfaTest extends FieldRdfaTestBase {
16 use EntityReferenceTestTrait;
21 protected $fieldType = 'entity_reference';
24 * The entity type used in this test.
28 protected $entityType = 'entity_test';
31 * The bundle used in this test.
35 protected $bundle = 'entity_test';
38 * The term for testing.
40 * @var \Drupal\taxonomy\Entity\Term
42 protected $targetEntity;
47 public static $modules = ['text', 'filter'];
49 protected function setUp() {
52 $this->installEntitySchema('entity_test_rev');
54 // Give anonymous users permission to view test entities.
55 $this->installConfig(['user']);
56 Role::load(RoleInterface::ANONYMOUS_ID)
57 ->grantPermission('view test entity')
60 $this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);
63 $mapping = rdf_get_mapping('entity_test', 'entity_test');
64 $mapping->setFieldMapping($this->fieldName, [
65 'properties' => ['schema:knows'],
68 // Create the entity to be referenced.
69 $this->targetEntity = $this->container->get('entity_type.manager')
70 ->getStorage($this->entityType)
71 ->create(['name' => $this->randomMachineName()]);
72 $this->targetEntity->save();
74 // Create the entity that will have the entity reference field.
75 $this->entity = $this->container->get('entity_type.manager')
76 ->getStorage($this->entityType)
77 ->create(['name' => $this->randomMachineName()]);
78 $this->entity->save();
79 $this->entity->{$this->fieldName}->entity = $this->targetEntity;
80 $this->uri = $this->getAbsoluteUri($this->entity);
84 * Tests all the entity reference formatters.
86 public function testAllFormatters() {
87 $entity_uri = $this->getAbsoluteUri($this->targetEntity);
89 // Tests the label formatter.
90 $this->assertFormatterRdfa(['type' => 'entity_reference_label'], 'http://schema.org/knows', ['value' => $entity_uri, 'type' => 'uri']);
91 // Tests the entity formatter.
92 $this->assertFormatterRdfa(['type' => 'entity_reference_entity_view'], 'http://schema.org/knows', ['value' => $entity_uri, 'type' => 'uri']);