PhpStorageFactoryTest.php 3.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * Contains \Drupal\system\Tests\Session\PhpStorage\PhpStorageFactoryTest.
 */

namespace Drupal\system\Tests\PhpStorage;

use Drupal\Component\PhpStorage\MTimeProtectedFileStorage;
use Drupal\Core\PhpStorage\PhpStorageFactory;
12
use Drupal\Core\Site\Settings;
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
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\system\PhpStorage\MockPhpStorage;

/**
 * Tests the PHP storage factory.
 *
 * @see \Drupal\Core\PhpStorage\PhpStorageFactory
 */
class PhpStorageFactoryTest extends DrupalUnitTestBase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'PHP storage factory tests',
      'description' => 'Tests the PHP storage factory..',
      'group' => 'PhpStorage',
    );
  }

  /**
   * Tests the get() method with no settings.
   */
  public function testGetNoSettings() {
    $php = PhpStorageFactory::get('test');
    // This should be the default class used.
    $this->assertTrue($php instanceof MTimeProtectedFileStorage, 'An MTimeProtectedFileStorage instance was returned with no settings.');
  }

  /**
   * Tests the get() method using the 'default' settings.
   */
  public function testGetDefault() {
    $this->setSettings();
    $php = PhpStorageFactory::get('test');
    $this->assertTrue($php instanceof MockPhpStorage, 'A FileReadOnlyStorage instance was returned with default settings.');
  }

  /**
   * Tests the get() method with overridden settings.
   */
  public function testGetOverride() {
    $this->setSettings('test');
    $php = PhpStorageFactory::get('test');
    // The FileReadOnlyStorage should be used from settings.
    $this->assertTrue($php instanceof MockPhpStorage, 'A MockPhpStorage instance was returned from overridden settings.');

    // Test that the name is used for the bin when it is NULL.
    $this->setSettings('test', array('bin' => NULL));
    $php = PhpStorageFactory::get('test');
    $this->assertTrue($php instanceof MockPhpStorage, 'An MockPhpStorage instance was returned from overridden settings.');
    $this->assertIdentical('test', $php->getConfigurationValue('bin'), 'Name value was used for bin.');

    // Test that a default directory is set if it's empty.
    $this->setSettings('test', array('directory' => NULL));
    $php = PhpStorageFactory::get('test');
    $this->assertTrue($php instanceof MockPhpStorage, 'An MockPhpStorage instance was returned from overridden settings.');
    $this->assertIdentical(DRUPAL_ROOT . '/' . PublicStream::basePath() . '/php', $php->getConfigurationValue('directory'), 'Default file directory was used.');

    // Test that a default storage class is set if it's empty.
    $this->setSettings('test', array('class' => NULL));
    $php = PhpStorageFactory::get('test');
    $this->assertTrue($php instanceof MTimeProtectedFileStorage, 'An MTimeProtectedFileStorage instance was returned from overridden settings with no class.');
  }

  /**
   * Sets the Settings() singleton.
   *
   * @param string $name
   *   The storage bin name to set.
   * @param array $configuration
   *   An array of configuration to set. Will be merged with default values.
   */
  protected function setSettings($name = 'default', array $configuration = array()) {
    $settings['php_storage'][$name] = $configuration + array(
      'class' => 'Drupal\system\PhpStorage\MockPhpStorage',
      'directory' => 'tmp://',
      'secret' => $this->randomString(),
      'bin' => 'test',
    );
    new Settings($settings);
  }

}