ConfigImportTest.php 3.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
<?php

/**
 * @file
 * Definition of Drupal\config\Tests\ConfigImportTest.
 */

namespace Drupal\config\Tests;

use Drupal\Core\Config\DatabaseStorage;
use Drupal\Core\Config\FileStorage;
use Drupal\simpletest\WebTestBase;

/**
 * Tests importing configuration from files into active store.
 */
class ConfigImportTest extends WebTestBase {
  public static function getInfo() {
    return array(
      'name' => 'Import configuration',
      'description' => 'Tests importing configuration from files into active store.',
      'group' => 'Configuration',
    );
  }

  function setUp() {
    parent::setUp(array('config_test'));
  }

  /**
   * Tests deletion of configuration during import.
   */
  function testDeleted() {
    $name = 'config_test.system';
    $dynamic_name = 'config_test.dynamic.default';

    // Verify the default configuration values exist.
    $config = config($name);
    $this->assertIdentical($config->get('foo'), 'bar');
    $config = config($dynamic_name);
    $this->assertIdentical($config->get('id'), 'default');

    // Export.
    config_export();

    // Delete the configuration objects.
    $file_storage = new FileStorage();
    $file_storage->delete($name);
    $file_storage->delete($dynamic_name);

    // Import.
    config_import();

    // Verify the values have disappeared.
    $database_storage = new DatabaseStorage();
    $this->assertIdentical($database_storage->read($name), FALSE);
    $this->assertIdentical($database_storage->read($dynamic_name), FALSE);

    $config = config($name);
    $this->assertIdentical($config->get('foo'), NULL);
    $config = config($dynamic_name);
    $this->assertIdentical($config->get('id'), NULL);
  }

  /**
   * Tests creation of configuration during import.
   */
  function testNew() {
    $name = 'config_test.new';
    $dynamic_name = 'config_test.dynamic.new';

    // Verify the configuration to create does not exist yet.
    $file_storage = new FileStorage();
    $this->assertIdentical($file_storage->exists($name), FALSE, $name . ' not found.');
    $this->assertIdentical($file_storage->exists($dynamic_name), FALSE, $dynamic_name . ' not found.');

    // Export.
    config_export();

    // Create new configuration objects.
    $file_storage->write($name, array(
      'add_me' => 'new value',
    ));
    $file_storage->write($dynamic_name, array(
      'id' => 'new',
      'label' => 'New',
    ));
    $this->assertIdentical($file_storage->exists($name), TRUE, $name . ' found.');
    $this->assertIdentical($file_storage->exists($dynamic_name), TRUE, $dynamic_name . ' found.');

    // Import.
    config_import();

    // Verify the values appeared.
    $config = config($name);
    $this->assertIdentical($config->get('add_me'), 'new value');
    $config = config($dynamic_name);
    $this->assertIdentical($config->get('label'), 'New');
  }

  /**
   * Tests updating of configuration during import.
   */
  function testUpdated() {
    $name = 'config_test.system';
    $dynamic_name = 'config_test.dynamic.default';

    // Export.
    config_export();

    // Replace the file content of the existing configuration objects.
    $file_storage = new FileStorage();
    $this->assertIdentical($file_storage->exists($name), TRUE, $name . ' found.');
    $this->assertIdentical($file_storage->exists($dynamic_name), TRUE, $dynamic_name . ' found.');
    $file_storage->write($name, array(
      'foo' => 'beer',
    ));
    $file_storage->write($dynamic_name, array(
      'id' => 'default',
      'label' => 'Updated',
    ));

    // Verify the active store still returns the default values.
    $config = config($name);
    $this->assertIdentical($config->get('foo'), 'bar');
    $config = config($dynamic_name);
    $this->assertIdentical($config->get('label'), 'Default');

    // Import.
    config_import();

    // Verify the values were updated.
    $config = config($name);
    $this->assertIdentical($config->get('foo'), 'beer');
    $config = config($dynamic_name);
    $this->assertIdentical($config->get('label'), 'Updated');
  }
}