function ConfigImportUITest::testImportDiff

Same name and namespace in other branches
  1. 9 core/modules/config/tests/src/Functional/ConfigImportUITest.php \Drupal\Tests\config\Functional\ConfigImportUITest::testImportDiff()
  2. 10 core/modules/config/tests/src/Functional/ConfigImportUITest.php \Drupal\Tests\config\Functional\ConfigImportUITest::testImportDiff()
  3. 11.x core/modules/config/tests/src/Functional/ConfigImportUITest.php \Drupal\Tests\config\Functional\ConfigImportUITest::testImportDiff()

Tests the screen that shows differences between active and sync.

File

core/modules/config/tests/src/Functional/ConfigImportUITest.php, line 278

Class

ConfigImportUITest
Tests the user interface for importing configuration.

Namespace

Drupal\Tests\config\Functional

Code

public function testImportDiff() {
    $sync = $this->container
        ->get('config.storage.sync');
    $config_name = 'config_test.system';
    $change_key = 'foo';
    $remove_key = '404';
    $add_key = 'biff';
    $add_data = '<em>bangpow</em>';
    $change_data = '<p><em>foobar</em></p>';
    $original_data = [
        'foo' => '<p>foobar</p>',
        'baz' => '<strong>no change</strong>',
        '404' => '<em>herp</em>',
    ];
    // Update active storage to have html in config data.
    $this->config($config_name)
        ->setData($original_data)
        ->save();
    // Change a configuration value in sync.
    $sync_data = $original_data;
    $sync_data[$change_key] = $change_data;
    $sync_data[$add_key] = $add_data;
    unset($sync_data[$remove_key]);
    $sync->write($config_name, $sync_data);
    // Load the diff UI and verify that the diff reflects the change.
    $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
    $this->assertNoRaw('&amp;nbsp;');
    $this->assertTitle("View changes of {$config_name} | Drupal");
    // The following assertions do not use $this::assertEscaped() because
    // \Drupal\Component\Diff\DiffFormatter adds markup that signifies what has
    // changed.
    // Changed values are escaped.
    $this->assertText(Html::escape("foo: '<p><em>foobar</em></p>'"));
    $this->assertText(Html::escape("foo: '<p>foobar</p>'"));
    // The no change values are escaped.
    $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
    // Added value is escaped.
    $this->assertText(Html::escape("biff: '<em>bangpow</em>'"));
    // Deleted value is escaped.
    $this->assertText(Html::escape("404: '<em>herp</em>'"));
    // Verify diff colors are displayed.
    $result = $this->xpath('//table[contains(@class, :class)]', [
        ':class' => 'diff',
    ]);
    $this->assertCount(1, $result, "Diff UI is displaying colors.");
    // Reset data back to original, and remove a key
    $sync_data = $original_data;
    unset($sync_data[$remove_key]);
    $sync->write($config_name, $sync_data);
    // Load the diff UI and verify that the diff reflects a removed key.
    $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
    // The no change values are escaped.
    $this->assertText(Html::escape("foo: '<p>foobar</p>'"));
    $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
    // Removed key is escaped.
    $this->assertText(Html::escape("404: '<em>herp</em>'"));
    // Reset data back to original and add a key
    $sync_data = $original_data;
    $sync_data[$add_key] = $add_data;
    $sync->write($config_name, $sync_data);
    // Load the diff UI and verify that the diff reflects an added key.
    $this->drupalGet('admin/config/development/configuration/sync/diff/' . $config_name);
    // The no change values are escaped.
    $this->assertText(Html::escape("baz: '<strong>no change</strong>'"));
    $this->assertText(Html::escape("404: '<em>herp</em>'"));
    // Added key is escaped.
    $this->assertText(Html::escape("biff: '<em>bangpow</em>'"));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.