Problem/Motivation

See current code:
https://git.drupalcode.org/project/block_class/-/blob/3.0.x/tests/src/Fu...

    // Assert the custom class in the content block.
    if (version_compare(\Drupal::VERSION, '10', '<')) {
      // Support tests for D9.
      $assert->responseContains('<div id="block-mainpagecontent" class="' . $test_classes_block_main . ' block block-system block-system-main-block">');
      // Assert the custom class in user menu.
      $assert->responseContains('<nav  id="block-useraccountmenu" class="' . $test_classes_block_user_menu . ' block block-menu navigation menu--account secondary-nav" aria-labelledby="block-useraccountmenu-menu" role="navigation">');
    }
    else {
      $assert->responseContains('<div id="block-' . $this->defaultTheme . '-mainpagecontent" class="' . $test_classes_block_main . ' block block-system block-system-main-block">');
      // Assert the custom class in user menu.
      $assert->responseContains('<nav  id="block-' . $this->defaultTheme . '-useraccountmenu" class="' . $test_classes_block_user_menu . ' block block-menu navigation menu--account secondary-nav" aria-labelledby="block-' . $this->defaultTheme . '-useraccountmenu-menu" role="navigation">');
    }

Currently Functional tests are checking for the exact markup in the response, with all the classes, attributes, etc...

The problem is that attributes can change position depending on the Drupal core versions, themes and there is no interest in checking any attributes other than CSS classes.

Steps to reproduce

Run module's PHPUNIT Functional Tests.

Proposed resolution

Replace the use of responseContains with something a bit more flexible, such as:
elementExists('xpath')
to check the existence of the tested HTML element, identified by its id and class attributes.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dydave created an issue. See original summary.

  • dydave committed 9a4d5b34 on 3.0.x
    Issue #3589811 by dydave: PHPUNIT: Improved Functional tests and fixed...
dydave’s picture

Quick follow-up on this issue:

Fixed failing Kernel tests on D9 :
Added a check for D9 versions to unset the migration which was added in related #3559954: Fix phpunit tests warnings for next minor and compatibility with D9:
'block_content_body_field_storage'
which was added in previous commit:
https://git.drupalcode.org/project/block_class/-/commit/350f20d80d9ab098...

Improved Functional tests:
Previously the tests were checking for the exact string of the expected tag element in the response HTML code.

Now the checks are using the method $assert->elementExists('xpath'), with an XPath expression with the following conditions:

  • Search for elements containing the CSS classes, and
  • with an ID attribute end with the tested block's expected ID.

 
Searching with XPath seemed a bit faster than with CSS.

Note XPath 1.0 does not have an end-with operator, therefore, the use of substring(@id, string-length(@id) - 14) was necessary.

Since the merge request passed all the tests and jobs 🟢, on D9.5.x as well, I went ahead and merged the changes above at #3.
The D9 pipeline is now passing 🟢
https://git.drupalcode.org/project/block_class/-/pipelines/818874

Marking issue as Fixed, for now.

Thanks in advance!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

dydave’s picture

Component: Code » Automated tests

Status: Fixed » Closed (fixed)

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