Problem/Motivation

The assertTrue() and assertFalse() methods have a very specific meaning in PHPUnit: a strong === comparison with TRUE/FALSE. Because of historic reasons we didn't have assertEmpty() and assertNotEmpty() methods on old Simpletest kernel tests, that's why we abused assertTrue/False() for the purposes of checking if something is empty.

This is problematic because developers knowing PHPUnit expect assertFalse() to do a strong comparison, which is important in security related tests for example.

Proposed resolution

Now that we have converted our kernel tests to PHPUnit we should deprecate the assertTrue/False() compatibility override from AssertLegacyTrait.php and fix invocations that rely on the old meaning to assertEmpty() or assertNotEmpty() or assertNull().

Remaining tasks

Fix test fails

User interface changes

none.

API changes

The meaning of assertTrue/False() changes on PHPUnit tests.

Data model changes

none.

CommentFileSizeAuthor
#143 2742585-hotfix-143.patch516 bytestim.plunkett
#137 2742585-9.0.x-137.patch440.99 KBalexpott
#137 2742585-8.9.x-137.patch441.05 KBalexpott
#135 2742585-9.0.x-135.patch440.99 KBalexpott
#133 2742585-2-133.patch441.05 KBalexpott
#133 130-133-interdiff.txt869 bytesalexpott
#130 2742585-130-D8.patch440.49 KBsokru
#127 2742585-127.patch441.35 KBmondrake
#124 2742585-124.patch441.57 KBmondrake
#119 2742585-118.patch440.04 KBmondrake
#118 interdiff_115-118.txt2.51 KBmondrake
#115 2742585-115.patch437.53 KBmondrake
#113 2742585-113.patch438.41 KBmondrake
#113 interdiff_110-113.txt732 bytesmondrake
#110 2742585-110.patch439.15 KBalexpott
#110 107-110-interdiff.txt2.21 KBalexpott
#107 2742585-107.patch441 KBalexpott
#100 2742585-100.patch442.57 KBalexpott
#100 97-100-interdiff.txt8.47 KBalexpott
#97 2742585-97.patch440.82 KBkrzysztof domański
#97 interdiff-90-97.txt3.33 KBkrzysztof domański
#90 2742585-90.patch442.45 KBalexpott
#90 88-90-interdiff.txt7.87 KBalexpott
#88 2742585-88.patch436.64 KBalexpott
#88 85-88-interdiff.txt112.77 KBalexpott
#85 2742585-85.patch315.51 KBalexpott
#78 interdiff-2742585-64-77.patch69.88 KBzeip
#77 2742585-77.patch330.16 KBzeip
#75 2742585-75.patch329.92 KBzeip
#73 2742585-73.patch329.91 KBzeip
#71 2742585-71.patch326.83 KBzeip
#69 2742585-69.patch326.83 KBzeip
#64 2742585-64.patch318.92 KBzeip
#62 2742585-62.patch318.33 KBzeip
#57 interdiff-2742585-53-56.txt1.81 KBzeip
#56 2742585-56.patch318.32 KBzeip
#54 2742585-54.patch318.31 KBzeip
#53 2742585-53.patch318.36 KBzeip
#51 2742585-51.patch317.58 KBzeip
#45 2742585-45.patch308.07 KBzeip
#43 2742585-43.patch308.07 KBzeip
#41 2742585-41.patch308.07 KBzeip
#39 2742585-39.patch286.89 KBzeip
#36 2742585-36.patch238.4 KBjofitz
#30 assert-true-2742585-30.patch238.05 KBzeip
#28 assert-true-2742585-28.patch205.84 KBzeip
#25 assert-true-2742585-25.patch156.76 KBzeip
#23 assert-true-2742585-23.patch133.43 KBzeip
#15 interdiff.txt16.82 KBtomasnagy
#15 assert-true-2742585-15.patch144.48 KBtomasnagy
#11 assert-true-2742585-11.patch125.62 KBtomasnagy
#6 interdiff.txt32.84 KBklausi
#6 assert-true-2742585-6.patch84.08 KBklausi
#4 interdiff.txt25.11 KBklausi
#4 assert-true-2742585-4.patch51.23 KBklausi
#2 assert-true-2742585-2.patch27.1 KBklausi

