2012-10-28 06:26:31 -04:00
|
|
|
<?php
|
2025-04-27 08:38:18 -04:00
|
|
|
|
2012-10-28 06:26:31 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2012-10-28 06:26:31 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\Files\Cache;
|
|
|
|
|
|
2015-11-02 19:52:41 -05:00
|
|
|
/**
|
2016-05-20 09:38:20 -04:00
|
|
|
* Class WatcherTest
|
2015-11-02 19:52:41 -05:00
|
|
|
*
|
|
|
|
|
* @group DB
|
|
|
|
|
*
|
|
|
|
|
* @package Test\Files\Cache
|
|
|
|
|
*/
|
2016-05-20 09:38:20 -04:00
|
|
|
class WatcherTest extends \Test\TestCase {
|
2012-10-28 06:26:31 -04:00
|
|
|
/**
|
2014-02-19 03:52:32 -05:00
|
|
|
* @var \OC\Files\Storage\Storage[] $storages
|
2012-10-28 06:26:31 -04:00
|
|
|
*/
|
2020-03-26 04:30:18 -04:00
|
|
|
private $storages = [];
|
2012-10-28 06:26:31 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-12 09:54:41 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2015-04-08 06:03:55 -04:00
|
|
|
$this->loginAsUser();
|
2012-10-28 06:26:31 -04:00
|
|
|
}
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function tearDown(): void {
|
2012-10-28 06:26:31 -04:00
|
|
|
foreach ($this->storages as $storage) {
|
|
|
|
|
$cache = $storage->getCache();
|
|
|
|
|
$ids = $cache->getAll();
|
|
|
|
|
$cache->clear();
|
|
|
|
|
}
|
2014-11-12 09:54:41 -05:00
|
|
|
|
2015-04-08 06:03:55 -04:00
|
|
|
$this->logout();
|
2014-11-12 09:54:41 -05:00
|
|
|
parent::tearDown();
|
2012-10-28 06:26:31 -04:00
|
|
|
}
|
|
|
|
|
|
2013-06-10 04:17:47 -04:00
|
|
|
/**
|
|
|
|
|
* @medium
|
|
|
|
|
*/
|
2020-04-10 10:51:06 -04:00
|
|
|
public function testWatcher() {
|
2012-10-28 06:26:31 -04:00
|
|
|
$storage = $this->getTestStorage();
|
|
|
|
|
$cache = $storage->getCache();
|
2013-01-01 12:04:29 -05:00
|
|
|
$updater = $storage->getWatcher();
|
2015-07-29 09:10:42 -04:00
|
|
|
$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);
|
2012-10-28 06:26:31 -04:00
|
|
|
|
|
|
|
|
//set the mtime to the past so it can detect an mtime change
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('', ['storage_mtime' => 10]);
|
2012-10-28 06: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']);
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('bar.test', ['storage_mtime' => 10]);
|
2012-10-28 06:26:31 -04:00
|
|
|
$storage->file_put_contents('bar.test', 'test data');
|
|
|
|
|
|
2013-11-05 10:58:05 -05:00
|
|
|
// make sure that PHP can read the new size correctly
|
|
|
|
|
clearstatcache();
|
|
|
|
|
|
2012-10-28 06:26:31 -04:00
|
|
|
$updater->checkUpdate('bar.test');
|
|
|
|
|
$cachedData = $cache->get('bar.test');
|
|
|
|
|
$this->assertEquals(9, $cachedData['size']);
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('folder', ['storage_mtime' => 10]);
|
2012-10-28 06: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 03:31:22 -04:00
|
|
|
/**
|
|
|
|
|
* @medium
|
|
|
|
|
*/
|
2012-12-14 21:10:56 -05:00
|
|
|
public function testFileToFolder() {
|
|
|
|
|
$storage = $this->getTestStorage();
|
|
|
|
|
$cache = $storage->getCache();
|
2013-01-01 12:04:29 -05:00
|
|
|
$updater = $storage->getWatcher();
|
2015-07-29 09:10:42 -04:00
|
|
|
$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);
|
2012-12-14 21:10:56 -05:00
|
|
|
|
|
|
|
|
//set the mtime to the past so it can detect an mtime change
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('', ['storage_mtime' => 10]);
|
2012-12-14 21:10:56 -05:00
|
|
|
|
|
|
|
|
$storage->unlink('foo.txt');
|
2013-01-01 12:04:29 -05:00
|
|
|
$storage->rename('folder', 'foo.txt');
|
2012-12-14 21:10:56 -05:00
|
|
|
$updater->checkUpdate('');
|
|
|
|
|
|
2013-01-01 12:04:29 -05:00
|
|
|
$entry = $cache->get('foo.txt');
|
2012-12-14 21:10:56 -05: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 12:04:29 -05:00
|
|
|
$updater = $storage->getWatcher();
|
2015-07-29 09:10:42 -04:00
|
|
|
$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);
|
2012-12-14 21:10:56 -05:00
|
|
|
|
|
|
|
|
//set the mtime to the past so it can detect an mtime change
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('foo.txt', ['storage_mtime' => 10]);
|
2012-12-14 21:10:56 -05:00
|
|
|
|
|
|
|
|
$storage->unlink('foo.txt');
|
2013-01-01 12:04:29 -05:00
|
|
|
$storage->rename('folder', 'foo.txt');
|
2012-12-14 21:10:56 -05:00
|
|
|
$updater->checkUpdate('foo.txt');
|
|
|
|
|
|
2013-01-01 12:04:29 -05:00
|
|
|
$entry = $cache->get('foo.txt');
|
2012-12-14 21:10:56 -05:00
|
|
|
$this->assertEquals('httpd/unix-directory', $entry['mimetype']);
|
|
|
|
|
$this->assertTrue($cache->inCache('foo.txt/bar.txt'));
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-19 03:52:32 -05:00
|
|
|
public function testPolicyNever() {
|
|
|
|
|
$storage = $this->getTestStorage();
|
|
|
|
|
$cache = $storage->getCache();
|
|
|
|
|
$updater = $storage->getWatcher();
|
|
|
|
|
|
|
|
|
|
//set the mtime to the past so it can detect an mtime change
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('foo.txt', ['storage_mtime' => 10]);
|
2014-02-19 03:52:32 -05:00
|
|
|
|
|
|
|
|
$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_NEVER);
|
|
|
|
|
|
|
|
|
|
$storage->file_put_contents('foo.txt', 'q');
|
|
|
|
|
$this->assertFalse($updater->checkUpdate('foo.txt'));
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('foo.txt', ['storage_mtime' => 20]);
|
2014-02-19 03:52:32 -05:00
|
|
|
$storage->file_put_contents('foo.txt', 'w');
|
|
|
|
|
$this->assertFalse($updater->checkUpdate('foo.txt'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPolicyOnce() {
|
|
|
|
|
$storage = $this->getTestStorage();
|
|
|
|
|
$cache = $storage->getCache();
|
|
|
|
|
$updater = $storage->getWatcher();
|
|
|
|
|
|
|
|
|
|
//set the mtime to the past so it can detect an mtime change
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('foo.txt', ['storage_mtime' => 10]);
|
2014-02-19 03:52:32 -05:00
|
|
|
|
|
|
|
|
$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE);
|
|
|
|
|
|
|
|
|
|
$storage->file_put_contents('foo.txt', 'q');
|
|
|
|
|
$this->assertTrue($updater->checkUpdate('foo.txt'));
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('foo.txt', ['storage_mtime' => 20]);
|
2014-02-19 03:52:32 -05:00
|
|
|
$storage->file_put_contents('foo.txt', 'w');
|
|
|
|
|
$this->assertFalse($updater->checkUpdate('foo.txt'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPolicyAlways() {
|
|
|
|
|
$storage = $this->getTestStorage();
|
|
|
|
|
$cache = $storage->getCache();
|
|
|
|
|
$updater = $storage->getWatcher();
|
|
|
|
|
|
|
|
|
|
//set the mtime to the past so it can detect an mtime change
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('foo.txt', ['storage_mtime' => 10]);
|
2014-02-19 03:52:32 -05:00
|
|
|
|
|
|
|
|
$updater->setPolicy(\OC\Files\Cache\Watcher::CHECK_ALWAYS);
|
|
|
|
|
|
|
|
|
|
$storage->file_put_contents('foo.txt', 'q');
|
|
|
|
|
$this->assertTrue($updater->checkUpdate('foo.txt'));
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$cache->put('foo.txt', ['storage_mtime' => 20]);
|
2014-02-19 03:52:32 -05:00
|
|
|
$storage->file_put_contents('foo.txt', 'w');
|
|
|
|
|
$this->assertTrue($updater->checkUpdate('foo.txt'));
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-28 06:26:31 -04:00
|
|
|
/**
|
|
|
|
|
* @param bool $scan
|
|
|
|
|
* @return \OC\Files\Storage\Storage
|
|
|
|
|
*/
|
|
|
|
|
private function getTestStorage($scan = true) {
|
2020-03-26 04:30:18 -04:00
|
|
|
$storage = new \OC\Files\Storage\Temporary([]);
|
2012-10-28 06:26:31 -04:00
|
|
|
$textData = "dummy file data\n";
|
2018-08-28 09:58:27 -04:00
|
|
|
$imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo/logo.png');
|
2012-10-28 06:26:31 -04:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
}
|