class FileStorageTest

Same name in this branch

Tests the simple file storage.

Hierarchy

Expanded class hierarchy of FileStorageTest

File

drupal/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php, line 13
Definition of Drupal\Tests\Component\PhpStorage\FileStorageTest.

Namespace

Drupal\Tests\Component\PhpStorage
View source
class FileStorageTest extends PhpStorageTestBase {
  public static function getInfo() {
    return array(
      'name' => 'Simple file storage',
      'description' => 'Tests the FileStorage implementation.',
      'group' => 'PHP Storage',
    );
  }
  function setUp() {
    global $conf;
    parent::setUp();
    $dir_path = sys_get_temp_dir() . '/php';
    $conf['php_storage']['simpletest'] = array(
      'class' => 'Drupal\\Component\\PhpStorage\\FileStorage',
      'directory' => $dir_path,
    );
    $conf['php_storage']['readonly'] = array(
      'class' => 'Drupal\\Component\\PhpStorage\\FileReadOnlyStorage',
      'directory' => $dir_path,
      // Let this read from the bin where the other instance is writing.
      'bin' => 'simpletest',
    );
  }

  /**
   * Tests basic load/save/delete operations.
   */
  function testCRUD() {
    $php = $this->storageFactory
      ->get('simpletest');
    $this
      ->assertInstanceOf('Drupal\\Component\\PhpStorage\\FileStorage', $php);
    $this
      ->assertCRUD($php);
  }

  /**
   * Tests writing with one class and reading with another.
   */
  function testReadOnly() {
    $php = $this->storageFactory
      ->get('simpletest');
    $name = $this
      ->randomName() . '/' . $this
      ->randomName() . '.php';

    // Find a global that doesn't exist.
    do {
      $random = mt_rand(10000, 100000);
    } while (isset($GLOBALS[$random]));

    // Write out a PHP file and ensure it's successfully loaded.
    $code = "<?php\n\$GLOBALS[{$random}] = TRUE;";
    $success = $php
      ->save($name, $code);
    $this
      ->assertSame($success, TRUE);
    $php_read = $this->storageFactory
      ->get('readonly');
    $php_read
      ->load($name);
    $this
      ->assertTrue($GLOBALS[$random]);

    // If the file was successfully loaded, it must also exist, but ensure the
    // exists() method returns that correctly.
    $this
      ->assertSame($php_read
      ->exists($name), TRUE);

    // Saving and deleting should always fail.
    $this
      ->assertFalse($php_read
      ->save($name, $code));
    $this
      ->assertFalse($php_read
      ->delete($name));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileStorageTest::getInfo public static function This method exists to support the simpletest UI runner. Overrides UnitTestCase::getInfo
FileStorageTest::setUp function Overrides \Drupal\Tests\UnitTestCase::setUp() Overrides PhpStorageTestBase::setUp
FileStorageTest::testCRUD function Tests basic load/save/delete operations.
FileStorageTest::testReadOnly function Tests writing with one class and reading with another.
PhpStorageTestBase::$storageFactory protected property The storage factory object.
PhpStorageTestBase::assertCRUD public function Assert that a PHP storage controller's load/save/delete operations work.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::randomName public static function Generates a random string containing letters and numbers.