View source  
  <?php
namespace Drupal\KernelTests\Core\Config;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigNameException;
use Drupal\Core\Config\ConfigValueException;
use Drupal\Core\Config\DatabaseStorage;
use Drupal\Core\Config\UnsupportedDataTypeConfigException;
use Drupal\KernelTests\KernelTestBase;
class ConfigCRUDTest extends KernelTestBase {
  
  protected $strictConfigSchema = FALSE;
  
  protected static $modules = [
    'system',
  ];
  
  public function testCRUD() {
    $event_dispatcher = $this->container
      ->get('event_dispatcher');
    $typed_config_manager = $this->container
      ->get('config.typed');
    $storage = $this->container
      ->get('config.storage');
    $collection_storage = $storage
      ->createCollection('test_collection');
    $config_factory = $this->container
      ->get('config.factory');
    $name = 'config_test.crud';
    
    $config = $this
      ->config($name);
    $this
      ->assertTrue($config
      ->isNew());
    $config
      ->set('value', 'initial');
    $config
      ->save();
    $this
      ->assertFalse($config
      ->isNew());
    
    $actual_data = $storage
      ->read($name);
    $this
      ->assertSame([
      'value' => 'initial',
    ], $actual_data);
    
    $actual_data = $config_factory
      ->get($name)
      ->getRawData();
    $this
      ->assertSame([
      'value' => 'initial',
    ], $actual_data);
    
    $collection_config = new Config($name, $collection_storage, $event_dispatcher, $typed_config_manager);
    $collection_config
      ->set('value', 'overridden');
    $collection_config
      ->save();
    
    $actual_data = $config_factory
      ->get($name)
      ->getRawData();
    $this
      ->assertSame([
      'value' => 'initial',
    ], $actual_data);
    
    $config
      ->set('value', 'instance-update');
    $config
      ->save();
    $this
      ->assertFalse($config
      ->isNew());
    
    $actual_data = $storage
      ->read($name);
    $this
      ->assertSame([
      'value' => 'instance-update',
    ], $actual_data);
    
    $new_config = $this
      ->config($name);
    $this
      ->assertSame($config
      ->get(), $new_config
      ->get());
    $this
      ->assertFalse($config
      ->isNew());
    
    $config_factory
      ->getEditable($name);
    
    $collection_config
      ->delete();
    $actual_config = $config_factory
      ->get($name);
    $this
      ->assertFalse($actual_config
      ->isNew());
    $this
      ->assertSame([
      'value' => 'instance-update',
    ], $actual_config
      ->getRawData());
    
    $config
      ->delete();
    
    $this
      ->assertSame([], $config
      ->get());
    $this
      ->assertTrue($config
      ->isNew());
    
    $this
      ->assertTrue($config_factory
      ->getEditable($name)
      ->isNew());
    
    $actual_data = $storage
      ->read($name);
    $this
      ->assertFalse($actual_data);
    
    $new_config = $this
      ->config($name);
    $this
      ->assertSame($config
      ->get(), $new_config
      ->get());
    $this
      ->assertTrue($config
      ->isNew());
    
    $config
      ->set('value', 're-created');
    $config
      ->save();
    $this
      ->assertFalse($config
      ->isNew());
    
    $actual_data = $storage
      ->read($name);
    $this
      ->assertSame([
      'value' => 're-created',
    ], $actual_data);
    
    $new_config = $this
      ->config($name);
    $this
      ->assertSame($config
      ->get(), $new_config
      ->get());
    $this
      ->assertFalse($config
      ->isNew());
    
    $new_name = 'config_test.crud_rename';
    $this->container
      ->get('config.factory')
      ->rename($name, $new_name);
    $renamed_config = $this
      ->config($new_name);
    $this
      ->assertSame($config
      ->get(), $renamed_config
      ->get());
    $this
      ->assertFalse($renamed_config
      ->isNew());
    
    $config = $this
      ->config($name);
    $this
      ->assertSame([], $config
      ->get());
    $this
      ->assertTrue($config
      ->isNew());
    
    $name = 'config_test.crud_rename';
    
    $config_factory
      ->getEditable($name);
    
    $config = $config_factory
      ->get($name);
    
    $new_name = 'config_test.crud_rename_no_cache';
    $config_factory
      ->rename($name, $new_name);
    $renamed_config = $config_factory
      ->get($new_name);
    $this
      ->assertSame($config
      ->get(), $renamed_config
      ->get());
    $this
      ->assertFalse($renamed_config
      ->isNew());
    
    $this
      ->assertTrue($config_factory
      ->get($name)
      ->isNew());
    
    $this
      ->assertTrue($config_factory
      ->getEditable($name)
      ->isNew());
    
    $new_config = $this
      ->config($new_name);
    $expected_values = [
      'value' => 'herp',
      '404' => 'derp',
    ];
    $new_config
      ->merge($expected_values);
    $new_config
      ->save();
    $this
      ->assertSame($expected_values['value'], $new_config
      ->get('value'));
    $this
      ->assertSame($expected_values['404'], $new_config
      ->get('404'));
    
    $new_config = $config_factory
      ->get('non_existing_key');
    $this
      ->assertTrue($new_config
      ->isNew());
    $this
      ->assertCount(0, $config_factory
      ->loadMultiple([
      'non_existing_key',
    ]), 'loadMultiple() does not return new objects');
  }
  
