2012-10-03 13:24:49 +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;
|
|
|
|
|
2013-01-27 00:41:14 +04:00
|
|
|
class Scanner extends \PHPUnit_Framework_TestCase {
|
2012-10-03 13:24:49 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
*/
|
|
|
|
private $storage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Cache\Scanner $scanner
|
|
|
|
*/
|
|
|
|
private $scanner;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Cache\Cache $cache
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
function testFile() {
|
|
|
|
$data = "dummy file data\n";
|
|
|
|
$this->storage->file_put_contents('foo.txt', $data);
|
|
|
|
$this->scanner->scanFile('foo.txt');
|
|
|
|
|
2013-01-27 00:41:14 +04:00
|
|
|
$this->assertEquals($this->cache->inCache('foo.txt'), true);
|
2012-10-03 13:24:49 +04:00
|
|
|
$cachedData = $this->cache->get('foo.txt');
|
2013-01-27 00:41:14 +04:00
|
|
|
$this->assertEquals($cachedData['size'], strlen($data));
|
|
|
|
$this->assertEquals($cachedData['mimetype'], 'text/plain');
|
|
|
|
$this->assertNotEquals($cachedData['parent'], -1); //parent folders should be scanned automatically
|
2012-10-03 13:40:09 +04:00
|
|
|
|
|
|
|
$data = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
|
|
|
|
$this->storage->file_put_contents('foo.png', $data);
|
|
|
|
$this->scanner->scanFile('foo.png');
|
|
|
|
|
2013-01-27 00:41:14 +04:00
|
|
|
$this->assertEquals($this->cache->inCache('foo.png'), true);
|
2012-10-03 13:40:09 +04:00
|
|
|
$cachedData = $this->cache->get('foo.png');
|
2013-01-27 00:41:14 +04:00
|
|
|
$this->assertEquals($cachedData['size'], strlen($data));
|
|
|
|
$this->assertEquals($cachedData['mimetype'], 'image/png');
|
2012-10-03 13:40:09 +04:00
|
|
|
}
|
|
|
|
|
2012-10-03 15:07:19 +04:00
|
|
|
private function fillTestFolders() {
|
2012-10-03 13:40:09 +04:00
|
|
|
$textData = "dummy file data\n";
|
|
|
|
$imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
|
2012-10-03 15:07:19 +04:00
|
|
|
$this->storage->mkdir('folder');
|
2012-10-03 13:40:09 +04:00
|
|
|
$this->storage->file_put_contents('foo.txt', $textData);
|
|
|
|
$this->storage->file_put_contents('foo.png', $imgData);
|
2012-10-03 15:07:19 +04:00
|
|
|
$this->storage->file_put_contents('folder/bar.txt', $textData);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testFolder() {
|
|
|
|
$this->fillTestFolders();
|
2012-10-03 13:40:09 +04:00
|
|
|
|
|
|
|
$this->scanner->scan('');
|
2013-01-27 00:41:14 +04:00
|
|
|
$this->assertEquals($this->cache->inCache(''), true);
|
|
|
|
$this->assertEquals($this->cache->inCache('foo.txt'), true);
|
|
|
|
$this->assertEquals($this->cache->inCache('foo.png'), true);
|
|
|
|
$this->assertEquals($this->cache->inCache('folder'), true);
|
|
|
|
$this->assertEquals($this->cache->inCache('folder/bar.txt'), true);
|
2012-10-03 15:07:19 +04:00
|
|
|
|
|
|
|
$cachedDataText = $this->cache->get('foo.txt');
|
|
|
|
$cachedDataText2 = $this->cache->get('foo.txt');
|
|
|
|
$cachedDataImage = $this->cache->get('foo.png');
|
|
|
|
$cachedDataFolder = $this->cache->get('');
|
|
|
|
$cachedDataFolder2 = $this->cache->get('folder');
|
|
|
|
|
2013-01-27 00:41:14 +04:00
|
|
|
$this->assertEquals($cachedDataImage['parent'], $cachedDataText['parent']);
|
|
|
|
$this->assertEquals($cachedDataFolder['fileid'], $cachedDataImage['parent']);
|
|
|
|
$this->assertEquals($cachedDataFolder['size'], $cachedDataImage['size'] + $cachedDataText['size'] + $cachedDataText2['size']);
|
|
|
|
$this->assertEquals($cachedDataFolder2['size'], $cachedDataText2['size']);
|
2012-10-03 15:07:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function testShallow() {
|
|
|
|
$this->fillTestFolders();
|
|
|
|
|
|
|
|
$this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
|
2013-01-27 00:41:14 +04:00
|
|
|
$this->assertEquals($this->cache->inCache(''), true);
|
|
|
|
$this->assertEquals($this->cache->inCache('foo.txt'), true);
|
|
|
|
$this->assertEquals($this->cache->inCache('foo.png'), true);
|
|
|
|
$this->assertEquals($this->cache->inCache('folder'), true);
|
|
|
|
$this->assertEquals($this->cache->inCache('folder/bar.txt'), false);
|
2012-10-03 15:07:19 +04:00
|
|
|
|
|
|
|
$cachedDataFolder = $this->cache->get('');
|
|
|
|
$cachedDataFolder2 = $this->cache->get('folder');
|
|
|
|
|
2013-01-27 00:41:14 +04:00
|
|
|
$this->assertEquals(-1, $cachedDataFolder['size']);
|
|
|
|
$this->assertEquals(-1, $cachedDataFolder2['size']);
|
2012-10-03 20:19:45 +04:00
|
|
|
|
|
|
|
$this->scanner->scan('folder', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
|
|
|
|
|
|
|
|
$cachedDataFolder2 = $this->cache->get('folder');
|
|
|
|
|
2013-01-27 00:41:14 +04:00
|
|
|
$this->assertNotEquals($cachedDataFolder2['size'], -1);
|
2012-10-27 20:05:40 +04:00
|
|
|
|
2012-11-08 21:10:54 +04:00
|
|
|
$this->cache->correctFolderSize('folder');
|
2012-10-27 20:05:40 +04:00
|
|
|
|
|
|
|
$cachedDataFolder = $this->cache->get('');
|
2013-01-27 00:41:14 +04:00
|
|
|
$this->assertNotEquals($cachedDataFolder['size'], -1);
|
2012-10-03 13:24:49 +04:00
|
|
|
}
|
|
|
|
|
2012-11-22 02:18:58 +04:00
|
|
|
function testBackgroundScan(){
|
|
|
|
$this->fillTestFolders();
|
|
|
|
$this->storage->mkdir('folder2');
|
|
|
|
$this->storage->file_put_contents('folder2/bar.txt', 'foobar');
|
|
|
|
|
|
|
|
$this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
|
|
|
|
$this->assertFalse($this->cache->inCache('folder/bar.txt'));
|
|
|
|
$this->assertFalse($this->cache->inCache('folder/2bar.txt'));
|
|
|
|
$cachedData = $this->cache->get('');
|
|
|
|
$this->assertEquals(-1, $cachedData['size']);
|
|
|
|
|
|
|
|
$this->scanner->backgroundScan();
|
|
|
|
|
|
|
|
$this->assertTrue($this->cache->inCache('folder/bar.txt'));
|
|
|
|
$this->assertTrue($this->cache->inCache('folder/bar.txt'));
|
|
|
|
|
|
|
|
$cachedData = $this->cache->get('');
|
|
|
|
$this->assertnotEquals(-1, $cachedData['size']);
|
|
|
|
|
|
|
|
$this->assertFalse($this->cache->getIncomplete());
|
|
|
|
}
|
|
|
|
|
2012-10-03 13:24:49 +04:00
|
|
|
function setUp() {
|
|
|
|
$this->storage = new \OC\Files\Storage\Temporary(array());
|
|
|
|
$this->scanner = new \OC\Files\Cache\Scanner($this->storage);
|
|
|
|
$this->cache = new \OC\Files\Cache\Cache($this->storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown() {
|
2012-10-27 12:01:20 +04:00
|
|
|
$ids = $this->cache->getAll();
|
2012-11-15 03:57:30 +04:00
|
|
|
$permissionsCache = $this->storage->getPermissionsCache();
|
|
|
|
$permissionsCache->removeMultiple($ids, \OC_User::getUser());
|
2012-10-03 13:40:09 +04:00
|
|
|
$this->cache->clear();
|
2012-10-03 13:24:49 +04:00
|
|
|
}
|
|
|
|
}
|