You are here

class LinkFieldRdfaTest in Drupal 10

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

Tests the placement of RDFa in link field formatters.

@group rdf

Hierarchy

Expanded class hierarchy of LinkFieldRdfaTest

File

core/modules/rdf/tests/src/Kernel/Field/LinkFieldRdfaTest.php, line 12

Namespace

Drupal\Tests\rdf\Kernel\Field
View source
class LinkFieldRdfaTest extends FieldRdfaTestBase {

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

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'link',
    'text',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this
      ->createTestField();

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

  /**
   * Tests all formatters with link to external page.
   */
  public function testAllFormattersExternal() {

    // Set up test values.
    $this->testValue = 'http://test.me/foo/bar/neque/porro/quisquam/est/qui-dolorem?path=foo/bar/neque/porro/quisquam/est/qui-dolorem';
    $this->entity = EntityTest::create([]);
    $this->entity->{$this->fieldName}->uri = $this->testValue;

    // Set up the expected result.
    $expected_rdf = [
      'value' => $this->testValue,
      'type' => 'uri',
    ];
    $this
      ->runTestAllFormatters($expected_rdf, 'external');
  }

  /**
   * Tests all formatters with link to internal page.
   */
  public function testAllFormattersInternal() {

    // Set up test values.
    $this->testValue = 'admin';
    $this->entity = EntityTest::create([]);
    $this->entity->{$this->fieldName}->uri = 'internal:/admin';

    // Set up the expected result.
    // AssertFormatterRdfa looks for a full path.
    $expected_rdf = [
      'value' => $this->uri . '/' . $this->testValue,
      'type' => 'uri',
    ];
    $this
      ->runTestAllFormatters($expected_rdf, 'internal');
  }

  /**
   * Tests all formatters with link to frontpage.
   */
  public function testAllFormattersFront() {

    // Set up test values.
    $this->testValue = '/';
    $this->entity = EntityTest::create([]);
    $this->entity->{$this->fieldName}->uri = 'internal:/';

    // Set up the expected result.
    $expected_rdf = [
      'value' => $this->uri . '/',
      'type' => 'uri',
    ];
    $this
      ->runTestAllFormatters($expected_rdf, 'front');
  }

  /**
   * Helper function to test all link formatters.
   */
  public function runTestAllFormatters($expected_rdf, $type = NULL) {

    // Test the link formatter: trim at 80, no other settings.
    $formatter = [
      'type' => 'link',
      'settings' => [
        'trim_length' => 80,
        'url_only' => FALSE,
        'url_plain' => FALSE,
        'rel' => '',
        'target' => '',
      ],
    ];
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);

    // Test the link formatter: trim at 40, nofollow, new window.
    $formatter = [
      'type' => 'link',
      'settings' => [
        'trim_length' => 40,
        'url_only' => FALSE,
        'url_plain' => FALSE,
        'rel' => 'nofollow',
        'target' => '_blank',
      ],
    ];
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);

    // Test the link formatter: trim at 40, URL only (not plaintext) nofollow,
    // new window.
    $formatter = [
      'type' => 'link',
      'settings' => [
        'trim_length' => 40,
        'url_only' => TRUE,
        'url_plain' => FALSE,
        'rel' => 'nofollow',
        'target' => '_blank',
      ],
    ];
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);

    // Test the link_separate formatter: trim at 40, nofollow, new window.
    $formatter = [
      'type' => 'link_separate',
      'settings' => [
        'trim_length' => 40,
        'rel' => 'nofollow',
        'target' => '_blank',
      ],
    ];
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);

    // Change the expected value here to literal. When formatted as plaintext
    // then the RDF is expecting a 'literal' not a 'uri'.
    $expected_rdf = [
      'value' => $this->testValue,
      'type' => 'literal',
    ];

    // Test the link formatter: trim at 20, url only (as plaintext.)
    $formatter = [
      'type' => 'link',
      'settings' => [
        'trim_length' => 20,
        'url_only' => TRUE,
        'url_plain' => TRUE,
        'rel' => '0',
        'target' => '0',
      ],
    ];
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);

    // Test the link formatter: do not trim, url only (as plaintext.)
    $formatter = [
      'type' => 'link',
      'settings' => [
        'trim_length' => 0,
        'url_only' => TRUE,
        'url_plain' => TRUE,
        'rel' => '0',
        'target' => '0',
      ],
    ];
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
LinkFieldRdfaTest::$fieldType protected property The machine name of the field type to test. Overrides FieldRdfaTestBase::$fieldType
LinkFieldRdfaTest::$modules protected static property Modules to enable. Overrides FieldRdfaTestBase::$modules
LinkFieldRdfaTest::runTestAllFormatters public function Helper function to test all link formatters.
LinkFieldRdfaTest::setUp protected function
LinkFieldRdfaTest::testAllFormattersExternal public function Tests all formatters with link to external page.
LinkFieldRdfaTest::testAllFormattersFront public function Tests all formatters with link to frontpage.
LinkFieldRdfaTest::testAllFormattersInternal public function Tests all formatters with link to internal page.
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.