2012-10-28 14:26:31 +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 Watcher extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage[] $storages;
|
|
|
|
*/
|
|
|
|
private $storages = array();
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
\OC\Files\Filesystem::clearMounts();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
foreach ($this->storages as $storage) {
|
|
|
|
$cache = $storage->getCache();
|
|
|
|
$ids = $cache->getAll();
|
2012-11-15 03:57:30 +04:00
|
|
|
$permissionsCache = $storage->getPermissionsCache();
|
|
|
|
$permissionsCache->removeMultiple($ids, \OC_User::getUser());
|
2012-10-28 14:26:31 +04:00
|
|
|
$cache->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 12:17:47 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2012-10-28 14:26:31 +04:00
|
|
|
function testWatcher() {
|
|
|
|
$storage = $this->getTestStorage();
|
|
|
|
$cache = $storage->getCache();
|
2013-01-01 21:04:29 +04:00
|
|
|
$updater = $storage->getWatcher();
|
2012-10-28 14:26:31 +04:00
|
|
|
|
|
|
|
//set the mtime to the past so it can detect an mtime change
|
2013-02-10 15:27:35 +04:00
|
|
|
$cache->put('', array('storage_mtime' => 10));
|
2012-10-28 14:26:31 +04:00
|
|
|
|
|
|
|
$this->assertTrue($cache->inCache('folder/bar.txt'));
|
|
|
|
$this->assertTrue($cache->inCache('folder/bar2.txt'));
|
|
|
|
|
|
|
|
$this->assertFalse($cache->inCache('bar.test'));
|
|
|
|
$storage->file_put_contents('bar.test', 'foo');
|
|
|
|
$updater->checkUpdate('');
|
|
|
|
$this->assertTrue($cache->inCache('bar.test'));
|
|
|
|
$cachedData = $cache->get('bar.test');
|
|
|
|
$this->assertEquals(3, $cachedData['size']);
|
|
|
|
|
2013-02-10 15:27:35 +04:00
|
|
|
$cache->put('bar.test', array('storage_mtime' => 10));
|
2012-10-28 14:26:31 +04:00
|
|
|
$storage->file_put_contents('bar.test', 'test data');
|
|
|
|
|
2013-11-05 19:58:05 +04:00
|
|
|
// make sure that PHP can read the new size correctly
|
|
|
|
clearstatcache();
|
|
|
|
|
2012-10-28 14:26:31 +04:00
|
|
|
$updater->checkUpdate('bar.test');
|
|
|
|
$cachedData = $cache->get('bar.test');
|
|
|
|
$this->assertEquals(9, $cachedData['size']);
|
|
|
|
|
2013-02-10 15:27:35 +04:00
|
|
|
$cache->put('folder', array('storage_mtime' => 10));
|
2012-10-28 14:26:31 +04:00
|
|
|
|
|
|
|
$storage->unlink('folder/bar2.txt');
|
|
|
|
$updater->checkUpdate('folder');
|
|
|
|
|
|
|
|
$this->assertTrue($cache->inCache('folder/bar.txt'));
|
|
|
|
$this->assertFalse($cache->inCache('folder/bar2.txt'));
|
|
|
|
}
|
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2012-12-15 06:10:56 +04:00
|
|
|
public function testFileToFolder() {
|
|
|
|
$storage = $this->getTestStorage();
|
|
|
|
$cache = $storage->getCache();
|
2013-01-01 21:04:29 +04:00
|
|
|
$updater = $storage->getWatcher();
|
2012-12-15 06:10:56 +04:00
|
|
|
|
|
|
|
//set the mtime to the past so it can detect an mtime change
|
2013-02-10 15:27:35 +04:00
|
|
|
$cache->put('', array('storage_mtime' => 10));
|
2012-12-15 06:10:56 +04:00
|
|
|
|
|
|
|
$storage->unlink('foo.txt');
|
2013-01-01 21:04:29 +04:00
|
|
|
$storage->rename('folder', 'foo.txt');
|
2012-12-15 06:10:56 +04:00
|
|
|
$updater->checkUpdate('');
|
|
|
|
|
2013-01-01 21:04:29 +04:00
|
|
|
$entry = $cache->get('foo.txt');
|
2012-12-15 06:10:56 +04:00
|
|
|
$this->assertEquals('httpd/unix-directory', $entry['mimetype']);
|
|
|
|
$this->assertFalse($cache->inCache('folder'));
|
|
|
|
$this->assertFalse($cache->inCache('folder/bar.txt'));
|
|
|
|
|
|
|
|
$storage = $this->getTestStorage();
|
|
|
|
$cache = $storage->getCache();
|
2013-01-01 21:04:29 +04:00
|
|
|
$updater = $storage->getWatcher();
|
2012-12-15 06:10:56 +04:00
|
|
|
|
|
|
|
//set the mtime to the past so it can detect an mtime change
|
2013-02-10 15:27:35 +04:00
|
|
|
$cache->put('foo.txt', array('storage_mtime' => 10));
|
2012-12-15 06:10:56 +04:00
|
|
|
|
|
|
|
$storage->unlink('foo.txt');
|
2013-01-01 21:04:29 +04:00
|
|
|
$storage->rename('folder', 'foo.txt');
|
2012-12-15 06:10:56 +04:00
|
|
|
$updater->checkUpdate('foo.txt');
|
|
|
|
|
2013-01-01 21:04:29 +04:00
|
|
|
$entry = $cache->get('foo.txt');
|
2012-12-15 06:10:56 +04:00
|
|
|
$this->assertEquals('httpd/unix-directory', $entry['mimetype']);
|
|
|
|
$this->assertTrue($cache->inCache('foo.txt/bar.txt'));
|
|
|
|
}
|
|
|
|
|
2012-10-28 14:26:31 +04:00
|
|
|
/**
|
|
|
|
* @param bool $scan
|
|
|
|
* @return \OC\Files\Storage\Storage
|
|
|
|
*/
|
|
|
|
private function getTestStorage($scan = true) {
|
|
|
|
$storage = new \OC\Files\Storage\Temporary(array());
|
|
|
|
$textData = "dummy file data\n";
|
|
|
|
$imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
|
|
|
|
$storage->mkdir('folder');
|
|
|
|
$storage->file_put_contents('foo.txt', $textData);
|
|
|
|
$storage->file_put_contents('foo.png', $imgData);
|
|
|
|
$storage->file_put_contents('folder/bar.txt', $textData);
|
|
|
|
$storage->file_put_contents('folder/bar2.txt', $textData);
|
|
|
|
|
|
|
|
if ($scan) {
|
|
|
|
$scanner = $storage->getScanner();
|
|
|
|
$scanner->scan('');
|
|
|
|
}
|
|
|
|
$this->storages[] = $storage;
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
}
|