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;
use Drupal\Tests\AssertHelperTrait as BaseAssertHelperTrait;
use Drupal\Tests\ConfigTestTrait;
use Drupal\Tests\RandomGeneratorTrait;
-use Drupal\Tests\SessionTestTrait;
use Drupal\Tests\Traits\Core\GeneratePermutationsTrait;
/**
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;
/**
* Current results of this test case.
*
- * @var Array
+ * @var array
*/
public $results = [
'#pass' => 0,
/**
* Assertions thrown in that test case.
*
- * @var Array
+ * @var array
*/
protected $assertions = [];
$this->testId = $test_id;
}
+ /**
+ * 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.
*/
* 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
* 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
* 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);
}
/**
* 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
* 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);
}
/**
* 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
* 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);
}
/**
* 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
* 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);
}
/**
* 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
$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);
* 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
$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);
* 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
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);
* 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
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);
* 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
* 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),
]);
*
* @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
*
* @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
*
* @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
* methods during debugging.
*/
public function run(array $methods = []) {
+ $this->checkTestHierarchyMismatch();
$class = get_class($this);
if ($missing_requirements = $this->checkRequirements()) {
TestServiceProvider::$currentTest = NULL;
// Clear out the error messages and restore error handler.
- drupal_get_messages();
+ \Drupal::messenger()->deleteAll();
restore_error_handler();
}
]);
$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));