FileStorageTest.php 2.81 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\config\Tests\Storage\FileStorageTest.
6 7 8 9
 */

namespace Drupal\config\Tests\Storage;

10
use Drupal\Component\Serialization\Yaml;
11
use Drupal\Core\Config\FileStorage;
12
use Drupal\Core\Config\UnsupportedDataTypeConfigException;
13 14

/**
15
 * Tests FileStorage operations.
16 17
 *
 * @group config
18 19
 */
class FileStorageTest extends ConfigStorageTestBase {
20

21 22 23 24 25 26 27
  /**
   * A directory to store configuration in.
   *
   * @var string
   */
  protected $directory;

28 29 30
  /**
   * {@inheritdoc}
   */
31
  protected function setUp() {
32
    parent::setUp();
33 34 35 36 37
    // Create a directory.
    $this->directory = $this->publicFilesDirectory . '/config';
    mkdir($this->directory);
    $this->storage = new FileStorage($this->directory);
    $this->invalidStorage = new FileStorage($this->directory . '/nonexisting');
38 39

    // FileStorage::listAll() requires other configuration data to exist.
40
    $this->storage->write('system.performance', $this->config('system.performance')->get());
41
    $this->storage->write('core.extension', array('module' => array()));
42 43 44 45
  }

  protected function read($name) {
    $data = file_get_contents($this->storage->getFilePath($name));
46
    return Yaml::decode($data);
47 48 49 50 51 52 53 54 55 56 57 58 59
  }

  protected function insert($name, $data) {
    file_put_contents($this->storage->getFilePath($name), $data);
  }

  protected function update($name, $data) {
    file_put_contents($this->storage->getFilePath($name), $data);
  }

  protected function delete($name) {
    unlink($this->storage->getFilePath($name));
  }
60 61 62 63

  /**
   * Tests the FileStorage::listAll method with a relative and absolute path.
   */
64
  public function testlistAll() {
65
    $expected_files = array(
66
      'core.extension',
67 68 69 70 71 72 73
      'system.performance',
    );

    $config_files = $this->storage->listAll();
    $this->assertIdentical($config_files, $expected_files, 'Relative path, two config files found.');

    // Initialize FileStorage with absolute file path.
74
    $absolute_path = realpath($this->directory);
75 76 77 78 79
    $storage_absolute_path = new FileStorage($absolute_path);
    $config_files = $storage_absolute_path->listAll();
    $this->assertIdentical($config_files, $expected_files, 'Absolute path, two config files found.');
  }

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  /**
   * Test UnsupportedDataTypeConfigException displays path of
   * erroneous file during read.
   */
  public function testReadUnsupportedDataTypeConfigException() {
    file_put_contents($this->storage->getFilePath('core.extension'), PHP_EOL . 'foo : [bar}', FILE_APPEND);
    try {
      $config_parsed = $this->storage->read('core.extension');
    }
    catch (UnsupportedDataTypeConfigException $e) {
      $this->pass('Exception thrown when trying to read a field containing invalid data type.');
      $this->assertTrue((strpos($e->getMessage(), $this->storage->getFilePath('core.extension')) !== FALSE), 'Erroneous file path is displayed.');
    }
  }

95
}