You are here

class EntityReferenceRdfaTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/rdf/tests/src/Kernel/Field/EntityReferenceRdfaTest.php \Drupal\Tests\rdf\Kernel\Field\EntityReferenceRdfaTest
  2. 9 core/modules/rdf/tests/src/Kernel/Field/EntityReferenceRdfaTest.php \Drupal\Tests\rdf\Kernel\Field\EntityReferenceRdfaTest

Tests the RDFa output of the entity reference field formatter.

@group rdf

Hierarchy

Expanded class hierarchy of EntityReferenceRdfaTest

File

core/modules/rdf/tests/src/Kernel/Field/EntityReferenceRdfaTest.php, line 14

Namespace

Drupal\Tests\rdf\Kernel\Field
View source
class EntityReferenceRdfaTest extends FieldRdfaTestBase {
  use EntityReferenceTestTrait;

  /**
   * {@inheritdoc}
   */
  protected $fieldType = 'entity_reference';

  /**
   * The entity type used in this test.
   *
   * @var string
   */
  protected $entityType = 'entity_test';

  /**
   * The bundle used in this test.
   *
   * @var string
   */
  protected $bundle = 'entity_test';

  /**
   * The term for testing.
   *
   * @var \Drupal\taxonomy\Entity\Term
   */
  protected $targetEntity;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'text',
    'filter',
  ];
  protected function setUp() : void {
    parent::setUp();
    $this
      ->installEntitySchema('entity_test_rev');

    // Give anonymous users permission to view test entities.
    $this
      ->installConfig([
      'user',
    ]);
    Role::load(RoleInterface::ANONYMOUS_ID)
      ->grantPermission('view test entity')
      ->save();
    $this
      ->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);

    // Add the mapping.
    $mapping = rdf_get_mapping('entity_test', 'entity_test');
    $mapping
      ->setFieldMapping($this->fieldName, [
      'properties' => [
        'schema:knows',
      ],
    ])
      ->save();

    // Create the entity to be referenced.
    $this->targetEntity = $this->container
      ->get('entity_type.manager')
      ->getStorage($this->entityType)
      ->create([
      'name' => $this
        ->randomMachineName(),
    ]);
    $this->targetEntity
      ->save();

    // Create the entity that will have the entity reference field.
    $this->entity = $this->container
      ->get('entity_type.manager')
      ->getStorage($this->entityType)
      ->create([
      'name' => $this
        ->randomMachineName(),
    ]);
    $this->entity
      ->save();
    $this->entity->{$this->fieldName}->entity = $this->targetEntity;
    $this->uri = $this
      ->getAbsoluteUri($this->entity);
  }

  /**
   * Tests all the entity reference formatters.
   */
  public function testAllFormatters() {
    $entity_uri = $this
      ->getAbsoluteUri($this->targetEntity);

    // Tests the label formatter.
    $this
      ->assertFormatterRdfa([
      'type' => 'entity_reference_label',
    ], 'http://schema.org/knows', [
      'value' => $entity_uri,
      'type' => 'uri',
    ]);

    // Tests the entity formatter.
    $this
      ->assertFormatterRdfa([
      'type' => 'entity_reference_entity_view',
    ], 'http://schema.org/knows', [
      'value' => $entity_uri,
      'type' => 'uri',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceRdfaTest::$bundle protected property The bundle used in this test.
EntityReferenceRdfaTest::$entityType protected property The entity type used in this test.
EntityReferenceRdfaTest::$fieldType protected property The machine name of the field type to test. Overrides FieldRdfaTestBase::$fieldType
EntityReferenceRdfaTest::$modules protected static property Modules to enable. Overrides FieldRdfaTestBase::$modules
EntityReferenceRdfaTest::$targetEntity protected property The term for testing.
EntityReferenceRdfaTest::setUp protected function
EntityReferenceRdfaTest::testAllFormatters public function Tests all the entity reference formatters.
EntityReferenceTestTrait::createEntityReferenceField protected function Creates a field of an entity reference field storage on the specified bundle.
FieldRdfaTestBase::$debug protected property TRUE if verbose debugging is enabled.
FieldRdfaTestBase::$entity protected property The entity to render for testing.
FieldRdfaTestBase::$fieldName protected property The name of the field to create for testing.
FieldRdfaTestBase::$testValue protected property
FieldRdfaTestBase::$uri protected property The URI to identify the entity.
FieldRdfaTestBase::assertFormatterRdfa protected function Helper function to test the formatter's RDFa.
FieldRdfaTestBase::createTestField protected function Creates the field for testing.
FieldRdfaTestBase::getAbsoluteUri protected function Gets the absolute URI of an entity.
FieldRdfaTestBase::parseContent protected function Parses a content and return the html element.
FieldRdfaTestBase::xpathContent protected function Performs an xpath search on a certain content.
RdfParsingTrait::getElementByRdfTypeCount protected function Counts the number of resources of the provided type.
RdfParsingTrait::getElementRdfType protected function Gets type of RDF Element.
RdfParsingTrait::hasRdfChildProperty protected function Checks if a html document contains a resource with a given property value.
RdfParsingTrait::hasRdfProperty protected function Checks if a html document contains a resource with a given property value.
RdfParsingTrait::rdfElementIsBlankNode protected function Checks if RDF Node property is blank.