From 891c39b37c91f17c3a941264ef62400f17f28eed Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Sat, 6 Jan 2018 14:20:01 +1000 Subject: [PATCH] Issue #2933991 by alexpott, neclimdul: Deprecation tests fail when all PHPUnit tests are run via PHPUnit --- .../Plugin/migrate/cckfield/LinkCckTest.php | 3 +- ...DrupalStandardsListenerDeprecatedClass.php | 28 +++++++++++++++++++ ...DrupalStandardsListenerDeprecationTest.php | 7 ++--- .../Listeners/DeprecationListenerTrait.php | 14 +++++++++- .../DrupalStandardsListenerTrait.php | 26 +++++++++++++++++ 5 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 core/modules/system/tests/modules/deprecation_test/src/Deprecation/DrupalStandardsListenerDeprecatedClass.php diff --git a/core/modules/link/tests/src/Unit/Plugin/migrate/cckfield/LinkCckTest.php b/core/modules/link/tests/src/Unit/Plugin/migrate/cckfield/LinkCckTest.php index dc3fe7731ee..fb4742ecac0 100644 --- a/core/modules/link/tests/src/Unit/Plugin/migrate/cckfield/LinkCckTest.php +++ b/core/modules/link/tests/src/Unit/Plugin/migrate/cckfield/LinkCckTest.php @@ -46,8 +46,7 @@ class LinkCckTest extends UnitTestCase { /** * @covers ::processCckFieldValues - * @expectedDeprecation CckFieldPluginBase is deprecated in Drupal 8.3.x and will be be removed before Drupal 9.0.x. Use \Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase instead. - * @expectedDeprecation MigrateCckFieldInterface is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \Drupal\migrate_drupal\Annotation\MigrateField instead. + * @expectedDeprecation LinkField is deprecated in Drupal 8.3.x and will be be removed before Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\field\d6\LinkField instead. */ public function testProcessCckFieldValues() { $this->plugin->processCckFieldValues($this->migration, 'somefieldname', []); diff --git a/core/modules/system/tests/modules/deprecation_test/src/Deprecation/DrupalStandardsListenerDeprecatedClass.php b/core/modules/system/tests/modules/deprecation_test/src/Deprecation/DrupalStandardsListenerDeprecatedClass.php new file mode 100644 index 00000000000..de4becba683 --- /dev/null +++ b/core/modules/system/tests/modules/deprecation_test/src/Deprecation/DrupalStandardsListenerDeprecatedClass.php @@ -0,0 +1,28 @@ +getName(FALSE); + if (strpos($method, 'testLegacy') === 0 + || strpos($method, 'provideLegacy') === 0 + || strpos($method, 'getLegacy') === 0 + || strpos(get_class($test), '\Legacy') + || in_array('legacy', $util_test_class::getGroups(get_class($test), $method), TRUE)) { + // This is a legacy test don't skip deprecations. + return; + } + + // Need to edit the file of deprecations to remove any skipped + // deprecations. $deprecations = file_get_contents($file); $deprecations = $deprecations ? unserialize($deprecations) : []; $resave = FALSE; diff --git a/core/tests/Drupal/Tests/Listeners/DrupalStandardsListenerTrait.php b/core/tests/Drupal/Tests/Listeners/DrupalStandardsListenerTrait.php index 2676c46a4fe..0ad0e1e239a 100644 --- a/core/tests/Drupal/Tests/Listeners/DrupalStandardsListenerTrait.php +++ b/core/tests/Drupal/Tests/Listeners/DrupalStandardsListenerTrait.php @@ -143,6 +143,28 @@ trait DrupalStandardsListenerTrait { } } + /** + * Handles errors to ensure deprecation messages are not triggered. + * + * @param int $type + * The severity level of the error. + * @param string $msg + * The error message. + * @param $file + * The file that caused the error. + * @param $line + * The line number that caused the error. + * @param array $context + * The error context. + */ + public static function errorHandler($type, $msg, $file, $line, $context = array()) { + if ($type === E_USER_DEPRECATED) { + return; + } + $error_handler = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_ErrorHandler' : 'PHPUnit\Util\ErrorHandler'; + return $error_handler::handleError($type, $msg, $file, $line, $context); + } + /** * Reacts to the end of a test. * @@ -166,7 +188,11 @@ trait DrupalStandardsListenerTrait { // our purpose, so we have to distinguish between the different known // subclasses. if ($test instanceof TestCase) { + // Change the error handler to ensure deprecation messages are not + // triggered. + set_error_handler([$this, 'errorHandler']); $this->checkValidCoversForTest($test); + restore_error_handler(); } elseif ($this->isTestSuite($test)) { foreach ($test->getGroupDetails() as $tests) {