Problem/Motivation

In #2121713: Move drupal_html_id() and drupal_html_class() to Drupal\Component\Utility, HTML-related code was moved into a utility class under the Component namespace.

Somehow the unit tests ended up under the Core namespace.

Furthermore, in #2294503: Component Utilities unit test cleanups. some extra tests were created for the Html class.

Proposed resolution

Move the tests to the Component namespace and add the other tests.

Remaining tasks

User interface changes

API changes

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Unfrozen changes Unfrozen because it fixes a namespace error in automated tests.

Comments

mile23’s picture

Status: Active » Needs review
StatusFileSize
new15.27 KB
new13.61 KB
new15.27 KB

Here are three patches, two set to test.

One is just a file move from core/tests/Drupal/Tests/Component/Utility/HtmlTest.php to core/tests/Drupal/Tests/Core/Utility/HtmlTest.php.

One adds minor coding standards changes.

One adds the tests from #2382011: Expand unit testing for Drupal\Component\Utility\UserAgent (and originally: #2294503: Component Utilities unit test cleanups.), including comments here: https://www.drupal.org/node/2382011#comment-9384979

The last submitted patch, 1: 2383945_1_file_move_standards.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 1: 2383945_1_file_move_add_2382011.patch, failed testing.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new15.27 KB

Let's try that again....

This patch moves HtmlTest to the Component namespace and adds tests.

mile23’s picture

StatusFileSize
new2.01 KB

OK, since that's working, here are the tests that were added, merged in from #2382011: Expand unit testing for Drupal\Component\Utility\UserAgent as mentioned above.

daffie’s picture

Status: Needs review » Needs work

I have two remarks for this patch.

+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -0,0 +1,278 @@
+    $property = new \ReflectionProperty('Drupal\Component\Utility\Html', 'seenIdsInit');
+    $property->setAccessible(TRUE);
+    $property->setValue(NULL);

Probably a stupid question: Why do we need this? The variable $property is local?

+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -0,0 +1,278 @@
+  public function testHtmlClass() {

Why is this function not called testGetClass? The same for the other functions?

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new15.37 KB
new1.16 KB
+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -0,0 +1,278 @@
+    $property = new \ReflectionProperty('Drupal\Component\Utility\Html', 'seenIdsInit');
+    $property->setAccessible(TRUE);
+    $property->setValue(NULL);

Probably a stupid question: Why do we need this? The variable $property is local?

Not a stupid question. :-) Reflections are a little counter-intutive.

$property is a local variable but it holds a ReflectionProperty that refers to a property of another object. In this case we need to poke a NULL value into Html::seenIdsInit to reinitialize it. Since it's static and all. What a pain. :-)

Why is this function not called testGetClass? The same for the other functions?

Yes, that's probably a better name. I was just moving those tests over without changing them too much.

So in this patch I changed $property to a more useful name, and also changed that function to testGetClass() to be consistent with our coding standards.

daffie’s picture

Status: Needs review » Needs work

@Mile23: I know what reflections are. Mostly. But my remark was that you put it in a local function variable. As far as I know you cannot use that variable outside of the function. Or am I wrong?

@Mile23: You only changed testHtmlClass(). There are more testHtml* functions.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new15.33 KB
new2.86 KB

@Mile23: I know what reflections are. Mostly. But my remark was that you put it in a local function variable. As far as I know you cannot use that variable outside of the function. Or am I wrong?

Right, the local variable within the method goes out of scope as soon as setUp() ends.

However, since seenIdsInit is a static property (of a static class, no less), the value we set using reflection will 'keep.' It's just a glorified global, really.

setUp() gets run before every test method, so now that variable is in a known state so the test can run. Some tests don't need it, others do, but it's just easy to do that initialization every time.

And... so I did miss a few other method names.. :-)

daffie’s picture

Status: Needs review » Needs work

@Mile23: Thank you for explaining. Again I learned something new.

+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -0,0 +1,279 @@
+   * Provides test data for testHtmlGetId().
...
+   * Provides test data for testHtmlGetId().
...
+   * Provides test data for testHtmlGetId().

The function name has changed to testGetId()

mile23’s picture

Ugh. Forget this comment. I shouldn't code before second cup of coffee.

mile23’s picture

StatusFileSize
new15.01 KB
new5.24 KB

Changes from #10, plus some docblock rearrangement for PHPUnit coding standards (document @returns, not @params for data providers).

mile23’s picture

Status: Needs work » Needs review
daffie’s picture

Status: Needs review » Needs work

Some minor comment remarks.