Comments

klausi created an issue. See original summary.

klausi’s picture

Status: Active » Needs review
StatusFileSize
new27.1 KB

Here is a start. Let's hope we can convert everything in this issue and the patch does not grow too much. Otherwise we need to open child issues.

Status: Needs review » Needs work

The last submitted patch, 2: assert-true-2742585-2.patch, failed testing.

klausi’s picture

Status: Needs work » Needs review
StatusFileSize
new51.23 KB
new25.11 KB

Some more fixes, but not done yet so this will fail.

This also revealed the first real test code bug in FieldImportCreateTest where we have wrong assertTrue() calls with 3 parameters.

Status: Needs review » Needs work

The last submitted patch, 4: assert-true-2742585-4.patch, failed testing.

klausi’s picture

Status: Needs work » Needs review
StatusFileSize
new84.08 KB
new32.84 KB

And another round of fixes, still not done.

Status: Needs review » Needs work

The last submitted patch, 6: assert-true-2742585-6.patch, failed testing.

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

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now 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.

sutharsan’s picture

Issue tags: +Dublin2016

Fixing failing tests may be daunting, but for someone with sufficient PHP experience it may be a good task.

jeroenbegyn’s picture

Assigned: Unassigned » jeroenbegyn

Me & @tomasnagy are working on this at DrupalCon Dublin 2016.

tomasnagy’s picture

StatusFileSize
new125.62 KB

Went over all the failing test together with @jeroenbegyn. All tests should validate correctly.

tomasnagy’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: assert-true-2742585-11.patch, failed testing.

tomasnagy’s picture

Assigned: jeroenbegyn » tomasnagy
tomasnagy’s picture

StatusFileSize
new144.48 KB
new16.82 KB

Fixed almost all failed tests introduced with 8.3.x. FrontPageTest keeps failing for now, can't figure out why it doesn't know about assertNotEmpty (PHPUnit specific functions?). Any assistance would be appreciated.

tomasnagy’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 15: assert-true-2742585-15.patch, failed testing.

dawehner’s picture

Conceptually I totally support this change. One question though we could ask: How do announce that change, so contrib/custom code can adapt without breaking immediately.

kristiaanvandeneynde’s picture

For now, how do I do a hard assertFalse()? Calling parent::assertFalse() seems to put me inside of the trait.

klausi’s picture

For now you need to do

$this->assertSame($x, FALSE);

to get a type safe assertion for FALSE.

kristiaanvandeneynde’s picture

Cool, thanks klausi!

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

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new133.43 KB

Re-rolled the patch against 8.4.x. Let's see how this goes.

Status: Needs review » Needs work

The last submitted patch, 23: assert-true-2742585-23.patch, failed testing.

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new156.76 KB

Made the change in a few other places also, and removed the change from core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php to fix the problem with FrontPageTest.

Status: Needs review » Needs work

The last submitted patch, 25: assert-true-2742585-25.patch, failed testing.

kristiaanvandeneynde’s picture

Great progress, thanks ZeiP

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new205.84 KB

Some more tests changed to use more correct assert methods.

Status: Needs review » Needs work

The last submitted patch, 28: assert-true-2742585-28.patch, failed testing.

zeip’s picture

Assigned: tomasnagy » Unassigned
Status: Needs work » Needs review
StatusFileSize
new238.05 KB

This should fix almost all of the remaining problems; only one test I was unable to run locally. Let's see how it goes.

Status: Needs review » Needs work

The last submitted patch, 30: assert-true-2742585-30.patch, failed testing.

zeip’s picture

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

Okay, so apparently we have two tests of which one works at a time: "Drupal\node\Tests\Views\FrontPageTest", which can't find assertNotEmpty(), and "Drupal\Tests\views\Kernel\RenderCacheIntegrationTest", which requires the change. Both of these use core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php.

Back to square one with an updated patch for 8.4.x. I'll try to continue with this in the coming days. Assigning to myself.

zeip’s picture

Status: Needs review » Needs work

The status shouldn't have been changed.

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.

zeip’s picture

Assigned: zeip » Unassigned
jofitz’s picture

Status: Needs work » Needs review
StatusFileSize
new238.4 KB

