Problem/Motivation

Developers are finding the new PHPUnit based kernel tests somewhat confusing. How declaring modules to enable via the $modules variable is counter intuitive, and is a cause of confusing even with some experienced developers.

Proposed resolution

At @berdir's suggestion, I'm fixing the language in KernelTestBase.php to make it more clear how this works.

Remaining tasks

Review the submitted patch :-)

Also, possibly back port this, since this is also a problem in 8.1.x.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

Torenware created an issue. See original summary.

Torenware’s picture

Status: Active » Needs review
StatusFileSize
new888 bytes
Torenware’s picture

dawehner’s picture

Sure why not. Can we add the same documentation to BrowserTestBase as well?

Torenware’s picture

Yeah, I'd be willing to do that. Any comments on the wording as changed?

jhodgdon’s picture

Status: Needs review » Needs work

Thanks for making this issue and patch! Seems like a good idea to clarify it, in all of the base classes that have this member variable.

Wording... hm...

+++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
@@ -139,7 +139,13 @@
    * Test classes extending this class, and any classes in the hierarchy up to
    * this class, may specify individual lists of modules to enable by setting
    * this property. The values of all properties in all classes in the class
-   * hierarchy are merged.
+   * hierarchy are merged. So you do not need to re-declare modules that are
+   * declared in superclasses of your test class.
+   *
+   * An example:
+   * @code
+   * public static $modules = ['my_extra_module'];
+   * @endcode

I think rather than adding extra wording to what is there and adding a code sample, we should instead fix the unclear wording that was there.

I suggest replacing the entire paragraph that starts with "Test classes..." with the following, or something like this:

The test runner will merge the $modules lists from this class, the class it extends, and so on up the class hierarchy. So, it is not necessary to include modules in your list that a parent class has already declared.

And I don't really think we need an @code example?

Torenware’s picture

Status: Needs work » Needs review
StatusFileSize
new929 bytes

@jhodgdon -- I've changed the language accordingly.

@dawehner -- Funny thing: there's no public static $modules declared in BrowserTestBase. So there's no comment to fix. But... looking at the code, I see the following:

    // Collect modules to install.
    $class = get_class($this);
    $modules = array();
    while ($class) {
      if (property_exists($class, 'modules')) {
        $modules = array_merge($modules, $class::$modules);
      }
      $class = get_parent_class($class);
    }

So my sense is that there really ought to be a $modules class variable declared in BrowserTestBase, even if there isn't one now. And it should probably have a similar comment to the one used in KernelTestBase.

That said, it probably needs a slightly different comment than KernelTestBase. So what do you want me to do here?

dawehner’s picture

@dawehner -- Funny thing: there's no public static $modules declared in BrowserTestBase. So there's no comment to fix. But... looking at the code, I see the following:

Oh yeah, well then let's add the same kind of documentation.

jhodgdon’s picture

Status: Needs review » Needs work

Agreed, we should add the $modules variable, and docs, to BrowserTestBase. I just checked and it is not inheriting $modules from any base class. I don't see why it would need a different comment from the one in KernelTestBase?

Setting to Needs Work for that.

dawehner’s picture

I don't see why it would need a different comment from the one in KernelTestBase?

Me neither, cool

dawehner’s picture

I totally agree that the new comment makes it easier to understand.

joachim’s picture

+++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
@@ -136,10 +136,9 @@
+   * it extends, and so on up the class hierarchy. So, it is not necessary to

The comma after 'So' isn't needed. In fact, the 'So' isn't either.

dawehner’s picture

The brits are coming, let's run :)

jhodgdon’s picture

Fair enough, let's remove the "So".

pepegarciag’s picture

Issue tags: +DrupalCampES

Add DrupalCampES tag

pepegarciag’s picture

StatusFileSize
new925 bytes
new677 bytes

Just remove the "So".

pepegarciag’s picture

Status: Needs work » Needs review
pushpinderchauhan’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me. Setting this ti RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 7338397 and pushed to 8.1.x and 8.2.x. Thanks!

alexpott’s picture

Also we need a followup for BrowserTestBase.

dawehner’s picture

abarrio’s picture

Congratulations @pepegarciag. When you will go to start with this, please call me.

Status: Fixed » Closed (fixed)

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