+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -0,0 +1,257 @@
+  /**
+   * Tests the Html::getUniqueId() method.
+   *
+   * @dataProvider providerTestGetUniqueIdWithAjaxIds
+   * @covers ::getUniqueId
+   */
+  public function testGetUniqueIdWithAjaxIds($expected, $source, $reset = FALSE) {

The first line: We are testing Html::getUniqueIdWithAjaxIds().

+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -0,0 +1,257 @@
+  /**
+   * Tests the Html::getUniqueId() method.
+   *
+   * @dataProvider providerTestGetId
+   * @covers ::getId
+   */
+  public function testGetId($expected, $source) {

The first line: We are testing Html::getId().

+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -0,0 +1,257 @@
+   * Test HTML document loading.
...
+   * Test HTML document serialization.
...
+   * Test HTML normalization.

In the rest of the class it is: "Tests the Html::theTestMethod() method."

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new14.68 KB
new1.64 KB

The first line: We are testing Html::getUniqueIdWithAjaxIds().

Nope. :-)

But anyway, removed all short summary lines.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The HtmlTest has been moved and has been extended.
The old test file has been deleted.
The comments are all in order.
It looks good to me so I give it a RTBC.

tstoeckler’s picture

Not downgrading but in these cases it really helps to roll patches with -C -M in order to allow for easier reviewing.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

The serialise test in giving a false impression of test coverage.

  public static function serialize(\DOMDocument $document) {
    $body_node = $document->getElementsByTagName('body')->item(0);
    $html = '';

    foreach ($body_node->getElementsByTagName('script') as $node) {
      static::escapeCdataElement($node);
    }
    foreach ($body_node->getElementsByTagName('style') as $node) {
      static::escapeCdataElement($node, '/*', '*/');
    }
    foreach ($body_node->childNodes as $node) {
      $html .= $document->saveXML($node);
    }
    return $html;
  }

We should be passing in some script and styles to test that that code has the expected affect.

alexpott’s picture

Also can the move and test additions and fixes happen in separate issues.

mile23’s picture

Title: Move Drupal\Tests\Core\Utility\HtmlTest to the proper namespace, expand test coverage » Expand test coverage for
Status: Needs work » Postponed
Related issues: +#2392673: Move Drupal\Tests\Core\Utility\HtmlTest to the proper namespace

Setting to postponed based on #2392673: Move Drupal\Tests\Core\Utility\HtmlTest to the proper namespace

This will obviously need a re-roll when that drops.

mile23’s picture

Title: Expand test coverage for » Expand test coverage for Drupal\Tests\Component\Utility\HtmlTest
daffie’s picture

Status: Postponed » Needs work
mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new9.77 KB

Straight-up reroll from #15.

daffie’s picture

Status: Needs review » Needs work
 * Tests \Drupal\Component\Utility\Html.
 *

This is already covered by @coversDefaultClass.

+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -67,47 +60,42 @@ public function providerTestCleanCssIdentifier() {
    * Tests that Html::getClass() cleans the class name properly.
    *

Can we remove this. We already have @covers ::getClass.

Can we rename providerTestCleanCssIdentifier() to providerCleanCssIdentifier().

Can we rename providerTestGetUniqueId() to providerGetUniqueId().

Can we rename providerTestGetUniqueIdWithAjaxIds() to providerGetUniqueIdWithAjaxIds().

Can we rename providerTestGetId() to providerGetId().

+++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@@ -210,4 +186,58 @@ public function providerTestHtmlGetId() {
+  public function providerTestLoad() {

Can we rename this provider function. It is used multiple times. So it can become providerLoad() or providerHtml().

In the function testLoad we do not have an assertion like:

$this->assertEquals($expected, $doc);

Why not? If we add such an assertion, can we make $expected the first parameter in the testLoad() method.

Status: Needs work » Needs review

daffie queued 23: 2383945_23.patch for re-testing.

daffie’s picture

Status: Needs review » Needs work

Still needs work

franksj’s picture

Status: Needs work » Needs review
StatusFileSize
new9.87 KB

I addressed each of @daffie's comments except for this guy:

In the function testLoad we do not have an assertion like:

$this->assertEquals($expected, $doc);
Why not? If we add such an assertion, can we make $expected the first parameter in the testLoad() method.

It looks like we're analyzing $doc by looking at its tags. Are you suggesting asserting that the entire doc is equal to something expected? Do you mind clarifying?

franksj’s picture

StatusFileSize
new4.61 KB

Whoops, forgot my interdiff.

mile23’s picture

In the function testLoad we do not have an assertion like:

$this->assertEquals($expected, $doc);
Why not? If we add such an assertion, can we make $expected the first parameter in the testLoad() method.

We're testing that given funky HTML we end up with good normalized HTML, and that we have the proper number of elements. We can't really generate a \DomDocument for some of the data, and we also can't compare equality for it.

josephdpurcell’s picture

Now reviewing.

josephdpurcell’s picture

Status: Needs review » Reviewed & tested by the community

I looked at the full patch, looks good.

I looked at the interdiff to see if it matched all of daffie's comments from #24 at it looks good.

Regarding daffie's comment about testLoad, it seems to me that the tests that are there for testLoad are sufficient for merging. If someone wanted to expand on them more I suggest a follow up.

Marking as RTCB.

tim.plunkett’s picture

Title: Expand test coverage for Drupal\Tests\Component\Utility\HtmlTest » Expand test coverage for Drupal\Component\Utility\Html
  1. +++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
    @@ -25,24 +23,14 @@ class HtmlTest extends UnitTestCase {
    -    $property = new \ReflectionProperty('Drupal\Component\Utility\Html', 'seenIdsInit');
    -    $property->setAccessible(TRUE);
    -    $property->setValue(NULL);
    ...
    +    $ref_seen_ids_init = new \ReflectionProperty('Drupal\Component\Utility\Html', 'seenIdsInit');
    +    $ref_seen_ids_init->setAccessible(TRUE);
    +    $ref_seen_ids_init->setValue(NULL);
    

    This doesn't actually change anything, just adding to the patch size.

  2. +++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
    @@ -210,4 +182,58 @@ public function providerTestHtmlGetId() {
    +    return array(
    

    Here and elsewhere, use [] for new code.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

The issue summary needs an update because we no longer move code around.

@tim.plunkett if they change all the new stuff to [] then this file will be half new half old... so I don't think that's really worth it - as far as I remember the current agreement is each file should be internally consistent. I'm not particularly bothered by #32.1 - fine either way by me - although in general I side on the lease change to achieve the task in the issue summary - which in this case is to add more test coverage and therefore in reality this change is out of scope.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)
Issue tags: +stale-issue-cleanup

Thank you for creating this issue to improve Drupal.

We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

Thanks!

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Active

This one actually appears to still be relevant

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.