Patch in #30 no longer applies so re-rolled.

Status: Needs review » Needs work

The last submitted patch, 36: 2742585-36.patch, failed testing. View results

zeip’s picture

Assigned: Unassigned » zeip

I have to say, I'm somewhat surprised. There's way more failures than I'd have thought.

I'll try to fix some of them tonight, let's see where that gets us.

zeip’s picture

Version: 8.5.x-dev » 8.4.x-dev
Status: Needs work » Needs review
StatusFileSize
new286.89 KB

This should be much closer. I'm changing the version back to 8.4.x, because I think that making this kind of test cleanup might be a good fit for the RC stage.

Status: Needs review » Needs work

The last submitted patch, 39: 2742585-39.patch, failed testing. View results

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new308.07 KB

This should be pretty close.

Status: Needs review » Needs work

The last submitted patch, 41: 2742585-41.patch, failed testing. View results

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new308.07 KB

Retry with correct patch.

Status: Needs review » Needs work

The last submitted patch, 43: 2742585-43.patch, failed testing. View results

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new308.07 KB

Perhaps this time it's the actually correct patch. Sorry for the spam.

dawehner’s picture

No worries about the spamming. @ZeiP I'm curious, how do you produce the patch?

zeip’s picture

I'm making the changes by hand using the testing results, then adding the changed lines with git add -p (trying to catch any possible mistakes I might've made) and then output git diff --cached to the patch file. On the last round I forgot the git add before outputting the ”new” patch... :)

dawehner’s picture

@ZeiP I'm wondering whether you could use a regex with sed or so to produce the same patch. I don't say that's better or worse, I'm just wondering :)

zeip’s picture

Probably not. I'm only replacing assertFalse/assertTrue calls that fail, since many of them probably are indeed comparing booleans and should be left alone. And the assertFalse calls are replaced with either assertEmpty or assertNull depending on the value etc. Also I think I should have most of them down again now, probably the same problematic ones to be solved after that – and that probably requires something completely else :)

Thanks for the suggestion, though! The previous issue I was meddling with was a more straightforward one and that one I managed to mostly do with just sed and related tools.

Status: Needs review » Needs work

The last submitted patch, 45: 2742585-45.patch, failed testing. View results

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new317.58 KB

Few more. Perhaps now we have all?

Status: Needs review » Needs work

The last submitted patch, 51: 2742585-51.patch, failed testing. View results

zeip’s picture

Status: Needs work » Needs review
Issue tags: -Novice
StatusFileSize
new318.36 KB

Suspected as much. One more to go. I'm a bit surprised that the constant errors we got a few months ago seem to be gone – perhaps this one'll be a green one?

zeip’s picture

StatusFileSize
new318.31 KB

There were a few calls to assertTrue that didn't look quite right. This patch tries to fix those to work correctly.

Status: Needs review » Needs work

The last submitted patch, 54: 2742585-54.patch, failed testing. View results

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new318.32 KB

Another try at fixing the assertTrue in CommentAdminViewUpdateTest.

zeip’s picture

StatusFileSize
new1.81 KB

Ok, this seems good to go. Attached is a interdiff for the last change that was more than just a simple assert function change. Please review.

kristiaanvandeneynde’s picture

Awesome work ZeiP!

