Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\Component\FileCache\FileCacheFactoryTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\Component\FileCache;
|
||||
|
||||
use Drupal\Component\FileCache\FileCacheFactory;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\Component\FileCache\FileCacheFactory
|
||||
* @group FileCache
|
||||
*/
|
||||
class FileCacheFactoryTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$settings = [
|
||||
'collection' => 'test-23',
|
||||
'cache_backend_class' => '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend',
|
||||
'cache_backend_configuration' => [
|
||||
'bin' => 'dog',
|
||||
],
|
||||
];
|
||||
$configuration = FileCacheFactory::getConfiguration();
|
||||
if (!$configuration) {
|
||||
$configuration = [];
|
||||
}
|
||||
$configuration += [ 'test_foo_settings' => $settings ];
|
||||
FileCacheFactory::setConfiguration($configuration);
|
||||
FileCacheFactory::setPrefix('prefix');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::get
|
||||
*/
|
||||
public function testGet() {
|
||||
$file_cache = FileCacheFactory::get('test_foo_settings', []);
|
||||
|
||||
// Ensure the right backend and configuration is used.
|
||||
$filename = __DIR__ . '/Fixtures/llama-23.txt';
|
||||
$realpath = realpath($filename);
|
||||
$cid = 'prefix:test-23:' . $realpath;
|
||||
|
||||
$file_cache->set($filename, 23);
|
||||
|
||||
$static_cache = new StaticFileCacheBackend(['bin' => 'dog']);
|
||||
$result = $static_cache->fetch([$cid]);
|
||||
$this->assertNotEmpty($result);
|
||||
|
||||
// Cleanup static caches.
|
||||
$file_cache->delete($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::get
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Required prefix configuration is missing
|
||||
*/
|
||||
public function testGetNoPrefix() {
|
||||
FileCacheFactory::setPrefix(NULL);
|
||||
FileCacheFactory::get('test_foo_settings', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getConfiguration
|
||||
* @covers ::setConfiguration
|
||||
*/
|
||||
public function testGetSetConfiguration() {
|
||||
$configuration = FileCacheFactory::getConfiguration();
|
||||
$configuration['test_foo_bar'] = ['bar' => 'llama'];
|
||||
FileCacheFactory::setConfiguration($configuration);
|
||||
$configuration = FileCacheFactory::getConfiguration();
|
||||
$this->assertEquals(['bar' => 'llama'], $configuration['test_foo_bar']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getPrefix
|
||||
* @covers ::setPrefix
|
||||
*/
|
||||
public function testGetSetPrefix() {
|
||||
$prefix = $this->randomMachineName();
|
||||
FileCacheFactory::setPrefix($prefix);
|
||||
$this->assertEquals($prefix, FileCacheFactory::getPrefix());
|
||||
}
|
||||
|
||||
}
|
147
core/tests/Drupal/Tests/Component/FileCache/FileCacheTest.php
Normal file
147
core/tests/Drupal/Tests/Component/FileCache/FileCacheTest.php
Normal file
|
@ -0,0 +1,147 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\Component\FileCache\FileCacheTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\Component\FileCache;
|
||||
|
||||
use Drupal\Component\FileCache\FileCache;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\Component\FileCache\FileCache
|
||||
* @group FileCache
|
||||
*/
|
||||
class FileCacheTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* FileCache object used for the tests.
|
||||
*
|
||||
* @var \Drupal\Component\FileCache\FileCacheInterface
|
||||
*/
|
||||
protected $fileCache;
|
||||
|
||||
/**
|
||||
* Static FileCache object used for verification of tests.
|
||||
*
|
||||
* @var \Drupal\Component\FileCache\FileCacheBackendInterface
|
||||
*/
|
||||
protected $staticFileCache;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->fileCache = new FileCache('prefix', 'test', '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend', ['bin' => 'llama']);
|
||||
$this->staticFileCache = new StaticFileCacheBackend(['bin' => 'llama']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::get
|
||||
* @covers ::__construct
|
||||
*/
|
||||
public function testGet() {
|
||||
// Test a cache miss.
|
||||
$result = $this->fileCache->get(__DIR__ . '/Fixtures/no-llama-42.yml');
|
||||
$this->assertNull($result);
|
||||
|
||||
// Test a cache hit.
|
||||
$filename = __DIR__ . '/Fixtures/llama-42.txt';
|
||||
$realpath = realpath($filename);
|
||||
$cid = 'prefix:test:' . $realpath;
|
||||
$data = [
|
||||
'mtime' => filemtime($realpath),
|
||||
'filepath' => $realpath,
|
||||
'data' => 42,
|
||||
];
|
||||
|
||||
$this->staticFileCache->store($cid, $data);
|
||||
|
||||
$result = $this->fileCache->get($filename);
|
||||
$this->assertEquals(42, $result);
|
||||
|
||||
// Cleanup static caches.
|
||||
$this->fileCache->delete($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getMultiple
|
||||
*/
|
||||
public function testGetMultiple() {
|
||||
// Test a cache miss.
|
||||
$result = $this->fileCache->getMultiple([__DIR__ . '/Fixtures/no-llama-42.yml']);
|
||||
$this->assertEmpty($result);
|
||||
|
||||
// Test a cache hit.
|
||||
$filename = __DIR__ . '/Fixtures/llama-42.txt';
|
||||
$realpath = realpath($filename);
|
||||
$cid = 'prefix:test:' . $realpath;
|
||||
$data = [
|
||||
'mtime' => filemtime($realpath),
|
||||
'filepath' => $realpath,
|
||||
'data' => 42,
|
||||
];
|
||||
|
||||
$this->staticFileCache->store($cid, $data);
|
||||
|
||||
$result = $this->fileCache->getMultiple([$filename]);
|
||||
$this->assertEquals([$filename => 42], $result);
|
||||
|
||||
// Test a static cache hit.
|
||||
$file2 = __DIR__ . '/Fixtures/llama-23.txt';
|
||||
$this->fileCache->set($file2, 23);
|
||||
|
||||
$result = $this->fileCache->getMultiple([$filename, $file2]);
|
||||
$this->assertEquals([$filename => 42, $file2 => 23], $result);
|
||||
|
||||
// Cleanup static caches.
|
||||
$this->fileCache->delete($filename);
|
||||
$this->fileCache->delete($file2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::set
|
||||
*/
|
||||
public function testSet() {
|
||||
$filename = __DIR__ . '/Fixtures/llama-23.txt';
|
||||
$realpath = realpath($filename);
|
||||
$cid = 'prefix:test:' . $realpath;
|
||||
$data = [
|
||||
'mtime' => filemtime($realpath),
|
||||
'filepath' => $realpath,
|
||||
'data' => 23,
|
||||
];
|
||||
|
||||
$this->fileCache->set($filename, 23);
|
||||
$result = $this->staticFileCache->fetch([$cid]);
|
||||
$this->assertEquals([$cid => $data], $result);
|
||||
|
||||
// Cleanup static caches.
|
||||
$this->fileCache->delete($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::delete
|
||||
*/
|
||||
public function testDelete() {
|
||||
$filename = __DIR__ . '/Fixtures/llama-23.txt';
|
||||
$realpath = realpath($filename);
|
||||
$cid = 'prefix:test:' . $realpath;
|
||||
|
||||
$this->fileCache->set($filename, 23);
|
||||
|
||||
// Ensure data is removed after deletion.
|
||||
$this->fileCache->delete($filename);
|
||||
|
||||
$result = $this->staticFileCache->fetch([$cid]);
|
||||
$this->assertEquals([], $result);
|
||||
|
||||
$result = $this->fileCache->get($filename);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
23
|
|
@ -0,0 +1 @@
|
|||
42
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\Component\FileCache\StaticFileCacheBackend.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\Component\FileCache;
|
||||
|
||||
use Drupal\Component\FileCache\FileCacheBackendInterface;
|
||||
|
||||
/**
|
||||
* Allows to cache data based on file modification dates in a static cache.
|
||||
*/
|
||||
class StaticFileCacheBackend implements FileCacheBackendInterface {
|
||||
|
||||
/**
|
||||
* Internal static cache.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $cache = [];
|
||||
|
||||
/**
|
||||
* Bin used for storing the data in the static cache.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $bin;
|
||||
|
||||
/**
|
||||
* Constructs a PHP Storage FileCache backend.
|
||||
*
|
||||
* @param array $configuration
|
||||
* (optional) Configuration used to configure this object.
|
||||
*/
|
||||
public function __construct($configuration) {
|
||||
$this->bin = isset($configuration['bin']) ? $configuration['bin'] : 'file_cache';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function fetch(array $cids) {
|
||||
$result = [];
|
||||
foreach ($cids as $cid) {
|
||||
if (isset(static::$cache[$this->bin][$cid])) {
|
||||
$result[$cid] = static::$cache[$this->bin][$cid];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function store($cid, $data) {
|
||||
static::$cache[$this->bin][$cid] = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete($cid) {
|
||||
unset(static::$cache[$this->bin][$cid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows tests to reset the static cache to avoid side effects.
|
||||
*/
|
||||
public static function reset() {
|
||||
static::$cache = [];
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue