From ab58ca41a1f4c4f4c4ec33758bb2dd16f4fb28da Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Tue, 29 Sep 2020 19:54:47 +0100
Subject: [PATCH] Issue #3142267 by xjm, alexpott, mohrerao, jungle, mondrake,
 longwave, larowlan: Allow the full list of deprecations from PHPUnit8Warning
 to respect the --suppress-deprecations behavior on DrupalCI

---
 .../BuildTests/Framework/BuildTestBase.php    |  4 +-
 .../Drupal/KernelTests/KernelTestBase.php     |  4 +-
 core/tests/Drupal/Tests/BrowserTestBase.php   |  4 +-
 .../Plugin/Scaffold/AssertUtilsTrait.php      |  4 +-
 .../Scaffold/Integration/AppendOpTest.php     |  4 +-
 .../Scaffold/Integration/ReplaceOpTest.php    |  4 +-
 .../Scaffold/Integration/SkipOpTest.php       |  4 +-
 .../Drupal/Tests/PhpUnitWarningsTest.php      | 91 +++++++++++++++++++
 .../Drupal/Tests/Traits/PHPUnit8Warnings.php  | 43 ---------
 .../Drupal/Tests/Traits/PhpUnitWarnings.php   | 64 +++++++++++++
 core/tests/Drupal/Tests/UnitTestCase.php      |  4 +-
 11 files changed, 171 insertions(+), 59 deletions(-)
 create mode 100644 core/tests/Drupal/Tests/PhpUnitWarningsTest.php
 delete mode 100644 core/tests/Drupal/Tests/Traits/PHPUnit8Warnings.php
 create mode 100644 core/tests/Drupal/Tests/Traits/PhpUnitWarnings.php

diff --git a/core/tests/Drupal/BuildTests/Framework/BuildTestBase.php b/core/tests/Drupal/BuildTests/Framework/BuildTestBase.php
index 0e8fee10215a..7bd4f7034714 100644
--- a/core/tests/Drupal/BuildTests/Framework/BuildTestBase.php
+++ b/core/tests/Drupal/BuildTests/Framework/BuildTestBase.php
@@ -7,7 +7,7 @@
 use Behat\Mink\Mink;
 use Behat\Mink\Session;
 use Drupal\Component\FileSystem\FileSystem as DrupalFilesystem;
-use Drupal\Tests\Traits\PHPUnit8Warnings;
+use Drupal\Tests\Traits\PhpUnitWarnings;
 use PHPUnit\Framework\TestCase;
 use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
 use Symfony\Component\Finder\Finder;
