1 config.test public ConfigurationHooksTest::testHooks()

Tests checking the validation hooks.

This test does not include a test of hook_config_data_validate() as it is tested by ConfigurationTest::testExistingConfig().

File

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

Class

ConfigurationHooksTest
Tests hooks for validation, creating, updating, and deleting configurations.

Code

public function testHooks() {
  $config_data = config_get('config_test.simple');
  module_load_include('inc', 'config', 'config.sync');

  // Check validation.
  $config_data['favorite_animal'] = 'rabbits';
  $errors = array(
    'create' => 'Sorry, only bunnies allowed as favorite animals.',
    'update' => 'Sorry, cats must be preferred over rabbits.',
    'delete' => 'Favorite animals are in use.',
  );
  foreach (array('create', 'update', 'delete') as $op) {
    try {
      config_sync_validate_file('config_test.simple', $op, NULL, $config_data);
      $this->fail('Validation during "' . $op . '" unexpectedly passed when it should have failed.');
    }
    catch (ConfigValidateException $e) {
      $this->assertEqual($errors[$op], $e->getMessage(), 'Validation during "' . $op . '" configuration threw an error correctly.');
    }
  }

  // Check create, update, and delete hooks.
  $expected_data = array(
    'create' => array(
      'favorite_animal' => 'rabbits',
      'favorite_color' => 'red',
    ),
    'update' => array(
      'favorite_animal' => 'rabbits',
      'favorite_color' => 'blue',
    ),
    'delete' => array(
      'favorite_animal' => 'cats',
      'favorite_color' => 'blue',
      'deleted_animal' => 'rabbits'
    ),
  );
  foreach (array('create', 'update') as $op) {
    config_sync_file('config_test.new_config', $op, $config_data);
    $new_data = config_get('config_test.new_config');
    $this->assertEqual($expected_data[$op], $new_data, 'Configuration hook for "' . $op . '" executed properly.');
  }

  // The delete hook modifies the "config_test.simple" configuration when the
  // "config_test.new_config" configuration is deleted.
  config_sync_file('config_test.new_config', 'delete');
  $new_data = config_get('config_test.simple');
  $this->assertEqual($expected_data['delete'], $new_data, 'Configuration hook for "delete" executed properly.');
}