From 3e7a86c6ecd332c268e690399a015ab618e87754 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 6 Aug 2013 15:59:06 +0200 Subject: remove deleted files while scanning --- tests/lib/files/cache/scanner.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests') diff --git a/tests/lib/files/cache/scanner.php b/tests/lib/files/cache/scanner.php index 263ceadccc7..bb90adce5c1 100644 --- a/tests/lib/files/cache/scanner.php +++ b/tests/lib/files/cache/scanner.php @@ -166,6 +166,16 @@ class Scanner extends \PHPUnit_Framework_TestCase { $this->assertFalse($this->cache->inCache('folder/bar.txt')); } + public function testScanRemovedFile(){ + $this->fillTestFolders(); + + $this->scanner->scan(''); + $this->assertTrue($this->cache->inCache('folder/bar.txt')); + $this->storage->unlink('folder/bar.txt'); + $this->scanner->scanFile('folder/bar.txt'); + $this->assertFalse($this->cache->inCache('folder/bar.txt')); + } + function setUp() { $this->storage = new \OC\Files\Storage\Temporary(array()); $this->scanner = new \OC\Files\Cache\Scanner($this->storage); -- cgit v1.2.3 From 3c026b7cf601c0b83dd02436f17714fcf48cb9a8 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Mon, 16 Sep 2013 12:09:15 +0200 Subject: recreate an etag within the scanner if the cache contains an empty etag --- lib/files/cache/scanner.php | 8 +++++++- tests/lib/files/cache/scanner.php | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/lib/files/cache/scanner.php b/lib/files/cache/scanner.php index 9d180820e9d..78cab6ed2da 100644 --- a/lib/files/cache/scanner.php +++ b/lib/files/cache/scanner.php @@ -97,13 +97,19 @@ class Scanner extends BasicEmitter { } $newData = $data; if ($reuseExisting and $cacheData = $this->cache->get($file)) { + // prevent empty etag + $etag = $cacheData['etag']; + if (empty($etag)) { + $etag = $data['etag']; + } + // only reuse data if the file hasn't explicitly changed if (isset($data['mtime']) && isset($cacheData['mtime']) && $data['mtime'] === $cacheData['mtime']) { if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) { $data['size'] = $cacheData['size']; } if ($reuseExisting & self::REUSE_ETAG) { - $data['etag'] = $cacheData['etag']; + $data['etag'] = $etag; } } // Only update metadata that has changed diff --git a/tests/lib/files/cache/scanner.php b/tests/lib/files/cache/scanner.php index f6deb93a49e..fa1b3406040 100644 --- a/tests/lib/files/cache/scanner.php +++ b/tests/lib/files/cache/scanner.php @@ -184,6 +184,23 @@ class Scanner extends \PHPUnit_Framework_TestCase { $this->assertFalse($this->cache->inCache('folder/bar.txt')); } + public function testETagRecreation() { + $this->fillTestFolders(); + + $this->scanner->scan(''); + + // manipulate etag to simulate an empty etag + $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG); + $data['etag'] = ''; + $this->cache->put('', $data); + + // rescan + $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG); + $newData = $this->cache->get(''); + $this->assertNotEmpty($newData['etag']); + + } + function setUp() { $this->storage = new \OC\Files\Storage\Temporary(array()); $this->scanner = new \OC\Files\Cache\Scanner($this->storage); -- cgit v1.2.3 From 07714d9a72bbc4d9bdc25a8c42d83b5a70fb5be3 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 14 Sep 2013 17:56:55 +0200 Subject: Tests whether expired/valid link share is still accessible. --- tests/lib/share/share.php | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'tests') diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php index e02b0e4354d..8e9eef65d32 100644 --- a/tests/lib/share/share.php +++ b/tests/lib/share/share.php @@ -535,4 +535,52 @@ class Test_Share extends PHPUnit_Framework_TestCase { 'Failed asserting that user 3 still has access to test.txt after expiration date has been set.' ); } + + protected function getShareByValidToken($token) { + $row = OCP\Share::getShareByToken($token); + $this->assertInternalType( + 'array', + $row, + "Failed asserting that a share for token $token exists." + ); + return $row; + } + + public function testShareItemWithLink() { + OC_User::setUserId($this->user1); + $token = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, OCP\PERMISSION_READ); + $this->assertInternalType( + 'string', + $token, + 'Failed asserting that user 1 successfully shared text.txt as link with token.' + ); + + // testGetShareByTokenNoExpiration + $row = $this->getShareByValidToken($token); + $this->assertEmpty( + $row['expiration'], + 'Failed asserting that the returned row does not have an expiration date.' + ); + + // testGetShareByTokenExpirationValid + $this->assertTrue( + OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture), + 'Failed asserting that user 1 successfully set a future expiration date for the test.txt share.' + ); + $row = $this->getShareByValidToken($token); + $this->assertNotEmpty( + $row['expiration'], + 'Failed asserting that the returned row has an expiration date.' + ); + + // testGetShareByTokenExpirationExpired + $this->assertTrue( + OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast), + 'Failed asserting that user 1 successfully set a past expiration date for the test.txt share.' + ); + $this->assertFalse( + OCP\Share::getShareByToken($token), + 'Failed asserting that an expired share could not be found.' + ); + } } -- cgit v1.2.3 From c8f9efeb94b136ed0906cefe946629a091796ff2 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Mon, 16 Sep 2013 23:32:17 +0200 Subject: etag changes are now propagated up the file tree --- lib/files/cache/scanner.php | 14 ++++++++++++++ tests/lib/files/cache/scanner.php | 21 +++++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/lib/files/cache/scanner.php b/lib/files/cache/scanner.php index 78cab6ed2da..fdbce0d51fe 100644 --- a/lib/files/cache/scanner.php +++ b/lib/files/cache/scanner.php @@ -99,8 +99,10 @@ class Scanner extends BasicEmitter { if ($reuseExisting and $cacheData = $this->cache->get($file)) { // prevent empty etag $etag = $cacheData['etag']; + $propagateETagChange = false; if (empty($etag)) { $etag = $data['etag']; + $propagateETagChange = true; } // only reuse data if the file hasn't explicitly changed @@ -110,6 +112,18 @@ class Scanner extends BasicEmitter { } if ($reuseExisting & self::REUSE_ETAG) { $data['etag'] = $etag; + if ($propagateETagChange) { + $parent = $file; + while ($parent !== '') { + $parent = dirname($parent); + if ($parent === '.') { + $parent = ''; + } + $parentCacheData = $this->cache->get($parent); + $parentCacheData['etag'] = $this->storage->getETag($parent); + $this->cache->put($parent, $parentCacheData); + } + } } } // Only update metadata that has changed diff --git a/tests/lib/files/cache/scanner.php b/tests/lib/files/cache/scanner.php index fa1b3406040..b137799bbcf 100644 --- a/tests/lib/files/cache/scanner.php +++ b/tests/lib/files/cache/scanner.php @@ -187,17 +187,26 @@ class Scanner extends \PHPUnit_Framework_TestCase { public function testETagRecreation() { $this->fillTestFolders(); - $this->scanner->scan(''); + $this->scanner->scan('folder/bar.txt'); // manipulate etag to simulate an empty etag $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG); - $data['etag'] = ''; - $this->cache->put('', $data); + $data0 = $this->cache->get('folder/bar.txt'); + $data1 = $this->cache->get('folder'); + $data2 = $this->cache->get(''); + $data0['etag'] = ''; + $this->cache->put('folder/bar.txt', $data0); // rescan - $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG); - $newData = $this->cache->get(''); - $this->assertNotEmpty($newData['etag']); + $this->scanner->scan('folder/bar.txt', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG); + + // verify cache content + $newData0 = $this->cache->get('folder/bar.txt'); + $newData1 = $this->cache->get('folder'); + $newData2 = $this->cache->get(''); + $this->assertNotEmpty($newData0['etag']); + $this->assertNotEquals($data1['etag'], $newData1['etag']); + $this->assertNotEquals($data2['etag'], $newData2['etag']); } -- cgit v1.2.3 From 445d34a2a90295c11ace24171c20a93991ebfa87 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sun, 3 Mar 2013 12:04:29 +0100 Subject: Convert OC_Preference to object interface --- lib/legacy/preferences.php | 137 ++++++++++++++++++++++++++++++++++ lib/preferences.php | 153 ++++++++++++++++++++------------------ tests/lib/preferences.php | 179 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 397 insertions(+), 72 deletions(-) create mode 100644 lib/legacy/preferences.php (limited to 'tests') diff --git a/lib/legacy/preferences.php b/lib/legacy/preferences.php new file mode 100644 index 00000000000..8bfac849a4a --- /dev/null +++ b/lib/legacy/preferences.php @@ -0,0 +1,137 @@ +. + * + */ + +/** + * This class provides an easy way for storing user preferences. + */ +OC_Preferences::$object = new \OC\Preferences(OC_DB::getConnection()); +class OC_Preferences{ + public static $object; + /** + * @brief Get all users using the preferences + * @return array with user ids + * + * This function returns a list of all users that have at least one entry + * in the preferences table. + */ + public static function getUsers() { + return self::$object->getUsers(); + } + + /** + * @brief Get all apps of a user + * @param string $user user + * @return array with app ids + * + * This function returns a list of all apps of the user that have at least + * one entry in the preferences table. + */ + public static function getApps( $user ) { + return self::$object->getApps( $user ); + } + + /** + * @brief Get the available keys for an app + * @param string $user user + * @param string $app the app we are looking for + * @return array with key names + * + * This function gets all keys of an app of an user. Please note that the + * values are not returned. + */ + public static function getKeys( $user, $app ) { + return self::$object->getKeys( $user, $app ); + } + + /** + * @brief Gets the preference + * @param string $user user + * @param string $app app + * @param string $key key + * @param string $default = null, default value if the key does not exist + * @return string the value or $default + * + * This function gets a value from the preferences table. If the key does + * not exist the default value will be returned + */ + public static function getValue( $user, $app, $key, $default = null ) { + return self::$object->getValue( $user, $app, $key, $default ); + } + + /** + * @brief sets a value in the preferences + * @param string $user user + * @param string $app app + * @param string $key key + * @param string $value value + * + * Adds a value to the preferences. If the key did not exist before, it + * will be added automagically. + */ + public static function setValue( $user, $app, $key, $value ) { + self::$object->setValue( $user, $app, $key, $value ); + } + + /** + * @brief Deletes a key + * @param string $user user + * @param string $app app + * @param string $key key + * + * Deletes a key. + */ + public function deleteKey( $user, $app, $key ) { + self::$object->deleteKey( $user, $app, $key ); + } + + /** + * @brief Remove app of user from preferences + * @param string $user user + * @param string $app app + * + * Removes all keys in preferences belonging to the app and the user. + */ + public static function deleteApp( $user, $app ) { + self::$object->deleteApp( $user, $app ); + } + + /** + * @brief Remove user from preferences + * @param string $user user + * + * Removes all keys in preferences belonging to the user. + */ + public static function deleteUser( $user ) { + self::$object->deleteUser( $user ); + } + + /** + * @brief Remove app from all users + * @param string $app app + * + * Removes all keys in preferences belonging to the app. + */ + public static function deleteAppFromAllUsers( $app ) { + self::$object->deleteAppFromAllUsers( $app ); + } +} diff --git a/lib/preferences.php b/lib/preferences.php index 11ca760830e..359d9a83589 100644 --- a/lib/preferences.php +++ b/lib/preferences.php @@ -34,10 +34,21 @@ * */ +namespace OC; + +use \OC\DB\Connection; + + /** * This class provides an easy way for storing user preferences. */ -class OC_Preferences{ +class Preferences { + protected $conn; + + public function __construct(Connection $conn) { + $this->conn = $conn; + } + /** * @brief Get all users using the preferences * @return array with user ids @@ -45,14 +56,13 @@ class OC_Preferences{ * This function returns a list of all users that have at least one entry * in the preferences table. */ - public static function getUsers() { - // No need for more comments - $query = OC_DB::prepare( 'SELECT DISTINCT( `userid` ) FROM `*PREFIX*preferences`' ); - $result = $query->execute(); + public function getUsers() { + $query = 'SELECT DISTINCT `userid` FROM `*PREFIX*preferences`'; + $result = $this->conn->executeQuery( $query ); $users = array(); - while( $row = $result->fetchRow()) { - $users[] = $row["userid"]; + while( $userid = $result->fetchColumn()) { + $users[] = $userid; } return $users; @@ -66,14 +76,13 @@ class OC_Preferences{ * This function returns a list of all apps of the user that have at least * one entry in the preferences table. */ - public static function getApps( $user ) { - // No need for more comments - $query = OC_DB::prepare( 'SELECT DISTINCT( `appid` ) FROM `*PREFIX*preferences` WHERE `userid` = ?' ); - $result = $query->execute( array( $user )); + public function getApps( $user ) { + $query = 'SELECT DISTINCT `appid` FROM `*PREFIX*preferences` WHERE `userid` = ?'; + $result = $this->conn->executeQuery( $query, array( $user ) ); $apps = array(); - while( $row = $result->fetchRow()) { - $apps[] = $row["appid"]; + while( $appid = $result->fetchColumn()) { + $apps[] = $appid; } return $apps; @@ -88,14 +97,13 @@ class OC_Preferences{ * This function gets all keys of an app of an user. Please note that the * values are not returned. */ - public static function getKeys( $user, $app ) { - // No need for more comments - $query = OC_DB::prepare( 'SELECT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?' ); - $result = $query->execute( array( $user, $app )); + public function getKeys( $user, $app ) { + $query = 'SELECT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?'; + $result = $this->conn->executeQuery( $query, array( $user, $app )); $keys = array(); - while( $row = $result->fetchRow()) { - $keys[] = $row["configkey"]; + while( $key = $result->fetchColumn()) { + $keys[] = $key; } return $keys; @@ -112,16 +120,14 @@ class OC_Preferences{ * This function gets a value from the preferences table. If the key does * not exist the default value will be returned */ - public static function getValue( $user, $app, $key, $default = null ) { + public function getValue( $user, $app, $key, $default = null ) { // Try to fetch the value, return default if not exists. - $query = OC_DB::prepare( 'SELECT `configvalue` FROM `*PREFIX*preferences`' - .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?' ); - $result = $query->execute( array( $user, $app, $key )); - - $row = $result->fetchRow(); + $query = 'SELECT `configvalue` FROM `*PREFIX*preferences`' + .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'; + $row = $this->conn->fetchAssoc( $query, array( $user, $app, $key )); if($row) { return $row["configvalue"]; - }else{ + } else { return $default; } } @@ -132,29 +138,36 @@ class OC_Preferences{ * @param string $app app * @param string $key key * @param string $value value - * @return bool * * Adds a value to the preferences. If the key did not exist before, it * will be added automagically. */ - public static function setValue( $user, $app, $key, $value ) { + public function setValue( $user, $app, $key, $value ) { // Check if the key does exist - $query = OC_DB::prepare( 'SELECT `configvalue` FROM `*PREFIX*preferences`' - .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?' ); - $values=$query->execute(array($user, $app, $key))->fetchAll(); - $exists=(count($values)>0); + $query = 'SELECT COUNT(*) FROM `*PREFIX*preferences`' + .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'; + $count = $this->conn->fetchColumn( $query, array( $user, $app, $key )); + $exists = $count > 0; if( !$exists ) { - $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*preferences`' - .' ( `userid`, `appid`, `configkey`, `configvalue` ) VALUES( ?, ?, ?, ? )' ); - $query->execute( array( $user, $app, $key, $value )); + $data = array( + 'userid' => $user, + 'appid' => $app, + 'configkey' => $key, + 'configvalue' => $value, + ); + $this->conn->insert('*PREFIX*preferences', $data); + } else { + $data = array( + 'configvalue' => $value, + ); + $where = array( + 'userid' => $user, + 'appid' => $app, + 'configkey' => $key, + ); + $this->conn->update('*PREFIX*preferences', $data, $where); } - else{ - $query = OC_DB::prepare( 'UPDATE `*PREFIX*preferences` SET `configvalue` = ?' - .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?' ); - $query->execute( array( $value, $user, $app, $key )); - } - return true; } /** @@ -162,62 +175,58 @@ class OC_Preferences{ * @param string $user user * @param string $app app * @param string $key key - * @return bool * * Deletes a key. */ - public static function deleteKey( $user, $app, $key ) { - // No need for more comments - $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences`' - .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?' ); - $query->execute( array( $user, $app, $key )); - - return true; + public function deleteKey( $user, $app, $key ) { + $where = array( + 'userid' => $user, + 'appid' => $app, + 'configkey' => $key, + ); + $this->conn->delete('*PREFIX*preferences', $where); } /** * @brief Remove app of user from preferences * @param string $user user * @param string $app app - * @return bool * - * Removes all keys in appconfig belonging to the app and the user. + * Removes all keys in preferences belonging to the app and the user. */ - public static function deleteApp( $user, $app ) { - // No need for more comments - $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?' ); - $query->execute( array( $user, $app )); - - return true; + public function deleteApp( $user, $app ) { + $where = array( + 'userid' => $user, + 'appid' => $app, + ); + $this->conn->delete('*PREFIX*preferences', $where); } /** * @brief Remove user from preferences * @param string $user user - * @return bool * - * Removes all keys in appconfig belonging to the user. + * Removes all keys in preferences belonging to the user. */ - public static function deleteUser( $user ) { - // No need for more comments - $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?' ); - $query->execute( array( $user )); - - return true; + public function deleteUser( $user ) { + $where = array( + 'userid' => $user, + ); + $this->conn->delete('*PREFIX*preferences', $where); } /** * @brief Remove app from all users * @param string $app app - * @return bool * * Removes all keys in preferences belonging to the app. */ - public static function deleteAppFromAllUsers( $app ) { - // No need for more comments - $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `appid` = ?' ); - $query->execute( array( $app )); - - return true; + public function deleteAppFromAllUsers( $app ) { + $where = array( + 'appid' => $app, + ); + $this->conn->delete('*PREFIX*preferences', $where); } } + +require_once __DIR__.'/legacy/'.basename(__FILE__); diff --git a/tests/lib/preferences.php b/tests/lib/preferences.php index 612cc81926b..68b794e9ea9 100644 --- a/tests/lib/preferences.php +++ b/tests/lib/preferences.php @@ -1,6 +1,7 @@ + * Copyright (c) 2013 Bart Visscher * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. @@ -124,3 +125,181 @@ class Test_Preferences extends PHPUnit_Framework_TestCase { $this->assertEquals(0, $result->numRows()); } } + +class Test_Preferences_Object extends PHPUnit_Framework_TestCase { + public function testGetUsers() + { + $statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false); + $statementMock->expects($this->exactly(2)) + ->method('fetchColumn') + ->will($this->onConsecutiveCalls('foo', false)); + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->once()) + ->method('executeQuery') + ->with($this->equalTo('SELECT DISTINCT `userid` FROM `*PREFIX*preferences`')) + ->will($this->returnValue($statementMock)); + + $preferences = new OC\Preferences($connectionMock); + $apps = $preferences->getUsers(); + $this->assertEquals(array('foo'), $apps); + } + + public function testGetApps() + { + $statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false); + $statementMock->expects($this->exactly(2)) + ->method('fetchColumn') + ->will($this->onConsecutiveCalls('foo', false)); + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->once()) + ->method('executeQuery') + ->with($this->equalTo('SELECT DISTINCT `appid` FROM `*PREFIX*preferences` WHERE `userid` = ?'), + $this->equalTo(array('bar'))) + ->will($this->returnValue($statementMock)); + + $preferences = new OC\Preferences($connectionMock); + $apps = $preferences->getApps('bar'); + $this->assertEquals(array('foo'), $apps); + } + + public function testGetKeys() + { + $statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false); + $statementMock->expects($this->exactly(2)) + ->method('fetchColumn') + ->will($this->onConsecutiveCalls('foo', false)); + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->once()) + ->method('executeQuery') + ->with($this->equalTo('SELECT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?'), + $this->equalTo(array('bar', 'moo'))) + ->will($this->returnValue($statementMock)); + + $preferences = new OC\Preferences($connectionMock); + $keys = $preferences->getKeys('bar', 'moo'); + $this->assertEquals(array('foo'), $keys); + } + + public function testGetValue() + { + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->exactly(2)) + ->method('fetchAssoc') + ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'), + $this->equalTo(array('grg', 'bar', 'red'))) + ->will($this->onConsecutiveCalls(array('configvalue'=>'foo'), null)); + + $preferences = new OC\Preferences($connectionMock); + $value = $preferences->getValue('grg', 'bar', 'red'); + $this->assertEquals('foo', $value); + $value = $preferences->getValue('grg', 'bar', 'red', 'def'); + $this->assertEquals('def', $value); + } + + public function testSetValue() + { + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->exactly(2)) + ->method('fetchColumn') + ->with($this->equalTo('SELECT COUNT(*) FROM `*PREFIX*preferences`' + .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'), + $this->equalTo(array('grg', 'bar', 'foo'))) + ->will($this->onConsecutiveCalls(0, 1)); + $connectionMock->expects($this->once()) + ->method('insert') + ->with($this->equalTo('*PREFIX*preferences'), + $this->equalTo( + array( + 'userid' => 'grg', + 'appid' => 'bar', + 'configkey' => 'foo', + 'configvalue' => 'v1', + ) + )); + $connectionMock->expects($this->once()) + ->method('update') + ->with($this->equalTo('*PREFIX*preferences'), + $this->equalTo( + array( + 'configvalue' => 'v2', + )), + $this->equalTo( + array( + 'userid' => 'grg', + 'appid' => 'bar', + 'configkey' => 'foo', + ) + )); + + $preferences = new OC\Preferences($connectionMock); + $preferences->setValue('grg', 'bar', 'foo', 'v1'); + $preferences->setValue('grg', 'bar', 'foo', 'v2'); + } + + public function testDeleteKey() + { + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->once()) + ->method('delete') + ->with($this->equalTo('*PREFIX*preferences'), + $this->equalTo( + array( + 'userid' => 'grg', + 'appid' => 'bar', + 'configkey' => 'foo', + ) + )); + + $preferences = new OC\Preferences($connectionMock); + $preferences->deleteKey('grg', 'bar', 'foo'); + } + + public function testDeleteApp() + { + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->once()) + ->method('delete') + ->with($this->equalTo('*PREFIX*preferences'), + $this->equalTo( + array( + 'userid' => 'grg', + 'appid' => 'bar', + ) + )); + + $preferences = new OC\Preferences($connectionMock); + $preferences->deleteApp('grg', 'bar'); + } + + public function testDeleteUser() + { + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->once()) + ->method('delete') + ->with($this->equalTo('*PREFIX*preferences'), + $this->equalTo( + array( + 'userid' => 'grg', + ) + )); + + $preferences = new OC\Preferences($connectionMock); + $preferences->deleteUser('grg'); + } + + public function testDeleteAppFromAllUsers() + { + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->once()) + ->method('delete') + ->with($this->equalTo('*PREFIX*preferences'), + $this->equalTo( + array( + 'appid' => 'bar', + ) + )); + + $preferences = new OC\Preferences($connectionMock); + $preferences->deleteAppFromAllUsers('bar'); + } +} -- cgit v1.2.3 From d84d548618651c0a66bd2696d6547b33ca6b8e87 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Fri, 20 Sep 2013 20:34:17 +0200 Subject: when storing back the data field 'encrypted' it is necessary to cast the boolean to an integer to make pg happy --- lib/files/cache/scanner.php | 2 ++ tests/lib/files/cache/scanner.php | 1 + 2 files changed, 3 insertions(+) (limited to 'tests') diff --git a/lib/files/cache/scanner.php b/lib/files/cache/scanner.php index fdbce0d51fe..d296c606865 100644 --- a/lib/files/cache/scanner.php +++ b/lib/files/cache/scanner.php @@ -121,6 +121,8 @@ class Scanner extends BasicEmitter { } $parentCacheData = $this->cache->get($parent); $parentCacheData['etag'] = $this->storage->getETag($parent); + // the boolean to int conversion is necessary to make pg happy + $parentCacheData['encrypted'] = $parentCacheData['encrypted'] ? 1 : 0; $this->cache->put($parent, $parentCacheData); } } diff --git a/tests/lib/files/cache/scanner.php b/tests/lib/files/cache/scanner.php index b137799bbcf..8112eada17c 100644 --- a/tests/lib/files/cache/scanner.php +++ b/tests/lib/files/cache/scanner.php @@ -195,6 +195,7 @@ class Scanner extends \PHPUnit_Framework_TestCase { $data1 = $this->cache->get('folder'); $data2 = $this->cache->get(''); $data0['etag'] = ''; + $data0['encrypted'] = $data0['encrypted'] ? 1: 0; $this->cache->put('folder/bar.txt', $data0); // rescan -- cgit v1.2.3 From de2e6e137b3be966622b0b608a3b69f6282e2e56 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 21 Sep 2013 00:12:13 +0200 Subject: Do not convert boolean to integer in tests. put() already does this. --- tests/lib/files/cache/scanner.php | 1 - 1 file changed, 1 deletion(-) (limited to 'tests') diff --git a/tests/lib/files/cache/scanner.php b/tests/lib/files/cache/scanner.php index 8112eada17c..b137799bbcf 100644 --- a/tests/lib/files/cache/scanner.php +++ b/tests/lib/files/cache/scanner.php @@ -195,7 +195,6 @@ class Scanner extends \PHPUnit_Framework_TestCase { $data1 = $this->cache->get('folder'); $data2 = $this->cache->get(''); $data0['etag'] = ''; - $data0['encrypted'] = $data0['encrypted'] ? 1: 0; $this->cache->put('folder/bar.txt', $data0); // rescan -- cgit v1.2.3 From a1d4eb1f956148fe9002dd17bdfef3bd66063bf0 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 22 Sep 2013 01:23:18 +0200 Subject: files: when filtering search results, ensure results are children of the fakeroot not just path starting the same --- lib/files/view.php | 14 ++++++++------ tests/lib/files/view.php | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/lib/files/view.php b/lib/files/view.php index 968b755a661..aa08a5f7cc9 100644 --- a/lib/files/view.php +++ b/lib/files/view.php @@ -500,7 +500,7 @@ class View { } else { if ($this->is_dir($path1) && ($dh = $this->opendir($path1))) { $result = $this->mkdir($path2); - if(is_resource($dh)) { + if (is_resource($dh)) { while (($file = readdir($dh)) !== false) { if (!Filesystem::isIgnoredDir($file)) { $result = $this->copy($path1 . '/' . $file, $path2 . '/' . $file); @@ -975,7 +975,7 @@ class View { /** * search for files by mimetype * - * @param string $query + * @param string $mimetype * @return array */ public function searchByMime($mimetype) { @@ -998,7 +998,7 @@ class View { $results = $cache->$method($query); foreach ($results as $result) { - if (substr($mountPoint . $result['path'], 0, $rootLength) === $this->fakeRoot) { + if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { $result['path'] = substr($mountPoint . $result['path'], $rootLength); $files[] = $result; } @@ -1012,9 +1012,11 @@ class View { $relativeMountPoint = substr($mountPoint, $rootLength); $results = $cache->$method($query); - foreach ($results as $result) { - $result['path'] = $relativeMountPoint . $result['path']; - $files[] = $result; + if ($results) { + foreach ($results as $result) { + $result['path'] = $relativeMountPoint . $result['path']; + $files[] = $result; + } } } } diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php index 0de436f570a..3043f132b73 100644 --- a/tests/lib/files/view.php +++ b/tests/lib/files/view.php @@ -354,8 +354,22 @@ class View extends \PHPUnit_Framework_TestCase { $this->hookPath = $params['path']; } + public function testSearchNotOutsideView() { + $storage1 = $this->getTestStorage(); + \OC\Files\Filesystem::mount($storage1, array(), '/'); + $storage1->rename('folder', 'foo'); + $scanner = $storage1->getScanner(); + $scanner->scan(''); + + $view = new \OC\Files\View('/foo'); + + $result = $view->search('.txt'); + $this->assertCount(1, $result); + } + /** * @param bool $scan + * @param string $class * @return \OC\Files\Storage\Storage */ private function getTestStorage($scan = true, $class = '\OC\Files\Storage\Temporary') { -- cgit v1.2.3 From d9a36ee82ec3bffb83515248b69c287f5fd0170f Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 23 Sep 2013 12:45:02 +0200 Subject: Move setUp() and tearDown() up in tests/lib/files/cache/scanner.php. --- tests/lib/files/cache/scanner.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'tests') diff --git a/tests/lib/files/cache/scanner.php b/tests/lib/files/cache/scanner.php index 6956e7aa948..3f3a045377a 100644 --- a/tests/lib/files/cache/scanner.php +++ b/tests/lib/files/cache/scanner.php @@ -24,6 +24,21 @@ class Scanner extends \PHPUnit_Framework_TestCase { */ private $cache; + 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() { + if ($this->cache) { + $ids = $this->cache->getAll(); + $permissionsCache = $this->storage->getPermissionsCache(); + $permissionsCache->removeMultiple($ids, \OC_User::getUser()); + $this->cache->clear(); + } + } + function testFile() { $data = "dummy file data\n"; $this->storage->file_put_contents('foo.txt', $data); @@ -218,19 +233,4 @@ class Scanner extends \PHPUnit_Framework_TestCase { $this->assertNotEquals($data1['etag'], $newData1['etag']); $this->assertNotEquals($data2['etag'], $newData2['etag']); } - - 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() { - if ($this->cache) { - $ids = $this->cache->getAll(); - $permissionsCache = $this->storage->getPermissionsCache(); - $permissionsCache->removeMultiple($ids, \OC_User::getUser()); - $this->cache->clear(); - } - } } -- cgit v1.2.3 From 235517f111a6d570e43cff1cd3701553412fc1a3 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 19 Sep 2013 21:37:52 +0200 Subject: clear permissions cache when scanning a file --- lib/files/cache/scanner.php | 15 ++++++++++++--- tests/lib/files/cache/permissions.php | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/lib/files/cache/scanner.php b/lib/files/cache/scanner.php index a986c1ca725..af819c47c61 100644 --- a/lib/files/cache/scanner.php +++ b/lib/files/cache/scanner.php @@ -36,6 +36,11 @@ class Scanner extends BasicEmitter { */ private $cache; + /** + * @var \OC\Files\Cache\Permissions $permissionsCache + */ + private $permissionsCache; + const SCAN_RECURSIVE = true; const SCAN_SHALLOW = false; @@ -46,6 +51,7 @@ class Scanner extends BasicEmitter { $this->storage = $storage; $this->storageId = $this->storage->getId(); $this->cache = $storage->getCache(); + $this->permissionsCache = $storage->getPermissionsCache(); } /** @@ -96,7 +102,11 @@ class Scanner extends BasicEmitter { } } $newData = $data; - if ($reuseExisting and $cacheData = $this->cache->get($file)) { + $cacheData = $this->cache->get($file); + if ($cacheData) { + $this->permissionsCache->remove($cacheData['fileid']); + } + if ($reuseExisting and $cacheData) { // prevent empty etag $etag = $cacheData['etag']; $propagateETagChange = false; @@ -104,7 +114,6 @@ class Scanner extends BasicEmitter { $etag = $data['etag']; $propagateETagChange = true; } - // only reuse data if the file hasn't explicitly changed if (isset($data['mtime']) && isset($cacheData['mtime']) && $data['mtime'] === $cacheData['mtime']) { if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) { @@ -182,7 +191,7 @@ class Scanner extends BasicEmitter { $newChildren = array(); if ($this->storage->is_dir($path) && ($dh = $this->storage->opendir($path))) { \OC_DB::beginTransaction(); - if(is_resource($dh)) { + if (is_resource($dh)) { while (($file = readdir($dh)) !== false) { $child = ($path) ? $path . '/' . $file : $file; if (!Filesystem::isIgnoredDir($file)) { diff --git a/tests/lib/files/cache/permissions.php b/tests/lib/files/cache/permissions.php index 7e6e11e2eb2..4b284c2c8e2 100644 --- a/tests/lib/files/cache/permissions.php +++ b/tests/lib/files/cache/permissions.php @@ -8,6 +8,8 @@ namespace Test\Files\Cache; +use OC\Files\Storage\Temporary; + class Permissions extends \PHPUnit_Framework_TestCase { /*** * @var \OC\Files\Cache\Permissions $permissionsCache @@ -55,4 +57,19 @@ class Permissions extends \PHPUnit_Framework_TestCase { $this->permissionsCache->removeMultiple($ids, $user); } + + public function testUpdatePermissionsOnRescan() { + $storage = new Temporary(array()); + $scanner = $storage->getScanner(); + $cache = $storage->getCache(); + $permissionsCache = $storage->getPermissionsCache(); + + $storage->file_put_contents('foo.txt', 'bar'); + $scanner->scan(''); + $id = $cache->getId('foo.txt'); + $permissionsCache->set($id, 'test', 1); + + $scanner->scan(''); + $this->assertEquals(-1, $permissionsCache->get($id, 'test')); + } } -- cgit v1.2.3