WebTestBase has plenty of methods to assert raw content + text content.

Now that DrupalUnitTestBase is able to render/theme output, too, it severely lacks assertion methods.

→ Move all content assertion methods into a trait. Use it for WebTestBase + DUTB.

Comments

sun’s picture

Status: Active » Needs review
StatusFileSize
new95.02 KB

First: The basic trait change. Should come back green.

Status: Needs review » Needs work

The last submitted patch, 1: test.content.1.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new97.62 KB
new3.18 KB

Fixed WebTestBaseTest.

sun’s picture

StatusFileSize
new98.09 KB
new483 bytes

Added trait to DrupalUnitTestBase.

→ Checking whether any DUTB test implements custom copies of the methods already (which should cause PHP strict warnings).

Status: Needs review » Needs work

The last submitted patch, 4: test.content.4.patch, failed testing.

The last submitted patch, 4: test.content.4.patch, failed testing.

sun’s picture

StatusFileSize
new8.98 KB

The test results are incomplete, so I quickly whipped up a script to check all existing DUTB tests for clashing methods.

Lines prefixed with a - sign are conflicting methods.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new119.27 KB
new22.12 KB
  1. Converted calls to @deprecated methods within WebTestBase itself.
  2. Add render() method to DrupalUnitTestBase.
  3. Converted TableTest.
  4. Converted FieldUnitTestBase.
  5. Converted TextPlainUnitTest. (interesting prior art)
  6. Converted RowEntityTest.
sun’s picture

larowlan’s picture

StatusFileSize
new39.79 KB
new122.1 KB
+++ b/core/modules/simpletest/lib/Drupal/simpletest/AssertContentTrait.php
@@ -0,0 +1,1213 @@
+  protected function getTextContent() {
...
+  protected function getDrupalSettings() {
...
+   * @return
+   *   A SimpleXMLElement or FALSE on failure.
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return
...
+   * @return

Should have the return type (I realise this is C/P but pretty sure we have to fix when we touch it) - fixed in attached (only changes in attached are coding standards)

  1. +++ b/core/modules/simpletest/lib/Drupal/simpletest/AssertContentTrait.php
    @@ -0,0 +1,1213 @@
    +      $htmlDom = new \DOMDocument();
    +      @$htmlDom->loadHTML('<?xml encoding="UTF-8">' . $this->getRawContent());
    +      if ($htmlDom) {
    

    $html_dom (c/p) - also fixed?

  2. +++ b/core/modules/simpletest/lib/Drupal/simpletest/AssertContentTrait.php
    @@ -0,0 +1,1213 @@
    +        $this->pass(String::format('Valid HTML found on "@path"', array('@path' => $this->getUrl())), 'Browser');
    ...
    +      $this->fail('Parsed page successfully.', 'Browser');
    ...
    +    return $this->assert(isset($links[$index]), $message, $group);
    

    pass, fail, assert and getUrl methods aren't in the trait - but should be if the trait needs them. Even if they're just abstract in the trait or throw Exceptions. Same for assertEqual, assertTrue, assertFalse, assertIdentical, verbose and assertNotEqual? I thought the idea here was that you could drop this trait into any class. But this seems to not fit that - it relies on these methods that are in TestBase.

larowlan’s picture

Of course I could totally have traits wrong

Status: Needs review » Needs work

The last submitted patch, 10: test-content-2257519.10.patch, failed testing.

wim leers’s picture

Now that DrupalUnitTestBase is able to render/theme output, too, it severely lacks assertion methods.

Where was that introduced? :) I'd love to convert as many tests as possible in the components I maintain!

larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new799 bytes
new122.88 KB
+++ b/core/modules/simpletest/lib/Drupal/simpletest/AssertContentTrait.php
@@ -0,0 +1,1213 @@
+  protected function assertTextHelper($text, $message = '', $group, $not_exists) {

couple of these where second argument is optional but subsequent arguments aren't.
I changed them around but it broke some stuff.
hoping this fixes.

sun’s picture

StatusFileSize
new123.43 KB
new1.36 KB

Thanks @larowlan! :-)

Updated recently revised ScriptTest for new trait methods.


re: #10.2: I don't know whether we have a custom Drupal "standard" for traits either, but IMO from a purely technical standpoint, traits are just a tool to copy/paste code at compile time... The two classes that use the trait are not in the same inheritance hierarchy, so the trait nicely resolves that issue. Even if there may be a policy for traits, I really hope that we can make an exception for the testing framework code here.

This change will be a huge help for all the DUTB conversions for #2258173: [meta] Various web tests are not performing any HTTP requests, so I'd really love to get this in ASAP. :-)


@Wim Leers: I can't remember since when exactly, but it's possible for at least ~1.5 years already. In essence, DrupalUnitTestBase is a KernelTestBase, so whatever you can do with a kernel you can do in a DUTB test. However, due to the lack of helper and assertion methods, it has been (too) complex/cumbersome to author content/output related tests. Hence this issue :-)

This improvement here is also a prerequisite for a new HttpKernelTestBase (DUTB + HttpKernel), which I already toyed around with on my local disk, and for which I'll create an issue after this patch has landed.

wim leers’s picture

Wow. Wow. The consequences of this issue are going to be *awesome*. Thanks for the explanation, and for working on this! :)

