2012-11-09 00:12:40 +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;
|
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
use OC\Files\Filesystem;
|
2013-10-29 17:18:57 +04:00
|
|
|
use OC\Files\Storage\Temporary;
|
2014-08-04 16:46:48 +04:00
|
|
|
use OC\Files\View;
|
2012-11-09 00:12:40 +04:00
|
|
|
|
|
|
|
class Updater extends \PHPUnit_Framework_TestCase {
|
|
|
|
/**
|
2014-08-04 16:46:48 +04:00
|
|
|
* @var \OC\Files\Storage\Storage
|
2012-11-09 00:12:40 +04:00
|
|
|
*/
|
2014-08-04 16:46:48 +04:00
|
|
|
protected $storage;
|
2012-11-09 00:12:40 +04:00
|
|
|
|
|
|
|
/**
|
2014-08-04 16:46:48 +04:00
|
|
|
* @var \OC\Files\Cache\Cache
|
2012-11-09 00:12:40 +04:00
|
|
|
*/
|
2014-08-04 16:46:48 +04:00
|
|
|
protected $cache;
|
2013-10-29 18:08:12 +04:00
|
|
|
|
2012-11-09 00:12:40 +04:00
|
|
|
/**
|
2014-08-04 16:46:48 +04:00
|
|
|
* @var \OC\Files\View
|
2012-11-09 00:12:40 +04:00
|
|
|
*/
|
2014-08-04 16:46:48 +04:00
|
|
|
protected $view;
|
2012-11-09 00:12:40 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Cache\Updater
|
|
|
|
*/
|
|
|
|
protected $updater;
|
2012-11-09 00:12:40 +04:00
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
/** @var \OC\Files\Storage\Storage */
|
|
|
|
private $originalStorage;
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->originalStorage = Filesystem::getStorage('/');
|
2014-08-04 16:46:48 +04:00
|
|
|
$this->storage = new Temporary(array());
|
|
|
|
Filesystem::clearMounts();
|
|
|
|
Filesystem::mount($this->storage, array(), '/');
|
|
|
|
$this->view = new View('');
|
|
|
|
$this->updater = new \OC\Files\Cache\Updater($this->view);
|
2012-11-09 00:12:40 +04:00
|
|
|
$this->cache = $this->storage->getCache();
|
2014-08-04 16:46:48 +04:00
|
|
|
}
|
2012-11-09 00:12:40 +04:00
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
protected function tearDown() {
|
|
|
|
Filesystem::clearMounts();
|
|
|
|
Filesystem::mount($this->originalStorage, array(), '/');
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
public function testNewFile() {
|
|
|
|
$this->storage->file_put_contents('foo.txt', 'bar');
|
|
|
|
$this->assertFalse($this->cache->inCache('foo.txt'));
|
2013-10-29 18:08:12 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$this->updater->update('/foo.txt');
|
2013-10-29 18:08:12 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$this->assertTrue($this->cache->inCache('foo.txt'));
|
|
|
|
$cached = $this->cache->get('foo.txt');
|
|
|
|
$this->assertEquals(3, $cached['size']);
|
|
|
|
$this->assertEquals('text/plain', $cached['mimetype']);
|
|
|
|
}
|
2012-11-09 00:12:40 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
public function testUpdatedFile() {
|
|
|
|
$this->view->file_put_contents('/foo.txt', 'bar');
|
|
|
|
$cached = $this->cache->get('foo.txt');
|
|
|
|
$this->assertEquals(3, $cached['size']);
|
|
|
|
$this->assertEquals('text/plain', $cached['mimetype']);
|
2012-11-09 00:12:40 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$this->storage->file_put_contents('foo.txt', 'qwerty');
|
2012-11-09 00:12:40 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$cached = $this->cache->get('foo.txt');
|
|
|
|
$this->assertEquals(3, $cached['size']);
|
2012-11-09 00:12:40 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$this->updater->update('/foo.txt');
|
2012-11-09 00:12:40 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$cached = $this->cache->get('foo.txt');
|
|
|
|
$this->assertEquals(6, $cached['size']);
|
2012-11-09 00:12:40 +04:00
|
|
|
}
|
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
public function testParentSize() {
|
|
|
|
$this->storage->getScanner()->scan('');
|
2013-06-12 23:32:00 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$parentCached = $this->cache->get('');
|
|
|
|
$this->assertEquals(0, $parentCached['size']);
|
2012-11-09 00:12:40 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$this->storage->file_put_contents('foo.txt', 'bar');
|
|
|
|
|
|
|
|
$parentCached = $this->cache->get('');
|
|
|
|
$this->assertEquals(0, $parentCached['size']);
|
|
|
|
|
|
|
|
$this->updater->update('foo.txt');
|
2012-11-09 00:12:40 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$parentCached = $this->cache->get('');
|
|
|
|
$this->assertEquals(3, $parentCached['size']);
|
|
|
|
|
|
|
|
$this->storage->file_put_contents('foo.txt', 'qwerty');
|
|
|
|
|
|
|
|
$parentCached = $this->cache->get('');
|
|
|
|
$this->assertEquals(3, $parentCached['size']);
|
|
|
|
|
|
|
|
$this->updater->update('foo.txt');
|
|
|
|
|
|
|
|
$parentCached = $this->cache->get('');
|
|
|
|
$this->assertEquals(6, $parentCached['size']);
|
|
|
|
|
|
|
|
$this->storage->unlink('foo.txt');
|
|
|
|
|
|
|
|
$parentCached = $this->cache->get('');
|
|
|
|
$this->assertEquals(6, $parentCached['size']);
|
|
|
|
|
|
|
|
$this->updater->remove('foo.txt');
|
|
|
|
|
|
|
|
$parentCached = $this->cache->get('');
|
|
|
|
$this->assertEquals(0, $parentCached['size']);
|
2013-06-12 23:32:00 +04:00
|
|
|
}
|
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
public function testMove() {
|
|
|
|
$this->storage->file_put_contents('foo.txt', 'qwerty');
|
|
|
|
$this->updater->update('foo.txt');
|
2012-11-09 00:12:40 +04:00
|
|
|
|
|
|
|
$this->assertTrue($this->cache->inCache('foo.txt'));
|
|
|
|
$this->assertFalse($this->cache->inCache('bar.txt'));
|
2014-08-04 16:46:48 +04:00
|
|
|
$cached = $this->cache->get('foo.txt');
|
2013-06-12 23:32:00 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$this->storage->rename('foo.txt', 'bar.txt');
|
2013-11-27 21:48:24 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$this->assertTrue($this->cache->inCache('foo.txt'));
|
|
|
|
$this->assertFalse($this->cache->inCache('bar.txt'));
|
2013-06-12 23:32:00 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$this->updater->rename('foo.txt', 'bar.txt');
|
2013-06-12 23:32:00 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$this->assertFalse($this->cache->inCache('foo.txt'));
|
|
|
|
$this->assertTrue($this->cache->inCache('bar.txt'));
|
2013-06-12 23:32:00 +04:00
|
|
|
|
2014-08-04 16:46:48 +04:00
|
|
|
$cachedTarget = $this->cache->get('bar.txt');
|
|
|
|
$this->assertEquals($cached['etag'], $cachedTarget['etag']);
|
|
|
|
$this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
|
|
|
|
$this->assertEquals($cached['size'], $cachedTarget['size']);
|
|
|
|
$this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
|
|
|
|
}
|
2012-11-09 00:12:40 +04:00
|
|
|
}
|