Problem/Motivation

I am trying to build Drupal 8 RPMs for Fedora/EPEL and I am required to unbundle libraries. One such bundled library is PHPUnit. Drupal 8 requires PHPUnit 4.1 while latest Fedora/EPEL provides 4.2 and there are a few incompatibility issues with deprecated functions -- see https://github.com/sebastianbergmann/phpunit/issues/1292 as to why they are deprecated.

  • PHPUnit_Framework_Assert::assertSelectEquals is deprecated
    • core/tests/Drupal/Tests/Core/Template/AttributeTest.php
      • Drupal\Tests\Core\Template\AttributeTest::testPrint
  • PHPUnit_Framework_Assert::assertTag is deprecated
    • core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateHrefs with data set #0 ('test_route_1', array(), false, '/test-route-1')
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateHrefs with data set #1 ('test_route_2', array('example'), false, '/test-route-2/example')
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateHrefs with data set #2 ('test_route_3', array(), true, 'http://example.com/test-route-3')
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateFromUrl
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateFromUrlExternal
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateAttributes
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateQuery
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateParametersAsQuery
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateOptions
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateXss
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateWithHtml
      • Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateActive

https://github.com/phpunit/phpunit-dom-assertions

Proposed resolution

Remaining tasks

User interface changes

API changes

Original report by @siwinski

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

siwinski’s picture

Issue summary: View changes
dawehner’s picture

I guess you could replace it with using symfony css selector + xpath without such a big effort.

siwinski’s picture

It looks like that's what https://github.com/phpunit/phpunit-dom-assertions (mentioned in the phpunit issue linked above) is intended to do. It also looks like assertSelectEquals is implemented but assertTag is not yet.

jhedstrom’s picture

Title: PHPUnit deprecated asserts » PHPUnit deprecated assertTag()
daffie’s picture

Title: PHPUnit deprecated assertTag() » PHPUnit deprecated assertTag() and assertSelectEquals()
daffie’s picture

Is it an idea to copy the functions assertTag() and assertSelectEquals() to the class UnitTestCase. Or replace the functions with an other assert call?

The development of phpunit-dom-assertions has stalled.

daffie’s picture

Category: Feature request » Bug report

Change the issue to a bug report. Because it is stalling #2387027: Upgrade PHPUnit to the latest stable release.

catch’s picture

Priority: Normal » Major
catch’s picture

Priority: Major » Critical
Issue tags: +Needs Drupal 8 critical triage

Actually I'm going to go ahead and bump this to critical.

Leaving #2387027: Upgrade PHPUnit to the latest stable release as major since I'm not clear whether the update counts as a minor or major upgrade. Not being able to upgrade PHPUnit at all due to a BC break feels critical though.

If there's a backwards compatible way to fix this, or if we agree that the API change here would be OK in a minor release, we can bump this back down to major. Really don't want to end up with a forked/outdated PHPUnit for the entire lifecycle of 8.x though.

dawehner’s picture

We could leverage https://github.com/phpunit/phpunit-dom-assertions

Given that we have our own base class I went with adding a PR for adding a nice test trait: https://github.com/phpunit/phpunit-dom-assertions/pull/4
but this PR is not a strict requirement to use that library.

dawehner’s picture

Issue summary: View changes

Added a proposed solution to the IS.

jibran’s picture

Status: Active » Needs review
FileSize
11.5 KB
271.14 KB

Here we go.

The last submitted patch, 12: core-only-do-not-test-2331685-12.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 12: 2331685-12.patch, failed testing.

dawehner’s picture

