Mercurial > hg > cmmr2012-drupal-site
diff core/modules/simpletest/src/TestBase.php @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | c75dbcec494b |
children | 12f9dff5fda9 |
line wrap: on
line diff
--- a/core/modules/simpletest/src/TestBase.php Thu Feb 28 11:14:44 2019 +0000 +++ b/core/modules/simpletest/src/TestBase.php Thu Feb 28 13:11:55 2019 +0000 @@ -5,7 +5,7 @@ use Drupal\Component\Assertion\Handle; use Drupal\Component\Render\MarkupInterface; use Drupal\Component\Utility\Crypt; -use Drupal\Component\Utility\SafeMarkup; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Database\Database; use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PublicStream; @@ -15,7 +15,6 @@ use Drupal\Tests\AssertHelperTrait as BaseAssertHelperTrait; use Drupal\Tests\ConfigTestTrait; use Drupal\Tests\RandomGeneratorTrait; -use Drupal\Tests\SessionTestTrait; use Drupal\Tests\Traits\Core\GeneratePermutationsTrait; /** @@ -27,10 +26,9 @@ use BaseAssertHelperTrait; use TestSetupTrait; - use SessionTestTrait; use RandomGeneratorTrait; use GeneratePermutationsTrait; - // For backwards compatibility switch the visbility of the methods to public. + // For backwards compatibility switch the visibility of the methods to public. use ConfigTestTrait { configImporter as public; copyConfig as public; @@ -53,7 +51,7 @@ /** * Current results of this test case. * - * @var Array + * @var array */ public $results = [ '#pass' => 0, @@ -65,7 +63,7 @@ /** * Assertions thrown in that test case. * - * @var Array + * @var array */ protected $assertions = []; @@ -249,6 +247,20 @@ } /** + * Fail the test if it belongs to a PHPUnit-based framework. + * + * This would probably be caused by automated test conversions such as those + * in https://www.drupal.org/project/drupal/issues/2770921. + */ + public function checkTestHierarchyMismatch() { + // We can use getPhpunitTestSuite() because it uses a regex on the class' + // namespace to deduce the PHPUnit test suite. + if (TestDiscovery::getPhpunitTestSuite(get_class($this)) !== FALSE) { + $this->fail(get_class($this) . ' incorrectly subclasses ' . __CLASS__ . ', it should probably extend \Drupal\Tests\BrowserTestBase instead.'); + } + } + + /** * Performs setup tasks before each individual test method is run. */ abstract protected function setUp(); @@ -291,7 +303,7 @@ * TRUE is a synonym for 'pass', FALSE for 'fail'. * @param string|\Drupal\Component\Render\MarkupInterface $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -449,7 +461,7 @@ * The value on which the assertion is to be done. * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -462,7 +474,7 @@ * TRUE if the assertion succeeded, FALSE otherwise. */ protected function assertTrue($value, $message = '', $group = 'Other') { - return $this->assert((bool) $value, $message ? $message : SafeMarkup::format('Value @value is TRUE.', ['@value' => var_export($value, TRUE)]), $group); + return $this->assert((bool) $value, $message ? $message : new FormattableMarkup('Value @value is TRUE.', ['@value' => var_export($value, TRUE)]), $group); } /** @@ -474,7 +486,7 @@ * The value on which the assertion is to be done. * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -487,7 +499,7 @@ * TRUE if the assertion succeeded, FALSE otherwise. */ protected function assertFalse($value, $message = '', $group = 'Other') { - return $this->assert(!$value, $message ? $message : SafeMarkup::format('Value @value is FALSE.', ['@value' => var_export($value, TRUE)]), $group); + return $this->assert(!$value, $message ? $message : new FormattableMarkup('Value @value is FALSE.', ['@value' => var_export($value, TRUE)]), $group); } /** @@ -497,7 +509,7 @@ * The value on which the assertion is to be done. * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -510,7 +522,7 @@ * TRUE if the assertion succeeded, FALSE otherwise. */ protected function assertNull($value, $message = '', $group = 'Other') { - return $this->assert(!isset($value), $message ? $message : SafeMarkup::format('Value @value is NULL.', ['@value' => var_export($value, TRUE)]), $group); + return $this->assert(!isset($value), $message ? $message : new FormattableMarkup('Value @value is NULL.', ['@value' => var_export($value, TRUE)]), $group); } /** @@ -520,7 +532,7 @@ * The value on which the assertion is to be done. * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -533,7 +545,7 @@ * TRUE if the assertion succeeded, FALSE otherwise. */ protected function assertNotNull($value, $message = '', $group = 'Other') { - return $this->assert(isset($value), $message ? $message : SafeMarkup::format('Value @value is not NULL.', ['@value' => var_export($value, TRUE)]), $group); + return $this->assert(isset($value), $message ? $message : new FormattableMarkup('Value @value is not NULL.', ['@value' => var_export($value, TRUE)]), $group); } /** @@ -545,7 +557,7 @@ * The second value to check. * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -565,7 +577,7 @@ $second = $this->castSafeStrings($second); $is_equal = $first == $second; if (!$is_equal || !$message) { - $default_message = SafeMarkup::format('Value @first is equal to value @second.', ['@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE)]); + $default_message = new FormattableMarkup('Value @first is equal to value @second.', ['@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE)]); $message = $message ? $message . PHP_EOL . $default_message : $default_message; } return $this->assert($is_equal, $message, $group); @@ -580,7 +592,7 @@ * The second value to check. * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -600,7 +612,7 @@ $second = $this->castSafeStrings($second); $not_equal = $first != $second; if (!$not_equal || !$message) { - $default_message = SafeMarkup::format('Value @first is not equal to value @second.', ['@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE)]); + $default_message = new FormattableMarkup('Value @first is not equal to value @second.', ['@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE)]); $message = $message ? $message . PHP_EOL . $default_message : $default_message; } return $this->assert($not_equal, $message, $group); @@ -615,7 +627,7 @@ * The second value to check. * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -630,7 +642,7 @@ protected function assertIdentical($first, $second, $message = '', $group = 'Other') { $is_identical = $first === $second; if (!$is_identical || !$message) { - $default_message = SafeMarkup::format('Value @first is identical to value @second.', ['@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE)]); + $default_message = new FormattableMarkup('Value @first is identical to value @second.', ['@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE)]); $message = $message ? $message . PHP_EOL . $default_message : $default_message; } return $this->assert($is_identical, $message, $group); @@ -645,7 +657,7 @@ * The second value to check. * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -660,7 +672,7 @@ protected function assertNotIdentical($first, $second, $message = '', $group = 'Other') { $not_identical = $first !== $second; if (!$not_identical || !$message) { - $default_message = SafeMarkup::format('Value @first is not identical to value @second.', ['@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE)]); + $default_message = new FormattableMarkup('Value @first is not identical to value @second.', ['@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE)]); $message = $message ? $message . PHP_EOL . $default_message : $default_message; } return $this->assert($not_identical, $message, $group); @@ -675,7 +687,7 @@ * The second object to check. * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -688,7 +700,7 @@ * TRUE if the assertion succeeded, FALSE otherwise. */ protected function assertIdenticalObject($object1, $object2, $message = '', $group = 'Other') { - $message = $message ?: SafeMarkup::format('@object1 is identical to @object2', [ + $message = $message ?: new FormattableMarkup('@object1 is identical to @object2', [ '@object1' => var_export($object1, TRUE), '@object2' => var_export($object2, TRUE), ]); @@ -755,7 +767,7 @@ * * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -776,7 +788,7 @@ * * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -797,7 +809,7 @@ * * @param $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * @param $group @@ -864,6 +876,7 @@ * methods during debugging. */ public function run(array $methods = []) { + $this->checkTestHierarchyMismatch(); $class = get_class($this); if ($missing_requirements = $this->checkRequirements()) { @@ -985,7 +998,7 @@ TestServiceProvider::$currentTest = NULL; // Clear out the error messages and restore error handler. - drupal_get_messages(); + \Drupal::messenger()->deleteAll(); restore_error_handler(); } @@ -1326,7 +1339,7 @@ ]); $decoded_exception = Error::decodeException($exception); unset($decoded_exception['backtrace']); - $message = SafeMarkup::format('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $decoded_exception + [ + $message = new FormattableMarkup('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $decoded_exception + [ '@backtrace' => Error::formatBacktrace($verbose_backtrace), ]); $this->error($message, 'Uncaught exception', Error::getLastCaller($backtrace));