sun’s picture

I consider this patch RTBC from my perspective.

larowlan’s picture

Spoke with folks in irc and both timplunkett and Crell agreed that the trait should provide abstract stubs for the methods it calls but does not implement.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

More irc discussions: Crell and timplunkett are happy with this as is because its only for test assertions, not runtime code.

sun’s picture

As this is RTBC already, and because it would really be great to move forward on DUTB conversions ASAP, and because the discussion on more explicit method declarations in test helper traits could very well take a bit longer, I'd like to move that into a dedicated follow-up issue:

#2260053: Make test helper traits explicitly define expected TestBase methods

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 15: test.content.15.patch, failed testing.

sun’s picture

15: test.content.15.patch queued for re-testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 15: test.content.15.patch, failed testing.

cs_shadow’s picture

Status: Needs work » Needs review
StatusFileSize
new123.45 KB

Patch in #14 no longer applies due to a recent change in WebTestBase.php.

Rerolled the patch in #14 against latest HEAD.

Status: Needs review » Needs work

The last submitted patch, 25: simpletest-2257519-25.patch, failed testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new123.48 KB

Merged 8.x + Fixed merge conflicts.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 27: test.content.27.patch, failed testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new123.44 KB

Merged 8.x + updated for renamed KernelTestBase.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 29: test.content.29.patch, failed testing.

sun’s picture

Status: Needs work » Needs review

29: test.content.29.patch queued for re-testing.

sun’s picture

Status: Needs review » Reviewed & tested by the community
sun’s picture

Any chance to get this in?

xjm’s picture

tim.plunkett’s picture

Issue tags: +Traits

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 34: 2257519-psr4-reroll.patch, failed testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new123.73 KB

Merged 8.x.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 37: test.content.37.patch, failed testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new123.53 KB

Merged 8.x.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs a reroll

git ac https://drupal.org/files/issues/test.content.39.patch
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  123k  100  123k    0     0  67750      0  0:00:01  0:00:01 --:--:-- 74758
error: patch failed: core/modules/simpletest/src/WebTestBase.php:2753
error: core/modules/simpletest/src/WebTestBase.php: patch does not apply
sun’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs reroll
StatusFileSize
new123.52 KB

Merged 8.x + resolved conflicts.

chx’s picture

I love traits but isn't this excessive? Why can't this be either in TestBase or a common base class one step above TestBase?

sun’s picture

A trait is the most appropriate implementation respecting the architecture. The base classes are not and should not be in the same inheritance chain. UnitTestBase should not have these content assertion methods. The trait also allows us to rethink and work on alternative test base classes.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 41: test.content.42.patch, failed testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new125.64 KB

Merged 8.x + resolved merge conflicts.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 45: test.content.46.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new125.89 KB
sun’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new125.63 KB

Thanks @larowlan. Still the same patch, so RTBC.

However, removing some unintentional garbage from #47:

diff --git a/core/vendor/behat/mink-goutte-driver b/core/vendor/behat/mink-goutte-driver
new file mode 160000
index 0000000..488f7f0
--- /dev/null
+++ b/core/vendor/behat/mink-goutte-driver
@@ -0,0 +1 @@
+Subproject commit 488f7f02b1e907888f4b156b635693daf51d760c-dirty
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record
+++ b/core/modules/simpletest/src/WebTestBase.php
@@ -1356,16 +1328,18 @@ protected function curlExec($curl_options, $redirect = FALSE) {
-    $this->assertTrue($this->drupalGetContent() !== FALSE, $message, 'Browser');
...
+    $this->assertTrue($this->getRawContent() !== FALSE, $message, 'Browser');

It would have been nice if the issue summary or somewhere documented this change... anyhow this needs a change notice before it can be committed since you are now changing public API method names. And we need to check exiting change records (unlikely I know)

diff --git a/core/modules/field/src/Tests/DisplayApiTest.php b/core/modules/field/src/Tests/DisplayApiTest.php
index c0abdb5..71fd775 100644
--- a/core/modules/field/src/Tests/DisplayApiTest.php
+++ b/core/modules/field/src/Tests/DisplayApiTest.php
@@ -165,7 +165,7 @@ function testFieldItemListView() {
       ),
     );
     $output = $items->view($display);
-    $view = $this->render($output);
+    $this->render($output);
     $setting = $display['settings']['test_formatter_setting_additional'];
     $this->assertNoText($this->label, 'Label was not displayed.');
     $this->assertNoText('field_test_entity_display_build_alter', 'Alter not fired.');
