Previously \Drupal\simpletest\AssertContentTrait::assertNoPattern() is used by many tests. It needs to be converted to BTB.

1/ Add it to AssertLegacyTrait
2/ Create change record

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

GoZ created an issue. See original summary.

GoZ’s picture

GoZ’s picture

GoZ’s picture

Status: Active » Needs review
dawehner’s picture

+1 for sure!

+++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php
@@ -668,6 +668,22 @@ protected function assertPattern($pattern) {
+   */
+  protected function assertNoPattern($pattern) {
+    @trigger_error('assertNoPattern() is deprecated and scheduled for removal in Drupal 9.0.0. Use $this->assertSession()->responseNotMatches($pattern) instead. See https://www.drupal.org/node/2864262.', E_USER_DEPRECATED);
+    $this->assertSession()->responseNotMatches($pattern);
+  }

Do you mind providing some test coverage for that in \Drupal\Tests\Core\Assert\AssertLegacyTraitTest?

GoZ’s picture

here is test coverage.

i'm forced to replace $this->assertSession()->responseNotMatches() in assertNoPattern() by duplicating code from responseNotMatches() so AssertLegacyTraitTest find tests to pass.

klausi’s picture

Thanks! I don't understand why we can't use $this->assertSession()->responseNotMatches(), can you explain that again? You just need to mock stuff on $this->webassert. See testAssertOptionSelected() for example.

That test would then just confirm that the call to assertNoPattern() is passed through.

jofitz’s picture

I have followed @klausi's suggestion and it works well for testAssertNoPatternFail(), but not testAssertNoPattern(). Any ideas?

GoZ’s picture

@klausi i don't see how to mock this.

responseNotMatches() return nothing, and assertNoPattern() only call to responseNotMatches(). responseNotMatches() only use page->getContent() to make a regex search and assert result.

  protected function assertNoPattern($pattern) {
    @trigger_error('assertNoPattern() is deprecated and scheduled for removal in Drupal 9.0.0. Use $this->assertSession()->responseNotMatches($pattern) instead. See https://www.drupal.org/node/2864262.', E_USER_DEPRECATED);
    $this->assertSession()->responseNotMatches($pattern);
  }

If i mock responseNotMatches(), i define which will be the result, so i don't really test assertNoPattern(). I make some obvious tests in this case.

May be i'm wrong on how mock works and how to deal with it. Feel free to show me or explain where i am wrong if it's the case.

GoZ’s picture

@Jo Fitzgerald testAssertNoPatternFail() succeed, since you explicitly ask to responseNotMatches() to throw exception.

We expect a PHPUnit_Framework_ExpectationFailedException::class.
assertNoPattern() only call to responseNotMatches()
We define responseNotMatches() will throw exception PHPUnit_Framework_ExpectationFailedException
We test assertNoPattern(), so it throw PHPUnit_Framework_ExpectationFailedException as we ask it to do.

For me, this test is a nonsense (as i also expain in #9)

Analogy:
Ask to a guy to say "Hey"
Ask to this guy to answer "Yes" when you will ask a question to him.
Ask him if he said "Hey".
He responds "Yes"
Test succeed.

Repeat asking him if he said "Tô" and he will answer "Yes".
Test fail

jofitz’s picture

@GoZ I couldn't agree more, but that is often the way with tests and mocks. The example @klausi recommended, testAssertOptionSelected(), is only one small step away from the same thing. I would be interested to hear @klausi's opinion...

klausi’s picture

@GoZ: you would be testing that assertNoPattern() works as you intend it to work: just passing through stuff to responseNotMatches(). So the unit test confirms that
1) the method assertNoPattern() exists and is callable
2) The calls are forwarded to responseNotMatches()

That is the nature of a unit test: it only tests that little piece that you just wrote.

However, we can also add a test to BrowserTestBaseTest instead if you prefer that?

jofitz’s picture

@klausi if the testbot replicates my local installation then testAssertNoPattern() will fail due to not containing an assert - are you aware how to avoid this?

klausi’s picture

Status: Needs review » Needs work

@Jo: you can do ->shouldBeCalled() or shouldNotBeCalled() on the mock. That will produce an assertion in PHPUnit.

  1. +++ b/core/tests/Drupal/Tests/Core/Assert/AssertLegacyTraitTest.php
    @@ -154,6 +154,33 @@ public function testAssertOptionSelectedFail() {
    +    $this->webAssert
    +      ->responseNotMatches('/.*foo$/')
    +      ->willReturn(NULL);
    

    You should add a ->shouldBeCalled() here.

  2. +++ b/core/tests/Drupal/Tests/Core/Assert/AssertLegacyTraitTest.php
    @@ -154,6 +154,33 @@ public function testAssertOptionSelectedFail() {
    +  /**
    +   * @covers ::assertNoPattern
    +   */
    +  public function testAssertNoPatternFail() {
    +    $this->page->getContent()->willReturn('foo bar bar');
    +    $this->setExpectedException(PHPUnit_Framework_ExpectationFailedException::class);
    +
    +    $this->webAssert
    +      ->responseNotMatches('/^foo.*$/')
    +      ->willThrow(\PHPUnit_Framework_ExpectationFailedException::class);
    +
    +    $this->assertNoPattern('/^foo.*$/');
    +  }
    

    I think we can remove this test. We only want to test that assertNoPattern() can be called and that it forwards calls to responseNotMatches(). That is covered by the first test already.

+++ b/core/tests/Drupal/Tests/Core/Assert/AssertLegacyTraitTest.php
@@ -154,6 +154,33 @@ public function testAssertOptionSelectedFail() {
+    $this->page->getContent()->willReturn('foo bar bar');

This line can be removed.

GoZ’s picture

Assigned: GoZ » Unassigned
Status: Needs work » Needs review
FileSize
2.31 KB
1.18 KB

Here we go

Thanks for the detailed solution @klausi

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

We have a test, we have the required documentation ...

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 47d0521 to 8.4.x and fd92fc7 to 8.3.x. Thanks!

Committed to 8.3.x because keeping the test API the same is important.

  • alexpott committed 47d0521 on 8.4.x
    Issue #2864257 by GoZ, Jo Fitzgerald, klausi: Convert web tests...

  • alexpott committed fd92fc7 on 8.3.x
    Issue #2864257 by GoZ, Jo Fitzgerald, klausi: Convert web tests...

Status: Fixed » Closed (fixed)

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

Mile23’s picture

Updated and published CR.