2012-09-22 17:43:10 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Files\Cache;
|
|
|
|
|
|
|
|
class Cache extends \UnitTestCase {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Temporary $storage;
|
|
|
|
*/
|
|
|
|
private $storage;
|
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Cache\Cache $cache
|
|
|
|
*/
|
|
|
|
private $cache;
|
2012-09-22 17:43:10 +04:00
|
|
|
|
|
|
|
public function testSimple() {
|
2012-09-26 19:52:02 +04:00
|
|
|
$file1 = 'foo';
|
|
|
|
$file2 = 'foo/bar';
|
2012-09-22 17:43:10 +04:00
|
|
|
$data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder');
|
|
|
|
$data2 = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
|
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->assertFalse($this->cache->inCache($file1));
|
|
|
|
$this->assertEqual($this->cache->get($file1), null);
|
2012-09-22 17:43:10 +04:00
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$id1 = $this->cache->put($file1, $data1);
|
|
|
|
$this->assertTrue($this->cache->inCache($file1));
|
|
|
|
$cacheData1 = $this->cache->get($file1);
|
2012-09-22 17:43:10 +04:00
|
|
|
foreach ($data1 as $key => $value) {
|
|
|
|
$this->assertEqual($value, $cacheData1[$key]);
|
|
|
|
}
|
2012-10-03 13:24:49 +04:00
|
|
|
$this->assertEqual($cacheData1['mimepart'], 'foo');
|
2012-09-22 17:43:10 +04:00
|
|
|
$this->assertEqual($cacheData1['fileid'], $id1);
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->assertEqual($id1, $this->cache->getId($file1));
|
2012-09-22 17:43:10 +04:00
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->assertFalse($this->cache->inCache($file2));
|
|
|
|
$id2 = $this->cache->put($file2, $data2);
|
|
|
|
$this->assertTrue($this->cache->inCache($file2));
|
|
|
|
$cacheData2 = $this->cache->get($file2);
|
2012-09-22 17:43:10 +04:00
|
|
|
foreach ($data2 as $key => $value) {
|
|
|
|
$this->assertEqual($value, $cacheData2[$key]);
|
|
|
|
}
|
|
|
|
$this->assertEqual($cacheData1['fileid'], $cacheData2['parent']);
|
|
|
|
$this->assertEqual($cacheData2['fileid'], $id2);
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->assertEqual($id2, $this->cache->getId($file2));
|
|
|
|
$this->assertEqual($id1, $this->cache->getParentId($file2));
|
2012-09-22 17:43:10 +04:00
|
|
|
|
|
|
|
$newSize = 1050;
|
2012-09-26 19:52:02 +04:00
|
|
|
$newId2 = $this->cache->put($file2, array('size' => $newSize));
|
|
|
|
$cacheData2 = $this->cache->get($file2);
|
2012-09-22 17:43:10 +04:00
|
|
|
$this->assertEqual($newId2, $id2);
|
|
|
|
$this->assertEqual($cacheData2['size'], $newSize);
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->assertEqual($cacheData1, $this->cache->get($file1));
|
2012-09-22 17:43:10 +04:00
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->cache->remove($file2);
|
|
|
|
$this->assertFalse($this->cache->inCache($file2));
|
|
|
|
$this->assertEqual($this->cache->get($file2), null);
|
|
|
|
$this->assertTrue($this->cache->inCache($file1));
|
2012-09-22 17:43:10 +04:00
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->assertEqual($cacheData1, $this->cache->get($id1));
|
2012-09-22 17:43:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPartial() {
|
2012-09-26 19:52:02 +04:00
|
|
|
$file1 = 'foo';
|
2012-09-22 17:43:10 +04:00
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->cache->put($file1, array('size' => 10));
|
|
|
|
$this->assertEqual(array('size' => 10), $this->cache->get($file1));
|
2012-09-22 17:43:10 +04:00
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->cache->put($file1, array('mtime' => 15));
|
|
|
|
$this->assertEqual(array('size' => 10, 'mtime' => 15), $this->cache->get($file1));
|
2012-09-22 17:43:10 +04:00
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->cache->put($file1, array('size' => 12));
|
|
|
|
$this->assertEqual(array('size' => 12, 'mtime' => 15), $this->cache->get($file1));
|
2012-09-22 17:43:10 +04:00
|
|
|
}
|
|
|
|
|
2012-09-23 17:25:03 +04:00
|
|
|
public function testFolder() {
|
2012-09-26 19:52:02 +04:00
|
|
|
$file1 = 'folder';
|
|
|
|
$file2 = 'folder/bar';
|
|
|
|
$file3 = 'folder/foo';
|
2012-09-23 17:25:03 +04:00
|
|
|
$data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder');
|
|
|
|
$fileData = array();
|
|
|
|
$fileData['bar'] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
|
|
|
|
$fileData['foo'] = array('size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file');
|
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->cache->put($file1, $data1);
|
|
|
|
$this->cache->put($file2, $fileData['bar']);
|
|
|
|
$this->cache->put($file3, $fileData['foo']);
|
2012-09-23 17:25:03 +04:00
|
|
|
|
2012-09-26 19:52:02 +04:00
|
|
|
$content = $this->cache->getFolderContents($file1);
|
2012-09-23 17:25:03 +04:00
|
|
|
$this->assertEqual(count($content), 2);
|
|
|
|
foreach ($content as $cachedData) {
|
|
|
|
$data = $fileData[$cachedData['name']];
|
|
|
|
foreach ($data as $name => $value) {
|
|
|
|
$this->assertEqual($value, $cachedData[$name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-08 16:58:21 +04:00
|
|
|
function testStatus() {
|
|
|
|
$this->assertEquals(\OC\Files\Cache\Cache::NOT_FOUND, $this->cache->getStatus('foo'));
|
|
|
|
$this->cache->put('foo', array('size' => -1));
|
|
|
|
$this->assertEquals(\OC\Files\Cache\Cache::PARTIAL, $this->cache->getStatus('foo'));
|
|
|
|
$this->cache->put('foo', array('size' => -1, 'mtime' => 20, 'mimetype' => 'foo/file'));
|
|
|
|
$this->assertEquals(\OC\Files\Cache\Cache::SHALLOW, $this->cache->getStatus('foo'));
|
|
|
|
$this->cache->put('foo', array('size' => 10));
|
|
|
|
$this->assertEquals(\OC\Files\Cache\Cache::COMPLETE, $this->cache->getStatus('foo'));
|
|
|
|
}
|
|
|
|
|
2012-10-26 15:23:15 +04:00
|
|
|
function testSearch() {
|
|
|
|
$file1 = 'folder';
|
|
|
|
$file2 = 'folder/foobar';
|
|
|
|
$file3 = 'folder/foo';
|
|
|
|
$data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder');
|
|
|
|
$fileData = array();
|
|
|
|
$fileData['foobar'] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
|
|
|
|
$fileData['foo'] = array('size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file');
|
|
|
|
|
|
|
|
$this->cache->put($file1, $data1);
|
|
|
|
$this->cache->put($file2, $fileData['foobar']);
|
|
|
|
$this->cache->put($file3, $fileData['foo']);
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($this->cache->search('%foo%')));
|
|
|
|
$this->assertEquals(1, count($this->cache->search('foo')));
|
|
|
|
$this->assertEquals(1, count($this->cache->search('%folder%')));
|
|
|
|
$this->assertEquals(1, count($this->cache->search('folder%')));
|
|
|
|
$this->assertEquals(3, count($this->cache->search('%')));
|
|
|
|
}
|
|
|
|
|
2012-09-22 17:43:10 +04:00
|
|
|
public function tearDown() {
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->cache->clear();
|
2012-09-22 17:43:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
$this->storage = new \OC\Files\Storage\Temporary(array());
|
2012-09-26 19:52:02 +04:00
|
|
|
$this->cache = new \OC\Files\Cache\Cache($this->storage);
|
2012-09-22 17:43:10 +04:00
|
|
|
}
|
|
|
|
}
|