Not to be a pain, but there are also instances of $this->assertSame(FALSE, ... and $this->assertSame(TRUE, ... that could perhaps be changed while you're at it.

fellaroon’s picture

Conceptually I totally support this change. One question though we could ask: How do announce that change, so contrib/custom code can adapt without breaking immediately.

zeip’s picture

@kristiaanvandeneynde, I could change those, but the patch is quite large as is, so I think we should make that a separate issue. I'm not even sure about the few changes in #56, since even those were perhaps slightly out of scope. Keeping the issue straightforward it's probably easier to get in.

Announcing the change is a good question. This also relates to the version question: Should we only do this in 8.5.x?

kristiaanvandeneynde’s picture

8.5.x only with a change record should make sure people have time to update their code? If people were doing a hard check for TRUE or FALSE, they would use assertSame() already. If people were getting (un)lucky because they meant to use assertFalse and an empty value actually passed their test, their tests will now rightfully fail.

Or am I missing something?

zeip’s picture

Version: 8.4.x-dev » 8.5.x-dev
StatusFileSize
new318.33 KB

Mostly yes. The main point is that apparently assertFalse/assertTrue was in some point actually meant to check for any kind of empty – FALSE, NULL, etc. So I'm not sure if all the tests would then rightfully fail, but they obviously would be now incorrect and therefore should be fixed. Basically IMO announcing this with the brand-new 8.5.x seems fair enough.

Changing the version back to 8.5.x. Attached is a patch for 8.5.x, seems that the previous patch didn't apply cleanly.

Status: Needs review » Needs work

The last submitted patch, 62: 2742585-62.patch, failed testing. View results

zeip’s picture

Assigned: zeip » Unassigned
Status: Needs work » Needs review
StatusFileSize
new318.92 KB

Apparently there had been added one instance to 8.5.x :) This should be ready for review.

dawehner’s picture

We certainly need to write a change notice so contrib module authors can fix their tests ... I think its okay to

  1. +++ b/core/modules/aggregator/tests/src/Functional/AggregatorTestBase.php
    @@ -210,7 +210,7 @@ public function deleteFeedItems(FeedInterface $feed) {
         $count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', [':fid' => $feed->id()])->fetchField();
    -    $this->assertTrue($count);
    +    $this->assertNotEmpty($count);
    

    I think this should actually be $this->assertSame(0, $count) don't you think so?

  2. +++ b/core/modules/aggregator/tests/src/Kernel/Migrate/d7/MigrateAggregatorFeedTest.php
    @@ -47,7 +47,7 @@ public function testAggregatorFeedImport() {
    -    $this->assertTrue(preg_match('/^"[a-z0-9]{32}"$/', $feed->getEtag()));
    +    $this->assertNotEmpty(preg_match('/^"[a-z0-9]{32}"$/', $feed->getEtag()));
    

    Given what preg_match returns I guess assertSame(1) would be better

  3. +++ b/core/modules/comment/tests/src/Kernel/Migrate/d6/MigrateCommentVariableInstanceTest.php
    @@ -42,9 +42,9 @@ public function testCommentFieldInstance() {
    -    $this->assertFalse($settings['anonymous']);
    +    $this->assertEmpty($settings['anonymous']);
    

    I'm curious whether we can be more specific?

... note: I've not reviewed the entire patch but it feels like using assertEmpty everywhere doesn't really improve the situation.

kristiaanvandeneynde’s picture

I think this should actually be $this->assertSame(0, $count) don't you think so?

I don't think so. The code used to check for a "true-ish" count; i.e.: the query having results.

zeip’s picture

There are indeed quite a bit of instances that should perhaps actually do something else than use any of the assert{False,True,Empty,NotEmpty,Null}. It would be possible to fix these while we're at it, but I'm wondering if that goes a bit too much outside the issue scope and makes reviewing the patch needlessly difficult?

The current patch tries to stay faithful to the original meaning of the assert call fixing just the True/False-assertion, except for two instances which IMO were clearly buggy. I somewhat disagree with @dawehner in that the patch greatly improves the situation for most of the calls, since now we're actually separating booleans from other values. It's only this limited number of calls that are basically in the same situation as before.

If we want to do all of the fixes in this issue, I'm quite fine with it, but IMO it seems like we should maybe create a follow-up task for that and keep this one as simple as possible (which unfortunately still is far from simple).

kristiaanvandeneynde’s picture

I tend to agree with Daniel here. If we are going to fix it, might as well fix it now. Otherwise, how will we find those that need fixing in a follow-up?

zeip’s picture

Assigned: Unassigned » zeip
StatusFileSize
new326.83 KB

Well, you are correct in that it would require going through the same changes again.

Attached is a patch trying to find the correct assert functions for each replacement. Optimally this should come out as green, but there's probably something I misinterpreted, so let's see.

Status: Needs review » Needs work

The last submitted patch, 69: 2742585-69.patch, failed testing. View results

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new326.83 KB

Few typos fixed.

Status: Needs review » Needs work

The last submitted patch, 71: 2742585-71.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new329.91 KB

A few missing castings and for some reason some previously unproblematic assertTrue/False calls fixed.

Status: Needs review » Needs work

The last submitted patch, 73: 2742585-73.patch, failed testing. View results

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new329.92 KB

One more error.

Status: Needs review » Needs work

The last submitted patch, 75: 2742585-75.patch, failed testing. View results

zeip’s picture

Status: Needs work » Needs review
StatusFileSize
new330.16 KB

Some more fixes for the new changes.

zeip’s picture

Assigned: zeip » Unassigned
StatusFileSize
new69.88 KB

Everything seems to be in order. Attached is an interdiff for the changes, please review.

Status: Needs review » Needs work

The last submitted patch, 78: interdiff-2742585-64-77.patch, failed testing. View results

zeip’s picture

Status: Needs work » Needs review

Damn, named the interdiff incorrectly. Resetting to Needs review.

kristiaanvandeneynde’s picture

Status: Needs review » Needs work

Interdiff generally looks great!

Just a minor nitpick:

-    $this->assertNotEmpty($page->isDefaultRevision());
+    $this->assertTrue((bool) $page->isDefaultRevision());

Typecasting return values with (bool) and then asserting their boolean value is essentially the same as checking for empty or not empty. Because 'foobar' would still pass the above test. So those typecasts need to go :/

I'm sorry if this discourages you, because I really feel your pain writing long patches like this, but... Needs work :(

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

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new315.51 KB

Wow this is a tonne of work - thanks @ZeiP. So the good news is we do have a way of progressing here. We can deprecate instead of remove. I've re-rolled the patch and added deprecations instead of removing the old methods. I guess this will fail with new deprecations as we'll have added new usages. Also we need a change record to inform people about the change.

No interdiff because the conflicts on reroll were many!

alexpott’s picture

Issue summary: View changes

Updated issue summary. Also this is not just about Kernel tests - it's about Functional and FunctionalJavascript tests too because they use the same trait via \Drupal\FunctionalTests\AssertLegacyTrait

Status: Needs review » Needs work

The last submitted patch, 85: 2742585-85.patch, failed testing. View results

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new112.77 KB
new436.64 KB

Here's some of the tests fixed and also one bug in \Drupal\user\Plugin\views\access\Role::access() which is quite important because this is an access function which is not adhering to the interface. Should probably split that out into a separate issue but it kinda proves the point about the danger of these overrides.

Status: Needs review » Needs work

The last submitted patch, 88: 2742585-88.patch, failed testing. View results

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new7.87 KB
new442.45 KB
mondrake’s picture

Issue tags: +Needs change record

I think this needs a CR, and maybe bumped to critical since this is a deprceation that will impact contrib a lot?

alexpott’s picture

@mondrake yep,,, thanks for tagging...

  1. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
    @@ -551,7 +551,7 @@ public static function onDependencyRemoval(FieldDefinitionInterface $field_defin
                   // setting, disable the auto-creation feature completely.
                   $auto_create_bundle = !empty($handler_settings['auto_create_bundle']) ? $handler_settings['auto_create_bundle'] : FALSE;
                   if ($auto_create_bundle && $auto_create_bundle == $bundle->id()) {
    -                $handler_settings['auto_create'] = NULL;
    +                $handler_settings['auto_create'] = FALSE;
                     $handler_settings['auto_create_bundle'] = NULL;
                   }
     
    

    This is bug fix. NULL is not a valid value according to config schema. One interesting thing to work out is how come the schema is not being applied. Needs its own issue I think. This issue should be test changes only.

  2. +++ b/core/modules/user/src/Plugin/views/access/Role.php
    @@ -69,7 +69,7 @@ public static function create(ContainerInterface $container, array $configuratio
        * {@inheritdoc}
        */
       public function access(AccountInterface $account) {
    -    return array_intersect(array_filter($this->options['role']), $account->getRoles());
    +    return !empty(array_intersect(array_filter($this->options['role']), $account->getRoles()));
       }
     
       /**
    

    This is bugfix too and needs an issue. This one feels quite serious as it's used in access and it's not returning the correct type. Bring on return typing!

  3. +++ b/core/tests/Drupal/KernelTests/AssertLegacyTrait.php
    @@ -35,6 +35,7 @@ public static function assertTrue($actual, $message = '') {
           parent::assertTrue($actual, $message);
         }
         else {
    +      @trigger_error('Support for asserting against non-boolean values in ::assertTrue is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use a different assert method, for example, ::assertNotEmpty(). See https://www.drupal.org/node/TODO', E_USER_DEPRECATED);
           parent::assertNotEmpty($actual, $message);
         }
       }
    @@ -47,6 +48,7 @@ public static function assertFalse($actual, $message = '') {
    
    @@ -47,6 +48,7 @@ public static function assertFalse($actual, $message = '') {
           parent::assertFalse($actual, $message);
         }
         else {
    +      @trigger_error('Support for asserting against non-boolean values in ::assertFalse is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use a different assert method, for example, ::assertEmpty(). See https://www.drupal.org/node/TODO', E_USER_DEPRECATED);
           parent::assertEmpty($actual, $message);
         }
       }
    

    Hence the https://www.drupal.org/node/TODO

krzysztof domański’s picture

Issue tags: +Needs title update

1. Needs title update. The title applies only to kernel tests.

Also this is not just about Kernel tests - it's about Functional and FunctionalJavascript tests too

2. Should we also improve tests on the simpletest module? The preg_match() function returns 1, 0 or FALSE.

--- a/core/modules/simpletest/src/Tests/SimpleTestBrowserTest.php
+++ b/core/modules/simpletest/src/Tests/SimpleTestBrowserTest.php
@@ -90,7 +90,7 @@ public function testUserAgentValidation() {
     $http_path = $system_path . '/tests/http.php/user/login';
     $https_path = $system_path . '/tests/https.php/user/login';
     // Generate a valid simpletest User-Agent to pass validation.
-    $this->assertTrue(preg_match('/test\d+/', $this->databasePrefix, $matches), 'Database prefix contains test prefix.');
+    $this->assertEquals(1, preg_match('/test\d+/', $this->databasePrefix, $matches) === 1, 'Database prefix contains test prefix.');
     $test_ua = drupal_generate_test_ua($matches[0]);
     $this->additionalCurlOptions = [CURLOPT_USERAGENT => $test_ua];
mondrake’s picture

Priority: Normal » Critical

per #91 and #92

alexpott’s picture

Title: Remove dangerous kernel tests assertTrue/False() compatibility overrides » Remove dangerous assertTrue/False() compatibility overrides in PHPUnit Kernel, Functional and FunctionalJavascript tests
krzysztof domański’s picture

Issue tags: -Needs title update
krzysztof domański’s picture

mondrake’s picture

AFAICT it's not 'Remove' but 'Deprecate' - valid for both the issue title and the CR

alexpott’s picture

Title: Remove dangerous assertTrue/False() compatibility overrides in PHPUnit Kernel, Functional and FunctionalJavascript tests » Deprecate dangerous assertTrue/False() compatibility overrides in PHPUnit Kernel, Functional and FunctionalJavascript tests

Updated the issue title and will rework the change record.

alexpott’s picture

StatusFileSize
new8.47 KB
new442.57 KB

I've fleshed out the change record and improved a couple of the new assertions - this task is pretty much unending because there many many places where we could use better assertions so I think we should just get the patch to pass and do our best to use the best semantic assertion in place but not quibble if the new assertion works. Whatever we replace it with it is likely to be more specific that the current duck-typing of HEAD.

@Krzysztof Domański what do you mean by #93.2 - the reason that can't use assertRegExp() is that we need $matches.

mondrake’s picture

How about deprecating also the assertEquals override in KernelTestBase and BrowserTestBase, so forcing tests to call explicitly castSafeStrings when needed? (I know, not here)

alexpott’s picture

re #102 - sure but yeah not here and the gain with it is less obvious.One thing that is interesting is instead of the assertEquals hack we could register a Comparator to compare MarkupInterface objects... but as before definitely not as part of this issue :)

krzysztof domański’s picture

this task is pretty much unending because there many many places where we could use better assertions

I've created a issue to track this and similar problems #3082735: [META] Identify dangerous and problematic cases of casting data types.

alexpott’s picture

StatusFileSize
new441 KB

Rerolled now the two bug fixes are in. So we should be good to go here unless some new assertTrue/False have been added.

alexpott’s picture

Doing

$ git diff 8.8.x HEAD --name-only | grep -v "/tests/"
core/modules/migrate_drupal/src/Tests/StubTestTrait.php
core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php

on a branch with this patch committed shows that all the changes in the this patch are to code contain in a /tests/ directory apart from the two test traits above. Those traits are test code. They probably should be moved to a better place but that is out-of-scope for this issue.

mondrake’s picture

Status: Needs review » Needs work

Nits:

  1. +++ b/core/modules/config/tests/src/Functional/ConfigEntityTest.php
    @@ -37,7 +38,6 @@ public function testCRUD() {
    -    $this->assertTrue($empty->uuid());
    

    This is removed and not replaced

  2. +++ b/core/modules/config/tests/src/Functional/ConfigEntityTest.php
    @@ -95,7 +95,6 @@ public function testCRUD() {
    -    $this->assertTrue($config_test->uuid());
    

    Same

  3. +++ b/core/modules/content_moderation/tests/src/Kernel/EntityOperationsTest.php
    @@ -74,7 +74,7 @@ public function testPendingRevisions() {
    +    $this->assertTrue((bool) $page->isDefaultRevision());
    

    Looks like the return value of isDefaultRevision is mixed and not bool as documented. Follow up to correct the docs? Then assertNotFalse instead?

  4. +++ b/core/modules/field/tests/src/Kernel/FieldCrudTest.php
    @@ -69,7 +69,7 @@ public function testCreateField() {
    +    $this->assertEquals('TRUE', $field->getSetting('field_setting_from_config_data'));
    

    assertSame?

  5. +++ b/core/modules/field/tests/src/Kernel/FieldImportCreateTest.php
    @@ -41,15 +41,17 @@ public function testImportCreateDefault() {
    +    $this->assertEquals('entity_test', $field2a->getTargetBundle(), 'The second field was created on bundle entity_test.');
    ...
    +    $this->assertEquals('test_bundle', $field2b->getTargetBundle(), 'The second field was created on bundle test_bundle.');
    

    assertSame?

  6. +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaViewsWizardTest.php
    @@ -43,7 +43,7 @@ public function testMediaWizard() {
    +    $this->assertSame($view->filter['status']->value, '1');
    
    @@ -77,7 +77,7 @@ public function testMediaRevisionWizard() {
    +    $this->assertSame($view->filter['status']->value, '1');
    

    Swap the arguments

  7. +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/Form/FormGroupingElementsTest.php
    @@ -126,8 +126,8 @@ public function testDetailsChildVisibility() {
    +    $this->assertEquals('true', $summary->getAttribute('aria-expanded'));
    +    $this->assertEquals('true', $summary->getAttribute('aria-pressed'));
    

    assertSame?

  8. +++ b/core/tests/Drupal/KernelTests/Core/File/StreamWrapperTest.php
    @@ -85,8 +85,8 @@ public function testUriFunctions() {
    +    $this->assertSame($stream_wrapper_manager::getTarget('public://'), '');
    +    $this->assertSame($stream_wrapper_manager::getTarget('data:'), '');
    

    Swap arguments

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new2.21 KB
new439.15 KB

@mondrake re #109
1. This is a duplicate assertion. This assertion is made twice in the test. We just remove one.
2. This is a duplicate assertion. This assertion is made twice in the test. We just remove one.
3. Nah - the cast is unnecessary here. Nice catch. ::isDefaultRevision() can only ever return a Boolean.
4, 5, 6, 7, 8: I don't think matter at all. The assertEquals on a text string is fine. It's not as if ->assertEquals('test', TRUE) results in a pass. And the argument order is wrong all over the place - we shouldn't fix that here we'd be here forever and not get the main win of this issue which is consistency with PHPUnit and catching the occasional bug when we've made assumptions about return types.

mondrake’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/content_moderation/tests/src/Kernel/EntityOperationsTest.php
@@ -178,7 +178,7 @@ public function testArchive() {
     $storage = \Drupal::entityTypeManager()->getStorage('node');
     $new_revision = $storage->loadRevision($new_revision_id);
     $this->assertFalse($new_revision->isPublished());
-    $this->assertTrue($new_revision->isDefaultRevision());
+    $this->assertTrue((bool) $new_revision->isDefaultRevision());
   }
 
 }

#110.3 - One more left.

This can also be fixed on commit. Follow-up #3082415: Replace assert(Not)Same/Identical() on booleans with assert(Not)True/False() in PHPUnit tests filed. RTBC when it turns back green.

Review of #3082340: Replace assertEquals() overrides in PHPUnit Kernel, Functional and FunctionalJavascript tests with a MarkupInterfaceComparator implementation would be appreciated ;)

mondrake’s picture

Status: Reviewed & tested by the community » Needs work

Mmm, maybe #111 cannot be fixed on commit after all, it's code change.

mondrake’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new732 bytes
new438.41 KB

Fixing #111. Minor change aligned with the rest of #110.3, I think I can RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs re-roll

Needs a re-roll. This is always going to be a problem with a 400+kb patch but also can't really think of a good way to split it up.

mondrake’s picture

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

Reroll.

mondrake’s picture

Issue tags: -Needs re-roll

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 115: 2742585-115.patch, failed testing. View results

mondrake’s picture

Assigned: Unassigned » mondrake
Status: Needs work » Reviewed & tested by the community
StatusFileSize
new2.51 KB

Removed usage that was added since #113.

mondrake’s picture

StatusFileSize
new440.04 KB

... and the patch ...

mondrake’s picture

Assigned: mondrake » Unassigned
alexpott’s picture

Issue tags: +beta target

As this is a large and disruptive change no point committing it right before the alpha - hopefully this will get it right after.

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mondrake’s picture

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

needs reroll

mondrake’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new441.57 KB

Reroll. The @trigger_error for assertTrue and assertFalse in AssertLegacyTrait needed to be moved to the compatibility layer.

mondrake’s picture

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

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll
mondrake’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new441.35 KB
mondrake’s picture

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

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll
sokru’s picture

Related issues: +#2233595: Deprecate the custom path alias storage
StatusFileSize
new440.49 KB

Reroll to 8.9, note that core/modules/path/tests/src/Kernel/Migrate/d6/MigrateUrlAliasTest.php was already fixed in #2233595

sokru’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll

Status: Needs review » Needs work

The last submitted patch, 130: 2742585-130-D8.patch, failed testing. View results

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new869 bytes
new441.05 KB

Fixing the last fail...

mondrake’s picture

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

StatusFileSize
new440.99 KB

We now need 8.x.x and 9.0.x versions because of a change in core/modules/system/tests/src/Functional/Theme/TwigTransTest.php due to Twig 2.

The patch in #133 is for 8.x.x and the patch attached here is for 9.0.x.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs re-roll

Needs one more re-roll. I'm hoping to commit this today just before the beta is tagged:

error: patch failed: core/modules/path/tests/src/Functional/PathAliasTest.php:327
error: core/modules/path/tests/src/Functional/PathAliasTest.php: patch does not apply

alexpott’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs re-roll
StatusFileSize
new441.05 KB
new440.99 KB

Rerolled.

catch’s picture

Adding commit credit..

  • catch committed 19f9474 on 9.0.x
    Issue #2742585 by ZeiP, alexpott, mondrake, klausi, tomasnagy, Krzysztof...
catch’s picture

Version: 8.9.x-dev » 8.8.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.0.x, 8.9.x and 8.8.x, thanks!

  • catch committed a02891d on 8.9.x
    Issue #2742585 by ZeiP, alexpott, mondrake, klausi, tomasnagy, Krzysztof...

  • catch committed e363a11 on 8.8.x
    Issue #2742585 by ZeiP, alexpott, mondrake, klausi, tomasnagy, Krzysztof...
tim.plunkett’s picture

Status: Fixed » Reviewed & tested by the community
StatusFileSize
new516 bytes

Hotfix for the fail caused by this not retesting after #3082655: Specify the $defaultTheme property in all functional tests landed.

xjm credited lauriii.

xjm’s picture

xjm’s picture

I committed the hotfix after @lauriii confirmed that it fixes the test. Didn't wait for QA because HEAD is broken and we need to have QA be green to do the beta release. So DrupalCI will give us our green test run in this case.

Thanks @tim.plunkett for the hotfix!

xjm’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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