Commit 7dee9f8f authored by effulgentsia's avatar effulgentsia

Issue #2566447 by stefan.r, joelpittet, alexpott, lauriii, dawehner: Cast safe...

Issue #2566447 by stefan.r, joelpittet, alexpott, lauriii, dawehner: Cast safe string objects to string or use assertEqual() in some assertIdentical() comparisons
parent 9343f6c7
...@@ -109,7 +109,7 @@ public function testBlockInterface() { ...@@ -109,7 +109,7 @@ public function testBlockInterface() {
$actual_form = $display_block->buildConfigurationForm(array(), $form_state); $actual_form = $display_block->buildConfigurationForm(array(), $form_state);
// Remove the visibility sections, as that just tests condition plugins. // Remove the visibility sections, as that just tests condition plugins.
unset($actual_form['visibility'], $actual_form['visibility_tabs']); unset($actual_form['visibility'], $actual_form['visibility_tabs']);
$this->assertIdentical($actual_form, $expected_form, 'Only the expected form elements were present.'); $this->assertIdentical($this->castSafeStrings($actual_form), $this->castSafeStrings($expected_form), 'Only the expected form elements were present.');
$expected_build = array( $expected_build = array(
'#children' => 'My custom display message.', '#children' => 'My custom display message.',
......
...@@ -46,7 +46,7 @@ public function testListing() { ...@@ -46,7 +46,7 @@ public function testListing() {
// Test the contents of each th cell. // Test the contents of each th cell.
$expected_items = array(t('Block description'), t('Operations')); $expected_items = array(t('Block description'), t('Operations'));
foreach ($elements as $key => $element) { foreach ($elements as $key => $element) {
$this->assertIdentical((string) $element[0], $expected_items[$key]); $this->assertEqual($element[0], $expected_items[$key]);
} }
$label = 'Antelope'; $label = 'Antelope';
......
...@@ -42,7 +42,7 @@ public function testListing() { ...@@ -42,7 +42,7 @@ public function testListing() {
$this->assertEqual(count($elements), 4, 'Correct number of table header cells found.'); $this->assertEqual(count($elements), 4, 'Correct number of table header cells found.');
// Test the contents of each th cell. // Test the contents of each th cell.
$expected_items = [t('Block description'), t('Block type'), t('Updated'), t('Operations')]; $expected_items = ['Block description', 'Block type', 'Updated', 'Operations'];
foreach ($elements as $key => $element) { foreach ($elements as $key => $element) {
if ($element->xpath('a')) { if ($element->xpath('a')) {
$this->assertIdentical(trim((string) $element->xpath('a')[0]), $expected_items[$key]); $this->assertIdentical(trim((string) $element->xpath('a')[0]), $expected_items[$key]);
......
...@@ -193,7 +193,7 @@ public function testBreakpointGroups() { ...@@ -193,7 +193,7 @@ public function testBreakpointGroups() {
); );
$breakpoint_groups = \Drupal::service('breakpoint.manager')->getGroups(); $breakpoint_groups = \Drupal::service('breakpoint.manager')->getGroups();
// Ensure the order is as expected. Should be sorted by label. // Ensure the order is as expected. Should be sorted by label.
$this->assertIdentical($expected, $breakpoint_groups); $this->assertIdentical($expected, $this->castSafeStrings($breakpoint_groups));
$expected = array( $expected = array(
'breakpoint_theme_test' => 'theme', 'breakpoint_theme_test' => 'theme',
......
...@@ -87,23 +87,23 @@ function testExistingFormat() { ...@@ -87,23 +87,23 @@ function testExistingFormat() {
// Button groups // Button groups
array( array(
array( array(
'name' => t('Formatting'), 'name' => 'Formatting',
'items' => array('Bold', 'Italic',), 'items' => array('Bold', 'Italic',),
), ),
array( array(
'name' => t('Links'), 'name' => 'Links',
'items' => array('DrupalLink', 'DrupalUnlink',), 'items' => array('DrupalLink', 'DrupalUnlink',),
), ),
array( array(
'name' => t('Lists'), 'name' => 'Lists',
'items' => array('BulletedList', 'NumberedList',), 'items' => array('BulletedList', 'NumberedList',),
), ),
array( array(
'name' => t('Media'), 'name' => 'Media',
'items' => array('Blockquote', 'DrupalImage',), 'items' => array('Blockquote', 'DrupalImage',),
), ),
array( array(
'name' => t('Tools'), 'name' => 'Tools',
'items' => array('Source',), 'items' => array('Source',),
), ),
), ),
...@@ -111,7 +111,7 @@ function testExistingFormat() { ...@@ -111,7 +111,7 @@ function testExistingFormat() {
), ),
'plugins' => array(), 'plugins' => array(),
); );
$this->assertIdentical($ckeditor->getDefaultSettings(), $expected_default_settings); $this->assertIdentical($this->castSafeStrings($ckeditor->getDefaultSettings()), $expected_default_settings);
// Keep the "CKEditor" editor selected and click the "Configure" button. // Keep the "CKEditor" editor selected and click the "Configure" button.
$this->drupalPostAjaxForm(NULL, $edit, 'editor_configure'); $this->drupalPostAjaxForm(NULL, $edit, 'editor_configure');
...@@ -279,7 +279,7 @@ function testNewFormat() { ...@@ -279,7 +279,7 @@ function testNewFormat() {
$expected_settings['plugins']['stylescombo']['styles'] = ''; $expected_settings['plugins']['stylescombo']['styles'] = '';
$editor = entity_load('editor', 'amazing_format'); $editor = entity_load('editor', 'amazing_format');
$this->assertTrue($editor instanceof Editor, 'An Editor config entity exists now.'); $this->assertTrue($editor instanceof Editor, 'An Editor config entity exists now.');
$this->assertIdentical($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.'); $this->assertIdentical($this->castSafeStrings($expected_settings), $this->castSafeStrings($editor->getSettings()), 'The Editor config entity has the correct settings.');
} }
} }
...@@ -107,12 +107,12 @@ function testLoading() { ...@@ -107,12 +107,12 @@ function testLoading() {
$expected = array('formats' => array('filtered_html' => array( $expected = array('formats' => array('filtered_html' => array(
'format' => 'filtered_html', 'format' => 'filtered_html',
'editor' => 'ckeditor', 'editor' => 'ckeditor',
'editorSettings' => $ckeditor_plugin->getJSSettings($editor), 'editorSettings' => $this->castSafeStrings($ckeditor_plugin->getJSSettings($editor)),
'editorSupportsContentFiltering' => TRUE, 'editorSupportsContentFiltering' => TRUE,
'isXssSafe' => FALSE, 'isXssSafe' => FALSE,
))); )));
$this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page."); $this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this->assertIdentical($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct."); $this->assertIdentical($expected, $this->castSafeStrings($settings['editor']), "Text Editor module's JavaScript settings on the page are correct.");
$this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.'); $this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this->assertTrue(count($body) === 1, 'A body field exists.'); $this->assertTrue(count($body) === 1, 'A body field exists.');
$this->assertTrue(count($format_selector) === 1, 'A single text format selector exists on the page.'); $this->assertTrue(count($format_selector) === 1, 'A single text format selector exists on the page.');
...@@ -138,12 +138,12 @@ function testLoading() { ...@@ -138,12 +138,12 @@ function testLoading() {
'filtered_html' => array( 'filtered_html' => array(
'format' => 'filtered_html', 'format' => 'filtered_html',
'editor' => 'ckeditor', 'editor' => 'ckeditor',
'editorSettings' => $ckeditor_plugin->getJSSettings($editor), 'editorSettings' => $this->castSafeStrings($ckeditor_plugin->getJSSettings($editor)),
'editorSupportsContentFiltering' => TRUE, 'editorSupportsContentFiltering' => TRUE,
'isXssSafe' => FALSE, 'isXssSafe' => FALSE,
))); )));
$this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page."); $this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this->assertIdentical($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct."); $this->assertIdentical($expected, $this->castSafeStrings($settings['editor']), "Text Editor module's JavaScript settings on the page are correct.");
$this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.'); $this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this->assertTrue(in_array('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries'])), 'CKEditor glue library is present.'); $this->assertTrue(in_array('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries'])), 'CKEditor glue library is present.');
} }
......
...@@ -94,8 +94,9 @@ function testGetJSSettings() { ...@@ -94,8 +94,9 @@ function testGetJSSettings() {
'drupallink' => file_create_url('core/modules/ckeditor/js/plugins/drupallink/plugin.js'), 'drupallink' => file_create_url('core/modules/ckeditor/js/plugins/drupallink/plugin.js'),
), ),
); );
$expected_config = $this->castSafeStrings($expected_config);
ksort($expected_config); ksort($expected_config);
$this->assertIdentical($expected_config, $this->ckeditor->getJSSettings($editor), 'Generated JS settings are correct for default configuration.'); $this->assertIdentical($expected_config, $this->castSafeStrings($this->ckeditor->getJSSettings($editor)), 'Generated JS settings are correct for default configuration.');
// Customize the configuration: add button, have two contextually enabled // Customize the configuration: add button, have two contextually enabled
// buttons, and configure a CKEditor plugin setting. // buttons, and configure a CKEditor plugin setting.
...@@ -116,7 +117,7 @@ function testGetJSSettings() { ...@@ -116,7 +117,7 @@ function testGetJSSettings() {
$expected_config['drupalExternalPlugins']['llama_contextual_and_button'] = file_create_url('core/modules/ckeditor/tests/modules/js/llama_contextual_and_button.js'); $expected_config['drupalExternalPlugins']['llama_contextual_and_button'] = file_create_url('core/modules/ckeditor/tests/modules/js/llama_contextual_and_button.js');
$expected_config['contentsCss'][] = file_create_url('core/modules/ckeditor/tests/modules/ckeditor_test.css'); $expected_config['contentsCss'][] = file_create_url('core/modules/ckeditor/tests/modules/ckeditor_test.css');
ksort($expected_config); ksort($expected_config);
$this->assertIdentical($expected_config, $this->ckeditor->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.'); $this->assertIdentical($expected_config, $this->castSafeStrings($this->ckeditor->getJSSettings($editor)), 'Generated JS settings are correct for customized configuration.');
// Change the allowed HTML tags; the "allowedContent" and "format_tags" // Change the allowed HTML tags; the "allowedContent" and "format_tags"
// settings for CKEditor should automatically be updated as well. // settings for CKEditor should automatically be updated as well.
...@@ -127,7 +128,7 @@ function testGetJSSettings() { ...@@ -127,7 +128,7 @@ function testGetJSSettings() {
$expected_config['allowedContent']['pre'] = array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE); $expected_config['allowedContent']['pre'] = array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE);
$expected_config['allowedContent']['h3'] = array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE); $expected_config['allowedContent']['h3'] = array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE);
$expected_config['format_tags'] = 'p;h2;h3;h4;h5;h6;pre'; $expected_config['format_tags'] = 'p;h2;h3;h4;h5;h6;pre';
$this->assertIdentical($expected_config, $this->ckeditor->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.'); $this->assertIdentical($expected_config, $this->castSafeStrings($this->ckeditor->getJSSettings($editor)), 'Generated JS settings are correct for customized configuration.');
// Disable the filter_html filter: allow *all *tags. // Disable the filter_html filter: allow *all *tags.
$format->setFilterConfig('filter_html', array('status' => 0)); $format->setFilterConfig('filter_html', array('status' => 0));
...@@ -136,7 +137,7 @@ function testGetJSSettings() { ...@@ -136,7 +137,7 @@ function testGetJSSettings() {
$expected_config['allowedContent'] = TRUE; $expected_config['allowedContent'] = TRUE;
$expected_config['disallowedContent'] = FALSE; $expected_config['disallowedContent'] = FALSE;
$expected_config['format_tags'] = 'p;h1;h2;h3;h4;h5;h6;pre'; $expected_config['format_tags'] = 'p;h1;h2;h3;h4;h5;h6;pre';
$this->assertIdentical($expected_config, $this->ckeditor->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.'); $this->assertIdentical($expected_config, $this->castSafeStrings($this->ckeditor->getJSSettings($editor)), 'Generated JS settings are correct for customized configuration.');
// Enable the filter_test_restrict_tags_and_attributes filter. // Enable the filter_test_restrict_tags_and_attributes filter.
$format->setFilterConfig('filter_test_restrict_tags_and_attributes', array( $format->setFilterConfig('filter_test_restrict_tags_and_attributes', array(
...@@ -205,7 +206,7 @@ function testGetJSSettings() { ...@@ -205,7 +206,7 @@ function testGetJSSettings() {
); );
$expected_config['format_tags'] = 'p'; $expected_config['format_tags'] = 'p';
ksort($expected_config); ksort($expected_config);
$this->assertIdentical($expected_config, $this->ckeditor->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.'); $this->assertIdentical($expected_config, $this->castSafeStrings($this->ckeditor->getJSSettings($editor)), 'Generated JS settings are correct for customized configuration.');
} }
/** /**
...@@ -216,7 +217,7 @@ function testBuildToolbarJSSetting() { ...@@ -216,7 +217,7 @@ function testBuildToolbarJSSetting() {
// Default toolbar. // Default toolbar.
$expected = $this->getDefaultToolbarConfig(); $expected = $this->getDefaultToolbarConfig();
$this->assertIdentical($expected, $this->ckeditor->buildToolbarJSSetting($editor), '"toolbar" configuration part of JS settings built correctly for default toolbar.'); $this->assertIdentical($expected, $this->castSafeStrings($this->ckeditor->buildToolbarJSSetting($editor)), '"toolbar" configuration part of JS settings built correctly for default toolbar.');
// Customize the configuration. // Customize the configuration.
$settings = $editor->getSettings(); $settings = $editor->getSettings();
...@@ -224,7 +225,7 @@ function testBuildToolbarJSSetting() { ...@@ -224,7 +225,7 @@ function testBuildToolbarJSSetting() {
$editor->setSettings($settings); $editor->setSettings($settings);
$editor->save(); $editor->save();
$expected[0]['items'][] = 'Strike'; $expected[0]['items'][] = 'Strike';
$this->assertIdentical($expected, $this->ckeditor->buildToolbarJSSetting($editor), '"toolbar" configuration part of JS settings built correctly for customized toolbar.'); $this->assertIdentical($expected, $this->castSafeStrings($this->ckeditor->buildToolbarJSSetting($editor)), '"toolbar" configuration part of JS settings built correctly for customized toolbar.');
// Enable the editor_test module, customize further. // Enable the editor_test module, customize further.
$this->enableModules(array('ckeditor_test')); $this->enableModules(array('ckeditor_test'));
...@@ -236,7 +237,7 @@ function testBuildToolbarJSSetting() { ...@@ -236,7 +237,7 @@ function testBuildToolbarJSSetting() {
$editor->save(); $editor->save();
$expected[0]['name'] = 'JunkScience'; $expected[0]['name'] = 'JunkScience';
$expected[0]['items'][] = 'Llama'; $expected[0]['items'][] = 'Llama';
$this->assertIdentical($expected, $this->ckeditor->buildToolbarJSSetting($editor), '"toolbar" configuration part of JS settings built correctly for customized toolbar with contrib module-provided CKEditor plugin.'); $this->assertIdentical($expected, $this->castSafeStrings($this->ckeditor->buildToolbarJSSetting($editor)), '"toolbar" configuration part of JS settings built correctly for customized toolbar with contrib module-provided CKEditor plugin.');
} }
/** /**
...@@ -442,23 +443,23 @@ protected function getDefaultDisallowedContentConfig() { ...@@ -442,23 +443,23 @@ protected function getDefaultDisallowedContentConfig() {
protected function getDefaultToolbarConfig() { protected function getDefaultToolbarConfig() {
return array( return array(
array( array(
'name' => t('Formatting'), 'name' => 'Formatting',
'items' => array('Bold', 'Italic',), 'items' => array('Bold', 'Italic',),
), ),
array( array(
'name' => t('Links'), 'name' => 'Links',
'items' => array('DrupalLink', 'DrupalUnlink',), 'items' => array('DrupalLink', 'DrupalUnlink',),
), ),
array( array(
'name' => t('Lists'), 'name' => 'Lists',
'items' => array('BulletedList', 'NumberedList',), 'items' => array('BulletedList', 'NumberedList',),
), ),
array( array(
'name' => t('Media'), 'name' => 'Media',
'items' => array('Blockquote', 'DrupalImage',), 'items' => array('Blockquote', 'DrupalImage',),
), ),
array( array(
'name' => t('Tools'), 'name' => 'Tools',
'items' => array('Source',), 'items' => array('Source',),
), ),
'/', '/',
......
...@@ -112,7 +112,7 @@ public function testRenameValidation() { ...@@ -112,7 +112,7 @@ public function testRenameValidation() {
$expected = array( $expected = array(
SafeMarkup::format('Entity type mismatch on rename. @old_type not equal to @new_type for existing configuration @old_name and staged configuration @new_name.', array('@old_type' => 'node_type', '@new_type' => 'config_test', '@old_name' => 'node.type.' . $content_type->id(), '@new_name' => 'config_test.dynamic.' . $test_entity_id)) SafeMarkup::format('Entity type mismatch on rename. @old_type not equal to @new_type for existing configuration @old_name and staged configuration @new_name.', array('@old_type' => 'node_type', '@new_type' => 'config_test', '@old_name' => 'node.type.' . $content_type->id(), '@new_name' => 'config_test.dynamic.' . $test_entity_id))
); );
$this->assertIdentical($expected, $this->configImporter->getErrors()); $this->assertEqual($expected, $this->configImporter->getErrors());
} }
} }
...@@ -155,7 +155,7 @@ public function testRenameSimpleConfigValidation() { ...@@ -155,7 +155,7 @@ public function testRenameSimpleConfigValidation() {
$expected = array( $expected = array(
SafeMarkup::format('Rename operation for simple configuration. Existing configuration @old_name and staged configuration @new_name.', array('@old_name' => 'config_test.old', '@new_name' => 'config_test.new')) SafeMarkup::format('Rename operation for simple configuration. Existing configuration @old_name and staged configuration @new_name.', array('@old_name' => 'config_test.old', '@new_name' => 'config_test.new'))
); );
$this->assertIdentical($expected, $this->configImporter->getErrors()); $this->assertEqual($expected, $this->configImporter->getErrors());
} }
} }
......
...@@ -238,8 +238,10 @@ public function testLanguageContext() { ...@@ -238,8 +238,10 @@ public function testLanguageContext() {
'langcode' => 'hr', 'langcode' => 'hr',
)); ));
$this->assertIdentical(t('May', array(), array('langcode' => 'hr', 'context' => 'Long month name')), 'Svibanj', 'Long month name context is working.'); // We cast the return value of t() to string so as to retrieve the
$this->assertIdentical(t('May', array(), array('langcode' => 'hr')), 'Svi.', 'Default context is working.'); // translated value, rendered as a string.
$this->assertIdentical((string) t('May', array(), array('langcode' => 'hr', 'context' => 'Long month name')), 'Svibanj', 'Long month name context is working.');
$this->assertIdentical((string) t('May', array(), array('langcode' => 'hr')), 'Svi.', 'Default context is working.');
} }
/** /**
...@@ -254,7 +256,7 @@ public function testEmptyMsgstr() { ...@@ -254,7 +256,7 @@ public function testEmptyMsgstr() {
)); ));
$this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.'); $this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.');
$this->assertIdentical(t('Operations', array(), array('langcode' => $langcode)), 'Műveletek', 'String imported and translated.'); $this->assertIdentical((string) t('Operations', array(), array('langcode' => $langcode)), 'Műveletek', 'String imported and translated.');
// Try importing a .po file. // Try importing a .po file.
$this->importPoFile($this->getPoFileWithEmptyMsgstr(), array( $this->importPoFile($this->getPoFileWithEmptyMsgstr(), array(
......
...@@ -69,7 +69,7 @@ public function testAggregatorMigrateDependencies() { ...@@ -69,7 +69,7 @@ public function testAggregatorMigrateDependencies() {
$executable = new MigrateExecutable($migration, $this); $executable = new MigrateExecutable($migration, $this);
$this->startCollectingMessages(); $this->startCollectingMessages();
$executable->import(); $executable->import();
$this->assertIdentical($this->migrateMessages['error'], array(SafeMarkup::format('Migration @id did not meet the requirements. Missing migrations d6_aggregator_feed. requirements: d6_aggregator_feed.', array('@id' => $migration->id())))); $this->assertEqual($this->migrateMessages['error'], array(SafeMarkup::format('Migration @id did not meet the requirements. Missing migrations d6_aggregator_feed. requirements: d6_aggregator_feed.', array('@id' => $migration->id()))));
$this->collectMessages = FALSE; $this->collectMessages = FALSE;
} }
......
...@@ -34,8 +34,8 @@ class MigrateNodeBuilderTest extends MigrateDrupal6TestBase { ...@@ -34,8 +34,8 @@ class MigrateNodeBuilderTest extends MigrateDrupal6TestBase {
protected function assertEntity($id, $label) { protected function assertEntity($id, $label) {
$migration = $this->builtMigrations[$id]; $migration = $this->builtMigrations[$id];
$this->assertTrue($migration instanceof Migration); $this->assertTrue($migration instanceof Migration);
$this->assertIdentical($id, $migration->Id()); $this->assertIdentical($id, $migration->id());
$this->assertIdentical($label, $migration->label()); $this->assertEqual($label, $migration->label());
} }
/** /**
......
...@@ -73,7 +73,7 @@ function testShortcutSetEdit() { ...@@ -73,7 +73,7 @@ function testShortcutSetEdit() {
// Test the contents of each th cell. // Test the contents of each th cell.
$expected_items = array(t('Name'), t('Weight'), t('Operations')); $expected_items = array(t('Name'), t('Weight'), t('Operations'));
foreach ($elements as $key => $element) { foreach ($elements as $key => $element) {
$this->assertIdentical((string) $element[0], $expected_items[$key]); $this->assertEqual((string) $element[0], $expected_items[$key]);
} }
// Look for test shortcuts in the table. // Look for test shortcuts in the table.
......
<?php
/**
* @file
* Contains \Drupal\simpletest\AssertHelperTrait.
*/
namespace Drupal\simpletest;
use Drupal\Component\Utility\SafeStringInterface;
/**
* Provides helper methods for assertions.
*/
trait AssertHelperTrait {
/**
* Casts SafeStringInterface objects into strings.
*
* @param string|array $value
* The value to act on.
*
* @return mixed
* The input value, with SafeStringInterface objects casted to string.
*/
protected function castSafeStrings($value) {
if ($value instanceof SafeStringInterface) {
$value = (string) $value;
}
if (is_array($value)) {
array_walk_recursive($value, function (&$item) {
if ($item instanceof SafeStringInterface) {
$item = (string) $item;
}
});
}
return $value;
}
}
...@@ -30,6 +30,7 @@ abstract class TestBase { ...@@ -30,6 +30,7 @@ abstract class TestBase {
use SessionTestTrait; use SessionTestTrait;
use RandomGeneratorTrait; use RandomGeneratorTrait;
use AssertHelperTrait;
/** /**
* The test run ID. * The test run ID.
...@@ -658,10 +659,11 @@ protected function assertNotNull($value, $message = '', $group = 'Other') { ...@@ -658,10 +659,11 @@ protected function assertNotNull($value, $message = '', $group = 'Other') {
* TRUE if the assertion succeeded, FALSE otherwise. * TRUE if the assertion succeeded, FALSE otherwise.
*/ */
protected function assertEqual($first, $second, $message = '', $group = 'Other') { protected function assertEqual($first, $second, $message = '', $group = 'Other') {
// We cast objects implementing SafeStringInterface to string ourself so as // Cast objects implementing SafeStringInterface to string instead of
// to not rely on PHP casting them to string depending on what we're // relying on PHP casting them to string depending on what they are being
// comparing with. // comparing with.
$this->castSafeStrings($first, $second); $first = $this->castSafeStrings($first);
$second = $this->castSafeStrings($second);
return $this->assert($first == $second, $message ? $message : SafeMarkup::format('Value @first is equal to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group); return $this->assert($first == $second, $message ? $message : SafeMarkup::format('Value @first is equal to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
} }
...@@ -690,40 +692,6 @@ protected function assertNotEqual($first, $second, $message = '', $group = 'Othe ...@@ -690,40 +692,6 @@ protected function assertNotEqual($first, $second, $message = '', $group = 'Othe
return $this->assert($first != $second, $message ? $message : SafeMarkup::format('Value @first is not equal to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group); return $this->assert($first != $second, $message ? $message : SafeMarkup::format('Value @first is not equal to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
} }
/**
* Casts SafeStringInterface objects into string in 2 compared values.
*
* @param string|array &$first
* The first value to act on.
* @param string|array &$second
* The second value to act on (optional).
*
* @return mixed
* The input values, with SafeStringInterface objects casted to string.
*/
protected function castSafeStrings(&$first, &$second = NULL) {
if ($first instanceof SafeStringInterface) {
$first = (string) $first;
}
if ($second instanceof SafeStringInterface) {
$second = (string) $second;
}
if (is_array($first)) {
array_walk_recursive($first, function (&$first) {
if ($first instanceof SafeStringInterface) {
$first = (string) $first;
}
});
}
if (is_array($second)) {
array_walk_recursive($second, function (&$second) {
if ($second instanceof SafeStringInterface) {
$second = (string) $second;
}
});
}
}
/** /**
* Check to see if two values are identical. * Check to see if two values are identical.
* *
......
...@@ -1697,7 +1697,7 @@ protected function drupalPostForm($path, $edit, $submit, array $options = array( ...@@ -1697,7 +1697,7 @@ protected function drupalPostForm($path, $edit, $submit, array $options = array(
$submit = (string) $submit; $submit = (string) $submit;
} }
if (is_array($edit)) { if (is_array($edit)) {
array_walk_recursive($edit, [$this, 'castSafeStrings']); $edit = $this->castSafeStrings($edit);
} }
$submit_matches = FALSE; $submit_matches = FALSE;
......
<?php
/**
* @file
* Contains \Drupal\Tests\simpletest\Unit\AssertHelperTraitTest.
*/
namespace Drupal\Tests\simpletest\Unit;
use Drupal\Core\Render\SafeString;
use Drupal\simpletest\AssertHelperTrait;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\simpletest\AssertHelperTrait
* @group simpletest
*/
class AssertHelperTraitTest extends UnitTestCase {
/**
* @covers ::castSafeStrings
* @dataProvider providerCastSafeStrings
*/
public function testCastSafeStrings($expected, $value) {
$class = new AssertHelperTestClass();
$this->assertSame($expected, $class->testMethod($value));
}
public function providerCastSafeStrings() {
$safe_string = SafeString::create('test safe string');