Testing the following output:

<select name="test">
  <option value="foo">Foo</option>
  <option value="bar" selected="selected">Bar</option>
</select>

with an assert:

$this->assertFieldByName('test', 'foo');

passes when it should fail.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

olli’s picture

Status: Active » Needs review
FileSize
1.14 KB

This seems to fix it.

Status: Needs review » Needs work

The last submitted patch, drupal-2105617-1.patch, failed testing.

olli’s picture

Status: Needs work » Needs review
FileSize
5.92 KB
4.79 KB

Those failing tests are checking if the element exists so we should use NULL as the second parameter.

olli’s picture

Issue tags: +PHPUnit, +Needs backport to D7

tagging

dawehner’s picture

  1. +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php
    @@ -0,0 +1,83 @@
    + * Tests helper methods provided by the abstract WebTestBase class.
    + */
    

    Let's add a @group Drupal at least.

  2. +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php
    @@ -0,0 +1,83 @@
    +class WebTestBaseTest extends UnitTestCase {
    +
    

    HAHAHAHHA

  3. +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php
    @@ -0,0 +1,83 @@
    +  public function assertFieldByNameProvider() {
    ...
    +   *
    +   * @dataProvider assertFieldByNameProvider
    

    Let's start with provider to avoid some confusion I had.

Mile23’s picture

+++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php
@@ -0,0 +1,83 @@
+  /**
+   * Provides data for testing the assertFieldByName() helper.
+   *
+   * @return array
+   *   An array of values passed to the test method.
+   */
+  public function assertFieldByNameProvider() {

Yah, this is super confusing, especially since method names beginning with 'assert' tend to have special meaning as well. ("Wait, what, is that a new assertion being defined??")

Name it providerTestAssertFieldByName() please.

+1 on @group Drupal and also add @group Simpletest.

olli’s picture

FileSize
4.51 KB
6.78 KB

Thanks for the reviews! I like that provider prefix.

Moved the output to test to separate files and added t() which is called in WebTestBase::parse().

Status: Needs review » Needs work

The last submitted patch, drupal-2105617-7.patch, failed testing.

olli’s picture

Status: Needs work » Needs review
FileSize
580 bytes
6.81 KB

This should fix it.

olli’s picture

FileSize
1.63 KB
7.28 KB

Replaced t() in parse() with String::format() instead.

dawehner’s picture

Issue summary: View changes
Status: Needs review » Needs work
  1. +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
    --- /dev/null
    +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/Fixtures/html/select_2nd_selected
    
    +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/Fixtures/html/select_2nd_selected
    --- /dev/null
    +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/Fixtures/html/select_none_selected
    

    I guess we should name the file with .html ending.

  2. +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php
    @@ -0,0 +1,84 @@
    +class WebTestBaseTest extends UnitTestCase {
    

    Still mindblowing :P

  3. +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php
    @@ -0,0 +1,84 @@
    +
    +  public static function getInfo() {
    

    Let's add a {@inheritdoc} now.

olli’s picture

Status: Needs work » Needs review
FileSize
7.32 KB
1.82 KB

Thanks for your review!
1. changed from html/filename to filename.html
2. we also have TestBaseTest
3. done

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you, that is perfect now.

Xano’s picture

12: drupal-2105617-12.patch queued for re-testing.

Xano’s picture

12: drupal-2105617-12.patch queued for re-testing.

Berdir’s picture

12: drupal-2105617-12.patch queued for re-testing.

The last submitted patch, 12: drupal-2105617-12.patch, failed testing.

olli’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
7.33 KB

Reroll

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Re-RTBC

webchick’s picture

18: drupal-2105617-18.patch queued for re-testing.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -1280,7 +1280,7 @@ protected function parse() {
-        $this->pass(t('Valid HTML found on "@path"', array('@path' => $this->getUrl())), t('Browser'));
+        $this->pass(String::format('Valid HTML found on "@path"', array('@path' => $this->getUrl())), 'Browser');

Why do we think that we can do this? t() is used all over WebTestBase including in assertFieldByName() just removing one call to it seems inconsistent. I would prefer the original solution to this issue in #9.

olli’s picture

Thanks for the review!

Why do we think that we can do this?

Because of #500866: [META] remove t() from assert message. Few t()s have already been converted to String::format().

alexpott’s picture

Yes but following that link to find the issues you find #1601146: Allow custom assertion messages using predefined placeholders which has a different solution - and one that addresses all usages of t() in WebTestBase - that's why I think just removing one usage is inconsistent.

olli’s picture

FileSize
1.63 KB
6.85 KB

Ok!

olli’s picture

Status: Needs work » Needs review
olli’s picture

FileSize
5.54 KB
758 bytes

Reroll.

Mile23’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php
@@ -0,0 +1,87 @@
+   * @see \Drupal\simpletest\WebTestBase::assertFieldByName().

Coding standards. Lose the period at the end.

Other than that, RTBC (again). :-)

alexpott’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed 20d5b0f and pushed to 8.x. Thanks!

Fixed on commit.

diff --git a/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php
index 1cf0600..bbfdadd 100644
--- a/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php
+++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php
@@ -57,7 +57,7 @@ public function providerAssertFieldByName() {
    * @param bool $expected
    *   The expected result of the assert.
    *
-   * @see \Drupal\simpletest\WebTestBase::assertFieldByName().
+   * @see \Drupal\simpletest\WebTestBase::assertFieldByName()
    *
    * @dataProvider providerAssertFieldByName
    */
alexpott’s picture

Status: Patch (to be ported) » Needs work
Issue tags: +Needs reroll

Oops an automated version of me got it wrong

alexpott’s picture

Status: Needs work » Patch (to be ported)
Issue tags: -Needs reroll
sun’s picture

FWIW, this is/was partially a duplicate of #2171939: Invocation of WebTestBase::assertField and WebTestBase::assertNoFieldByName methods with wrong parameters — which discovered many more bogus invocations.

  • alexpott committed 20d5b0f on 8.3.x
    Issue #2105617 by olli: False pass with WebTestBase::assertFieldByName...

  • alexpott committed 20d5b0f on 8.3.x
    Issue #2105617 by olli: False pass with WebTestBase::assertFieldByName...

  • alexpott committed 20d5b0f on 8.4.x
    Issue #2105617 by olli: False pass with WebTestBase::assertFieldByName...

  • alexpott committed 20d5b0f on 8.4.x
    Issue #2105617 by olli: False pass with WebTestBase::assertFieldByName...