diff --git a/core/modules/config/src/Tests/ConfigDiffTest.php b/core/modules/config/src/Tests/ConfigDiffTest.php index 10f6902..12b948d 100644 --- a/core/modules/config/src/Tests/ConfigDiffTest.php +++ b/core/modules/config/src/Tests/ConfigDiffTest.php @@ -52,9 +52,9 @@ function testDiff() { // Verify that the diff reflects a change. $diff = \Drupal::service('config.manager')->diff($active, $staging, $config_name); $edits = $diff->getEdits(); - $this->assertEqual($edits[0]->type, 'change', 'The first item in the diff is a change.'); - $this->assertEqual($edits[0]->orig[0], $change_key . ': ' . $original_data[$change_key], format_string("The active value for key '%change_key' is '%original_data'.", array('%change_key' => $change_key, '%original_data' => $original_data[$change_key]))); - $this->assertEqual($edits[0]->closing[0], $change_key . ': ' . $change_data, format_string("The staging value for key '%change_key' is '%change_data'.", array('%change_key' => $change_key, '%change_data' => $change_data))); + $this->assertYamlEdit($edits, $change_key, 'change', + [$change_key . ': ' . $original_data[$change_key]], + [$change_key . ': ' . $change_data]); // Reset data back to original, and remove a key $staging_data = $original_data; @@ -64,10 +64,11 @@ function testDiff() { // Verify that the diff reflects a removed key. $diff = \Drupal::service('config.manager')->diff($active, $staging, $config_name); $edits = $diff->getEdits(); - $this->assertEqual($edits[0]->type, 'copy', 'The first item in the diff is a copy.'); - $this->assertEqual($edits[1]->type, 'delete', 'The second item in the diff is a delete.'); - $this->assertEqual($edits[1]->orig[0], $remove_key . ': ' . $original_data[$remove_key], format_string("The active value for key '%remove_key' is '%original_data'.", array('%remove_key' => $remove_key, '%original_data' => $original_data[$remove_key]))); - $this->assertFalse($edits[1]->closing, format_string("The key '%remove_key' does not exist in staging.", array('%remove_key' => $remove_key))); + $this->assertYamlEdit($edits, $change_key, 'copy'); + $this->assertYamlEdit($edits, $remove_key, 'delete', + [$remove_key . ': ' . $original_data[$remove_key]], + FALSE + ); // Reset data back to original and add a key $staging_data = $original_data; @@ -77,10 +78,8 @@ function testDiff() { // Verify that the diff reflects an added key. $diff = \Drupal::service('config.manager')->diff($active, $staging, $config_name); $edits = $diff->getEdits(); - $this->assertEqual($edits[0]->type, 'copy', 'The first item in the diff is a copy.'); - $this->assertEqual($edits[1]->type, 'add', 'The second item in the diff is an add.'); - $this->assertFalse($edits[1]->orig, format_string("The key '%add_key' does not exist in active.", array('%add_key' => $add_key))); - $this->assertEqual($edits[1]->closing[0], $add_key . ': ' . $add_data, format_string("The staging value for key '%add_key' is '%add_data'.", array('%add_key' => $add_key, '%add_data' => $add_data))); + $this->assertYamlEdit($edits, $change_key, 'copy'); + $this->assertYamlEdit($edits, $add_key, 'add', FALSE, [$add_key . ': ' . $add_data]); // Test diffing a renamed config entity. $test_entity_id = $this->randomMachineName(); @@ -105,10 +104,11 @@ function testDiff() { $diff = \Drupal::service('config.manager')->diff($active, $staging, 'config_test.dynamic.' . $new_test_entity_id, $config_name); $edits = $diff->getEdits(); - $this->assertEqual($edits[0]->type, 'copy', 'The first item in the diff is a copy.'); - $this->assertEqual($edits[1]->type, 'change', 'The second item in the diff is a change.'); - $this->assertEqual($edits[1]->orig, array('id: ' . $new_test_entity_id)); - $this->assertEqual($edits[1]->closing, array('id: ' . $test_entity_id)); + $this->assertYamlEdit($edits, 'uuid', 'copy'); + $this->assertYamlEdit($edits, 'id', 'change', + ['id: ' . $new_test_entity_id], + ['id: ' . $test_entity_id]); + $this->assertYamlEdit($edits, 'label', 'copy'); $this->assertEqual($edits[2]->type, 'copy', 'The third item in the diff is a copy.'); $this->assertEqual(count($edits), 3, 'There are three items in the diff.'); } @@ -141,10 +141,54 @@ function testCollectionDiff() { // Test that the differences are detected when diffing the collection. $diff = \Drupal::service('config.manager')->diff($active, $staging, $config_name, NULL, 'test'); $edits = $diff->getEdits(); - $this->assertEqual($edits[0]->type, 'change', 'The second item in the diff is a copy.'); - $this->assertEqual($edits[0]->orig, array('foo: bar')); - $this->assertEqual($edits[0]->closing, array('foo: baz')); - $this->assertEqual($edits[1]->type, 'copy', 'The second item in the diff is a copy.'); + $this->assertYamlEdit($edits, 'foo', 'change', ['foo: bar'], ['foo: baz']); + } + + /** + * Helper method to test that an edit is found in a diff'd yaml file. + * + * @param array $edits + * A list of edits. + * @param $field + * The field key that is being asserted. + * @param $type + * The type of edit that is being asserted. + * @param mixed $orig + * (optional) The original value of of the edit. If not supplied, assertion is skipped. + * @param mixed $closing + * (optional) The closing value of of the edit. If not supplied, assertion is skipped. + */ + protected function assertYamlEdit(array $edits, $field, $type, $orig = null, $closing = null) { + $match = FALSE; + foreach ($edits as $edit) { + // Choose which section to search for the field. + $haystack = $type == 'add' ? $edit->closing : $edit->orig; + // Look through each line and try and find the key. + if (is_array($haystack)) { + foreach ($haystack as $item) { + if (strpos($item, $field . ':') === 0) { + $match = TRUE; + // Assert that the edit is of the type specified. + $this->assertEqual($edit->type, $type, "The $field item in the diff is a $type"); + // If an original value was given, assert that it matches. + if (isset($orig)) { + $this->assertIdentical($edit->orig, $orig, "The original value for key '$field' is correct."); + } + // If a closing value was given, assert that it matches. + if (isset($closing)) { + $this->assertIdentical($edit->closing, $closing, "The closing value for key '$field' is correct."); + } + // Break out of the search entirely. + break 2; + } + } + } + } + + // If we didn't match anything, fail. + if (!$match) { + $this->fail("$field edit was not matched"); + } } } diff --git a/core/modules/config/src/Tests/ConfigSingleImportExportTest.php b/core/modules/config/src/Tests/ConfigSingleImportExportTest.php index 7b3336d..922f113 100644 --- a/core/modules/config/src/Tests/ConfigSingleImportExportTest.php +++ b/core/modules/config/src/Tests/ConfigSingleImportExportTest.php @@ -179,8 +179,8 @@ public function testExport() { $this->assertFieldByXPath('//select[@name="config_name"]//option[@selected="selected"]', t('Fallback date format'), 'The fallback date format config entity is selected when specified in the URL.'); $fallback_date = \Drupal::entityManager()->getStorage('date_format')->load('fallback'); - $data = Yaml::encode($fallback_date->toArray()); - $this->assertFieldByXPath('//textarea[@name="export"]', $data, 'The fallback date format config entity export code is displayed.'); + $yaml_text = (string) $this->xpath('//textarea[@name="export"]')[0]; + $this->assertEqual(Yaml::decode($yaml_text), $fallback_date->toArray(), 'The fallback date format config entity export code is displayed.'); } } diff --git a/core/modules/system/src/Tests/Installer/StandardInstallerTest.php b/core/modules/system/src/Tests/Installer/StandardInstallerTest.php index 184e07b..8c7d123 100644 --- a/core/modules/system/src/Tests/Installer/StandardInstallerTest.php +++ b/core/modules/system/src/Tests/Installer/StandardInstallerTest.php @@ -47,15 +47,15 @@ public function testStandardConfig() { $skipped_config = []; // \Drupal\simpletest\WebTestBase::installParameters() uses // simpletest@example.com as mail address. - $skipped_config['contact.form.feedback'][] = ' - simpletest@example.com'; + $skipped_config['contact.form.feedback'][] = '- simpletest@example.com'; // \Drupal\filter\Entity\FilterFormat::toArray() drops the roles of filter // formats. $skipped_config['filter.format.basic_html'][] = 'roles:'; - $skipped_config['filter.format.basic_html'][] = ' - authenticated'; + $skipped_config['filter.format.basic_html'][] = '- authenticated'; $skipped_config['filter.format.full_html'][] = 'roles:'; - $skipped_config['filter.format.full_html'][] = ' - administrator'; + $skipped_config['filter.format.full_html'][] = '- administrator'; $skipped_config['filter.format.restricted_html'][] = 'roles:'; - $skipped_config['filter.format.restricted_html'][] = ' - anonymous'; + $skipped_config['filter.format.restricted_html'][] = '- anonymous'; $this->assertInstalledConfig($skipped_config); }