View source
<?php
class FeedsHTTPCacheTest extends FeedsWebTestCase {
const FEEDS_CACHE_DIR = 'private://feeds/cache';
protected $cache;
public static function getInfo() {
return array(
'name' => 'FeedsHTTPCache class test',
'description' => 'Covers class FeedsHTTPCache.',
'group' => 'Feeds',
);
}
public function setUp() {
parent::setUp();
$this->cache = new FeedsHTTPCache('cache_feeds_http');
}
protected function createHttpResponse($with_data = TRUE) {
$response = new stdClass();
$response->code = 200;
$response->headers = array(
'content-type' => 'text/plain; charset=utf-8',
'x-host' => 'http://www.example.com',
);
if ($with_data) {
$response->data = static::randomString(255);
}
return $response;
}
protected function createCacheRecord($cid = NULL, $response = NULL) {
if (empty($cid)) {
$cid = static::randomName();
}
if (empty($response)) {
$response = $this
->createHttpResponse(FALSE);
$response->file_path = static::FEEDS_CACHE_DIR . '/' . $cid;
}
$record = new stdClass();
$record->cid = $cid;
$record->data = serialize($response);
$record->expire = CACHE_PERMANENT;
$record->created = REQUEST_TIME;
$record->serialized = TRUE;
drupal_write_record('cache_feeds_http', $record);
return $cid;
}
protected function createCacheRecordUsingApi($cid = NULL, $response = NULL) {
if (empty($cid)) {
$cid = static::randomName();
}
if (empty($response)) {
$response = $this
->createHttpResponse();
}
$this->cache
->set($cid, $response);
return $cid;
}
protected function assertCacheItemExists($cid) {
$message = format_string('Cache item @cid exists.', array(
'@cid' => $cid,
));
$count = db_select('cache_feeds_http')
->fields('cache_feeds_http', array(
'cid',
))
->condition('cid', $cid)
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(1, $count, $message);
}
protected function assertNoCacheItemExists($cid) {
$message = format_string('Cache item @cid does not exist.', array(
'@cid' => $cid,
));
$count = db_select('cache_feeds_http')
->fields('cache_feeds_http', array(
'cid',
))
->condition('cid', $cid)
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(0, $count, $message);
}
protected function assertCacheFileExists($cid) {
$message = format_string('Cache file @cid exists.', array(
'@cid' => $cid,
));
$this
->assertTrue(file_exists(static::FEEDS_CACHE_DIR . '/' . $cid), $message);
}
protected function assertNoCacheFileExists($cid) {
$message = format_string('Cache file @cid does not exist.', array(
'@cid' => $cid,
));
$this
->assertFalse(file_exists(static::FEEDS_CACHE_DIR . '/' . $cid), $message);
}
public function testGet() {
$cid = static::randomName();
$file_data = static::randomName();
$dir = static::FEEDS_CACHE_DIR;
file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
file_put_contents(static::FEEDS_CACHE_DIR . '/' . $cid, $file_data);
$this
->createCacheRecord($cid);
$item = $this->cache
->get($cid);
$this
->assertEqual($file_data, $item->data
->getFileContents());
}
public function testGetMultiple() {
$cid1 = static::randomName();
$cid2 = static::randomName();
$cid3 = static::randomName();
$file1 = static::randomName();
$file2 = static::randomName();
$file3 = static::randomName();
$dir = static::FEEDS_CACHE_DIR;
file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
file_put_contents(static::FEEDS_CACHE_DIR . '/' . $cid1, $file1);
file_put_contents(static::FEEDS_CACHE_DIR . '/' . $cid2, $file2);
file_put_contents(static::FEEDS_CACHE_DIR . '/' . $cid3, $file3);
$this
->createCacheRecord($cid1);
$this
->createCacheRecord($cid2);
$this
->createCacheRecord($cid3);
$cids = array(
$cid2,
$cid3,
);
$items = $this->cache
->getMultiple($cids);
$this
->assertFalse(isset($items[$cid1]));
$this
->assertTrue(isset($items[$cid2]));
$this
->assertTrue(isset($items[$cid3]));
$this
->assertEqual($file2, $items[$cid2]->data
->getFileContents());
$this
->assertEqual($file3, $items[$cid3]->data
->getFileContents());
}
public function testSet() {
$response = $this
->createHttpResponse();
$cid = $this
->createCacheRecordUsingApi(NULL, $response);
$record = db_select('cache_feeds_http')
->fields('cache_feeds_http', array())
->condition('cid', $cid)
->execute()
->fetch();
$this
->assertEqual($cid, $record->cid);
$this
->assertEqual(CACHE_PERMANENT, $record->expire);
$this
->assertFalse(strpos($record->data, $response->data), 'The raw data was not saved in the database.');
$saved_response = unserialize($record->data);
$this
->assertTrue($saved_response instanceof FeedsHTTPCacheItem, 'Cached data is instance of class FeedsHTTPCacheItem.');
$this
->assertEqual($response->headers, $saved_response->headers);
$this
->assertEqual(static::FEEDS_CACHE_DIR . '/' . $cid, $saved_response->file_path);
$this
->assertTrue(file_exists(static::FEEDS_CACHE_DIR . '/' . $cid));
}
public function testClear() {
$cid1 = $this
->createCacheRecordUsingApi();
$cid2 = $this
->createCacheRecordUsingApi();
$this
->assertCacheItemExists($cid1);
$this
->assertCacheItemExists($cid2);
$this
->assertCacheFileExists($cid1);
$this
->assertCacheFileExists($cid2);
$this->cache
->clear($cid1);
$this
->assertNoCacheItemExists($cid1);
$this
->assertCacheItemExists($cid2);
$this
->assertNoCacheFileExists($cid1);
$this
->assertCacheFileExists($cid2);
}
public function testClearMultiple() {
$cid1 = $this
->createCacheRecordUsingApi();
$cid2 = $this
->createCacheRecordUsingApi();
$cid3 = $this
->createCacheRecordUsingApi();
$cid4 = $this
->createCacheRecordUsingApi();
$this->cache
->clear(array(
$cid2,
$cid4,
));
$this
->assertCacheItemExists($cid1);
$this
->assertNoCacheItemExists($cid2);
$this
->assertCacheItemExists($cid3);
$this
->assertNoCacheItemExists($cid4);
$this
->assertCacheFileExists($cid1);
$this
->assertNoCacheFileExists($cid2);
$this
->assertCacheFileExists($cid3);
$this
->assertNoCacheFileExists($cid4);
}
public function testClearExpired() {
$cid_permanent = $this
->createCacheRecordUsingApi();
$cid_expire_past = static::randomName();
$this->cache
->set($cid_expire_past, $this
->createHttpResponse(), REQUEST_TIME - 60);
$cid_expire_now = static::randomName();
$this->cache
->set($cid_expire_now, $this
->createHttpResponse(), REQUEST_TIME);
$cid_expire_future = static::randomName();
$this->cache
->set($cid_expire_future, $this
->createHttpResponse(), REQUEST_TIME + 60);
$this->cache
->clear();
$this
->assertCacheItemExists($cid_permanent);
$this
->assertNoCacheItemExists($cid_expire_past);
$this
->assertCacheItemExists($cid_expire_now);
$this
->assertCacheItemExists($cid_expire_future);
$this
->assertCacheFileExists($cid_permanent);
$this
->assertNoCacheFileExists($cid_expire_past);
$this
->assertCacheFileExists($cid_expire_now);
$this
->assertCacheFileExists($cid_expire_future);
}
public function testClearExpiredUsingCacheLifeTime() {
variable_set('cache_lifetime', 120);
$cid_permanent = $this
->createCacheRecordUsingApi();
$cid_expire_past = static::randomName();
$this->cache
->set($cid_expire_past, $this
->createHttpResponse(), REQUEST_TIME - 60);
$cid_expire_now = static::randomName();
$this->cache
->set($cid_expire_now, $this
->createHttpResponse(), REQUEST_TIME);
$cid_expire_future = static::randomName();
$this->cache
->set($cid_expire_future, $this
->createHttpResponse(), REQUEST_TIME + 60);
variable_set('cache_flush_cache_feeds_http', REQUEST_TIME - 90);
$this->cache
->clear();
$this
->assertCacheItemExists($cid_permanent);
$this
->assertCacheItemExists($cid_expire_past);
$this
->assertCacheItemExists($cid_expire_now);
$this
->assertCacheItemExists($cid_expire_future);
$this
->assertCacheFileExists($cid_permanent);
$this
->assertCacheFileExists($cid_expire_past);
$this
->assertCacheFileExists($cid_expire_now);
$this
->assertCacheFileExists($cid_expire_future);
variable_set('cache_flush_cache_feeds_http', REQUEST_TIME - 121);
$this->cache
->clear();
$this
->assertCacheItemExists($cid_permanent);
$this
->assertNoCacheItemExists($cid_expire_past);
$this
->assertCacheItemExists($cid_expire_now);
$this
->assertCacheItemExists($cid_expire_future);
$this
->assertCacheFileExists($cid_permanent);
$this
->assertNoCacheFileExists($cid_expire_past);
$this
->assertCacheFileExists($cid_expire_now);
$this
->assertCacheFileExists($cid_expire_future);
}
public function testClearAll() {
$cid1 = $this
->createCacheRecordUsingApi();
$cid2 = $this
->createCacheRecordUsingApi();
$cid3 = $this
->createCacheRecordUsingApi();
$this->cache
->clear('*', TRUE);
$count = db_select('cache_feeds_http')
->fields('cache_feeds_http', array(
'cid',
))
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(0, $count, 'The cache_feeds_http item is empty.');
$empty = count(glob(static::FEEDS_CACHE_DIR . '/*')) === 0;
$this
->assertTrue($empty, 'The feeds cache directory is empty.');
}
public function testClearUsingWildcard() {
$cid1 = 'abc123';
$cid2 = 'def456';
$cid3 = 'abc789';
$this
->createCacheRecordUsingApi($cid1);
$this
->createCacheRecordUsingApi($cid2);
$this
->createCacheRecordUsingApi($cid3);
$this->cache
->clear('abc', TRUE);
$this
->assertNoCacheItemExists($cid1);
$this
->assertCacheItemExists($cid2);
$this
->assertNoCacheItemExists($cid3);
$this
->assertNoCacheFileExists($cid1);
$this
->assertCacheFileExists($cid2);
$this
->assertNoCacheFileExists($cid3);
}
public function testIsEmpty() {
$this
->createCacheRecord();
$this
->assertFalse($this->cache
->isEmpty(), 'The cache is empty.');
db_truncate('cache_feeds_http')
->execute();
$this
->assertTrue($this->cache
->isEmpty());
$dir = static::FEEDS_CACHE_DIR;
file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
file_put_contents(static::FEEDS_CACHE_DIR . '/abc123', static::randomName());
$this
->assertFalse($this->cache
->isEmpty());
}
public function testSaveFile() {
$cid = static::randomName();
$response = $this
->createHttpResponse();
$this->cache
->saveFile($cid, $response);
$this
->assertCacheFileExists($cid);
}
public function testSaveFileException() {
variable_set('feeds_http_file_cache_dir', 'file://non-writeable-dir/feeds');
$cid = static::randomName();
$response = $this
->createHttpResponse();
try {
$this->cache
->saveFile($cid, $response);
} catch (Exception $e) {
}
$this
->assertTrue(isset($e), 'An exception is thrown.');
$this
->assertNoCacheFileExists($cid);
}
}