@@ -274,7 +274,7 @@ function testFieldEmpty() {
     // $this->entity is set by the setUp() method and by default contains 4
     // numeric values.  We only want to test the display of this one field.
     $output = $this->entity->get($this->field_name)->view($display);
-    $view = $this->render($output);
+    $this->render($output);
     // The test field by default contains values, so should not display the
     // default "empty" text.
     $this->assertNoText($display['settings']['test_empty_string']);
@@ -283,7 +283,7 @@ function testFieldEmpty() {
     $this->entity->{$this->field_name} = array();
     $this->entity->save();
     $output = $this->entity->get($this->field_name)->view($display);
-    $view = $this->render($output);
+    $this->render($output);
     // This time, as the field values have been removed, we *should* show the
     // default "empty" text.
     $this->assertText($display['settings']['test_empty_string']);
diff --git a/core/modules/field/src/Tests/FieldUnitTestBase.php b/core/modules/field/src/Tests/FieldUnitTestBase.php
index a840ead..ecc278a 100644
--- a/core/modules/field/src/Tests/FieldUnitTestBase.php
+++ b/core/modules/field/src/Tests/FieldUnitTestBase.php
@@ -7,7 +7,6 @@
 
 namespace Drupal\field\Tests;
 
-use Drupal\Component\Utility\Xss;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Language\LanguageInterface;
 use Drupal\simpletest\DrupalUnitTestBase;
diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php
index fef56c4..805c63b 100644
--- a/core/modules/simpletest/src/WebTestBase.php
+++ b/core/modules/simpletest/src/WebTestBase.php
@@ -11,7 +11,6 @@
 use Drupal\Component\Utility\Crypt;
 use Drupal\Component\Utility\NestedArray;
 use Drupal\Component\Utility\String;
-use Drupal\Component\Utility\Xss;
 use Drupal\Core\DrupalKernel;
 use Drupal\Core\Database\Database;
 use Drupal\Core\Database\ConnectionNotDefinedException;
@@ -25,7 +24,6 @@
 use Drupal\Core\Datetime\DrupalDateTime;
 use Drupal\block\Entity\Block;
 use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\CssSelector\CssSelector;
 
 /**
  * Test case for typical Drupal tests.
diff --git a/core/modules/system/src/Tests/System/ScriptTest.php b/core/modules/system/src/Tests/System/ScriptTest.php
index 5e8d294..d712991 100644
--- a/core/modules/system/src/Tests/System/ScriptTest.php
+++ b/core/modules/system/src/Tests/System/ScriptTest.php
@@ -7,7 +7,6 @@
 
 namespace Drupal\system\Tests\System;
 
-use Drupal\Component\Utility\String;
 use Drupal\simpletest\DrupalUnitTestBase;
 
 /**
diff --git a/core/modules/system/src/Tests/Theme/TableTest.php b/core/modules/system/src/Tests/Theme/TableTest.php
index d00acb3..fd8e8fe 100644
--- a/core/modules/system/src/Tests/Theme/TableTest.php
+++ b/core/modules/system/src/Tests/Theme/TableTest.php
@@ -7,7 +7,6 @@
 
 namespace Drupal\system\Tests\Theme;
 
-use Drupal\Component\Utility\String;
 use Drupal\simpletest\DrupalUnitTestBase;
 
 /**
diff --git a/core/modules/views/src/Tests/Plugin/RowEntityTest.php b/core/modules/views/src/Tests/Plugin/RowEntityTest.php
index c60f1c7..70511f3 100644
--- a/core/modules/views/src/Tests/Plugin/RowEntityTest.php
+++ b/core/modules/views/src/Tests/Plugin/RowEntityTest.php
@@ -7,7 +7,6 @@
 
 namespace Drupal\views\Tests\Plugin;
 
-use Drupal\Component\Utility\Xss;
 use Drupal\views\Views;
 use Drupal\views\Tests\ViewUnitTestBase;
 

Above are changes I would have made on commit - that need doing.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new126.77 KB
new4 KB

Incorporated diff from #49.

this needs a change notice before it can be committed since you are now changing public API method names.

Actually, the existing methods on WebTestBase are not removed by this patch; they are retained as BC wrappers/aliases for the new methods and have been marked as @deprecated. I would have added an API change notice to the issue summary otherwise.

Unless I'm mistaken, we only publish CRs when functions are removed, no?

sun queued 50: test.content.50.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 50: test.content.50.patch, failed testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record
StatusFileSize
new128.05 KB
new6.68 KB

Updated for latest DisplayApiTest in HEAD.

With that we should be back to RTBC. Regarding CR, see #50.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 8501b7f and pushed to 8.x. Thanks!

  • alexpott committed 8501b7f on 8.x
    Issue #2257519 by sun, larowlan, xjm, cs_shadow: Move content assertion...
alexpott’s picture

We should update https://www.drupal.org/node/1829160 to note that these assertions are available.

Status: Fixed » Closed (fixed)

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