2012-11-08 20:12:40 +00: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 12:46:48 +00:00
|
|
|
use OC\Files\Filesystem;
|
2013-10-29 13:18:57 +00:00
|
|
|
use OC\Files\Storage\Temporary;
|
2014-08-04 12:46:48 +00:00
|
|
|
use OC\Files\View;
|
2012-11-08 20:12:40 +00:00
|
|
|
|
2015-11-03 00:52:41 +00:00
|
|
|
/**
|
|
|
|
* Class Updater
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
|
|
|
* @package Test\Files\Cache
|
|
|
|
*/
|
2014-11-07 14:23:15 +00:00
|
|
|
class Updater extends \Test\TestCase {
|
2012-11-08 20:12:40 +00:00
|
|
|
/**
|
2014-08-04 12:46:48 +00:00
|
|
|
* @var \OC\Files\Storage\Storage
|
2012-11-08 20:12:40 +00:00
|
|
|
*/
|
2014-08-04 12:46:48 +00:00
|
|
|
protected $storage;
|
2012-11-08 20:12:40 +00:00
|
|
|
|
|
|
|
/**
|
2014-08-04 12:46:48 +00:00
|
|
|
* @var \OC\Files\Cache\Cache
|
2012-11-08 20:12:40 +00:00
|
|
|
*/
|
2014-08-04 12:46:48 +00:00
|
|
|
protected $cache;
|
2013-10-29 14:08:12 +00:00
|
|
|
|
2012-11-08 20:12:40 +00:00
|
|
|
/**
|
2014-08-04 12:46:48 +00:00
|
|
|
* @var \OC\Files\View
|
2012-11-08 20:12:40 +00:00
|
|
|
*/
|
2014-08-04 12:46:48 +00:00
|
|
|
protected $view;
|
2012-11-08 20:12:40 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Cache\Updater
|
|
|
|
*/
|
|
|
|
protected $updater;
|
2012-11-08 20:12:40 +00:00
|
|
|
|
2014-11-12 14:54:41 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-04-08 10:03:55 +00:00
|
|
|
$this->loginAsUser();
|
2014-11-07 14:23:15 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
$this->storage = new Temporary(array());
|
2015-11-25 12:53:31 +00:00
|
|
|
$this->updater = $this->storage->getUpdater();
|
2012-11-08 20:12:40 +00:00
|
|
|
$this->cache = $this->storage->getCache();
|
2014-08-04 12:46:48 +00:00
|
|
|
}
|
2012-11-08 20:12:40 +00:00
|
|
|
|
2014-11-12 14:54:41 +00:00
|
|
|
protected function tearDown() {
|
|
|
|
Filesystem::clearMounts();
|
|
|
|
|
2015-04-08 10:03:55 +00:00
|
|
|
$this->logout();
|
2014-11-12 14:54:41 +00:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
public function testNewFile() {
|
|
|
|
$this->storage->file_put_contents('foo.txt', 'bar');
|
|
|
|
$this->assertFalse($this->cache->inCache('foo.txt'));
|
2013-10-29 14:08:12 +00:00
|
|
|
|
2015-11-25 12:53:31 +00:00
|
|
|
$this->updater->update('foo.txt');
|
2013-10-29 14:08:12 +00:00
|
|
|
|
2014-08-04 12:46:48 +00: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-08 20:12:40 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
public function testUpdatedFile() {
|
2015-11-25 12:53:31 +00:00
|
|
|
$this->storage->file_put_contents('foo.txt', 'bar');
|
|
|
|
$this->updater->update('foo.txt');
|
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
$cached = $this->cache->get('foo.txt');
|
|
|
|
$this->assertEquals(3, $cached['size']);
|
|
|
|
$this->assertEquals('text/plain', $cached['mimetype']);
|
2012-11-08 20:12:40 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
$this->storage->file_put_contents('foo.txt', 'qwerty');
|
2012-11-08 20:12:40 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
$cached = $this->cache->get('foo.txt');
|
|
|
|
$this->assertEquals(3, $cached['size']);
|
2012-11-08 20:12:40 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
$this->updater->update('/foo.txt');
|
2012-11-08 20:12:40 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
$cached = $this->cache->get('foo.txt');
|
|
|
|
$this->assertEquals(6, $cached['size']);
|
2012-11-08 20:12:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
public function testParentSize() {
|
|
|
|
$this->storage->getScanner()->scan('');
|
2013-06-12 19:32:00 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
$parentCached = $this->cache->get('');
|
|
|
|
$this->assertEquals(0, $parentCached['size']);
|
2012-11-08 20:12:40 +00:00
|
|
|
|
2014-08-04 12:46:48 +00: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-08 20:12:40 +00:00
|
|
|
|
2014-08-04 12:46:48 +00: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 19:32:00 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
public function testMove() {
|
|
|
|
$this->storage->file_put_contents('foo.txt', 'qwerty');
|
|
|
|
$this->updater->update('foo.txt');
|
2012-11-08 20:12:40 +00:00
|
|
|
|
|
|
|
$this->assertTrue($this->cache->inCache('foo.txt'));
|
|
|
|
$this->assertFalse($this->cache->inCache('bar.txt'));
|
2014-08-04 12:46:48 +00:00
|
|
|
$cached = $this->cache->get('foo.txt');
|
2013-06-12 19:32:00 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
$this->storage->rename('foo.txt', 'bar.txt');
|
2013-11-27 17:48:24 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
$this->assertTrue($this->cache->inCache('foo.txt'));
|
|
|
|
$this->assertFalse($this->cache->inCache('bar.txt'));
|
2013-06-12 19:32:00 +00:00
|
|
|
|
2015-11-25 12:53:31 +00:00
|
|
|
$this->updater->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');
|
2013-06-12 19:32:00 +00:00
|
|
|
|
2014-08-04 12:46:48 +00:00
|
|
|
$this->assertFalse($this->cache->inCache('foo.txt'));
|
|
|
|
$this->assertTrue($this->cache->inCache('bar.txt'));
|
2013-06-12 19:32:00 +00:00
|
|
|
|
2014-08-04 12:46:48 +00: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']);
|
|
|
|
}
|
2015-02-27 14:49:17 +00:00
|
|
|
|
2015-10-12 15:11:16 +00:00
|
|
|
public function testMoveNonExistingOverwrite() {
|
|
|
|
$this->storage->file_put_contents('bar.txt', 'qwerty');
|
|
|
|
$this->updater->update('bar.txt');
|
|
|
|
|
|
|
|
$cached = $this->cache->get('bar.txt');
|
|
|
|
|
2015-11-25 12:53:31 +00:00
|
|
|
$this->updater->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');
|
2015-10-12 15:11:16 +00:00
|
|
|
|
|
|
|
$this->assertFalse($this->cache->inCache('foo.txt'));
|
|
|
|
$this->assertTrue($this->cache->inCache('bar.txt'));
|
|
|
|
|
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
|
2015-10-16 16:28:45 +00:00
|
|
|
public function testUpdateStorageMTime() {
|
|
|
|
$this->storage->mkdir('sub');
|
|
|
|
$this->storage->mkdir('sub2');
|
|
|
|
$this->storage->file_put_contents('sub/foo.txt', 'qwerty');
|
|
|
|
|
|
|
|
$this->updater->update('sub');
|
|
|
|
$this->updater->update('sub/foo.txt');
|
|
|
|
$this->updater->update('sub2');
|
|
|
|
|
|
|
|
$cachedSourceParent = $this->cache->get('sub');
|
|
|
|
$cachedSource = $this->cache->get('sub/foo.txt');
|
|
|
|
|
|
|
|
$this->storage->rename('sub/foo.txt', 'sub2/bar.txt');
|
|
|
|
|
|
|
|
// simulate storage having a different mtime
|
|
|
|
$testmtime = 1433323578;
|
|
|
|
|
|
|
|
// source storage mtime change
|
|
|
|
$this->storage->touch('sub', $testmtime);
|
|
|
|
|
|
|
|
// target storage mtime change
|
|
|
|
$this->storage->touch('sub2', $testmtime);
|
|
|
|
// some storages (like Dropbox) change storage mtime on rename
|
|
|
|
$this->storage->touch('sub2/bar.txt', $testmtime);
|
|
|
|
|
2015-11-25 12:53:31 +00:00
|
|
|
$this->updater->renameFromStorage($this->storage, 'sub/foo.txt', 'sub2/bar.txt');
|
2015-10-16 16:28:45 +00:00
|
|
|
|
|
|
|
$cachedTargetParent = $this->cache->get('sub2');
|
|
|
|
$cachedTarget = $this->cache->get('sub2/bar.txt');
|
|
|
|
|
|
|
|
$this->assertEquals($cachedSource['mtime'], $cachedTarget['mtime'], 'file mtime preserved');
|
|
|
|
|
|
|
|
$this->assertNotEquals($cachedTarget['storage_mtime'], $cachedTarget['mtime'], 'mtime is not storage_mtime for moved file');
|
|
|
|
|
|
|
|
$this->assertEquals($testmtime, $cachedTarget['storage_mtime'], 'target file storage_mtime propagated');
|
|
|
|
$this->assertNotEquals($testmtime, $cachedTarget['mtime'], 'target file mtime changed, not from storage');
|
|
|
|
|
|
|
|
$this->assertEquals($testmtime, $cachedTargetParent['storage_mtime'], 'target parent storage_mtime propagated');
|
|
|
|
$this->assertNotEquals($testmtime, $cachedTargetParent['mtime'], 'target folder mtime changed, not from storage');
|
|
|
|
}
|
|
|
|
|
2015-02-27 14:49:17 +00:00
|
|
|
public function testNewFileDisabled() {
|
|
|
|
$this->storage->file_put_contents('foo.txt', 'bar');
|
|
|
|
$this->assertFalse($this->cache->inCache('foo.txt'));
|
|
|
|
|
|
|
|
$this->updater->disable();
|
|
|
|
$this->updater->update('/foo.txt');
|
|
|
|
|
|
|
|
$this->assertFalse($this->cache->inCache('foo.txt'));
|
|
|
|
}
|
|
|
|
|
2015-04-01 13:12:59 +00:00
|
|
|
public function testMoveCrossStorage() {
|
|
|
|
$storage2 = new Temporary(array());
|
|
|
|
$cache2 = $storage2->getCache();
|
|
|
|
Filesystem::mount($storage2, array(), '/bar');
|
|
|
|
$this->storage->file_put_contents('foo.txt', 'qwerty');
|
|
|
|
|
|
|
|
$this->updater->update('foo.txt');
|
|
|
|
|
|
|
|
$this->assertTrue($this->cache->inCache('foo.txt'));
|
|
|
|
$this->assertFalse($cache2->inCache('bar.txt'));
|
|
|
|
$cached = $this->cache->get('foo.txt');
|
|
|
|
|
|
|
|
// "rename"
|
|
|
|
$storage2->file_put_contents('bar.txt', 'qwerty');
|
|
|
|
$this->storage->unlink('foo.txt');
|
|
|
|
|
|
|
|
$this->assertTrue($this->cache->inCache('foo.txt'));
|
|
|
|
$this->assertFalse($cache2->inCache('bar.txt'));
|
|
|
|
|
2015-11-25 12:53:31 +00:00
|
|
|
$storage2->getUpdater()->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');
|
2015-04-01 13:12:59 +00:00
|
|
|
|
|
|
|
$this->assertFalse($this->cache->inCache('foo.txt'));
|
|
|
|
$this->assertTrue($cache2->inCache('bar.txt'));
|
|
|
|
|
|
|
|
$cachedTarget = $cache2->get('bar.txt');
|
|
|
|
$this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
|
|
|
|
$this->assertEquals($cached['size'], $cachedTarget['size']);
|
|
|
|
$this->assertEquals($cached['etag'], $cachedTarget['etag']);
|
|
|
|
$this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMoveFolderCrossStorage() {
|
|
|
|
$storage2 = new Temporary(array());
|
|
|
|
$cache2 = $storage2->getCache();
|
|
|
|
Filesystem::mount($storage2, array(), '/bar');
|
|
|
|
$this->storage->mkdir('foo');
|
|
|
|
$this->storage->mkdir('foo/bar');
|
|
|
|
$this->storage->file_put_contents('foo/foo.txt', 'qwerty');
|
|
|
|
$this->storage->file_put_contents('foo/bar.txt', 'foo');
|
|
|
|
$this->storage->file_put_contents('foo/bar/bar.txt', 'qwertyuiop');
|
|
|
|
|
|
|
|
$this->storage->getScanner()->scan('');
|
|
|
|
|
2015-05-21 08:57:58 +00:00
|
|
|
$this->assertTrue($this->cache->inCache('foo'));
|
2015-04-01 13:12:59 +00:00
|
|
|
$this->assertTrue($this->cache->inCache('foo/foo.txt'));
|
|
|
|
$this->assertTrue($this->cache->inCache('foo/bar.txt'));
|
2015-05-21 08:57:58 +00:00
|
|
|
$this->assertTrue($this->cache->inCache('foo/bar'));
|
2015-04-01 13:12:59 +00:00
|
|
|
$this->assertTrue($this->cache->inCache('foo/bar/bar.txt'));
|
|
|
|
$cached = [];
|
2015-05-21 08:57:58 +00:00
|
|
|
$cached[] = $this->cache->get('foo');
|
2015-04-01 13:12:59 +00:00
|
|
|
$cached[] = $this->cache->get('foo/foo.txt');
|
|
|
|
$cached[] = $this->cache->get('foo/bar.txt');
|
2015-05-21 08:57:58 +00:00
|
|
|
$cached[] = $this->cache->get('foo/bar');
|
2015-04-01 13:12:59 +00:00
|
|
|
$cached[] = $this->cache->get('foo/bar/bar.txt');
|
|
|
|
|
2015-05-21 08:57:58 +00:00
|
|
|
// add extension to trigger the possible mimetype change
|
2015-11-25 12:53:31 +00:00
|
|
|
$storage2->moveFromStorage($this->storage, 'foo', 'foo.b');
|
|
|
|
$storage2->getUpdater()->renameFromStorage($this->storage, 'foo', 'foo.b');
|
2015-04-01 13:12:59 +00:00
|
|
|
|
2015-05-21 08:57:58 +00:00
|
|
|
$this->assertFalse($this->cache->inCache('foo'));
|
2015-04-01 13:12:59 +00:00
|
|
|
$this->assertFalse($this->cache->inCache('foo/foo.txt'));
|
|
|
|
$this->assertFalse($this->cache->inCache('foo/bar.txt'));
|
2015-05-21 08:57:58 +00:00
|
|
|
$this->assertFalse($this->cache->inCache('foo/bar'));
|
2015-04-01 13:12:59 +00:00
|
|
|
$this->assertFalse($this->cache->inCache('foo/bar/bar.txt'));
|
2015-05-21 08:57:58 +00:00
|
|
|
$this->assertTrue($cache2->inCache('foo.b'));
|
|
|
|
$this->assertTrue($cache2->inCache('foo.b/foo.txt'));
|
|
|
|
$this->assertTrue($cache2->inCache('foo.b/bar.txt'));
|
|
|
|
$this->assertTrue($cache2->inCache('foo.b/bar'));
|
|
|
|
$this->assertTrue($cache2->inCache('foo.b/bar/bar.txt'));
|
2015-04-01 13:12:59 +00:00
|
|
|
|
|
|
|
$cachedTarget = [];
|
2015-05-21 08:57:58 +00:00
|
|
|
$cachedTarget[] = $cache2->get('foo.b');
|
|
|
|
$cachedTarget[] = $cache2->get('foo.b/foo.txt');
|
|
|
|
$cachedTarget[] = $cache2->get('foo.b/bar.txt');
|
|
|
|
$cachedTarget[] = $cache2->get('foo.b/bar');
|
|
|
|
$cachedTarget[] = $cache2->get('foo.b/bar/bar.txt');
|
2015-04-01 13:12:59 +00:00
|
|
|
|
|
|
|
foreach ($cached as $i => $old) {
|
|
|
|
$new = $cachedTarget[$i];
|
|
|
|
$this->assertEquals($old['mtime'], $new['mtime']);
|
|
|
|
$this->assertEquals($old['size'], $new['size']);
|
|
|
|
$this->assertEquals($old['etag'], $new['etag']);
|
|
|
|
$this->assertEquals($old['fileid'], $new['fileid']);
|
2015-05-21 08:57:58 +00:00
|
|
|
$this->assertEquals($old['mimetype'], $new['mimetype']);
|
2015-04-01 13:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-08 20:12:40 +00:00
|
|
|
}
|