Problem/Motivation

Similarly to #2034975: Test RDFa output in email formatter and other fields, we need to verify whether the default placement of the RDFa markup on the field wrapper is sufficient for the number field, and we need to add tests.

Proposed resolution

The markup will have to be altered when there is a prefix, suffix, or when there is a thousand separator. The easiest way to know is to check in NumericFormatterBase::viewElements() if the output is different from the value, in which case we will add a content attribute in the same way as the link formatter.

Tests should cover those combinations.

Remaining tasks

patch

User interface changes

none

API changes

none

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kay_v’s picture

using text formatter as pattern...

kay_v’s picture

FileSize
29.24 KB

Looks like number field formatter is working correctly (see attached screenshot, which uses generic schema:number markup in article content type)

To reproduce in, say the jobposting content type, add mapping to:
sites/files/config_*/active/rdf.mapping.node.[content-type].yml
(replace [content type] above with the name of the content type on which you've added the field)

Example mapping for base salary (add to the bottom of the yml file):
field_number:
properties:
- 'schema:baseSalary'

kay_v’s picture

testing just integer for now; needs also to test decimal and float

Status: Needs review » Needs work

The last submitted patch, 3: 2149257-3-NumberFieldRdfaTest.patch, failed testing.

scor’s picture

Issue tags: +RDF code sprint
krlucas’s picture

Status: Needs work » Needs review
FileSize
1.71 KB

Previous patch was missing a closing bracket. Re-rolling.

krlucas’s picture

Adds tests for decimal and float as well.

krlucas’s picture

Fix comment typos for float and decimal tests.

lokapujya’s picture

Status: Needs review » Needs work

The last submitted patch, 8: 2149257-8-NumberFieldRdfaTest.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
2.46 KB
2.09 KB

Reroll and added testValue to the class. Also had to change the field types (removed 'number_').

scor’s picture

Status: Needs review » Needs work

We need to extend the tests to cover the case when there is a prefix or a suffix and also thousand separators. This will also require to add some logic in the viewElements methods, I think we can achieve this by pushing a @content attribute to the field template similar to #2188889: Support RDFa output in link field formatter.

lokapujya’s picture

Assigned: Unassigned » lokapujya
Status: Needs work » Needs review
FileSize
3.47 KB

Rerolled and added some tests.

Plus added to the test setup because the display needs to be modified for the thousands separator. This part needs more work, and I'll post an interdiff later.

Status: Needs review » Needs work

The last submitted patch, 13: 2149257-13.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
2.22 KB

Rerolled because getInfo() was removed from tests.

lokapujya’s picture

Same as 13, now including the interdiff.

Status: Needs review » Needs work

The last submitted patch, 16: 2149257-16.patch, failed testing.

lokapujya’s picture

Right now the display value (with prefixes, suffixes, and separator) shows up when viewing a node, but not in the test output.

lokapujya’s picture

We are able to set the display mode settings with what we pass in the $formatter to assertFormatterRdfa(). However, the field instance settings need to be set differently, in the createTestField().

scor’s picture

Issue summary: View changes
lokapujya’s picture

Status: Needs work » Needs review
FileSize
4.47 KB
3.64 KB

A little bit closer. This works and seems to test all the functionality. But the tests can be improved some more still.

Questions:
Would we prefer to have only one field and pass the settings to the base class?
Do we want to separate any of the test cases? Right now the separator, prefix, and suffix are all within one test.
Do we need to test that the content attribute does not show up?

Status: Needs review » Needs work

The last submitted patch, 21: 2149257-21.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
4.47 KB
3.64 KB

EDIT: This wasn't the new patch.

Status: Needs review » Needs work

The last submitted patch, 23: 2149257-21.patch, failed testing.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
5.15 KB
2.45 KB

This is the version that passes the settings to the base class.

scor’s picture

Status: Needs review » Needs work
+++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
@@ -0,0 +1,116 @@
+  protected function createTestField($field_settings = array()) {

no longer needed I believe, since you fixed the parent class method.

+++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
@@ -0,0 +1,116 @@
+  protected function createMapping($field_settings = array()) {

We're doing more than creating a mapping here. I would name this method createTestEntity and take the createTestField() out of it.

+++ b/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php
@@ -105,7 +105,7 @@ protected function assertFormatterRdfa($formatter, $property, $expected_rdf_valu
+  protected function createTestField($field_settings = array()) {

$field_settings should be called $field_instance_settings to be more accurate.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
4.65 KB
3.2 KB

Changes from #26.

scor’s picture

Status: Needs review » Needs work
+++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
@@ -0,0 +1,96 @@
+        'url_plain' => FALSE,

url_plain is irrelevant here.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
6.46 KB
3.27 KB

Fixed #28 and added some tests for float and integer with settings.

scor’s picture

Status: Needs review » Needs work
+++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
@@ -0,0 +1,157 @@
+  /**
+   * Create the RDF mapping for the field.
+   */

Creates

lokapujya’s picture

Status: Needs work » Needs review
FileSize
7.15 KB
1.75 KB

Just posting this to show how I'm trying to do xpath.

lokapujya’s picture

Updated to use xpath (without using the Drupal assertFieldByXpath() function because it uses getUrl(). See related issue.)

The last submitted patch, 31: 2149257-31.patch, failed testing.

scor’s picture

Status: Needs review » Needs work

Looks like #32 is only using the xpathContent() on the testFloatFormatter() test, is that intentional? Seems to me we should be using on pretty much all the tests to see whether the @content is there or not. It should be there when we expect the value to differ from the one in the HTML element, and it should check that it's not there when the values are the same, no?

Patch looks great otherwise, I really like how this issue is evolving!

lokapujya’s picture

Status: Needs work » Needs review
FileSize
10.03 KB
3.57 KB
scor’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
    @@ -31,6 +31,10 @@ public function testIntegerFormatter() {
    +    // Test that the content attribute is not created.
    +    $result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content=:testValue]', array(':testValue' => $this->testValue));
    +    $this->assertFalse($result);
    

    Looks good, but I'd rather test for the presence of the content attribute period (regardless of the value is has).

  2. +++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
    @@ -53,6 +57,10 @@ public function testIntegerFormatterWithSettings() {
    +    // Test that the content attribute is not created.
    +    $result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content=:testValue]', array(':testValue' => $this->testValue));
    +    $this->assertTrue($result);
    

    In the positive case, I agree we should test the value of the content attribute. The comment needs to be fixed though, and the same applies to the other instances of the assertTrue() of the content attribute (comment is inaccurate).

lokapujya’s picture

Status: Needs work » Needs review
FileSize
10.23 KB
3.09 KB

Good points.

Added a test for no content attribute when the scale of a float changes.

scor’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php
    @@ -78,6 +78,11 @@ public function viewElements(FieldItemListInterface $items) {
    +      if ($item->value != $output) {
    +        if (!empty($item->_attributes)) {
    +          $item->_attributes += array('content' => $item->value);
    +        }
    +      }
    

    Could add a comment to give some context: Output the raw value in a content attribute if the text of the HTML element differs from the raw value (for example when a prefix is used)

  2. +++ b/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php
    @@ -131,4 +133,52 @@ protected function getAbsoluteUri($entity) {
    +   * Parse a content and return the html element.
    

    "Parses"

  3. +++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
    @@ -0,0 +1,185 @@
    + * Tests RDFa output by number field formatters.
    ...
    +  /**
    +   * The 'value' property value for testing.
    +   *
    +   * @var string
    +   */
    +  protected $testValue = 'test_text_value';
    

    the testvalue property is already set by the base test class, and we are setting it inside each test method, so I don't think it's needed here at all.

  4. +++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
    @@ -0,0 +1,185 @@
    +    // Test that the content attribute is not created.
    +    $result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content]', array(':testValue' => $this->testValue));
    +    $this->assertFalse($result);
    

    Looks good now, though we no longer use the :testValue in the XPath expression anymore, so it can be removed from the arguments. (same for all other negative tests).

  5. +++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
    @@ -0,0 +1,185 @@
    +  /**
    +   * Tests the float formatter with a scale.
    +   */
    +  public function testFloatFormatterWithScale() {
    +    $this->fieldType = 'float';
    +    $formatter = array(
    +      'type' => $this->fieldType,
    +      'settings' => array(
    +        'scale' => 5,
    +      ),
    +    );
    +    $this->testValue = 3.33;
    +    $this->createTestField();
    +    $this->createTestEntity();
    +    $this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $this->testValue));
    +
    +    // Test that the content attribute is not created.
    +    $result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content]', array(':testValue' => $this->testValue));
    +    $this->assertFalse($result);
    +  }
    

    I'm was surprised this passed and that we didn't have a content attribute with the scale setting, but this is because the scale is not exercised. The test value should include more than 5 digits after the ".". In which case there should be a content attribute.

lokapujya’s picture

5. Scale is not exercised, but zeroes are added and we get no content attribute. Is that ok?

lokapujya’s picture

Status: Needs work » Needs review
FileSize
10.84 KB
6.23 KB

All 5 points incorporated. Added the new test for a scale that is exercised and left the old one in.

scor’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php
@@ -78,6 +78,13 @@ public function viewElements(FieldItemListInterface $items) {
+      // Output the raw value in a content attribute if the text of the HTML
+      // element differs from the raw value (for example when a prefix is used).
+      if ($item->value != $output) {
+        if (!empty($item->_attributes)) {
+          $item->_attributes += array('content' => $item->value);
+        }
+      }

These 2 if conditions could go on the same line, with the empty() being first. Otherwise the rest looks good.

lokapujya’s picture

Status: Needs work » Needs review
FileSize
10.81 KB
986 bytes

merged if statements.

scor’s picture

Status: Needs review » Reviewed & tested by the community

Thanks Jamie, great work as usual! Probably the most sophisticated field to test!

  • alexpott committed b55880f on 8.0.x
    Issue #2149257 by lokapujya, krlucas, kay_v | scor: Support RDFa output...
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed b55880f and pushed to 8.0.x. Thanks!

diff --git a/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php b/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php
index 02652ab..e14eb91 100644
--- a/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php
+++ b/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php
@@ -110,6 +110,9 @@ protected function assertFormatterRdfa($formatter, $property, $expected_rdf_valu
 
   /**
    * Creates the field for testing.
+   *
+   * @param array $field_settings
+   *   (optional) An array of field settings.
    */
   protected function createTestField($field_settings = array()) {
     entity_create('field_storage_config', array(
diff --git a/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
index ae094bb..a8a607d 100644
--- a/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
+++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php
@@ -6,8 +6,6 @@
 
 namespace Drupal\rdf\Tests\Field;
 
-use Drupal\rdf\Tests\Field\FieldRdfaTestBase;
-
 /**
  * Tests RDFa output by number field formatters.
  *

Fixed on commit.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.