diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/karma.config.js | 18 | ||||
-rw-r--r-- | tests/lib/activitymanager.php | 202 | ||||
-rw-r--r-- | tests/lib/appconfig.php | 66 | ||||
-rw-r--r-- | tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php | 4 | ||||
-rw-r--r-- | tests/lib/connector/sabre/aborteduploaddetectionplugin.php | 100 | ||||
-rw-r--r-- | tests/lib/connector/sabre/file.php | 51 | ||||
-rw-r--r-- | tests/lib/db/migrator.php | 44 | ||||
-rw-r--r-- | tests/lib/files/cache/cache.php | 6 | ||||
-rw-r--r-- | tests/lib/files/objectstore/swift.php | 106 | ||||
-rw-r--r-- | tests/lib/group/backend.php | 44 | ||||
-rw-r--r-- | tests/lib/group/database.php | 6 | ||||
-rw-r--r-- | tests/lib/preferences-singleton.php | 163 | ||||
-rw-r--r-- | tests/lib/preferences.php | 219 | ||||
-rw-r--r-- | tests/lib/repair/repaircollation.php | 73 | ||||
-rw-r--r-- | tests/lib/repair/repairinnodb.php | 65 | ||||
-rw-r--r-- | tests/lib/repair/repairmimetypes.php | 207 | ||||
-rw-r--r-- | tests/lib/user/backend.php | 17 | ||||
-rw-r--r-- | tests/lib/user/database.php | 3 | ||||
-rw-r--r-- | tests/lib/util.php | 68 |
19 files changed, 1176 insertions, 286 deletions
diff --git a/tests/karma.config.js b/tests/karma.config.js index 290790686b0..14a0d7e8464 100644 --- a/tests/karma.config.js +++ b/tests/karma.config.js @@ -110,15 +110,16 @@ module.exports = function(config) { // core mocks files.push(corePath + 'tests/specHelper.js'); + var srcFile, i; // add core library files - for ( var i = 0; i < coreModule.libraries.length; i++ ) { - var srcFile = corePath + coreModule.libraries[i]; + for ( i = 0; i < coreModule.libraries.length; i++ ) { + srcFile = corePath + coreModule.libraries[i]; files.push(srcFile); } // add core modules files - for ( var i = 0; i < coreModule.modules.length; i++ ) { - var srcFile = corePath + coreModule.modules[i]; + for ( i = 0; i < coreModule.modules.length; i++ ) { + srcFile = corePath + coreModule.modules[i]; files.push(srcFile); if (enableCoverage) { preprocessors[srcFile] = 'coverage'; @@ -155,12 +156,15 @@ module.exports = function(config) { } // add source files for apps to test - for ( var i = 0; i < appsToTest.length; i++ ) { + for ( i = 0; i < appsToTest.length; i++ ) { addApp(appsToTest[i]); } // serve images to avoid warnings files.push({pattern: 'core/img/**/*', watched: false, included: false, served: true}); + + // include core CSS + files.push({pattern: 'core/css/*.css', watched: true, included: true, served: true}); config.set({ @@ -180,7 +184,9 @@ module.exports = function(config) { proxies: { // prevent warnings for images - '/context.html//core/img/': 'http://localhost:9876/base/core/img/' + '/context.html//core/img/': 'http://localhost:9876/base/core/img/', + '/context.html//core/css/': 'http://localhost:9876/base/core/css/', + '/context.html//core/fonts/': 'http://localhost:9876/base/core/fonts/' }, // test results reporter to use diff --git a/tests/lib/activitymanager.php b/tests/lib/activitymanager.php new file mode 100644 index 00000000000..f21b82c52c3 --- /dev/null +++ b/tests/lib/activitymanager.php @@ -0,0 +1,202 @@ +<?php + +/** + * Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + * +*/ + +class Test_ActivityManager extends PHPUnit_Framework_TestCase { + + /** @var \OC\ActivityManager */ + private $activityManager; + + public function setUp() { + $this->activityManager = new \OC\ActivityManager(); + $this->activityManager->registerExtension(function() { + return new NoOpExtension(); + }); + $this->activityManager->registerExtension(function() { + return new SimpleExtension(); + }); + } + + public function testNotificationTypes() { + $result = $this->activityManager->getNotificationTypes('en'); + $this->assertTrue(is_array($result)); + $this->assertEquals(2, sizeof($result)); + } + + public function testFilterNotificationTypes() { + $result = $this->activityManager->filterNotificationTypes(array('NT0', 'NT1', 'NT2', 'NT3'), 'FILTER1'); + $this->assertTrue(is_array($result)); + $this->assertEquals(3, sizeof($result)); + + $result = $this->activityManager->filterNotificationTypes(array('NT0', 'NT1', 'NT2', 'NT3'), 'FILTER2'); + $this->assertTrue(is_array($result)); + $this->assertEquals(4, sizeof($result)); + } + + public function testDefaultTypes() { + $result = $this->activityManager->getDefaultTypes('stream'); + $this->assertTrue(is_array($result)); + $this->assertEquals(1, sizeof($result)); + + $result = $this->activityManager->getDefaultTypes('email'); + $this->assertTrue(is_array($result)); + $this->assertEquals(0, sizeof($result)); + } + + public function testTranslate() { + $result = $this->activityManager->translate('APP0', '', '', array(), false, false, 'en'); + $this->assertEquals('Stupid translation', $result); + + $result = $this->activityManager->translate('APP1', '', '', array(), false, false, 'en'); + $this->assertFalse($result); + } + + public function testTypeIcon() { + $result = $this->activityManager->getTypeIcon('NT1'); + $this->assertEquals('icon-nt-one', $result); + + $result = $this->activityManager->getTypeIcon('NT2'); + $this->assertEquals('', $result); + } + + public function testGroupParameter() { + $result = $this->activityManager->getGroupParameter(array()); + $this->assertEquals(5, $result); + } + + public function testNavigation() { + $result = $this->activityManager->getNavigation(); + $this->assertEquals(4, sizeof($result['apps'])); + $this->assertEquals(2, sizeof($result['top'])); + } + + public function testIsFilterValid() { + $result = $this->activityManager->isFilterValid('fv01'); + $this->assertTrue($result); + + $result = $this->activityManager->isFilterValid('FV2'); + $this->assertFalse($result); + } + + public function testQueryForFilter() { + $result = $this->activityManager->getQueryForFilter('filter1'); + $this->assertEquals( + array( + '`app` = ? and `message` like ?', + array('mail', 'ownCloud%') + ), $result + ); + + $result = $this->activityManager->isFilterValid('filter2'); + $this->assertFalse($result); + } +} + +class SimpleExtension implements \OCP\Activity\IExtension { + + public function getNotificationTypes($languageCode) { + return array('NT1', 'NT2'); + } + + public function filterNotificationTypes($types, $filter) { + if ($filter === 'FILTER1') { + unset($types[0]); + } + return $types; + } + + public function getDefaultTypes($method) { + if ($method === 'stream') { + return array('DT0'); + } + + return array(); + } + + public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { + if ($app === 'APP0') { + return "Stupid translation"; + } + + return false; + } + + public function getTypeIcon($type) { + if ($type === 'NT1') { + return 'icon-nt-one'; + } + return ''; + } + + public function getGroupParameter($activity) { + return 5; + } + + public function getNavigation() { + return array( + 'apps' => array('nav1', 'nav2', 'nav3', 'nav4'), + 'top' => array('top1', 'top2') + ); + } + + public function isFilterValid($filterValue) { + if ($filterValue === 'fv01') { + return true; + } + + return false; + } + + public function getQueryForFilter($filter) { + if ($filter === 'filter1') { + return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%')); + } + + return false; + } +} + +class NoOpExtension implements \OCP\Activity\IExtension { + + public function getNotificationTypes($languageCode) { + return false; + } + + public function filterNotificationTypes($types, $filter) { + return false; + } + + public function getDefaultTypes($method) { + return false; + } + + public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { + return false; + } + + public function getTypeIcon($type) { + return false; + } + + public function getGroupParameter($activity) { + return false; + } + + public function getNavigation() { + return false; + } + + public function isFilterValid($filterValue) { + return false; + } + + public function getQueryForFilter($filter) { + return false; + } +} diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php index 6ae790a9edd..9257ae45b0e 100644 --- a/tests/lib/appconfig.php +++ b/tests/lib/appconfig.php @@ -95,6 +95,72 @@ class Test_Appconfig extends PHPUnit_Framework_TestCase { $this->assertEquals('somevalue', $value['configvalue']); } + public function testSetValueUnchanged() { + $statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false); + $statementMock->expects($this->once()) + ->method('fetch') + ->will($this->returnValue(false)); + + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->once()) + ->method('executeQuery') + ->with($this->equalTo('SELECT `configvalue`, `configkey` FROM `*PREFIX*appconfig`' + .' WHERE `appid` = ?'), $this->equalTo(array('bar'))) + ->will($this->returnValue($statementMock)); + $connectionMock->expects($this->once()) + ->method('insert') + ->with($this->equalTo('*PREFIX*appconfig'), + $this->equalTo( + array( + 'appid' => 'bar', + 'configkey' => 'foo', + 'configvalue' => 'v1', + ) + )); + $connectionMock->expects($this->never()) + ->method('update'); + + $appconfig = new OC\AppConfig($connectionMock); + $appconfig->setValue('bar', 'foo', 'v1'); + $appconfig->setValue('bar', 'foo', 'v1'); + $appconfig->setValue('bar', 'foo', 'v1'); + } + + public function testSetValueUnchanged2() { + $statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false); + $statementMock->expects($this->once()) + ->method('fetch') + ->will($this->returnValue(false)); + + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->once()) + ->method('executeQuery') + ->with($this->equalTo('SELECT `configvalue`, `configkey` FROM `*PREFIX*appconfig`' + .' WHERE `appid` = ?'), $this->equalTo(array('bar'))) + ->will($this->returnValue($statementMock)); + $connectionMock->expects($this->once()) + ->method('insert') + ->with($this->equalTo('*PREFIX*appconfig'), + $this->equalTo( + array( + 'appid' => 'bar', + 'configkey' => 'foo', + 'configvalue' => 'v1', + ) + )); + $connectionMock->expects($this->once()) + ->method('update') + ->with($this->equalTo('*PREFIX*appconfig'), + $this->equalTo(array('configvalue' => 'v2')), + $this->equalTo(array('appid' => 'bar', 'configkey' => 'foo')) + ); + + $appconfig = new OC\AppConfig($connectionMock); + $appconfig->setValue('bar', 'foo', 'v1'); + $appconfig->setValue('bar', 'foo', 'v2'); + $appconfig->setValue('bar', 'foo', 'v2'); + } + public function testDeleteKey() { \OC_Appconfig::deleteKey('testapp', 'deletethis'); $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); diff --git a/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php b/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php index 47556ca9542..74fc7907fb5 100644 --- a/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php +++ b/tests/lib/appframework/middleware/security/SecurityMiddlewareTest.php @@ -313,7 +313,8 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase { public function testAfterExceptionReturnsRedirect(){ $this->request = new Request( array('server' => - array('HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8') + array('HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + 'REQUEST_URI' => 'owncloud/index.php/apps/specialapp') ) ); $this->middleware = $this->getMiddleware(true, true); @@ -321,6 +322,7 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase { $this->secException); $this->assertTrue($response instanceof RedirectResponse); + $this->assertEquals('?redirect_url=owncloud%2Findex.php%2Fapps%2Fspecialapp', $response->getRedirectURL()); } diff --git a/tests/lib/connector/sabre/aborteduploaddetectionplugin.php b/tests/lib/connector/sabre/aborteduploaddetectionplugin.php deleted file mode 100644 index 7e9f70ddcd3..00000000000 --- a/tests/lib/connector/sabre/aborteduploaddetectionplugin.php +++ /dev/null @@ -1,100 +0,0 @@ -<?php - -/** - * Copyright (c) 2013 Thomas Müller <thomas.mueller@tmit.eu> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ -class Test_OC_Connector_Sabre_AbortedUploadDetectionPlugin extends PHPUnit_Framework_TestCase { - - /** - * @var \Sabre\DAV\Server - */ - private $server; - - /** - * @var OC_Connector_Sabre_AbortedUploadDetectionPlugin - */ - private $plugin; - - private function init($view) { - $this->server = new \Sabre\DAV\Server(); - $this->plugin = new OC_Connector_Sabre_AbortedUploadDetectionPlugin($view); - $this->plugin->initialize($this->server); - } - - /** - * @dataProvider lengthProvider - */ - public function testLength($expected, $headers) { - $this->init(null); - - $this->server->httpRequest = new \Sabre\HTTP\Request($headers); - $length = $this->plugin->getLength(); - $this->assertEquals($expected, $length); - } - - /** - * @dataProvider verifyContentLengthProvider - */ - public function testVerifyContentLength($method, $fileSize, $headers) { - $this->init($this->buildFileViewMock($fileSize)); - - $headers['REQUEST_METHOD'] = $method; - $this->server->httpRequest = new Sabre\HTTP\Request($headers); - $this->plugin->verifyContentLength('foo.txt'); - $this->assertTrue(true); - } - - /** - * @dataProvider verifyContentLengthFailedProvider - * @expectedException \Sabre\DAV\Exception\BadRequest - */ - public function testVerifyContentLengthFailed($method, $fileSize, $headers) { - $view = $this->buildFileViewMock($fileSize); - $this->init($view); - // we expect unlink to be called - $view->expects($this->once())->method('unlink'); - - $headers['REQUEST_METHOD'] = $method; - $this->server->httpRequest = new Sabre\HTTP\Request($headers); - $this->plugin->verifyContentLength('foo.txt'); - } - - public function verifyContentLengthProvider() { - return array( - array('PUT', 1024, array()), - array('PUT', 1024, array('HTTP_X_EXPECTED_ENTITY_LENGTH' => '1024')), - array('PUT', 512, array('HTTP_CONTENT_LENGTH' => '512')), - array('LOCK', 1024, array()), - array('LOCK', 1024, array('HTTP_X_EXPECTED_ENTITY_LENGTH' => '1024')), - array('LOCK', 512, array('HTTP_CONTENT_LENGTH' => '512')), - ); - } - - public function verifyContentLengthFailedProvider() { - return array( - array('PUT', 1025, array('HTTP_X_EXPECTED_ENTITY_LENGTH' => '1024')), - array('PUT', 525, array('HTTP_CONTENT_LENGTH' => '512')), - ); - } - - public function lengthProvider() { - return array( - array(null, array()), - array(1024, array('HTTP_X_EXPECTED_ENTITY_LENGTH' => '1024')), - array(512, array('HTTP_CONTENT_LENGTH' => '512')), - array(2048, array('HTTP_X_EXPECTED_ENTITY_LENGTH' => '2048', 'HTTP_CONTENT_LENGTH' => '1024')), - ); - } - - private function buildFileViewMock($fileSize) { - // mock filesystem - $view = $this->getMock('\OC\Files\View', array('filesize', 'unlink'), array(), '', false); - $view->expects($this->any())->method('filesize')->withAnyParameters()->will($this->returnValue($fileSize)); - - return $view; - } - -} diff --git a/tests/lib/connector/sabre/file.php b/tests/lib/connector/sabre/file.php index 3dd5b328f46..ba4e775813b 100644 --- a/tests/lib/connector/sabre/file.php +++ b/tests/lib/connector/sabre/file.php @@ -29,7 +29,7 @@ class Test_OC_Connector_Sabre_File extends PHPUnit_Framework_TestCase { $file = new OC_Connector_Sabre_File($view, $info); // action - $etag = $file->put('test data'); + $file->put('test data'); } /** @@ -37,7 +37,9 @@ class Test_OC_Connector_Sabre_File extends PHPUnit_Framework_TestCase { */ public function testSimplePutFailsOnRename() { // setup - $view = $this->getMock('\OC\Files\View', array('file_put_contents', 'rename', 'getRelativePath'), array(), '', false); + $view = $this->getMock('\OC\Files\View', + array('file_put_contents', 'rename', 'getRelativePath', 'filesize'), + array(), '', false); $view->expects($this->any()) ->method('file_put_contents') ->withAnyParameters() @@ -46,10 +48,14 @@ class Test_OC_Connector_Sabre_File extends PHPUnit_Framework_TestCase { ->method('rename') ->withAnyParameters() ->will($this->returnValue(false)); - $view->expects($this->any()) ->method('getRelativePath') ->will($this->returnValue('/test.txt')); + $view->expects($this->any()) + ->method('filesize') + ->will($this->returnValue(123456)); + + $_SERVER['CONTENT_LENGTH'] = 123456; $info = new \OC\Files\FileInfo('/test.txt', null, null, array( 'permissions' => \OCP\PERMISSION_ALL @@ -58,7 +64,7 @@ class Test_OC_Connector_Sabre_File extends PHPUnit_Framework_TestCase { $file = new OC_Connector_Sabre_File($view, $info); // action - $etag = $file->put('test data'); + $file->put('test data'); } /** @@ -81,7 +87,7 @@ class Test_OC_Connector_Sabre_File extends PHPUnit_Framework_TestCase { $file = new OC_Connector_Sabre_File($view, $info); // action - $etag = $file->put('test data'); + $file->put('test data'); } /** @@ -102,4 +108,39 @@ class Test_OC_Connector_Sabre_File extends PHPUnit_Framework_TestCase { $file = new OC_Connector_Sabre_File($view, $info); $file->setName('/super*star.txt'); } + + /** + * @expectedException \Sabre\DAV\Exception\BadRequest + */ + public function testUploadAbort() { + // setup + $view = $this->getMock('\OC\Files\View', + array('file_put_contents', 'rename', 'getRelativePath', 'filesize'), + array(), '', false); + $view->expects($this->any()) + ->method('file_put_contents') + ->withAnyParameters() + ->will($this->returnValue(true)); + $view->expects($this->any()) + ->method('rename') + ->withAnyParameters() + ->will($this->returnValue(false)); + $view->expects($this->any()) + ->method('getRelativePath') + ->will($this->returnValue('/test.txt')); + $view->expects($this->any()) + ->method('filesize') + ->will($this->returnValue(123456)); + + $_SERVER['CONTENT_LENGTH'] = 12345; + + $info = new \OC\Files\FileInfo('/test.txt', null, null, array( + 'permissions' => \OCP\PERMISSION_ALL + )); + + $file = new OC_Connector_Sabre_File($view, $info); + + // action + $file->put('test data'); + } } diff --git a/tests/lib/db/migrator.php b/tests/lib/db/migrator.php index e94d550f836..2e49086bd63 100644 --- a/tests/lib/db/migrator.php +++ b/tests/lib/db/migrator.php @@ -19,6 +19,11 @@ class Migrator extends \PHPUnit_Framework_TestCase { */ private $connection; + /** + * @var \OC\DB\MDB2SchemaManager + */ + private $manager; + private $tableName; public function setUp() { @@ -26,6 +31,7 @@ class Migrator extends \PHPUnit_Framework_TestCase { if ($this->connection->getDriver() instanceof \Doctrine\DBAL\Driver\OCI8\Driver) { $this->markTestSkipped('DB migration tests arent supported on OCI'); } + $this->manager = new \OC\DB\MDB2SchemaManager($this->connection); $this->tableName = 'test_' . uniqid(); } @@ -62,14 +68,6 @@ class Migrator extends \PHPUnit_Framework_TestCase { return $this->connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver; } - private function getMigrator() { - if ($this->isSQLite()) { - return new \OC\DB\SQLiteMigrator($this->connection); - } else { - return new \OC\DB\Migrator($this->connection); - } - } - /** * @expectedException \OC\DB\MigrationException */ @@ -78,7 +76,7 @@ class Migrator extends \PHPUnit_Framework_TestCase { $this->markTestSkipped('sqlite doesnt throw errors when creating a new key on existing data'); } list($startSchema, $endSchema) = $this->getDuplicateKeySchemas(); - $migrator = $this->getMigrator(); + $migrator = $this->manager->getMigrator(); $migrator->migrate($startSchema); $this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo')); @@ -91,7 +89,7 @@ class Migrator extends \PHPUnit_Framework_TestCase { public function testUpgrade() { list($startSchema, $endSchema) = $this->getDuplicateKeySchemas(); - $migrator = $this->getMigrator(); + $migrator = $this->manager->getMigrator(); $migrator->migrate($startSchema); $this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo')); @@ -105,7 +103,7 @@ class Migrator extends \PHPUnit_Framework_TestCase { public function testInsertAfterUpgrade() { list($startSchema, $endSchema) = $this->getDuplicateKeySchemas(); - $migrator = $this->getMigrator(); + $migrator = $this->manager->getMigrator(); $migrator->migrate($startSchema); $migrator->migrate($endSchema); @@ -132,7 +130,29 @@ class Migrator extends \PHPUnit_Framework_TestCase { $table->addColumn('name', 'string'); $table->setPrimaryKey(array('id')); - $migrator = $this->getMigrator(); + $migrator = $this->manager->getMigrator(); + $migrator->migrate($startSchema); + + $migrator->checkMigrate($endSchema); + $migrator->migrate($endSchema); + + $this->assertTrue(true); + } + + public function testReservedKeywords() { + $startSchema = new Schema(array(), array(), $this->getSchemaConfig()); + $table = $startSchema->createTable($this->tableName); + $table->addColumn('id', 'integer', array('autoincrement' => true)); + $table->addColumn('user', 'string', array('length' => 255)); + $table->setPrimaryKey(array('id')); + + $endSchema = new Schema(array(), array(), $this->getSchemaConfig()); + $table = $endSchema->createTable($this->tableName); + $table->addColumn('id', 'integer', array('autoincrement' => true)); + $table->addColumn('user', 'string', array('length' => 64)); + $table->setPrimaryKey(array('id')); + + $migrator = $this->manager->getMigrator(); $migrator->migrate($startSchema); $migrator->checkMigrate($endSchema); diff --git a/tests/lib/files/cache/cache.php b/tests/lib/files/cache/cache.php index 8ed2ecabd98..bf17f7a1620 100644 --- a/tests/lib/files/cache/cache.php +++ b/tests/lib/files/cache/cache.php @@ -239,6 +239,12 @@ class Cache extends \PHPUnit_Framework_TestCase { $this->assertEquals(1, count($this->cache->search('folder%'))); $this->assertEquals(3, count($this->cache->search('%'))); + // case insensitive search should match the same files + $this->assertEquals(2, count($this->cache->search('%Foo%'))); + $this->assertEquals(1, count($this->cache->search('Foo'))); + $this->assertEquals(1, count($this->cache->search('%Folder%'))); + $this->assertEquals(1, count($this->cache->search('Folder%'))); + $this->assertEquals(3, count($this->cache->searchByMime('foo'))); $this->assertEquals(2, count($this->cache->searchByMime('foo/file'))); } diff --git a/tests/lib/files/objectstore/swift.php b/tests/lib/files/objectstore/swift.php new file mode 100644 index 00000000000..7f3b45c0dab --- /dev/null +++ b/tests/lib/files/objectstore/swift.php @@ -0,0 +1,106 @@ +<?php +/** + * @author Jörn Friedrich Dreyer + * @copyright (c) 2014 Jörn Friedrich Dreyer <jfd@owncloud.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\ObjectStore\Tests\Unit; + +use OC\Files\ObjectStore\ObjectStoreStorage; +use OC\Files\ObjectStore\Swift as ObjectStoreToTest; + +use PHPUnit_Framework_TestCase; + +//class Swift extends PHPUnit_Framework_TestCase { +class Swift extends \Test\Files\Storage\Storage { + + private $objectStorage; + + public function setUp() { + + \OC_App::disable('files_sharing'); + \OC_App::disable('files_versions'); + + // reset backend + \OC_User::clearBackends(); + \OC_User::useBackend('database'); + + // create users + $users = array('test'); + foreach($users as $userName) { + \OC_User::deleteUser($userName); + \OC_User::createUser($userName, $userName); + } + + // main test user + $userName = 'test'; + \OC_Util::tearDownFS(); + \OC_User::setUserId(''); + \OC\Files\Filesystem::tearDown(); + \OC_User::setUserId('test'); + + $testContainer = 'oc-test-container-'.substr( md5(rand()), 0, 7); + + $params = array( + 'username' => 'facebook100000330192569', + 'password' => 'Dbdj1sXnRSHxIGc4', + 'container' => $testContainer, + 'autocreate' => true, + 'region' => 'RegionOne', //required, trystack defaults to 'RegionOne' + 'url' => 'http://8.21.28.222:5000/v2.0', // The Identity / Keystone endpoint + 'tenantName' => 'facebook100000330192569', // required on trystack + 'serviceName' => 'swift', //trystack uses swift by default, the lib defaults to 'cloudFiles' if omitted + 'user' => \OC_User::getManager()->get($userName) + ); + $this->objectStorage = new ObjectStoreToTest($params); + $params['objectstore'] = $this->objectStorage; + $this->instance = new ObjectStoreStorage($params); + } + + public function tearDown() { + if (is_null($this->instance)) { + return; + } + $this->objectStorage->deleteContainer(true); + $this->instance->getCache()->clear(); + } + + public function testStat() { + $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt'; + $ctimeStart = time(); + $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile)); + $this->assertTrue($this->instance->isReadable('/lorem.txt')); + $ctimeEnd = time(); + $mTime = $this->instance->filemtime('/lorem.txt'); + + // check that ($ctimeStart - 5) <= $mTime <= ($ctimeEnd + 1) + $this->assertGreaterThanOrEqual(($ctimeStart - 5), $mTime); + $this->assertLessThanOrEqual(($ctimeEnd + 1), $mTime); + $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt')); + + $stat = $this->instance->stat('/lorem.txt'); + //only size and mtime are required in the result + $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt')); + $this->assertEquals($stat['mtime'], $mTime); + + if ($this->instance->touch('/lorem.txt', 100) !== false) { + $mTime = $this->instance->filemtime('/lorem.txt'); + $this->assertEquals($mTime, 100); + } + } + +} diff --git a/tests/lib/group/backend.php b/tests/lib/group/backend.php index 2c563ae9ac9..95a5cf5f49c 100644 --- a/tests/lib/group/backend.php +++ b/tests/lib/group/backend.php @@ -31,8 +31,12 @@ abstract class Test_Group_Backend extends PHPUnit_Framework_TestCase { * test cases can override this in order to clean up created groups * @return string */ - public function getGroupName() { - return uniqid('test_'); + public function getGroupName($name = null) { + if(is_null($name)) { + return uniqid('test_'); + } else { + return $name; + } } /** @@ -88,7 +92,7 @@ abstract class Test_Group_Backend extends PHPUnit_Framework_TestCase { $this->assertFalse($this->backend->inGroup($user2, $group1)); $this->assertFalse($this->backend->inGroup($user1, $group2)); $this->assertFalse($this->backend->inGroup($user2, $group2)); - + $this->assertFalse($this->backend->addToGroup($user1, $group1)); $this->assertEquals(array($user1), $this->backend->usersInGroup($group1)); @@ -102,4 +106,38 @@ abstract class Test_Group_Backend extends PHPUnit_Framework_TestCase { $this->assertEquals(array(), $this->backend->usersInGroup($group1)); $this->assertFalse($this->backend->inGroup($user1, $group1)); } + + public function testSearchGroups() { + $name1 = $this->getGroupName('foobarbaz'); + $name2 = $this->getGroupName('bazbarfoo'); + $name3 = $this->getGroupName('notme'); + + $this->backend->createGroup($name1); + $this->backend->createGroup($name2); + $this->backend->createGroup($name3); + + $result = $this->backend->getGroups('bar'); + $this->assertSame(2, count($result)); + } + + public function testSearchUsers() { + $group = $this->getGroupName(); + $this->backend->createGroup($group); + + $name1 = 'foobarbaz'; + $name2 = 'bazbarfoo'; + $name3 = 'notme'; + + $this->backend->addToGroup($name1, $group); + $this->backend->addToGroup($name2, $group); + $this->backend->addToGroup($name3, $group); + + $result = $this->backend->usersInGroup($group, 'bar'); + $this->assertSame(2, count($result)); + + $result = $this->backend->countUsersInGroup($group, 'bar'); + $this->assertSame(2, $result); + } + + } diff --git a/tests/lib/group/database.php b/tests/lib/group/database.php index 3e05c656061..9b39ac00452 100644 --- a/tests/lib/group/database.php +++ b/tests/lib/group/database.php @@ -28,8 +28,10 @@ class Test_Group_Database extends Test_Group_Backend { * test cases can override this in order to clean up created groups * @return string */ - public function getGroupName() { - $name=uniqid('test_'); + public function getGroupName($name = null) { + if(is_null($name)) { + $name=uniqid('test_'); + } $this->groups[]=$name; return $name; } diff --git a/tests/lib/preferences-singleton.php b/tests/lib/preferences-singleton.php new file mode 100644 index 00000000000..17fe54ad1f5 --- /dev/null +++ b/tests/lib/preferences-singleton.php @@ -0,0 +1,163 @@ +<?php +/** + * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it> + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class Test_Preferences extends PHPUnit_Framework_TestCase { + public static function setUpBeforeClass() { + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*preferences` VALUES(?, ?, ?, ?)'); + $query->execute(array("Someuser", "someapp", "somekey", "somevalue")); + + $query->execute(array("Someuser", "getusersapp", "somekey", "somevalue")); + $query->execute(array("Anotheruser", "getusersapp", "somekey", "someothervalue")); + $query->execute(array("Anuser", "getusersapp", "somekey", "somevalue")); + + $query->execute(array("Someuser", "getappsapp", "somekey", "somevalue")); + + $query->execute(array("Someuser", "getkeysapp", "firstkey", "somevalue")); + $query->execute(array("Someuser", "getkeysapp", "anotherkey", "somevalue")); + $query->execute(array("Someuser", "getkeysapp", "key-tastic", "somevalue")); + + $query->execute(array("Someuser", "getvalueapp", "key", "a value for a key")); + + $query->execute(array("Deleteuser", "deleteapp", "deletekey", "somevalue")); + $query->execute(array("Deleteuser", "deleteapp", "somekey", "somevalue")); + $query->execute(array("Deleteuser", "someapp", "somekey", "somevalue")); + } + + public static function tearDownAfterClass() { + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?'); + $query->execute(array('Someuser')); + $query->execute(array('Anotheruser')); + $query->execute(array('Anuser')); + } + + public function testGetUsers() { + $query = \OC_DB::prepare('SELECT DISTINCT `userid` FROM `*PREFIX*preferences`'); + $result = $query->execute(); + $expected = array(); + while ($row = $result->fetchRow()) { + $expected[] = $row['userid']; + } + + $this->assertEquals($expected, \OC_Preferences::getUsers()); + } + + public function testGetApps() { + $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*preferences` WHERE `userid` = ?'); + $result = $query->execute(array('Someuser')); + $expected = array(); + while ($row = $result->fetchRow()) { + $expected[] = $row['appid']; + } + + $this->assertEquals($expected, \OC_Preferences::getApps('Someuser')); + } + + public function testGetKeys() { + $query = \OC_DB::prepare('SELECT DISTINCT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?'); + $result = $query->execute(array('Someuser', 'getkeysapp')); + $expected = array(); + while ($row = $result->fetchRow()) { + $expected[] = $row['configkey']; + } + + $this->assertEquals($expected, \OC_Preferences::getKeys('Someuser', 'getkeysapp')); + } + + public function testGetValue() { + $this->assertNull(\OC_Preferences::getValue('nonexistant', 'nonexistant', 'nonexistant')); + + $this->assertEquals('default', \OC_Preferences::getValue('nonexistant', 'nonexistant', 'nonexistant', 'default')); + + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('Someuser', 'getvalueapp', 'key')); + $row = $result->fetchRow(); + $expected = $row['configvalue']; + $this->assertEquals($expected, \OC_Preferences::getValue('Someuser', 'getvalueapp', 'key')); + } + + public function testSetValue() { + $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'newvalue')); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); + $row = $result->fetchRow(); + $value = $row['configvalue']; + $this->assertEquals('newvalue', $value); + + $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'othervalue')); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); + $row = $result->fetchRow(); + $value = $row['configvalue']; + $this->assertEquals('othervalue', $value); + } + + public function testSetValueWithPreCondition() { + // remove existing key + $this->assertTrue(\OC_Preferences::deleteKey('Someuser', 'setvalueapp', 'newkey')); + + // add new preference with pre-condition should fails + $this->assertFalse(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'newvalue', 'preCondition')); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); + $row = $result->fetchRow(); + $this->assertFalse($row); + + // add new preference without pre-condition should insert the new value + $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'newvalue')); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); + $row = $result->fetchRow(); + $value = $row['configvalue']; + $this->assertEquals('newvalue', $value); + + // wrong pre-condition, value should stay the same + $this->assertFalse(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'othervalue', 'preCondition')); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); + $row = $result->fetchRow(); + $value = $row['configvalue']; + $this->assertEquals('newvalue', $value); + + // correct pre-condition, value should change + $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'othervalue', 'newvalue')); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); + $row = $result->fetchRow(); + $value = $row['configvalue']; + $this->assertEquals('othervalue', $value); + } + + public function testDeleteKey() { + $this->assertTrue(\OC_Preferences::deleteKey('Deleteuser', 'deleteapp', 'deletekey')); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('Deleteuser', 'deleteapp', 'deletekey')); + $this->assertEquals(0, count($result->fetchAll())); + } + + public function testDeleteApp() { + $this->assertTrue(\OC_Preferences::deleteApp('Deleteuser', 'deleteapp')); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?'); + $result = $query->execute(array('Deleteuser', 'deleteapp')); + $this->assertEquals(0, count($result->fetchAll())); + } + + public function testDeleteUser() { + $this->assertTrue(\OC_Preferences::deleteUser('Deleteuser')); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'); + $result = $query->execute(array('Deleteuser')); + $this->assertEquals(0, count($result->fetchAll())); + } + + public function testDeleteAppFromAllUsers() { + $this->assertTrue(\OC_Preferences::deleteAppFromAllUsers('someapp')); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `appid` = ?'); + $result = $query->execute(array('someapp')); + $this->assertEquals(0, count($result->fetchAll())); + } +} diff --git a/tests/lib/preferences.php b/tests/lib/preferences.php index d31b0257bad..fe8e3e8b48c 100644 --- a/tests/lib/preferences.php +++ b/tests/lib/preferences.php @@ -7,161 +7,6 @@ * See the COPYING-README file. */ -class Test_Preferences extends PHPUnit_Framework_TestCase { - public static function setUpBeforeClass() { - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*preferences` VALUES(?, ?, ?, ?)'); - $query->execute(array("Someuser", "someapp", "somekey", "somevalue")); - - $query->execute(array("Someuser", "getusersapp", "somekey", "somevalue")); - $query->execute(array("Anotheruser", "getusersapp", "somekey", "someothervalue")); - $query->execute(array("Anuser", "getusersapp", "somekey", "somevalue")); - - $query->execute(array("Someuser", "getappsapp", "somekey", "somevalue")); - - $query->execute(array("Someuser", "getkeysapp", "firstkey", "somevalue")); - $query->execute(array("Someuser", "getkeysapp", "anotherkey", "somevalue")); - $query->execute(array("Someuser", "getkeysapp", "key-tastic", "somevalue")); - - $query->execute(array("Someuser", "getvalueapp", "key", "a value for a key")); - - $query->execute(array("Deleteuser", "deleteapp", "deletekey", "somevalue")); - $query->execute(array("Deleteuser", "deleteapp", "somekey", "somevalue")); - $query->execute(array("Deleteuser", "someapp", "somekey", "somevalue")); - } - - public static function tearDownAfterClass() { - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?'); - $query->execute(array('Someuser')); - $query->execute(array('Anotheruser')); - $query->execute(array('Anuser')); - } - - public function testGetUsers() { - $query = \OC_DB::prepare('SELECT DISTINCT `userid` FROM `*PREFIX*preferences`'); - $result = $query->execute(); - $expected = array(); - while ($row = $result->fetchRow()) { - $expected[] = $row['userid']; - } - - $this->assertEquals($expected, \OC_Preferences::getUsers()); - } - - public function testGetApps() { - $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*preferences` WHERE `userid` = ?'); - $result = $query->execute(array('Someuser')); - $expected = array(); - while ($row = $result->fetchRow()) { - $expected[] = $row['appid']; - } - - $this->assertEquals($expected, \OC_Preferences::getApps('Someuser')); - } - - public function testGetKeys() { - $query = \OC_DB::prepare('SELECT DISTINCT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?'); - $result = $query->execute(array('Someuser', 'getkeysapp')); - $expected = array(); - while ($row = $result->fetchRow()) { - $expected[] = $row['configkey']; - } - - $this->assertEquals($expected, \OC_Preferences::getKeys('Someuser', 'getkeysapp')); - } - - public function testGetValue() { - $this->assertNull(\OC_Preferences::getValue('nonexistant', 'nonexistant', 'nonexistant')); - - $this->assertEquals('default', \OC_Preferences::getValue('nonexistant', 'nonexistant', 'nonexistant', 'default')); - - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('Someuser', 'getvalueapp', 'key')); - $row = $result->fetchRow(); - $expected = $row['configvalue']; - $this->assertEquals($expected, \OC_Preferences::getValue('Someuser', 'getvalueapp', 'key')); - } - - public function testSetValue() { - $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'newvalue')); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); - $row = $result->fetchRow(); - $value = $row['configvalue']; - $this->assertEquals('newvalue', $value); - - $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'othervalue')); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); - $row = $result->fetchRow(); - $value = $row['configvalue']; - $this->assertEquals('othervalue', $value); - } - - public function testSetValueWithPreCondition() { - // remove existing key - $this->assertTrue(\OC_Preferences::deleteKey('Someuser', 'setvalueapp', 'newkey')); - - // add new preference with pre-condition should fails - $this->assertFalse(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'newvalue', 'preCondition')); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); - $row = $result->fetchRow(); - $this->assertFalse($row); - - // add new preference without pre-condition should insert the new value - $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'newvalue')); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); - $row = $result->fetchRow(); - $value = $row['configvalue']; - $this->assertEquals('newvalue', $value); - - // wrong pre-condition, value should stay the same - $this->assertFalse(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'othervalue', 'preCondition')); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); - $row = $result->fetchRow(); - $value = $row['configvalue']; - $this->assertEquals('newvalue', $value); - - // correct pre-condition, value should change - $this->assertTrue(\OC_Preferences::setValue('Someuser', 'setvalueapp', 'newkey', 'othervalue', 'newvalue')); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('Someuser', 'setvalueapp', 'newkey')); - $row = $result->fetchRow(); - $value = $row['configvalue']; - $this->assertEquals('othervalue', $value); - } - - public function testDeleteKey() { - $this->assertTrue(\OC_Preferences::deleteKey('Deleteuser', 'deleteapp', 'deletekey')); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('Deleteuser', 'deleteapp', 'deletekey')); - $this->assertEquals(0, count($result->fetchAll())); - } - - public function testDeleteApp() { - $this->assertTrue(\OC_Preferences::deleteApp('Deleteuser', 'deleteapp')); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?'); - $result = $query->execute(array('Deleteuser', 'deleteapp')); - $this->assertEquals(0, count($result->fetchAll())); - } - - public function testDeleteUser() { - $this->assertTrue(\OC_Preferences::deleteUser('Deleteuser')); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'); - $result = $query->execute(array('Deleteuser')); - $this->assertEquals(0, count($result->fetchAll())); - } - - public function testDeleteAppFromAllUsers() { - $this->assertTrue(\OC_Preferences::deleteAppFromAllUsers('someapp')); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `appid` = ?'); - $result = $query->execute(array('someapp')); - $this->assertEquals(0, count($result->fetchAll())); - } -} - class Test_Preferences_Object extends PHPUnit_Framework_TestCase { public function testGetUsers() { @@ -185,10 +30,69 @@ class Test_Preferences_Object extends PHPUnit_Framework_TestCase { $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); $connectionMock->expects($this->exactly(2)) ->method('fetchColumn') - ->with($this->equalTo('SELECT COUNT(*) FROM `*PREFIX*preferences`' + ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences`' + .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'), + $this->equalTo(array('grg', 'bar', 'foo'))) + ->will($this->onConsecutiveCalls(false, 'v1')); + $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('executeUpdate') + ->with($this->equalTo("UPDATE `*PREFIX*preferences` SET `configvalue` = ?" + . " WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?"), + $this->equalTo(array('v2', 'grg', 'bar', 'foo')) + ); + + $preferences = new OC\Preferences($connectionMock); + $preferences->setValue('grg', 'bar', 'foo', 'v1'); + $preferences->setValue('grg', 'bar', 'foo', 'v2'); + } + + public function testSetValueUnchanged() { + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->exactly(3)) + ->method('fetchColumn') + ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences`' .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'), $this->equalTo(array('grg', 'bar', 'foo'))) - ->will($this->onConsecutiveCalls(0, 1)); + ->will($this->onConsecutiveCalls(false, 'v1', 'v1')); + $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->never()) + ->method('executeUpdate'); + + $preferences = new OC\Preferences($connectionMock); + $preferences->setValue('grg', 'bar', 'foo', 'v1'); + $preferences->setValue('grg', 'bar', 'foo', 'v1'); + $preferences->setValue('grg', 'bar', 'foo', 'v1'); + } + + public function testSetValueUnchanged2() { + $connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false); + $connectionMock->expects($this->exactly(3)) + ->method('fetchColumn') + ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences`' + .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'), + $this->equalTo(array('grg', 'bar', 'foo'))) + ->will($this->onConsecutiveCalls(false, 'v1', 'v2')); $connectionMock->expects($this->once()) ->method('insert') ->with($this->equalTo('*PREFIX*preferences'), @@ -210,6 +114,7 @@ class Test_Preferences_Object extends PHPUnit_Framework_TestCase { $preferences = new OC\Preferences($connectionMock); $preferences->setValue('grg', 'bar', 'foo', 'v1'); $preferences->setValue('grg', 'bar', 'foo', 'v2'); + $preferences->setValue('grg', 'bar', 'foo', 'v2'); } public function testGetUserValues() diff --git a/tests/lib/repair/repaircollation.php b/tests/lib/repair/repaircollation.php new file mode 100644 index 00000000000..362feb8463f --- /dev/null +++ b/tests/lib/repair/repaircollation.php @@ -0,0 +1,73 @@ +<?php +/** + * Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class TestCollationRepair extends \OC\Repair\Collation { + /** + * @param \Doctrine\DBAL\Connection $connection + * @return string[] + */ + public function getAllNonUTF8BinTables($connection) { + return parent::getAllNonUTF8BinTables($connection); + } +} + +/** + * Tests for the converting of MySQL tables to InnoDB engine + * + * @see \OC\Repair\RepairMimeTypes + */ +class TestRepairCollation extends PHPUnit_Framework_TestCase { + + /** + * @var TestCollationRepair + */ + private $repair; + + /** + * @var \Doctrine\DBAL\Connection + */ + private $connection; + + /** + * @var string + */ + private $tableName; + + /** + * @var \OCP\IConfig + */ + private $config; + + public function setUp() { + $this->connection = \OC_DB::getConnection(); + $this->config = \OC::$server->getConfig(); + if (!$this->connection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\MySqlPlatform) { + $this->markTestSkipped("Test only relevant on MySql"); + } + + $dbPrefix = $this->config->getSystemValue("dbtableprefix"); + $this->tableName = uniqid($dbPrefix . "_collation_test"); + $this->connection->exec("CREATE TABLE $this->tableName(text VARCHAR(16)) COLLATE utf8_unicode_ci"); + + $this->repair = new TestCollationRepair($this->config, $this->connection); + } + + public function tearDown() { + $this->connection->getSchemaManager()->dropTable($this->tableName); + } + + public function testCollationConvert() { + $tables = $this->repair->getAllNonUTF8BinTables($this->connection); + $this->assertGreaterThanOrEqual(1, count($tables)); + + $this->repair->run(); + + $tables = $this->repair->getAllNonUTF8BinTables($this->connection); + $this->assertCount(0, $tables); + } +} diff --git a/tests/lib/repair/repairinnodb.php b/tests/lib/repair/repairinnodb.php new file mode 100644 index 00000000000..e7d2442f127 --- /dev/null +++ b/tests/lib/repair/repairinnodb.php @@ -0,0 +1,65 @@ +<?php +/** + * Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +/** + * Tests for the converting of MySQL tables to InnoDB engine + * + * @see \OC\Repair\RepairMimeTypes + */ +class TestRepairInnoDB extends PHPUnit_Framework_TestCase { + + /** @var \OC\RepairStep */ + private $repair; + + /** @var \Doctrine\DBAL\Connection */ + private $connection; + + /** @var string */ + private $tableName; + + public function setUp() { + $this->connection = \OC_DB::getConnection(); + if (!$this->connection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\MySqlPlatform) { + $this->markTestSkipped("Test only relevant on MySql"); + } + + $dbPrefix = \OC::$server->getConfig()->getSystemValue("dbtableprefix"); + $this->tableName = uniqid($dbPrefix . "_innodb_test"); + $this->connection->exec("CREATE TABLE $this->tableName(id INT) ENGINE MyISAM"); + + $this->repair = new \OC\Repair\InnoDB(); + } + + public function tearDown() { + $this->connection->getSchemaManager()->dropTable($this->tableName); + } + + public function testInnoDBConvert() { + $result = $this->countMyIsamTables(); + $this->assertEquals(1, $result); + + $this->repair->run(); + + $result = $this->countMyIsamTables(); + $this->assertEquals(0, $result); + } + + /** + * @param $dbName + * @return mixed + */ + private function countMyIsamTables() { + $dbName = \OC::$server->getConfig()->getSystemValue("dbname"); + + $result = $this->connection->fetchColumn( + "SELECT count(*) FROM information_schema.tables WHERE table_schema = ? and table_name = ? AND engine = 'MyISAM'", + array($dbName, $this->tableName) + ); + return $result; + } +} diff --git a/tests/lib/repair/repairmimetypes.php b/tests/lib/repair/repairmimetypes.php new file mode 100644 index 00000000000..3ed19bd55bb --- /dev/null +++ b/tests/lib/repair/repairmimetypes.php @@ -0,0 +1,207 @@ +<?php +/** + * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +/** + * Tests for the converting of legacy storages to home storages. + * + * @see \OC\Repair\RepairMimeTypes + */ +class TestRepairMimeTypes extends PHPUnit_Framework_TestCase { + + /** @var \OC\RepairStep */ + private $repair; + + private $storage; + + public function setUp() { + $this->storage = new \OC\Files\Storage\Temporary(array()); + + $this->repair = new \OC\Repair\RepairMimeTypes(); + } + + public function tearDown() { + $this->storage->getCache()->clear(); + $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'; + \OC_DB::executeAudited($sql, array($this->storage->getId())); + $this->clearMimeTypes(); + + DummyFileCache::clearCachedMimeTypes(); + } + + private function clearMimeTypes() { + $sql = 'DELETE FROM `*PREFIX*mimetypes`'; + \OC_DB::executeAudited($sql); + } + + private function addEntries($entries) { + // create files for the different extensions, this + // will also automatically create the corresponding mime types + foreach ($entries as $entry) { + $this->storage->getCache()->put( + $entry[0], + array( + 'size' => 0, + 'mtime' => 0, + 'mimetype' => $entry[1] + ) + ); + } + + } + + private function checkEntries($entries) { + foreach ($entries as $entry) { + $data = $this->storage->getCache()->get($entry[0]); + $this->assertEquals($entry[1], $data['mimetype']); + } + } + + /** + * Returns the id of a given mime type or null + * if it does not exist. + */ + private function getMimeTypeIdFromDB($mimeType) { + $sql = 'SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?'; + $results = \OC_DB::executeAudited($sql, array($mimeType)); + $result = $results->fetchOne(); + if ($result) { + return $result['id']; + } + return null; + } + + /** + * Test renaming and splitting old office mime types + */ + public function testRenameOfficeMimeTypes() { + $this->addEntries( + array( + array('test.doc', 'application/msword'), + array('test.docx', 'application/msword'), + array('test.xls', 'application/msexcel'), + array('test.xlsx', 'application/msexcel'), + array('test.ppt', 'application/mspowerpoint'), + array('test.pptx', 'application/mspowerpoint'), + ) + ); + + $this->repair->run(); + + // force mimetype reload + DummyFileCache::clearCachedMimeTypes(); + $this->storage->getCache()->loadMimeTypes(); + + $this->checkEntries( + array( + array('test.doc', 'application/msword'), + array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), + array('test.xls', 'application/vnd.ms-excel'), + array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'), + array('test.ppt', 'application/vnd.ms-powerpoint'), + array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'), + ) + ); + } + + /** + * Test renaming and splitting old office mime types when + * new ones already exist + */ + public function testRenameOfficeMimeTypesWhenExist() { + $this->addEntries( + array( + array('test.doc', 'application/msword'), + array('test.docx', 'application/msword'), + array('test.xls', 'application/msexcel'), + array('test.xlsx', 'application/msexcel'), + array('test.ppt', 'application/mspowerpoint'), + array('test.pptx', 'application/mspowerpoint'), + // make it so that the new mimetypes already exist + array('bogus.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), + array('bogus.xlsx', 'application/vnd.ms-excel'), + array('bogus.pptx', 'application/vnd.ms-powerpoint'), + array('bogus2.docx', 'application/wrong'), + array('bogus2.xlsx', 'application/wrong'), + array('bogus2.pptx', 'application/wrong'), + ) + ); + + $this->repair->run(); + + // force mimetype reload + DummyFileCache::clearCachedMimeTypes(); + $this->storage->getCache()->loadMimeTypes(); + + $this->checkEntries( + array( + array('test.doc', 'application/msword'), + array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), + array('test.xls', 'application/vnd.ms-excel'), + array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'), + array('test.ppt', 'application/vnd.ms-powerpoint'), + array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'), + array('bogus.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), + array('bogus.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'), + array('bogus.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'), + array('bogus2.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), + array('bogus2.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'), + array('bogus2.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'), + ) + ); + + // wrong mimetypes are gone + $this->assertNull($this->getMimeTypeIdFromDB('application/msexcel')); + $this->assertNull($this->getMimeTypeIdFromDB('application/mspowerpoint')); + } + + /** + * Test that nothing happens and no error happens when all mimetypes are + * already correct and no old ones exist.. + */ + public function testDoNothingWhenOnlyNewFiles() { + $this->addEntries( + array( + array('test.doc', 'application/msword'), + array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), + array('test.xls', 'application/vnd.ms-excel'), + array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'), + array('test.ppt', 'application/vnd.ms-powerpoint'), + array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'), + ) + ); + + $this->repair->run(); + + // force mimetype reload + DummyFileCache::clearCachedMimeTypes(); + $this->storage->getCache()->loadMimeTypes(); + + $this->checkEntries( + array( + array('test.doc', 'application/msword'), + array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), + array('test.xls', 'application/vnd.ms-excel'), + array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'), + array('test.ppt', 'application/vnd.ms-powerpoint'), + array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'), + ) + ); + } +} + +/** + * Dummy class to access protected members + */ +class DummyFileCache extends \OC\Files\Cache\Cache { + + public static function clearCachedMimeTypes() { + self::$mimetypeIds = array(); + self::$mimetypes = array(); + } +} + diff --git a/tests/lib/user/backend.php b/tests/lib/user/backend.php index 1384c54a921..0d3914c7ca6 100644 --- a/tests/lib/user/backend.php +++ b/tests/lib/user/backend.php @@ -96,4 +96,21 @@ abstract class Test_User_Backend extends PHPUnit_Framework_TestCase { $this->assertSame($name1, $this->backend->checkPassword($name1, 'newpass1')); $this->assertFalse($this->backend->checkPassword($name2, 'newpass1')); } + + public function testSearch() { + $name1 = 'foobarbaz'; + $name2 = 'bazbarfoo'; + $name3 = 'notme'; + + $this->backend->createUser($name1, 'pass1'); + $this->backend->createUser($name2, 'pass2'); + $this->backend->createUser($name3, 'pass3'); + + $result = $this->backend->getUsers('bar'); + $this->assertSame(2, count($result)); + + $result = $this->backend->getDisplayNames('bar'); + $this->assertSame(2, count($result)); + } + } diff --git a/tests/lib/user/database.php b/tests/lib/user/database.php index d7cc39ae387..a8e497720c2 100644 --- a/tests/lib/user/database.php +++ b/tests/lib/user/database.php @@ -32,6 +32,9 @@ class Test_User_Database extends Test_User_Backend { } public function tearDown() { + if(!isset($this->users)) { + return; + } foreach($this->users as $user) { $this->backend->deleteUser($user); } diff --git a/tests/lib/util.php b/tests/lib/util.php index aaa47f033de..c2bb99c3b2e 100644 --- a/tests/lib/util.php +++ b/tests/lib/util.php @@ -290,4 +290,72 @@ class Test_Util extends PHPUnit_Framework_TestCase { array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1', 'g2', 'g3'), true), ); } + + /** + * Test default apps + * + * @dataProvider defaultAppsProvider + */ + function testDefaultApps($defaultAppConfig, $expectedPath, $enabledApps) { + $oldDefaultApps = \OCP\Config::getSystemValue('core', 'defaultapp', ''); + // CLI is doing messy stuff with the webroot, so need to work it around + $oldWebRoot = \OC::$WEBROOT; + \OC::$WEBROOT = ''; + + Dummy_OC_App::setEnabledApps($enabledApps); + \OCP\Config::setSystemValue('defaultapp', $defaultAppConfig); + $this->assertEquals('http://localhost/' . $expectedPath, \OC_Util::getDefaultPageUrl()); + + // restore old state + \OC::$WEBROOT = $oldWebRoot; + Dummy_OC_App::restore(); + \OCP\Config::setSystemValue('defaultapp', $oldDefaultApps); + } + + function defaultAppsProvider() { + return array( + // none specified, default to files + array( + '', + 'index.php/apps/files/', + array('files'), + ), + // unexisting or inaccessible app specified, default to files + array( + 'unexist', + 'index.php/apps/files/', + array('files'), + ), + // non-standard app + array( + 'calendar', + 'index.php/apps/calendar/', + array('files', 'calendar'), + ), + // non-standard app with fallback + array( + 'contacts,calendar', + 'index.php/apps/calendar/', + array('files', 'calendar'), + ), + ); + } + +} + +/** + * Dummy OC Apps class to make it possible to override + * enabled apps + */ +class Dummy_OC_App extends OC_App { + private static $enabledAppsCacheBackup; + + public static function setEnabledApps($enabledApps) { + self::$enabledAppsCacheBackup = self::$enabledAppsCache; + self::$enabledAppsCache = $enabledApps; + } + + public static function restore() { + self::$enabledAppsCache = self::$enabledAppsCacheBackup; + } } |