1 config.test ConfigurationUITest::testImport()

Tests importing configuration.

File

core/modules/config/tests/config.test, line 281
Tests for Configuration module.

Class

ConfigurationUITest
Tests the UI for syncing, importing, and exporting.

Code

function testImport() {
  $name = 'config_test.simple';
  $dynamic_name = 'config_test.dynamic.new';
  $staging_storage = new ConfigFileStorage(config_get_config_directory('staging'));

  $this->backdropGet('admin/config/development/configuration');
  $this->assertText('There are no configuration changes currently staged.');
  $this->assertNoFieldById('edit-submit', t('Import all'));

  // Copy all configuration to staging before modification.
  $this->copyConfig('active', 'staging');

  // Create updated configuration object.
  $favorite_animal = 'Animal ' . $this->randomString();
  $staging_config = config($name, 'staging');
  $staging_config->set('favorite_animal', $favorite_animal);
  $staging_config->save();

  $this->assertIdentical($staging_storage->exists($name), TRUE, $name . ' found.');

  // Create new config.
  $original_dynamic_data = array(
    'id' => 'new',
    'label' => 'New',
    'weight' => 0,
    'status' => TRUE,
  );
  $staging_storage->write($dynamic_name, $original_dynamic_data);
  $this->assertIdentical($staging_storage->exists($dynamic_name), TRUE, $dynamic_name . ' found in staging.');

  // Verify that both appear as ready to import.
  $this->backdropGet('admin/config/development/configuration');
  $this->assertText($name);
  $this->assertText($dynamic_name);
  $this->assertFieldById('edit-submit', t('Import all'));

  // Check that the locking message works when an import is in-progress.
  state_set('config_sync', REQUEST_TIME);
  $this->backdropPost(NULL, array(), t('Import all'));
  $this->assertText('Another request may be synchronizing configuration already or a sync failed unexpectedly.');
  state_del('config_sync');

  // Import and verify that both do not appear anymore.
  $this->backdropPost(NULL, array(), t('Import all'));
  $this->assertNoText($name);
  $this->assertNoText($dynamic_name);
  $this->assertNoFieldById('edit-submit', t('Import all'));

  // Verify that there are no further changes to import.
  $this->assertText('There are no configuration changes currently staged.');

  // Verify configuration has changed.
  $this->assertIdentical($favorite_animal, config_get($name, 'favorite_animal'));

  // Verify that new config exists.
  $this->assertIdentical($original_dynamic_data, config_get($dynamic_name));

  // Create a second config that is not declared in hook_config_info().
  $this->copyConfig('active', 'staging');
  $dynamic_name2 = $dynamic_name . '2';
  $staging_storage->write($dynamic_name2, $original_dynamic_data);
  $this->assertIdentical($staging_storage->exists($dynamic_name2), TRUE, $dynamic_name2 . ' found in staging.');

  // Attempt an import which should fail.
  $this->backdropPost('admin/config/development/configuration', array(), t('Import all'));

  // Validation should fail, as this config file is not owned by a module.
  $error_message = t('The configuration "@name" is not owned by any module. Try enabling the module that provides this configuration, then importing again.', array('@name' => $dynamic_name2));
  $this->assertText($error_message, 'Validation properly prevents import when a config is not declared by a module.');

  // Empty out the staging config.
  $staging_storage->deleteAll();
}