@@ -52,7 +52,7 @@
 abstract class BuildTestBase extends TestCase {
 
   use ExternalCommandRequirementsTrait;
-  use PHPUnit8Warnings;
+  use PhpUnitWarnings;
 
   /**
    * The working directory where this test will manipulate files.
diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php
index ae72732df2da..ba7c5712fa12 100644
--- a/core/tests/Drupal/KernelTests/KernelTestBase.php
+++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
@@ -19,7 +19,7 @@
 use Drupal\Tests\ConfigTestTrait;
 use Drupal\Tests\RandomGeneratorTrait;
 use Drupal\Tests\TestRequirementsTrait;
-use Drupal\Tests\Traits\PHPUnit8Warnings;
+use Drupal\Tests\Traits\PhpUnitWarnings;
 use Drupal\TestTools\Comparator\MarkupInterfaceComparator;
 use PHPUnit\Framework\Exception;
 use PHPUnit\Framework\TestCase;
@@ -78,7 +78,7 @@ abstract class KernelTestBase extends TestCase implements ServiceProviderInterfa
   use RandomGeneratorTrait;
   use ConfigTestTrait;
   use TestRequirementsTrait;
-  use PHPUnit8Warnings;
+  use PhpUnitWarnings;
 
   /**
    * {@inheritdoc}
diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php
index 0aac9ce74798..448bb6a22b60 100644
--- a/core/tests/Drupal/Tests/BrowserTestBase.php
+++ b/core/tests/Drupal/Tests/BrowserTestBase.php
@@ -16,7 +16,7 @@
 use Drupal\Tests\block\Traits\BlockCreationTrait;
 use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
 use Drupal\Tests\node\Traits\NodeCreationTrait;
-use Drupal\Tests\Traits\PHPUnit8Warnings;
+use Drupal\Tests\Traits\PhpUnitWarnings;
 use Drupal\Tests\user\Traits\UserCreationTrait;
 use Drupal\TestTools\Comparator\MarkupInterfaceComparator;
 use GuzzleHttp\Cookie\CookieJar;
@@ -64,7 +64,7 @@ abstract class BrowserTestBase extends TestCase {
     createUser as drupalCreateUser;
   }
   use XdebugRequestTrait;
-  use PHPUnit8Warnings;
+  use PhpUnitWarnings;
 
   /**
    * The database prefix of this test run.
diff --git a/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/AssertUtilsTrait.php b/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/AssertUtilsTrait.php
index 96dab849123b..ecbd4f83c123 100644
--- a/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/AssertUtilsTrait.php
+++ b/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/AssertUtilsTrait.php
@@ -2,13 +2,13 @@
 
 namespace Drupal\Tests\Composer\Plugin\Scaffold;
 
-use Drupal\Tests\Traits\PHPUnit8Warnings;
+use Drupal\Tests\Traits\PhpUnitWarnings;
 
 /**
  * Convenience class for creating fixtures.
  */
 trait AssertUtilsTrait {
-  use PHPUnit8Warnings;
+  use PhpUnitWarnings;
 
   /**
    * Asserts that a given file exists and is/is not a symlink.
diff --git a/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/AppendOpTest.php b/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/AppendOpTest.php
index e37d06f1f544..ff189a37880b 100644
--- a/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/AppendOpTest.php
+++ b/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/AppendOpTest.php
@@ -5,7 +5,7 @@
 use Drupal\Composer\Plugin\Scaffold\Operations\AppendOp;
 use Drupal\Composer\Plugin\Scaffold\ScaffoldOptions;
 use Drupal\Tests\Composer\Plugin\Scaffold\Fixtures;
-use Drupal\Tests\Traits\PHPUnit8Warnings;
+use Drupal\Tests\Traits\PhpUnitWarnings;
 use PHPUnit\Framework\TestCase;
 
 /**
@@ -14,7 +14,7 @@
  * @group Scaffold
  */
 class AppendOpTest extends TestCase {
-  use PHPUnit8Warnings;
+  use PhpUnitWarnings;
 
   /**
    * @covers ::process
diff --git a/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/ReplaceOpTest.php b/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/ReplaceOpTest.php
index cb5d6c7c4bb9..65de3c77c057 100644
--- a/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/ReplaceOpTest.php
+++ b/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/ReplaceOpTest.php
@@ -5,7 +5,7 @@
 use Drupal\Composer\Plugin\Scaffold\Operations\ReplaceOp;
 use Drupal\Composer\Plugin\Scaffold\ScaffoldOptions;
 use Drupal\Tests\Composer\Plugin\Scaffold\Fixtures;
-use Drupal\Tests\Traits\PHPUnit8Warnings;
+use Drupal\Tests\Traits\PhpUnitWarnings;
 use PHPUnit\Framework\TestCase;
 
 /**
@@ -14,7 +14,7 @@
  * @group Scaffold
  */
 class ReplaceOpTest extends TestCase {
-  use PHPUnit8Warnings;
+  use PhpUnitWarnings;
 
   /**
    * @covers ::process
diff --git a/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/SkipOpTest.php b/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/SkipOpTest.php
index ba8313bbce6c..f2cb6cbd6e88 100644
--- a/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/SkipOpTest.php
+++ b/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/SkipOpTest.php
@@ -5,7 +5,7 @@
 use Drupal\Composer\Plugin\Scaffold\Operations\SkipOp;
 use Drupal\Composer\Plugin\Scaffold\ScaffoldOptions;
 use Drupal\Tests\Composer\Plugin\Scaffold\Fixtures;
-use Drupal\Tests\Traits\PHPUnit8Warnings;
+use Drupal\Tests\Traits\PhpUnitWarnings;
 use PHPUnit\Framework\TestCase;
 
 /**
@@ -14,7 +14,7 @@
  * @group Scaffold
  */
 class SkipOpTest extends TestCase {
-  use PHPUnit8Warnings;
+  use PhpUnitWarnings;
 
   /**
    * @covers ::process
diff --git a/core/tests/Drupal/Tests/PhpUnitWarningsTest.php b/core/tests/Drupal/Tests/PhpUnitWarningsTest.php
new file mode 100644
index 000000000000..6a741265e343
--- /dev/null
+++ b/core/tests/Drupal/Tests/PhpUnitWarningsTest.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace Drupal\Tests;
+
+/**
+ * @coversDefaultClass \Drupal\Tests\Traits\PhpUnitWarnings
+ * @group legacy
+ */
+class PhpUnitWarningsTest extends UnitTestCase {
+
+  /**
+   * @expectedDeprecation Test warning for \Drupal\Tests\PhpUnitWarningsTest::testAddWarning()
+   */
+  public function testAddWarning() {
+    $this->addWarning('Test warning for \Drupal\Tests\PhpUnitWarningsTest::testAddWarning()');
+  }
+
+  /**
+   * @expectedDeprecation Using assertContains() with string haystacks is deprecated and will not be supported in PHPUnit 9. Refactor your test to use assertStringContainsString() or assertStringContainsStringIgnoringCase() instead.
+   * @expectedDeprecation The optional $ignoreCase parameter of assertContains() is deprecated and will be removed in PHPUnit 9.
+   */
+  public function testAssertContains() {
+    $this->assertContains('string', 'aaaa_string_aaa');
+    $this->assertContains('STRING', 'aaaa_string_aaa', '', TRUE);
+  }
+
+  /**
+   * @expectedDeprecation Using assertNotContains() with string haystacks is deprecated and will not be supported in PHPUnit 9. Refactor your test to use assertStringNotContainsString() or assertStringNotContainsStringIgnoringCase() instead.
+   * @expectedDeprecation The optional $ignoreCase parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9.
+   */
+  public function testAssertNotContains() {
+    $this->assertNotContains('foo', 'bar');
+    $this->assertNotContains('FOO', 'bar', '', TRUE);
+  }
+
+  /**
+   * @expectedDeprecation assertArraySubset() is deprecated and will be removed in PHPUnit 9.
+   */
+  public function testAssertArraySubset() {
+    $this->assertArraySubset(['a'], ['a', 'b']);
+  }
+
+  /**
+   * @expectedDeprecation assertInternalType() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(), assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(), assertIsResource(), assertIsString(), assertIsScalar(), assertIsCallable(), or assertIsIterable() instead.
+   */
+  public function testAssertInternalType() {
+    $this->assertInternalType('string', 'string');
+  }
+
+  /**
+   * @expectedDeprecation assertAttributeEquals() is deprecated and will be removed in PHPUnit 9.
+   * @expectedDeprecation readAttribute() is deprecated and will be removed in PHPUnit 9.
+   * @expectedDeprecation getObjectAttribute() is deprecated and will be removed in PHPUnit 9.
+   * @expectedDeprecation assertAttributeSame() is deprecated and will be removed in PHPUnit 9.
+   * @expectedDeprecation assertAttributeInstanceOf() is deprecated and will be removed in PHPUnit 9.
+   * @expectedDeprecation assertAttributeEmpty() is deprecated and will be removed in PHPUnit 9.
+   */
+  public function testAssertAttribute() {
+    $obj = new class() {
+      protected $attribute = 'value';
+      protected $class;
+      protected $empty;
+
+      public function __construct() {
+        $this->class = new \stdClass();
+      }
+
+    };
+    $this->assertAttributeEquals('value', 'attribute', $obj);
+    $this->assertAttributeSame('value', 'attribute', $obj);
+    $this->assertAttributeInstanceOf(\stdClass::class, 'class', $obj);
+    $this->assertAttributeEmpty('empty', $obj);
+  }
+
+  /**
+   * @expectedDeprecation The optional $canonicalize parameter of assertEquals() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertEqualsCanonicalizing() instead.
+   */
+  public function testAssertEquals() {
+    $this->assertEquals(['a', 'b'], ['b', 'a'], '', 0.0, 10, TRUE);
+  }
+
+  /**
+   * @expectedDeprecation expectExceptionMessageRegExp() is deprecated in PHPUnit 8 and will be removed in PHPUnit 9.
+   */
+  public function testExpectExceptionMessageRegExp() {
+    $this->expectException(\Exception::class);
+    $this->expectExceptionMessageRegExp('/An exception .*/');
+    throw new \Exception('An exception has been triggered');
+  }
+
+}
diff --git a/core/tests/Drupal/Tests/Traits/PHPUnit8Warnings.php b/core/tests/Drupal/Tests/Traits/PHPUnit8Warnings.php
deleted file mode 100644
index 5b80264681a4..000000000000
--- a/core/tests/Drupal/Tests/Traits/PHPUnit8Warnings.php
+++ /dev/null
@@ -1,43 +0,0 @@
-<?php
-
-namespace Drupal\Tests\Traits;
-
-/**
- * Used to ignore warnings being added by PHPUnit 8.
- *
- * This trait exists to allow Drupal 8 tests using PHPUnit 7 and Drupal 9 tests
- * using PHPUnit 8 to happily co-exist. Once Drupal 8 and Drupal 9 are not so
- * closely aligned these will be fixed in core and the warnings will be emitted
- * from the test runner.
- *
- * @todo https://www.drupal.org/project/drupal/issues/3110543 Remove the ignored
- *   warnings to support PHPUnit 9.
- *
- * @internal
- */
-trait PHPUnit8Warnings {
-
-  /**
-   * The list of warnings to ignore.
-   *
-   * @var string[]
-   */
-  private static $ignoredWarnings = [
-    'expectExceptionMessageRegExp() is deprecated in PHPUnit 8 and will be removed in PHPUnit 9.',
-  ];
-
-  /**
-   * Ignores specific PHPUnit 8 warnings.
-   *
-   * @see \PHPUnit\Framework\TestCase::addWarning()
-   *
-   * @internal
-   */
-  public function addWarning(string $warning): void {
-    if (in_array($warning, self::$ignoredWarnings, TRUE)) {
-      return;
-    }
-    parent::addWarning($warning);
-  }
-
-}
diff --git a/core/tests/Drupal/Tests/Traits/PhpUnitWarnings.php b/core/tests/Drupal/Tests/Traits/PhpUnitWarnings.php
new file mode 100644
index 000000000000..d2e3039d208e
--- /dev/null
+++ b/core/tests/Drupal/Tests/Traits/PhpUnitWarnings.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace Drupal\Tests\Traits;
+
+/**
+ * Converts deprecation warnings added by PHPUnit to silenced deprecations.
+ *
+ * This trait exists to allow Drupal to run tests with multiple versions of
+ * PHPUnit without failing due to PHPUnit's deprecation warnings.
+ *
+ * @internal
+ */
+trait PhpUnitWarnings {
+
+  /**
+   * Deprecation warnings from PHPUnit to raise with @trigger_error().
+   *
+   * Add any PHPUnit deprecations that should be handled as deprecation warnings
+   * (rather than unconditional failures) for core and contrib.
+   *
+   * @var string[]
+   */
+  private static $deprecationWarnings = [
+    'Using assertContains() with string haystacks is deprecated and will not be supported in PHPUnit 9. Refactor your test to use assertStringContainsString() or assertStringContainsStringIgnoringCase() instead.',
+    'Using assertNotContains() with string haystacks is deprecated and will not be supported in PHPUnit 9. Refactor your test to use assertStringNotContainsString() or assertStringNotContainsStringIgnoringCase() instead.',
+    'assertArraySubset() is deprecated and will be removed in PHPUnit 9.',
+    'assertInternalType() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(), assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(), assertIsResource(), assertIsString(), assertIsScalar(), assertIsCallable(), or assertIsIterable() instead.',
+    'readAttribute() is deprecated and will be removed in PHPUnit 9.',
+    'getObjectAttribute() is deprecated and will be removed in PHPUnit 9.',
+    'The optional $canonicalize parameter of assertEquals() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertEqualsCanonicalizing() instead.',
+    'assertAttributeEquals() is deprecated and will be removed in PHPUnit 9.',
+    'assertAttributeSame() is deprecated and will be removed in PHPUnit 9.',
+    'assertAttributeInstanceOf() is deprecated and will be removed in PHPUnit 9.',
+    'assertAttributeEmpty() is deprecated and will be removed in PHPUnit 9.',
+    'The optional $ignoreCase parameter of assertContains() is deprecated and will be removed in PHPUnit 9.',
+    'The optional $ignoreCase parameter of assertNotContains() is deprecated and will be removed in PHPUnit 9.',
+    'expectExceptionMessageRegExp() is deprecated in PHPUnit 8 and will be removed in PHPUnit 9.',
+    // Warning for testing.
+    'Test warning for \Drupal\Tests\PhpUnitWarningsTest::testAddWarning()',
+  ];
+
+  /**
+   * Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
+   *
+   * @param string $warning
+   *   The warning message raised in tests.
+   *
+   * @see \PHPUnit\Framework\TestCase::addWarning()
+   *
+   * @internal
+   */
+  public function addWarning(string $warning): void {
+    if (in_array($warning, self::$deprecationWarnings, TRUE)) {
+      // Convert listed PHPUnit deprecations into E_USER_DEPRECATED and prevent
+      // each from being raised as a test warning.
+      @trigger_error($warning, E_USER_DEPRECATED);
+      return;
+    }
+
+    // Otherwise, let the parent raise any warning not specifically listed.
+    parent::addWarning($warning);
+  }
+
+}
diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php
index 2c2257b2dabe..55978ba413c8 100644
--- a/core/tests/Drupal/Tests/UnitTestCase.php
+++ b/core/tests/Drupal/Tests/UnitTestCase.php
@@ -9,7 +9,7 @@
 use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Core\StringTranslation\TranslatableMarkup;
 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
-use Drupal\Tests\Traits\PHPUnit8Warnings;
+use Drupal\Tests\Traits\PhpUnitWarnings;
 use PHPUnit\Framework\TestCase;
 
 /**
@@ -19,7 +19,7 @@
  */
 abstract class UnitTestCase extends TestCase {
 
-  use PHPUnit8Warnings;
+  use PhpUnitWarnings;
 
   /**
    * The random generator.
-- 
GitLab