+++ b/core/tests/Drupal/Tests/UnitTestCase.php
@@ -17,7 +17,7 @@
-abstract class UnitTestCase extends \PHPUnit_Framework_TestCase {
+abstract class UnitTestCase extends \PHPUnit_Framework_DOMTestCase {

Ideally not all our unit tests would extend from dom test, but well, maybe we have to wait until the trait is in.
Does someone wants to create an issue which moves all our helper methods of the base test into some helper trait?

jibran’s picture

test runs fine locally with the patch

dawehner’s picture

@jibran
Ensure to proxy the phpunit tests via simpletest. It looks like the autoloading doesn't work somehow.

larowlan’s picture

Status: Needs work » Needs review
FileSize
1.04 KB
272.18 KB
5.8 KB

composer dump-autoload was the sauce

kim.pepper’s picture

+++ b/composer.json
@@ -28,7 +28,8 @@
+    "phpunit/phpunit-dom-assertions": "1.0.*@dev"

Can we ask them to tag a stable release?

jibran’s picture

There is no stable release yet.

jibran’s picture

larowlan’s picture

So should we pin a particular commit in the meantime?

jibran’s picture

Well last commit was on Aug 27, 2014. So I think it doesn't make much difference.

larowlan’s picture

dawehner’s picture

Does someone wants to create an issue which moves all our helper methods of the base test into some helper trait?

jibran’s picture

daffie’s picture

Can we not rewrite the tests to use xpath processor. In #1333730: [Meta] PHP DOM (libxml2) misinterprets HTML5 we are adding one for HTML5. If I am wrong about this please tell me.

dawehner’s picture

@daffie
We talk about a phpunit test here, all that xpath functionality is just included in the simpletest side of things, afaik.

dawehner’s picture

Status: Needs review » Needs work

So should we pin a particular commit in the meantime?

I would still argue that we should point to a specific commit, it is the good practise, people read the composer.json file of core.

jibran’s picture

Status: Needs work » Needs review
FileSize
886 bytes
272.22 KB

Here we go.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I think the latest stable dom crawler is 2.6.3 but this patch is coming with 2.6.1

jibran’s picture

Status: Needs work » Needs review
FileSize
22.97 KB
274.47 KB

Added "symfony/dom-crawler": "2.6.*", to drupal composer.

larowlan’s picture

Can we use the more flexible ~2.6 or failing that ^2.6.3 if ~2.6 doesn't force 2.6.3?

jibran’s picture

+++ b/composer.json
@@ -17,6 +17,7 @@
     "symfony/validator": "2.6.*",
     "symfony/process": "2.6.*",
     "symfony/yaml": "2.6.*",
+    "symfony/dom-crawler": "2.6.*",

This is what we are doing with all the symfony components so it only seems fair to add 2.6.*

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Ok, thanks for that

daffie’s picture

Status: Reviewed & tested by the community » Postponed

The only thing in this patch being added is the symfony/dom-crawler library. I cannot see why that is enough to fix the problem as stated in the issue summary.
In #1333730: [Meta] PHP DOM (libxml2) misinterprets HTML5 is spoken about the symfony/dom-crawler library and the consensus is that an other library is far better then this one.
The problem with the symfony/dom-crawler library is that it only supports html4 and Drupal 8 wants html5 support.
So if there is a need for a dom-crawler library I am postponing this issue on #1333730: [Meta] PHP DOM (libxml2) misinterprets HTML5.

jibran’s picture

Status: Postponed » Needs review

@daffie we are not adding symfony/dom-crawler we are adding phpunit/phpunit-dom-assertions. This is a critical issue if you have any other suggestion to fix this please explain.

larowlan’s picture

cilefen’s picture

reroll

jibran’s picture

Thanks for the reroll @cilefen. RTBC anyone?

kim.pepper’s picture

Status: Needs review » Reviewed & tested by the community

Lets do it.

daffie’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/composer.json
    @@ -17,6 +17,7 @@
    +    "symfony/dom-crawler": "2.6.*",
    
    @daffie we are not adding symfony/dom-crawler

    Yes, you do!!! I can also see that is a requirement for the phpunit/phpunit-dom-assertions library.

  2. But is it necessary to add the symfony/dom-crawler library to the core/composer.json file? Won't it be included with the core/vendor/phpunit/phpunit-dom-assertions/composer.json file?
  3. The phpunit/phpunit-dom-assertions library still does not support the assertTag functionality. So the problem as described in the issue summary is not solved. For that I change the status to "needs work".
dawehner’s picture

Yes, you do!!! I can also see that is a requirement for the phpunit/phpunit-dom-assertions library.

So? The tests for HTML are fine with just HTML 4 support, if we need HTML 5 support we can still switch over later.
This issue allows us to update phpunit in the meantime.

jhedstrom’s picture

This issue won't be properly resolved until phpunit/phpunit-dom-assertions supports assertTag() and the other methods marked as deprecated, since without that, our tests that use those assertions fail when running phpunit 4.2+ (the failure is because of the deprecated flag).

pfrenssen’s picture

assertTag() is only used in \Drupal\Tests\Core\Utility\LinkGeneratorTest. Isn't it a good plan to replace them with another approach so this can move forward?

pfrenssen’s picture

assertSelectEquals() is also only used in one test: \Drupal\Tests\Core\Template\AttributeTest. If we can replace these instances we can update to the new PHPUnit and do not need to add a dependency on phpunit/phpunit-dom-assertions.

pfrenssen’s picture

FileSize
2.04 KB

Patch overrides the assertTag() method that is used in LinkGeneratorTest with an alternative implementation that uses PHP built-in DOMDocument and DOMXPath classes to assert the tags.

If we combine this with the patch from #40 then we already do not need to wait for phpunit/phpunit-dom-assertions to add support for assertTag() before moving on with this issue. I would also propose to investigate whether we can replace assertSelectEquals() with an alternative solution.

Setting to needs review for the bot. This still needs work.

pfrenssen’s picture

Status: Needs work » Needs review
pfrenssen’s picture

FileSize
5.25 KB

This patch replaces all occurrences of assertTag() and assertSelectEquals() with self rolled implementations. This should make it unnecessary to include phpunit/phpunit-dom-assertions.

Please review carefully. I am very drunk.

Nick_vh’s picture

Status: Needs review » Needs work
  1. +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
    @@ -331,16 +331,88 @@ public function testPrint() {
    +    $document->preserveWhiteSpace = FALSE;
    

    Would be useful if there is some documentation why preserveWhiteSpace needs to be set to FALSE.

  2. +++ b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
    @@ -445,11 +445,44 @@ public function testGenerateActive() {
    +    // The parent method is deprecated in PHPUnit 4.2. This is an alternative
    

    if it is deprecated, should we still use inheritdoc? If the method will disappear, so will the documentation.

pfrenssen’s picture

Ok let's try it out for real. Attached patches update PHPUnit to 4.2.6. The first is only the update, without patch. This one should fail with deprecated messages. The second one includes the patch from #50 and should hopefully be green.

If this goes through we can make a followup issue to update PHPUnit itself.

pfrenssen’s picture

Assigned: Unassigned » pfrenssen
Status: Needs work » Needs review

@Nick_vh, thanks for the review! I'll address your remarks and will post an updated patch. In the meanwhile I'll set the status back to needs review temporarily so we can get feedback from the bot.

The last submitted patch, 52: 2331685-51-phpunit-update-only.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 52: 2331685-51-phpunit-update-and-patch.patch, failed testing.

pfrenssen’s picture

Title: PHPUnit deprecated assertTag() and assertSelectEquals() » PHPUnit deprecated assertTag(), assertNotTag() and assertSelectEquals()

Ok so we need to replace assertNotTag() as well.

pfrenssen’s picture

Assigned: pfrenssen » Unassigned
Status: Needs work » Needs review
FileSize
11.38 KB
544.15 KB
9.53 KB

Addressed the remarks from #51.

  1. The preserveWhiteSpace() is not actually needed, so I removed it.
  2. Good point. I renamed the method to assertLink() since we are only testing links here, not tags in general. This also allowed to simplify the calling code a bit.

I also implemented an alternative for the 2 calls to assertNotTag().

2 patches attached: first is the vanilla patch, the second one includes the update of PHPUnit to 4.2.6 to prove that it actually works with the new version.

pfrenssen’s picture

Another experiment, PHPUnit 4.2.x is also quite old already. Let's see how our test suite fares using the patch and the very latest PHPUnit 4.4.2.

The actual patch to review is the first patch from #57.

jibran’s picture

Awesome work @pfrenssen. Why not move these new function to UnitTestBase?

pfrenssen’s picture

Issue summary: View changes

Wow all tests green on PHPUnit 4.4.2! That is great news, I always had understood that it would be very hard to update PHPUnit due to their lack of backwards compatibility.

I have created a followup issue to do this: #2409667: Update to PHPUnit 4.4.2.

pfrenssen’s picture

@jibran, I think these tests methods are too narrow in scope to be of general use. It's true we have a need for parsing HTML, but #1333730: [Meta] PHP DOM (libxml2) misinterprets HTML5 seems like the ideal solution for this. I'd rather get that in than maintaining our own set of DOMDocument based parsing in UnitTestBase.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Ok then. From #50 onward issue completely changed directions so I think I can RTBC this. Thanks @pfrenssen for the explanation.

jibran’s picture

pfrenssen’s picture

Ohh I had been searching the issue queue extensively before creating the issue, but I was using the keyword "update" instead of "upgrade".

Thanks for the RTBC!

pfrenssen’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs Drupal 8 critical triage

This issue addresses a critical bug and is allowed per https://www.drupal.org/core/beta-changes. Committed 3e43de7 and pushed to 8.0.x. Thanks!

  • alexpott committed 3e43de7 on 8.0.x
    Issue #2331685 by pfrenssen, jibran, larowlan, cilefen: PHPUnit...

Status: Fixed » Closed (fixed)

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