  public function testNameValidation() {
    
    $name = 'nonamespace';
    try {
      $this
        ->config($name)
        ->save();
      $this
        ->fail('Expected ConfigNameException was thrown for a name without a namespace.');
    } catch (\Exception $e) {
      $this
        ->assertInstanceOf(ConfigNameException::class, $e);
    }
    
    $name = 'config_test.herman_melville.moby_dick_or_the_whale.harper_1851.now_small_fowls_flew_screaming_over_the_yet_yawning_gulf_a_sullen_white_surf_beat_against_its_steep_sides_then_all_collapsed_and_the_great_shroud_of_the_sea_rolled_on_as_it_rolled_five_thousand_years_ago';
    try {
      $this
        ->config($name)
        ->save();
      $this
        ->fail('Expected ConfigNameException was thrown for a name longer than Config::MAX_NAME_LENGTH.');
    } catch (\Exception $e) {
      $this
        ->assertInstanceOf(ConfigNameException::class, $e);
    }
    
    $characters = $test_characters = [
      ':',
      '?',
      '*',
      '<',
      '>',
      '"',
      '\'',
      '/',
      '\\',
    ];
    foreach ($test_characters as $i => $c) {
      try {
        $name = 'namespace.object' . $c;
        $config = $this
          ->config($name);
        $config
          ->save();
      } catch (ConfigNameException $e) {
        unset($test_characters[$i]);
      }
    }
    $this
      ->assertTrue(empty($test_characters), new FormattableMarkup('Expected ConfigNameException was thrown for all invalid name characters: @characters', [
      '@characters' => implode(' ', $characters),
    ]));
    
    $name = 'namespace.object';
    try {
      $config = $this
        ->config($name);
      $config
        ->save();
    } catch (ConfigNameException $e) {
      $this
        ->fail('ConfigNameException was not thrown for a valid object name.');
    }
  }
  
  public function testValueValidation() {
    
    try {
      $this
        ->config('namespace.object')
        ->setData([
        'key.value' => 12,
      ])
        ->save();
      $this
        ->fail('Expected ConfigValueException was thrown from setData() for value with dotted keys.');
    } catch (\Exception $e) {
      $this
        ->assertInstanceOf(ConfigValueException::class, $e);
    }
    
    try {
      $this
        ->config('namespace.object')
        ->set('foo', [
        'key.value' => 12,
      ])
        ->save();
      $this
        ->fail('Expected ConfigValueException was thrown from set() for value with dotted keys.');
    } catch (\Exception $e) {
      $this
        ->assertInstanceOf(ConfigValueException::class, $e);
    }
  }
  
  public function testDataTypes() {
    \Drupal::service('module_installer')
      ->install([
      'config_test',
    ]);
    $storage = new DatabaseStorage($this->container
      ->get('database'), 'config');
    $name = 'config_test.types';
    $config = $this
      ->config($name);
    
    $data = [
      'array' => [],
      'boolean' => TRUE,
      'exp' => 1.2E+34,
      'float' => 3.14159,
      'float_as_integer' => (double) 1,
      'hex' => 0xc,
      'int' => 99,
      
      'string' => 'string',
      'string_int' => '1',
    ];
    $data['_core']['default_config_hash'] = Crypt::hashBase64(serialize($data));
    $this
      ->assertSame($data, $config
      ->get());
    
    foreach ($data as $key => $value) {
      $config
        ->set($key, $value);
    }
    $config
      ->save();
    $this
      ->assertSame($data, $config
      ->get());
    
    $this
      ->assertSame($data, $storage
      ->read($name));
    
    $config
      ->setData($data)
      ->save();
    $this
      ->assertSame($data, $config
      ->get());
    $this
      ->assertSame($data, $storage
      ->read($name));
    
    $this
      ->assertSame(99, $config
      ->get('int'));
    $config
      ->set('int', '99')
      ->save(TRUE);
    $this
      ->assertSame('99', $config
      ->get('int'));
    
    $config
      ->save();
    $this
      ->assertSame($data, $config
      ->get());
    
    try {
      $config
        ->set('stream', fopen(__FILE__, 'r'))
        ->save();
      $this
        ->fail('No Exception thrown upon saving invalid data type.');
    } catch (UnsupportedDataTypeConfigException $e) {
      
    }
    
    $typed_config_manager = $this->container
      ->get('config.typed');
    $config_name = 'config_test.no_schema';
    $config = $this
      ->config($config_name);
    $this
      ->assertFalse($typed_config_manager
      ->hasConfigSchema($config_name));
    try {
      $config
        ->set('stream', fopen(__FILE__, 'r'))
        ->save();
      $this
        ->fail('No Exception thrown upon saving invalid data type.');
    } catch (UnsupportedDataTypeConfigException $e) {
      
    }
  }
}