Automated testing: Fix deprecation notices: Replace assertEqual() with assertSame()

Created on 8 January 2024, 10 months ago
Updated 23 January 2024, 10 months ago

Problem/Motivation

Running tests locally prompts out the following deprecation notices:

Remaining self deprecation notices (4)

  1x: Using assert[Not]Equals() to compare markup between MarkupInterface objects and plain strings is deprecated in drupal:10.1.0 and will throw an error from drupal:11.0.0. Expected: '<img alt="" loading="lazy" src="/sites/simpletest/28384757/files/2024-01/image-test.png" width="40" height="20" />' - Actual '<img alt="" src="/sites/simpletest/28384757/files/2024-01/image-test.png" width="40" height="20" loading="lazy" />'. Use assert[Not]Same() and cast objects to string instead. See https://www.drupal.org/node/3334057
    1x in ResponsiveImageLinkFormatterTest::testResponsiveImageLinkFormatterWrappedImage from Drupal\Tests\responsive_image_link_formatter\Functional

  1x: Using assert[Not]Equals() to compare markup between MarkupInterface objects and plain strings is deprecated in drupal:10.1.0 and will throw an error from drupal:11.0.0. Expected: '<a href="http://example.com"><img alt="" loading="lazy" src="/sites/simpletest/28384757/files/2024-01/image-test.png" width="40" height="20" /></a>' - Actual '<a href="http://example.com"><img alt="" src="/sites/simpletest/28384757/files/2024-01/image-test.png" width="40" height="20" loading="lazy" /></a>'. Use assert[Not]Same() and cast objects to string instead. See https://www.drupal.org/node/3334057
    1x in ResponsiveImageLinkFormatterTest::testResponsiveImageLinkFormatterWrappedImage from Drupal\Tests\responsive_image_link_formatter\Functional

  1x: Using assert[Not]Equals() to compare markup between MarkupInterface objects and plain strings is deprecated in drupal:10.1.0 and will throw an error from drupal:11.0.0. Expected: '<img loading="lazy" src="/sites/simpletest/83066819/files/2024-01/image-test.png" width="40" height="20" alt="" />' - Actual '<img loading="lazy" src="/sites/simpletest/83066819/files/2024-01/image-test.png" width="40" height="20" alt="" />'. Use assert[Not]Same() and cast objects to string instead. See https://www.drupal.org/node/3334057
    1x in ImageLinkFormatterTest::testImageLinkFormatterWrappedImage from Drupal\Tests\image_link_formatter\Functional

  1x: Using assert[Not]Equals() to compare markup between MarkupInterface objects and plain strings is deprecated in drupal:10.1.0 and will throw an error from drupal:11.0.0. Expected: '<a href="http://example.com"><img loading="lazy" src="/sites/simpletest/83066819/files/2024-01/image-test.png" width="40" height="20" alt="" /></a>' - Actual '<a href="http://example.com"><img loading="lazy" src="/sites/simpletest/83066819/files/2024-01/image-test.png" width="40" height="20" alt="" /></a>'. Use assert[Not]Same() and cast objects to string instead. See https://www.drupal.org/node/3334057
    1x in ImageLinkFormatterTest::testImageLinkFormatterWrappedImage from Drupal\Tests\image_link_formatter\Functional

See PHPUnit documentation: https://docs.phpunit.de/en/9.6/assertions.html#assertsame

Steps to reproduce

Run tests locally with PHPUnit:

cd /var/www/html
./vendor/bin/phpunit ./web/modules/contrib/image_link_formatter/

Proposed resolution

Replace the call to assertEquals with assertSame as suggested in the deprecation notices and described in change record:
Using assert[Not]Equals() to compare markup between MarkupInterface objects and plain strings is deprecated .

Single call to be replaced:
https://git.drupalcode.org/project/image_link_formatter/-/blob/2.1.1/tes...

📌 Task
Status

Fixed

Version

2.1

Component

Automated testing

Created by

🇫🇷France dydave

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @dydave
    • DYdave committed 311284a3 on 2.1.x
      Issue #3413306 by DYdave: Automated testing: Fixed deprecation notices...
    • 9b65e2f5 committed on 2.1.x
      Issue #3413306 by DYdave: Automated testing: Refactored Tests expected...
    • 8cbf97af committed on 2.1.x
      Issue #3413306 by DYdave: Automated testing: Minor fix of Test '...
  • Status changed to Fixed 10 months ago
  • 🇫🇷France dydave

    Quick follow-up on this issue:
    Replaced the call:

    $this->assertEquals($rendered_output, new FormattableMarkup($expected_result[$drupal_version][$show_link], ["@file_url" => $file_url]));
    

    with

    $this->assertSame($rendered_output, (string) new FormattableMarkup($expected_result[$drupal_version][$show_link], ["@file_url" => $file_url]));
    

    casting the second argument to string.

    After making this change I noticed a test for the responsive_image_link_formatter started failing with error:

    1) Drupal\Tests\responsive_image_link_formatter\Functional\ResponsiveImageLinkFormatterTest::testResponsiveImageLinkFormatterWrappedImage
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'<a href="http://example.com"><img alt="" loading="lazy" src="/sites/simpletest/76904023/files/2024-01/image-test.png" width="40" height="20" /></a>'
    +'<a href="http://example.com"><img alt="" src="/sites/simpletest/76904023/files/2024-01/image-test.png" width="40" height="20" loading="lazy" /></a>'
    

    It appeared the generated markup for D10 had changed and the position of the attribute loading="lazy" had moved.

    So the tests expected HTML results had to be refactored to work with the different versions of Core supported by the module:
    Currently, for the responsive_image_link_formatter:

    • Drupal core < 10.1: <img alt="" src="/web/sites/simpletest/48924654/files/2024-01/image-test.png" width="40" height="20" loading="lazy" />
    • Drupal core >= 10.1: <img alt="" loading="lazy" src="/web/sites/simpletest/48924654/files/2024-01/image-test.png" width="40" height="20" />

     
    For image_link_formatter:

    • Drupal core < 9.4: <img src="/web/sites/simpletest/48924654/files/2024-01/image-test.png" width="40" height="20" alt="" loading="lazy" />
    • Drupal core >= 9.4: <img loading="lazy" src="/web/sites/simpletest/48924654/files/2024-01/image-test.png" width="40" height="20" alt="" />

     
    Added a minor change to the documentation.

    The changes have been committed directly to the 2.1.x branch which seems to still be passing all tests, see:
    https://git.drupalcode.org/project/image_link_formatter/-/pipelines/73888

    We're still getting PHPStan warnings but at least all PHPUnit tests pass.

    Feel free to let us know if you have any questions or concerns on any aspects of these changes or the ticket in general, we would surely be happy to help.
    Thanks in advance!

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

Production build 0.71.5 2024