You are here

class NumberFieldRdfaTest in Drupal 10

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

Tests RDFa output by number field formatters.

@group rdf

Hierarchy

Expanded class hierarchy of NumberFieldRdfaTest

File

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

Namespace

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

  /**
   * Tests the integer formatter.
   */
  public function testIntegerFormatter() {
    $this->fieldType = 'integer';
    $testValue = 3;
    $this
      ->createTestField();
    $this
      ->createTestEntity($testValue);
    $this
      ->assertFormatterRdfa([
      'type' => 'number_integer',
    ], 'http://schema.org/baseSalary', [
      'value' => $testValue,
    ]);

    // Test that the content attribute is not created.
    $result = $this
      ->xpathContent($this
      ->getRawContent(), '//div[contains(@class, "field__items") and @content]');
    $this
      ->assertEmpty($result);
  }

  /**
   * Tests the integer formatter with settings.
   */
  public function testIntegerFormatterWithSettings() {
    \Drupal::service('theme_installer')
      ->install([
      'classy',
    ]);
    $this
      ->config('system.theme')
      ->set('default', 'classy')
      ->save();
    $this->fieldType = 'integer';
    $formatter = [
      'type' => 'number_integer',
      'settings' => [
        'thousand_separator' => '.',
        'prefix_suffix' => TRUE,
      ],
    ];
    $testValue = 3333333.33;
    $field_settings = [
      'prefix' => '#',
      'suffix' => ' llamas.',
    ];
    $this
      ->createTestField($field_settings);
    $this
      ->createTestEntity($testValue);
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', [
      'value' => $testValue,
    ]);

    // Test that the content attribute is created.
    $result = $this
      ->xpathContent($this
      ->getRawContent(), '//div[contains(@class, "field__item") and @content=:testValue]', [
      ':testValue' => $testValue,
    ]);
    $this
      ->assertNotEmpty($result);
  }

  /**
   * Tests the float formatter.
   */
  public function testFloatFormatter() {
    $this->fieldType = 'float';
    $testValue = 3.33;
    $this
      ->createTestField();
    $this
      ->createTestEntity($testValue);
    $this
      ->assertFormatterRdfa([
      'type' => 'number_unformatted',
    ], 'http://schema.org/baseSalary', [
      'value' => $testValue,
    ]);

    // Test that the content attribute is not created.
    $result = $this
      ->xpathContent($this
      ->getRawContent(), '//div[contains(@class, "field__items") and @content]');
    $this
      ->assertEmpty($result);
  }

  /**
   * Tests the float formatter with settings.
   */
  public function testFloatFormatterWithSettings() {
    \Drupal::service('theme_installer')
      ->install([
      'classy',
    ]);
    $this
      ->config('system.theme')
      ->set('default', 'classy')
      ->save();
    $this->fieldType = 'float';
    $formatter = [
      'type' => 'number_decimal',
      'settings' => [
        'thousand_separator' => '.',
        'decimal_separator' => ',',
        'prefix_suffix' => TRUE,
      ],
    ];
    $testValue = 3333333.33;
    $field_settings = [
      'prefix' => '$',
      'suffix' => ' more.',
    ];
    $this
      ->createTestField($field_settings);
    $this
      ->createTestEntity($testValue);
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', [
      'value' => $testValue,
    ]);

    // Test that the content attribute is created.
    $result = $this
      ->xpathContent($this
      ->getRawContent(), '//div[contains(@class, "field__item") and @content=:testValue]', [
      ':testValue' => $testValue,
    ]);
    $this
      ->assertNotEmpty($result);
  }

  /**
   * Tests the float formatter with a scale. Scale is not exercised.
   */
  public function testFloatFormatterWithScale() {
    $this->fieldType = 'float';
    $formatter = [
      'type' => 'number_decimal',
      'settings' => [
        'scale' => 5,
      ],
    ];
    $testValue = 3.33;
    $this
      ->createTestField();
    $this
      ->createTestEntity($testValue);
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', [
      'value' => $testValue,
    ]);

    // Test that the content attribute is not created.
    $result = $this
      ->xpathContent($this
      ->getRawContent(), '//div[contains(@class, "field__items") and @content]');
    $this
      ->assertEmpty($result);
  }

  /**
   * Tests the float formatter with a scale. Scale is exercised.
   */
  public function testFloatFormatterWithScaleExercised() {
    \Drupal::service('theme_installer')
      ->install([
      'classy',
    ]);
    $this
      ->config('system.theme')
      ->set('default', 'classy')
      ->save();
    $this->fieldType = 'float';
    $formatter = [
      'type' => 'number_decimal',
      'settings' => [
        'scale' => 5,
      ],
    ];
    $testValue = 3.1234567;
    $this
      ->createTestField();
    $this
      ->createTestEntity($testValue);
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', [
      'value' => $testValue,
    ]);

    // Test that the content attribute is created.
    $result = $this
      ->xpathContent($this
      ->getRawContent(), '//div[contains(@class, "field__item") and @content=:testValue]', [
      ':testValue' => $testValue,
    ]);
    $this
      ->assertNotEmpty($result);
  }

  /**
   * Tests the decimal formatter.
   */
  public function testDecimalFormatter() {
    $this->fieldType = 'decimal';
    $testValue = 3.33;
    $this
      ->createTestField();
    $this
      ->createTestEntity($testValue);
    $this
      ->assertFormatterRdfa([
      'type' => 'number_decimal',
    ], 'http://schema.org/baseSalary', [
      'value' => $testValue,
    ]);

    // Test that the content attribute is not created.
    $result = $this
      ->xpathContent($this
      ->getRawContent(), '//div[contains(@class, "field__items") and @content]');
    $this
      ->assertEmpty($result);
  }

  /**
   * Tests the decimal formatter with settings.
   */
  public function testDecimalFormatterWithSettings() {
    \Drupal::service('theme_installer')
      ->install([
      'classy',
    ]);
    $this
      ->config('system.theme')
      ->set('default', 'classy')
      ->save();
    $this->fieldType = 'decimal';
    $formatter = [
      'type' => 'number_decimal',
      'settings' => [
        'thousand_separator' => 't',
        'decimal_separator' => '#',
        'prefix_suffix' => TRUE,
      ],
    ];
    $testValue = 3333333.33;
    $field_settings = [
      'prefix' => '$',
      'suffix' => ' more.',
    ];
    $this
      ->createTestField($field_settings);
    $this
      ->createTestEntity($testValue);
    $this
      ->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', [
      'value' => $testValue,
    ]);

    // Test that the content attribute is created.
    $result = $this
      ->xpathContent($this
      ->getRawContent(), '//div[contains(@class, "field__item") and @content=:testValue]', [
      ':testValue' => $testValue,
    ]);
    $this
      ->assertNotEmpty($result);
  }

  /**
   * Creates the RDF mapping for the field.
   */
  protected function createTestEntity($testValue) {

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

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

}

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::$fieldType protected property The machine name of the field type to test. 8
FieldRdfaTestBase::$modules protected static property Modules to enable. 7
FieldRdfaTestBase::$testValue protected property 4
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.
NumberFieldRdfaTest::createTestEntity protected function Creates the RDF mapping for the field.
NumberFieldRdfaTest::testDecimalFormatter public function Tests the decimal formatter.
NumberFieldRdfaTest::testDecimalFormatterWithSettings public function Tests the decimal formatter with settings.
NumberFieldRdfaTest::testFloatFormatter public function Tests the float formatter.
NumberFieldRdfaTest::testFloatFormatterWithScale public function Tests the float formatter with a scale. Scale is not exercised.
NumberFieldRdfaTest::testFloatFormatterWithScaleExercised public function Tests the float formatter with a scale. Scale is exercised.
NumberFieldRdfaTest::testFloatFormatterWithSettings public function Tests the float formatter with settings.
NumberFieldRdfaTest::testIntegerFormatter public function Tests the integer formatter.
NumberFieldRdfaTest::testIntegerFormatterWithSettings public function Tests the integer formatter with settings.
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.