diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/apps.php | 3 | ||||
-rw-r--r-- | tests/core/command/encryption/disabletest.php | 2 | ||||
-rw-r--r-- | tests/core/command/encryption/enabletest.php | 50 | ||||
-rw-r--r-- | tests/data/app/code-checker/test-equal.php | 11 | ||||
-rw-r--r-- | tests/data/app/code-checker/test-identical-operator.php | 13 | ||||
-rw-r--r-- | tests/data/app/code-checker/test-not-equal.php | 11 | ||||
-rw-r--r-- | tests/lib/app/codechecker.php | 25 | ||||
-rw-r--r-- | tests/lib/appconfig.php | 225 | ||||
-rw-r--r-- | tests/lib/connector/sabre/copyetagheaderplugintest.php | 2 | ||||
-rw-r--r-- | tests/lib/connector/sabre/file.php | 232 | ||||
-rw-r--r-- | tests/lib/connector/sabre/quotaplugin.php | 5 | ||||
-rw-r--r-- | tests/lib/encryption/updatetest.php | 115 | ||||
-rw-r--r-- | tests/lib/encryption/utiltest.php | 1 | ||||
-rw-r--r-- | tests/lib/files/storage/wrapper/encryption.php | 133 | ||||
-rw-r--r-- | tests/lib/helper.php | 23 | ||||
-rw-r--r-- | tests/lib/hookhelper.php | 112 | ||||
-rw-r--r-- | tests/lib/share/share.php | 33 | ||||
-rw-r--r-- | tests/lib/testcase.php | 2 | ||||
-rw-r--r-- | tests/settings/controller/userscontrollertest.php | 70 |
19 files changed, 887 insertions, 181 deletions
diff --git a/tests/apps.php b/tests/apps.php index 3e27b81df61..f13a996772f 100644 --- a/tests/apps.php +++ b/tests/apps.php @@ -7,6 +7,9 @@ */ function loadDirectory($path) { + if (strpos($path, 'integration')) { + return; + } if ($dh = opendir($path)) { while ($name = readdir($dh)) { if ($name[0] !== '.') { diff --git a/tests/core/command/encryption/disabletest.php b/tests/core/command/encryption/disabletest.php index 48a6539b243..26c814a9c4e 100644 --- a/tests/core/command/encryption/disabletest.php +++ b/tests/core/command/encryption/disabletest.php @@ -72,7 +72,7 @@ class DisableTest extends TestCase { $this->consoleOutput->expects($this->once()) ->method('writeln') - ->with($expectedString); + ->with($this->stringContains($expectedString)); if ($isUpdating) { $this->config->expects($this->once()) diff --git a/tests/core/command/encryption/enabletest.php b/tests/core/command/encryption/enabletest.php index 217329ca291..377d0e2a528 100644 --- a/tests/core/command/encryption/enabletest.php +++ b/tests/core/command/encryption/enabletest.php @@ -29,6 +29,8 @@ class EnableTest extends TestCase { /** @var \PHPUnit_Framework_MockObject_MockObject */ protected $config; /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $manager; + /** @var \PHPUnit_Framework_MockObject_MockObject */ protected $consoleInput; /** @var \PHPUnit_Framework_MockObject_MockObject */ protected $consoleOutput; @@ -42,18 +44,25 @@ class EnableTest extends TestCase { $config = $this->config = $this->getMockBuilder('OCP\IConfig') ->disableOriginalConstructor() ->getMock(); + $manager = $this->manager = $this->getMockBuilder('OCP\Encryption\IManager') + ->disableOriginalConstructor() + ->getMock(); $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); /** @var \OCP\IConfig $config */ - $this->command = new Enable($config); + /** @var \OCP\Encryption\IManager $manager */ + $this->command = new Enable($config, $manager); } public function dataEnable() { return [ - ['no', true, 'Encryption enabled'], - ['yes', false, 'Encryption is already enabled'], + ['no', null, [], true, 'Encryption enabled', 'No encryption module is loaded'], + ['yes', null, [], false, 'Encryption is already enabled', 'No encryption module is loaded'], + ['no', null, ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'No default module is set'], + ['no', 'OC_NO_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'The current default module does not exist: OC_NO_MODULE'], + ['no', 'OC_TEST_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'Default module: OC_TEST_MODULE'], ]; } @@ -61,36 +70,49 @@ class EnableTest extends TestCase { * @dataProvider dataEnable * * @param string $oldStatus + * @param string $defaultModule + * @param array $availableModules * @param bool $isUpdating * @param string $expectedString + * @param string $expectedDefaultModuleString */ - public function testEnable($oldStatus, $isUpdating, $expectedString) { - $invoceCount = 0; - $this->config->expects($this->at($invoceCount)) + public function testEnable($oldStatus, $defaultModule, $availableModules, $isUpdating, $expectedString, $expectedDefaultModuleString) { + $invokeCount = 0; + $this->config->expects($this->at($invokeCount)) ->method('getAppValue') ->with('core', 'encryption_enabled', $this->anything()) ->willReturn($oldStatus); - $invoceCount++; + $invokeCount++; if ($isUpdating) { $this->config->expects($this->once()) ->method('setAppValue') ->with('core', 'encryption_enabled', 'yes'); - $invoceCount++; + $invokeCount++; } - $this->config->expects($this->at($invoceCount)) - ->method('getAppValue') - ->with('core', 'default_encryption_module', $this->anything()) - ->willReturnArgument(2); + $this->manager->expects($this->atLeastOnce()) + ->method('getEncryptionModules') + ->willReturn($availableModules); + + if (!empty($availableModules)) { + $this->config->expects($this->at($invokeCount)) + ->method('getAppValue') + ->with('core', 'default_encryption_module', $this->anything()) + ->willReturn($defaultModule); + } $this->consoleOutput->expects($this->at(0)) ->method('writeln') - ->with($expectedString); + ->with($this->stringContains($expectedString)); $this->consoleOutput->expects($this->at(1)) ->method('writeln') - ->with($this->stringContains('Default module')); + ->with(''); + + $this->consoleOutput->expects($this->at(2)) + ->method('writeln') + ->with($this->stringContains($expectedDefaultModuleString)); \Test_Helper::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } diff --git a/tests/data/app/code-checker/test-equal.php b/tests/data/app/code-checker/test-equal.php new file mode 100644 index 00000000000..90543ba7258 --- /dev/null +++ b/tests/data/app/code-checker/test-equal.php @@ -0,0 +1,11 @@ +<?php + +/** + * Class BadClass - uses equal instead of identical operator + */ +class BadClass { + public function foo() { + if (true == false) { + } + } +} diff --git a/tests/data/app/code-checker/test-identical-operator.php b/tests/data/app/code-checker/test-identical-operator.php new file mode 100644 index 00000000000..4c7641ede89 --- /dev/null +++ b/tests/data/app/code-checker/test-identical-operator.php @@ -0,0 +1,13 @@ +<?php + +/** + * Class GoodClass - uses identical operator + */ +class GoodClass { + public function foo() { + if (true === false) { + } + if (true !== false) { + } + } +} diff --git a/tests/data/app/code-checker/test-not-equal.php b/tests/data/app/code-checker/test-not-equal.php new file mode 100644 index 00000000000..d9a8d1c25c6 --- /dev/null +++ b/tests/data/app/code-checker/test-not-equal.php @@ -0,0 +1,11 @@ +<?php + +/** + * Class BadClass - uses equal instead of identical operator + */ +class BadClass { + public function foo() { + if (true != false) { + } + } +} diff --git a/tests/lib/app/codechecker.php b/tests/lib/app/codechecker.php index 64403fd0f23..f45ee02d185 100644 --- a/tests/lib/app/codechecker.php +++ b/tests/lib/app/codechecker.php @@ -9,12 +9,14 @@ namespace Test\App; use OC; +use Test\TestCase; -class CodeChecker extends \Test\TestCase { +class CodeChecker extends TestCase { /** * @dataProvider providesFilesToCheck - * @param $expectedErrors + * @param $expectedErrorToken + * @param $expectedErrorCode * @param $fileToVerify */ public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { @@ -33,6 +35,25 @@ class CodeChecker extends \Test\TestCase { ['OC_App', 1002, 'test-static-call.php'], ['OC_API', 1003, 'test-const.php'], ['OC_AppConfig', 1004, 'test-new.php'], + ['==', 1005, 'test-equal.php'], + ['!=', 1005, 'test-not-equal.php'], + ]; + } + + /** + * @dataProvider validFilesData + * @param $fileToVerify + */ + public function testPassValidUsage($fileToVerify) { + $checker = new OC\App\CodeChecker(); + $errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(0, count($errors)); + } + + public function validFilesData() { + return [ + ['test-identical-operator.php'], ]; } } diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php index ead5b859277..adff45706cc 100644 --- a/tests/lib/appconfig.php +++ b/tests/lib/appconfig.php @@ -8,16 +8,25 @@ */ class Test_Appconfig extends \Test\TestCase { - public static function setUpBeforeClass() { - parent::setUpBeforeClass(); + /** @var \OCP\IAppConfig */ + protected $appConfig; - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + /** @var \OCP\IDBConnection */ + protected $connection; + + public function setUp() { + parent::setUp(); + + $this->connection = \OC::$server->getDatabaseConnection(); + $this->registerAppConfig(new \OC\AppConfig(\OC::$server->getDatabaseConnection())); + + $query = $this->connection->prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?'); $query->execute(array('testapp')); $query->execute(array('someapp')); $query->execute(array('123456')); $query->execute(array('anotherapp')); - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*appconfig` VALUES (?, ?, ?)'); + $query = $this->connection->prepare('INSERT INTO `*PREFIX*appconfig` VALUES (?, ?, ?)'); $query->execute(array('testapp', 'enabled', 'true')); $query->execute(array('testapp', 'installed_version', '1.2.3')); @@ -35,17 +44,41 @@ class Test_Appconfig extends \Test\TestCase { $query->execute(array('anotherapp', 'enabled', 'false')); } - public static function tearDownAfterClass() { - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + public function tearDown() { + $query = $this->connection->prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?'); $query->execute(array('testapp')); $query->execute(array('someapp')); $query->execute(array('123456')); $query->execute(array('anotherapp')); - parent::tearDownAfterClass(); + $this->registerAppConfig(new \OC\AppConfig(\OC::$server->getDatabaseConnection())); + parent::tearDown(); + } + + /** + * Register an app config object for testing purposes. + * + * @param \OCP\IAppConfig $appConfig + */ + protected function registerAppConfig($appConfig) { + \OC::$server->registerService('AppConfig', function ($c) use ($appConfig) { + return $appConfig; + }); } - public function testGetApps() { + public function getAppConfigs() { + return [ + ['\OC_Appconfig'], + [new \OC\AppConfig(\OC::$server->getDatabaseConnection())], + ]; + } + + /** + * @dataProvider getAppConfigs + * + * @param mixed $callable + */ + public function testGetApps($callable) { $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*appconfig` ORDER BY `appid`'); $result = $query->execute(); $expected = array(); @@ -53,11 +86,16 @@ class Test_Appconfig extends \Test\TestCase { $expected[] = $row['appid']; } sort($expected); - $apps = \OC_Appconfig::getApps(); + $apps = call_user_func([$callable, 'getApps']); $this->assertEquals($expected, $apps); } - public function testGetKeys() { + /** + * @dataProvider getAppConfigs + * + * @param mixed $callable + */ + public function testGetKeys($callable) { $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); $result = $query->execute(array('testapp')); $expected = array(); @@ -65,44 +103,103 @@ class Test_Appconfig extends \Test\TestCase { $expected[] = $row["configkey"]; } sort($expected); - $keys = \OC_Appconfig::getKeys('testapp'); + $keys = call_user_func([$callable, 'getKeys'], 'testapp'); $this->assertEquals($expected, $keys); } - public function testGetValue() { - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('testapp', 'installed_version')); - $expected = $result->fetchRow(); - $value = \OC_Appconfig::getValue('testapp', 'installed_version'); - $this->assertEquals($expected['configvalue'], $value); + /** + * @dataProvider getAppConfigs + * + * @param mixed $callable + */ + public function testGetValue($callable) { + $value = call_user_func([$callable, 'getValue'], 'testapp', 'installed_version'); + $this->assertConfigKey('testapp', 'installed_version', $value); - $value = \OC_Appconfig::getValue('testapp', 'nonexistant'); + $value = call_user_func([$callable, 'getValue'], 'testapp', 'nonexistant'); $this->assertNull($value); - $value = \OC_Appconfig::getValue('testapp', 'nonexistant', 'default'); + $value = call_user_func([$callable, 'getValue'], 'testapp', 'nonexistant', 'default'); $this->assertEquals('default', $value); } - public function testHasKey() { - $value = \OC_Appconfig::hasKey('testapp', 'installed_version'); + /** + * @dataProvider getAppConfigs + * + * @param mixed $callable + */ + public function testHasKey($callable) { + $value = call_user_func([$callable, 'hasKey'], 'testapp', 'installed_version'); $this->assertTrue($value); - $value = \OC_Appconfig::hasKey('nonexistant', 'nonexistant'); + $value = call_user_func([$callable, 'hasKey'], 'nonexistant', 'nonexistant'); $this->assertFalse($value); } - public function testSetValue() { - \OC_Appconfig::setValue('testapp', 'installed_version', '1.33.7'); - $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('testapp', 'installed_version')); - $value = $result->fetchRow(); - $this->assertEquals('1.33.7', $value['configvalue']); + /** + * @dataProvider getAppConfigs + * + * @param mixed $callable + */ + public function testSetValue($callable) { + call_user_func([$callable, 'setValue'], 'testapp', 'installed_version', '1.33.7'); + $this->assertConfigKey('testapp', 'installed_version', '1.33.7'); - \OC_Appconfig::setValue('someapp', 'somekey', 'somevalue'); + call_user_func([$callable, 'setValue'], 'someapp', 'somekey', 'somevalue'); + $this->assertConfigKey('someapp', 'somekey', 'somevalue'); + } + + /** + * @dataProvider getAppConfigs + * + * @param mixed $callable + */ + public function testDeleteKey($callable) { + call_user_func([$callable, 'deleteKey'], 'testapp', 'deletethis'); $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); - $result = $query->execute(array('someapp', 'somekey')); - $value = $result->fetchRow(); - $this->assertEquals('somevalue', $value['configvalue']); + $query->execute(array('testapp', 'deletethis')); + $result = (bool)$query->fetchRow(); + $this->assertFalse($result); + } + + /** + * @dataProvider getAppConfigs + * + * @param mixed $callable + */ + public function testDeleteApp($callable) { + call_user_func([$callable, 'deleteApp'], 'someapp'); + $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + $query->execute(array('someapp')); + $result = (bool)$query->fetchRow(); + $this->assertFalse($result); + } + + /** + * @dataProvider getAppConfigs + * + * @param mixed $callable + */ + public function testGetValues($callable) { + $this->assertFalse(call_user_func([$callable, 'getValues'], 'testapp', 'enabled')); + + $query = \OC_DB::prepare('SELECT `configkey`, `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + $query->execute(array('testapp')); + $expected = array(); + while ($row = $query->fetchRow()) { + $expected[$row['configkey']] = $row['configvalue']; + } + $values = call_user_func([$callable, 'getValues'], 'testapp', false); + $this->assertEquals($expected, $values); + + $query = \OC_DB::prepare('SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig` WHERE `configkey` = ?'); + $query->execute(array('enabled')); + $expected = array(); + while ($row = $query->fetchRow()) { + $expected[$row['appid']] = $row['configvalue']; + } + $values = call_user_func([$callable, 'getValues'], false, 'enabled'); + $this->assertEquals($expected, $values); } public function testSetValueUnchanged() { @@ -118,7 +215,7 @@ class Test_Appconfig extends \Test\TestCase { .' WHERE `appid` = ?'), $this->equalTo(array('bar'))) ->will($this->returnValue($statementMock)); $connectionMock->expects($this->once()) - ->method('insert') + ->method('insertIfNotExist') ->with($this->equalTo('*PREFIX*appconfig'), $this->equalTo( array( @@ -126,7 +223,8 @@ class Test_Appconfig extends \Test\TestCase { 'configkey' => 'foo', 'configvalue' => 'v1', ) - )); + ), $this->equalTo(['appid', 'configkey'])) + ->willReturn(1); $connectionMock->expects($this->never()) ->method('update'); @@ -149,7 +247,7 @@ class Test_Appconfig extends \Test\TestCase { .' WHERE `appid` = ?'), $this->equalTo(array('bar'))) ->will($this->returnValue($statementMock)); $connectionMock->expects($this->once()) - ->method('insert') + ->method('insertIfNotExist') ->with($this->equalTo('*PREFIX*appconfig'), $this->equalTo( array( @@ -157,7 +255,8 @@ class Test_Appconfig extends \Test\TestCase { 'configkey' => 'foo', 'configvalue' => 'v1', ) - )); + ), $this->equalTo(['appid', 'configkey'])) + ->willReturn(1); $connectionMock->expects($this->once()) ->method('update') ->with($this->equalTo('*PREFIX*appconfig'), @@ -171,41 +270,29 @@ class Test_Appconfig extends \Test\TestCase { $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` = ?'); - $query->execute(array('testapp', 'deletethis')); - $result = (bool)$query->fetchRow(); - $this->assertFalse($result); - } - - public function testDeleteApp() { - \OC_Appconfig::deleteApp('someapp'); - $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); - $query->execute(array('someapp')); - $result = (bool)$query->fetchRow(); - $this->assertFalse($result); - } + public function testSettingConfigParallel() { + $appConfig1 = new OC\AppConfig(\OC::$server->getDatabaseConnection()); + $appConfig2 = new OC\AppConfig(\OC::$server->getDatabaseConnection()); + $appConfig1->getValue('testapp', 'foo', 'v1'); + $appConfig2->getValue('testapp', 'foo', 'v1'); - public function testGetValues() { - $this->assertFalse(\OC_Appconfig::getValues('testapp', 'enabled')); + $appConfig1->setValue('testapp', 'foo', 'v1'); + $this->assertConfigKey('testapp', 'foo', 'v1'); - $query = \OC_DB::prepare('SELECT `configkey`, `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); - $query->execute(array('testapp')); - $expected = array(); - while ($row = $query->fetchRow()) { - $expected[$row['configkey']] = $row['configvalue']; - } - $values = \OC_Appconfig::getValues('testapp', false); - $this->assertEquals($expected, $values); + $appConfig2->setValue('testapp', 'foo', 'v2'); + $this->assertConfigKey('testapp', 'foo', 'v2'); + } - $query = \OC_DB::prepare('SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig` WHERE `configkey` = ?'); - $query->execute(array('enabled')); - $expected = array(); - while ($row = $query->fetchRow()) { - $expected[$row['appid']] = $row['configvalue']; - } - $values = \OC_Appconfig::getValues(false, 'enabled'); - $this->assertEquals($expected, $values); + /** + * @param string $app + * @param string $key + * @param string $expected + * @throws \OC\DatabaseException + */ + protected function assertConfigKey($app, $key, $expected) { + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); + $result = $query->execute([$app, $key]); + $actual = $result->fetchRow(); + $this->assertEquals($expected, $actual['configvalue']); } } diff --git a/tests/lib/connector/sabre/copyetagheaderplugintest.php b/tests/lib/connector/sabre/copyetagheaderplugintest.php index 176f9c8d0ca..6b1b5e96fda 100644 --- a/tests/lib/connector/sabre/copyetagheaderplugintest.php +++ b/tests/lib/connector/sabre/copyetagheaderplugintest.php @@ -18,7 +18,7 @@ class CopyEtagPluginTest extends \Test\TestCase { public function setUp() { parent::setUp(); $this->server = new \Sabre\DAV\Server(); - $this->plugin = new \OC\Connector\Sabre\CopyEtagHeaderPlugin($this->tree); + $this->plugin = new \OC\Connector\Sabre\CopyEtagHeaderPlugin(); $this->plugin->initialize($this->server); } diff --git a/tests/lib/connector/sabre/file.php b/tests/lib/connector/sabre/file.php index ee9c20fd9cb..6602a2df24f 100644 --- a/tests/lib/connector/sabre/file.php +++ b/tests/lib/connector/sabre/file.php @@ -8,8 +8,35 @@ namespace Test\Connector\Sabre; +use Test\HookHelper; +use OC\Files\Filesystem; + class File extends \Test\TestCase { + /** + * @var string + */ + private $user; + + public function setUp() { + parent::setUp(); + + \OC_Hook::clear(); + + $this->user = $this->getUniqueID('user_'); + $userManager = \OC::$server->getUserManager(); + $userManager->createUser($this->user, 'pass'); + + $this->loginAsUser($this->user); + } + + public function tearDown() { + $userManager = \OC::$server->getUserManager(); + $userManager->get($this->user)->delete(); + + parent::tearDown(); + } + private function getStream($string) { $stream = fopen('php://temp', 'r+'); fwrite($stream, $string); @@ -23,7 +50,7 @@ class File extends \Test\TestCase { public function testSimplePutFails() { // setup $storage = $this->getMock('\OC\Files\Storage\Local', ['fopen'], [['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]]); - $view = $this->getMock('\OC\Files\View', array('file_put_contents', 'getRelativePath', 'resolvePath'), array()); + $view = $this->getMock('\OC\Files\View', array('getRelativePath', 'resolvePath'), array()); $view->expects($this->any()) ->method('resolvePath') ->will($this->returnValue(array($storage, ''))); @@ -45,28 +72,21 @@ class File extends \Test\TestCase { $file->put('test data'); } - public function testPutSingleFileShare() { - // setup - $stream = fopen('php://temp', 'w+'); - $storage = $this->getMock('\OC\Files\Storage\Local', ['fopen'], [['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]]); - $view = $this->getMock('\OC\Files\View', array('file_put_contents', 'getRelativePath', 'resolvePath'), array()); - $view->expects($this->any()) - ->method('resolvePath') - ->will($this->returnValue(array($storage, ''))); - $view->expects($this->any()) - ->method('getRelativePath') - ->will($this->returnValue('')); - $view->expects($this->any()) - ->method('file_put_contents') - ->with('') - ->will($this->returnValue(true)); - $storage->expects($this->once()) - ->method('fopen') - ->will($this->returnValue($stream)); - - $info = new \OC\Files\FileInfo('/foo.txt', null, null, array( - 'permissions' => \OCP\Constants::PERMISSION_ALL - ), null); + private function doPut($path, $viewRoot = null) { + $view = \OC\Files\Filesystem::getView(); + if (!is_null($viewRoot)) { + $view = new \OC\Files\View($viewRoot); + } else { + $viewRoot = '/' . $this->user . '/files'; + } + + $info = new \OC\Files\FileInfo( + $viewRoot . '/' . ltrim($path, '/'), + null, + null, + ['permissions' => \OCP\Constants::PERMISSION_ALL], + null + ); $file = new \OC\Connector\Sabre\File($view, $info); @@ -74,16 +94,144 @@ class File extends \Test\TestCase { } /** + * Test putting a single file + */ + public function testPutSingleFile() { + $this->doPut('/foo.txt'); + } + + /** + * Test that putting a file triggers create hooks + */ + public function testPutSingleFileTriggersHooks() { + HookHelper::setUpHooks(); + + $this->doPut('/foo.txt'); + + $this->assertCount(4, HookHelper::$hookCalls); + $this->assertHookCall( + HookHelper::$hookCalls[0], + Filesystem::signal_create, + '/foo.txt' + ); + $this->assertHookCall( + HookHelper::$hookCalls[1], + Filesystem::signal_write, + '/foo.txt' + ); + $this->assertHookCall( + HookHelper::$hookCalls[2], + Filesystem::signal_post_create, + '/foo.txt' + ); + $this->assertHookCall( + HookHelper::$hookCalls[3], + Filesystem::signal_post_write, + '/foo.txt' + ); + } + + /** + * Test that putting a file triggers update hooks + */ + public function testPutOverwriteFileTriggersHooks() { + $view = \OC\Files\Filesystem::getView(); + $view->file_put_contents('/foo.txt', 'some content that will be replaced'); + + HookHelper::setUpHooks(); + + $this->doPut('/foo.txt'); + + $this->assertCount(4, HookHelper::$hookCalls); + $this->assertHookCall( + HookHelper::$hookCalls[0], + Filesystem::signal_update, + '/foo.txt' + ); + $this->assertHookCall( + HookHelper::$hookCalls[1], + Filesystem::signal_write, + '/foo.txt' + ); + $this->assertHookCall( + HookHelper::$hookCalls[2], + Filesystem::signal_post_update, + '/foo.txt' + ); + $this->assertHookCall( + HookHelper::$hookCalls[3], + Filesystem::signal_post_write, + '/foo.txt' + ); + } + + /** + * Test that putting a file triggers hooks with the correct path + * if the passed view was chrooted (can happen with public webdav + * where the root is the share root) + */ + public function testPutSingleFileTriggersHooksDifferentRoot() { + $view = \OC\Files\Filesystem::getView(); + $view->mkdir('noderoot'); + + HookHelper::setUpHooks(); + + // happens with public webdav where the view root is the share root + $this->doPut('/foo.txt', '/' . $this->user . '/files/noderoot'); + + $this->assertCount(4, HookHelper::$hookCalls); + $this->assertHookCall( + HookHelper::$hookCalls[0], + Filesystem::signal_create, + '/noderoot/foo.txt' + ); + $this->assertHookCall( + HookHelper::$hookCalls[1], + Filesystem::signal_write, + '/noderoot/foo.txt' + ); + $this->assertHookCall( + HookHelper::$hookCalls[2], + Filesystem::signal_post_create, + '/noderoot/foo.txt' + ); + $this->assertHookCall( + HookHelper::$hookCalls[3], + Filesystem::signal_post_write, + '/noderoot/foo.txt' + ); + } + + public static function cancellingHook($params) { + self::$hookCalls[] = array( + 'signal' => Filesystem::signal_post_create, + 'params' => $params + ); + } + + /** + * Test put file with cancelled hook + * + * @expectedException \Sabre\DAV\Exception + */ + public function testPutSingleFileCancelPreHook() { + \OCP\Util::connectHook( + Filesystem::CLASSNAME, + Filesystem::signal_create, + '\Test\HookHelper', + 'cancellingCallback' + ); + + $this->doPut('/foo.txt'); + } + + /** * @expectedException \Sabre\DAV\Exception */ public function testSimplePutFailsOnRename() { // setup $view = $this->getMock('\OC\Files\View', - array('file_put_contents', 'rename', 'getRelativePath', 'filesize')); - $view->expects($this->any()) - ->method('file_put_contents') - ->withAnyParameters() - ->will($this->returnValue(true)); + array('rename', 'getRelativePath', 'filesize')); $view->expects($this->any()) ->method('rename') ->withAnyParameters() @@ -113,11 +261,7 @@ class File extends \Test\TestCase { */ public function testSimplePutInvalidChars() { // setup - $view = $this->getMock('\OC\Files\View', array('file_put_contents', 'getRelativePath')); - $view->expects($this->any()) - ->method('file_put_contents') - ->will($this->returnValue(false)); - + $view = $this->getMock('\OC\Files\View', array('getRelativePath')); $view->expects($this->any()) ->method('getRelativePath') ->will($this->returnValue('/*')); @@ -157,11 +301,7 @@ class File extends \Test\TestCase { public function testUploadAbort() { // setup $view = $this->getMock('\OC\Files\View', - array('file_put_contents', 'rename', 'getRelativePath', 'filesize')); - $view->expects($this->any()) - ->method('file_put_contents') - ->withAnyParameters() - ->will($this->returnValue(true)); + array('rename', 'getRelativePath', 'filesize')); $view->expects($this->any()) ->method('rename') ->withAnyParameters() @@ -248,4 +388,20 @@ class File extends \Test\TestCase { // action $file->delete(); } + + /** + * Asserts hook call + * + * @param array $callData hook call data to check + * @param string $signal signal name + * @param string $hookPath hook path + */ + protected function assertHookCall($callData, $signal, $hookPath) { + $this->assertEquals($signal, $callData['signal']); + $params = $callData['params']; + $this->assertEquals( + $hookPath, + $params[Filesystem::signal_param_path] + ); + } } diff --git a/tests/lib/connector/sabre/quotaplugin.php b/tests/lib/connector/sabre/quotaplugin.php index 48f8f319ae4..3d9cd9b5da0 100644 --- a/tests/lib/connector/sabre/quotaplugin.php +++ b/tests/lib/connector/sabre/quotaplugin.php @@ -92,7 +92,10 @@ class Test_OC_Connector_Sabre_QuotaPlugin extends \Test\TestCase { private function buildFileViewMock($quota) { // mock filesysten $view = $this->getMock('\OC\Files\View', array('free_space'), array(), '', false); - $view->expects($this->any())->method('free_space')->withAnyParameters()->will($this->returnValue($quota)); + $view->expects($this->any()) + ->method('free_space') + ->with($this->identicalTo('')) + ->will($this->returnValue($quota)); return $view; } diff --git a/tests/lib/encryption/updatetest.php b/tests/lib/encryption/updatetest.php index 790d071aa68..b222bc42d7a 100644 --- a/tests/lib/encryption/updatetest.php +++ b/tests/lib/encryption/updatetest.php @@ -68,10 +68,6 @@ class UpdateTest extends TestCase { $this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') ->disableOriginalConstructor()->getMock(); - $this->encryptionManager->expects($this->once()) - ->method('getEncryptionModule') - ->willReturn($this->encryptionModule); - $this->uid = 'testUser1'; $this->update = new Update( @@ -93,6 +89,10 @@ class UpdateTest extends TestCase { */ public function testUpdate($path, $isDir, $allFiles, $numberOfFiles) { + $this->encryptionManager->expects($this->once()) + ->method('getEncryptionModule') + ->willReturn($this->encryptionModule); + $this->view->expects($this->once()) ->method('is_dir') ->willReturn($isDir); @@ -126,4 +126,111 @@ class UpdateTest extends TestCase { ); } + /** + * @dataProvider dataTestPostRename + * + * @param string $source + * @param string $target + * @param boolean $encryptionEnabled + */ + public function testPostRename($source, $target, $encryptionEnabled) { + + $updateMock = $this->getUpdateMock(['update', 'getOwnerPath']); + + $this->encryptionManager->expects($this->once()) + ->method('isEnabled') + ->willReturn($encryptionEnabled); + + if (dirname($source) === dirname($target) || $encryptionEnabled === false) { + $updateMock->expects($this->never())->method('getOwnerPath'); + $updateMock->expects($this->never())->method('update'); + } else { + $updateMock->expects($this->once()) + ->method('getOwnerPath') + ->willReturnCallback(function($path) use ($target) { + $this->assertSame( + $target, + $path, + 'update needs to be executed for the target destination'); + return ['owner', $path]; + + }); + $updateMock->expects($this->once())->method('update'); + } + + $updateMock->postRename(['oldpath' => $source, 'newpath' => $target]); + } + + /** + * test data for testPostRename() + * + * @return array + */ + public function dataTestPostRename() { + return array( + array('/test.txt', '/testNew.txt', true), + array('/test.txt', '/testNew.txt', false), + array('/folder/test.txt', '/testNew.txt', true), + array('/folder/test.txt', '/testNew.txt', false), + array('/folder/test.txt', '/testNew.txt', true), + array('/test.txt', '/folder/testNew.txt', false), + ); + } + + + /** + * @dataProvider dataTestPostRestore + * + * @param boolean $encryptionEnabled + */ + public function testPostRestore($encryptionEnabled) { + + $updateMock = $this->getUpdateMock(['update']); + + $this->encryptionManager->expects($this->once()) + ->method('isEnabled') + ->willReturn($encryptionEnabled); + + if ($encryptionEnabled) { + $updateMock->expects($this->once())->method('update'); + + } else { + $updateMock->expects($this->never())->method('update'); + } + + $updateMock->postRestore(['filePath' => '/folder/test.txt']); + } + + /** + * test data for testPostRestore() + * + * @return array + */ + public function dataTestPostRestore() { + return array( + array(true), + array(false), + ); + } + + /** + * create mock of the update method + * + * @param array$methods methods which should be set + * @return \OC\Encryption\Update | \PHPUnit_Framework_MockObject_MockObject + */ + protected function getUpdateMock($methods) { + return $this->getMockBuilder('\OC\Encryption\Update') + ->setConstructorArgs( + [ + $this->view, + $this->util, + $this->mountManager, + $this->encryptionManager, + $this->fileHelper, + $this->uid + ] + )->setMethods($methods)->getMock(); + } + } diff --git a/tests/lib/encryption/utiltest.php b/tests/lib/encryption/utiltest.php index 7de57043920..0154fa30f7d 100644 --- a/tests/lib/encryption/utiltest.php +++ b/tests/lib/encryption/utiltest.php @@ -135,6 +135,7 @@ class UtilTest extends TestCase { public function providePathsForTestIsExcluded() { return array( + array('/files_encryption', true), array('files_encryption/foo.txt', true), array('test/foo.txt', false), array('/user1/files_encryption/foo.txt', true), diff --git a/tests/lib/files/storage/wrapper/encryption.php b/tests/lib/files/storage/wrapper/encryption.php index 6fc9b17fad0..97810c9c5dd 100644 --- a/tests/lib/files/storage/wrapper/encryption.php +++ b/tests/lib/files/storage/wrapper/encryption.php @@ -13,7 +13,7 @@ class Encryption extends \Test\Files\Storage\Storage { private $sourceStorage; /** - * @var \OC\Files\Storage\Wrapper\Encryption + * @var \OC\Files\Storage\Wrapper\Encryption | \PHPUnit_Framework_MockObject_MockObject */ protected $instance; @@ -27,7 +27,6 @@ class Encryption extends \Test\Files\Storage\Storage { */ private $util; - /** * @var \OC\Encryption\Manager | \PHPUnit_Framework_MockObject_MockObject */ @@ -38,12 +37,19 @@ class Encryption extends \Test\Files\Storage\Storage { */ private $encryptionModule; - /** * @var \OC\Encryption\Update | \PHPUnit_Framework_MockObject_MockObject */ private $update; + /** + * @var \OC\Files\Cache\Cache | \PHPUnit_Framework_MockObject_MockObject + */ + private $cache; + + /** @var integer dummy unencrypted size */ + private $dummySize = -1; + protected function setUp() { parent::setUp(); @@ -56,9 +62,6 @@ class Encryption extends \Test\Files\Storage\Storage { $this->encryptionManager->expects($this->any()) ->method('getEncryptionModule') ->willReturn($mockModule); - $this->encryptionManager->expects($this->any()) - ->method('isEnabled') - ->willReturn(true); $config = $this->getMockBuilder('\OCP\IConfig') ->disableOriginalConstructor() @@ -83,23 +86,47 @@ class Encryption extends \Test\Files\Storage\Storage { $logger = $this->getMock('\OC\Log'); $this->sourceStorage = new Temporary(array()); + $this->keyStore = $this->getMockBuilder('\OC\Encryption\Keys\Storage') ->disableOriginalConstructor()->getMock(); + $this->update = $this->getMockBuilder('\OC\Encryption\Update') ->disableOriginalConstructor()->getMock(); + $mount = $this->getMockBuilder('\OC\Files\Mount\MountPoint') ->disableOriginalConstructor() ->setMethods(['getOption']) ->getMock(); $mount->expects($this->any())->method('getOption')->willReturn(true); - $this->instance = new \OC\Files\Storage\Wrapper\Encryption([ - 'storage' => $this->sourceStorage, - 'root' => 'foo', - 'mountPoint' => '/', - 'mount' => $mount - ], - $this->encryptionManager, $this->util, $logger, $file, null, $this->keyStore, $this->update - ); + + $this->cache = $this->getMockBuilder('\OC\Files\Cache\Cache') + ->disableOriginalConstructor()->getMock(); + $this->cache->expects($this->any()) + ->method('get') + ->willReturn(['encrypted' => false]); + + $this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption') + ->setConstructorArgs( + [ + [ + 'storage' => $this->sourceStorage, + 'root' => 'foo', + 'mountPoint' => '/', + 'mount' => $mount + ], + $this->encryptionManager, $this->util, $logger, $file, null, $this->keyStore, $this->update + ] + ) + ->setMethods(['getMetaData', 'getCache']) + ->getMock(); + + $this->instance->expects($this->any()) + ->method('getMetaData') + ->willReturn(['encrypted' => true, 'size' => $this->dummySize]); + + $this->instance->expects($this->any()) + ->method('getCache') + ->willReturn($this->cache); } /** @@ -128,23 +155,26 @@ class Encryption extends \Test\Files\Storage\Storage { * * @param string $source * @param string $target + * @param $encryptionEnabled * @param boolean $renameKeysReturn - * @param boolean $shouldUpdate */ - public function testRename($source, $target, $renameKeysReturn, $shouldUpdate) { - $this->keyStore - ->expects($this->once()) - ->method('renameKeys') - ->willReturn($renameKeysReturn); - $this->util->expects($this->any()) - ->method('isFile')->willReturn(true); - if ($shouldUpdate) { - $this->update->expects($this->once()) - ->method('update'); + public function testRename($source, + $target, + $encryptionEnabled, + $renameKeysReturn) { + if ($encryptionEnabled) { + $this->keyStore + ->expects($this->once()) + ->method('renameKeys') + ->willReturn($renameKeysReturn); } else { - $this->update->expects($this->never()) - ->method('update'); + $this->keyStore + ->expects($this->never())->method('renameKeys'); } + $this->util->expects($this->any()) + ->method('isFile')->willReturn(true); + $this->encryptionManager->expects($this->once()) + ->method('isEnabled')->willReturn($encryptionEnabled); $this->instance->mkdir($source); $this->instance->mkdir(dirname($target)); @@ -156,16 +186,33 @@ class Encryption extends \Test\Files\Storage\Storage { * * @param string $source * @param string $target + * @param $encryptionEnabled * @param boolean $copyKeysReturn * @param boolean $shouldUpdate */ - public function testCopyTesting($source, $target, $copyKeysReturn, $shouldUpdate) { - $this->keyStore - ->expects($this->once()) - ->method('copyKeys') - ->willReturn($copyKeysReturn); + public function testCopyEncryption($source, + $target, + $encryptionEnabled, + $copyKeysReturn, + $shouldUpdate) { + + if ($encryptionEnabled) { + $this->keyStore + ->expects($this->once()) + ->method('copyKeys') + ->willReturn($copyKeysReturn); + $this->cache->expects($this->once()) + ->method('put') + ->with($this->anything(), ['encrypted' => true]) + ->willReturn(true); + } else { + $this->cache->expects($this->never())->method('put'); + $this->keyStore->expects($this->never())->method('copyKeys'); + } $this->util->expects($this->any()) ->method('isFile')->willReturn(true); + $this->encryptionManager->expects($this->once()) + ->method('isEnabled')->willReturn($encryptionEnabled); if ($shouldUpdate) { $this->update->expects($this->once()) ->method('update'); @@ -177,13 +224,12 @@ class Encryption extends \Test\Files\Storage\Storage { $this->instance->mkdir($source); $this->instance->mkdir(dirname($target)); $this->instance->copy($source, $target); - } - /** - * @dataProvider copyAndMoveProvider - */ - public function testCopy($source, $target) { - $this->assertTrue(true, 'Replaced by testCopyTesting()'); + if ($encryptionEnabled) { + $this->assertSame($this->dummySize, + $this->instance->filesize($target) + ); + } } /** @@ -193,14 +239,17 @@ class Encryption extends \Test\Files\Storage\Storage { */ public function dataTestCopyAndRename() { return array( - array('source', 'target', false, false), - array('source', 'target', true, false), - array('source', '/subFolder/target', false, false), - array('source', '/subFolder/target', true, true), + array('source', 'target', true, false, false), + array('source', 'target', true, true, false), + array('source', '/subFolder/target', true, false, false), + array('source', '/subFolder/target', true, true, true), + array('source', '/subFolder/target', false, true, false), ); } public function testIsLocal() { + $this->encryptionManager->expects($this->once()) + ->method('isEnabled')->willReturn(true); $this->assertFalse($this->instance->isLocal()); } } diff --git a/tests/lib/helper.php b/tests/lib/helper.php index 53a3e1a0ec8..b7aa185f4e3 100644 --- a/tests/lib/helper.php +++ b/tests/lib/helper.php @@ -53,21 +53,22 @@ class Test_Helper extends \Test\TestCase { } /** - * @dataProvider computerFileSizeProvider + * @dataProvider providesComputerFileSize */ function testComputerFileSize($expected, $input) { $result = OC_Helper::computerFileSize($input); $this->assertEquals($expected, $result); } - function computerFileSizeProvider(){ - return array( - array(0.0, "0 B"), - array(1024.0, "1 kB"), - array(1395864371.0, '1.3 GB'), - array(9961472.0, "9.5 MB"), - array(500041567437.0, "465.7 GB"), - ); + function providesComputerFileSize(){ + return [ + [0.0, "0 B"], + [1024.0, "1 kB"], + [1395864371.0, '1.3 GB'], + [9961472.0, "9.5 MB"], + [500041567437.0, "465.7 GB"], + [false, "12 GB etfrhzui"] + ]; } function testGetMimeType() { @@ -523,6 +524,10 @@ class Test_Helper extends \Test\TestCase { $property->setAccessible(true); + if (!empty($parameters)) { + $property->setValue($object, array_pop($parameters)); + } + return $property->getValue($object); } diff --git a/tests/lib/hookhelper.php b/tests/lib/hookhelper.php new file mode 100644 index 00000000000..93411bd068b --- /dev/null +++ b/tests/lib/hookhelper.php @@ -0,0 +1,112 @@ +<?php +/** + * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test; + +use OC\Files\Filesystem; + +/** + * Helper class to register hooks on + */ +class HookHelper { + public static $hookCalls; + + public static function setUpHooks() { + self::clear(); + \OCP\Util::connectHook( + Filesystem::CLASSNAME, + Filesystem::signal_create, + '\Test\HookHelper', + 'createCallback' + ); + \OCP\Util::connectHook( + Filesystem::CLASSNAME, + Filesystem::signal_update, + '\Test\HookHelper', + 'updateCallback' + ); + \OCP\Util::connectHook( + Filesystem::CLASSNAME, + Filesystem::signal_write, + '\Test\HookHelper', + 'writeCallback' + ); + + \OCP\Util::connectHook( + Filesystem::CLASSNAME, + Filesystem::signal_post_create, + '\Test\HookHelper', + 'postCreateCallback' + ); + \OCP\Util::connectHook( + Filesystem::CLASSNAME, + Filesystem::signal_post_update, + '\Test\HookHelper', + 'postUpdateCallback' + ); + \OCP\Util::connectHook( + Filesystem::CLASSNAME, + Filesystem::signal_post_write, + '\Test\HookHelper', + 'postWriteCallback' + ); + } + + public static function clear() { + self::$hookCalls = []; + } + + public static function createCallback($params) { + self::$hookCalls[] = array( + 'signal' => Filesystem::signal_create, + 'params' => $params + ); + } + + public static function updateCallback($params) { + self::$hookCalls[] = array( + 'signal' => Filesystem::signal_update, + 'params' => $params + ); + } + + public static function writeCallback($params) { + self::$hookCalls[] = array( + 'signal' => Filesystem::signal_write, + 'params' => $params + ); + } + + public static function postCreateCallback($params) { + self::$hookCalls[] = array( + 'signal' => Filesystem::signal_post_create, + 'params' => $params + ); + } + + public static function postUpdateCallback($params) { + self::$hookCalls[] = array( + 'signal' => Filesystem::signal_post_update, + 'params' => $params + ); + } + + public static function postWriteCallback($params) { + self::$hookCalls[] = array( + 'signal' => Filesystem::signal_post_write, + 'params' => $params + ); + } + + /** + * Callback that sets the run paramter to false + */ + public static function cancellingCallback($params) { + $params[Filesystem::signal_param_run] = false; + } +} diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php index abdddfb5584..5909102f797 100644 --- a/tests/lib/share/share.php +++ b/tests/lib/share/share.php @@ -501,6 +501,38 @@ class Test_Share extends \Test\TestCase { } + public function testSharingAFolderThatIsSharedWithAGroupOfTheOwner() { + OC_User::setUserId($this->user1); + $view = new \OC\Files\View('/' . $this->user1 . '/'); + $view->mkdir('files/test'); + $view->mkdir('files/test/sub1'); + $view->mkdir('files/test/sub1/sub2'); + + $fileInfo = $view->getFileInfo('files/test/sub1'); + $fileId = $fileInfo->getId(); + + $this->assertTrue( + OCP\Share::shareItem('folder', $fileId, OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_CREATE), + 'Failed asserting that user 1 successfully shared "test/sub1" with group 1.' + ); + + $result = OCP\Share::getItemShared('folder', $fileId, Test_Share_Backend::FORMAT_SOURCE); + $this->assertNotEmpty($result); + $this->assertEquals(\OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_CREATE, $result['permissions']); + + $fileInfo = $view->getFileInfo('files/test/sub1/sub2'); + $fileId = $fileInfo->getId(); + + $this->assertTrue( + OCP\Share::shareItem('folder', $fileId, OCP\Share::SHARE_TYPE_USER, $this->user4, \OCP\Constants::PERMISSION_READ), + 'Failed asserting that user 1 successfully shared "test/sub1/sub2" with user 4.' + ); + + $result = OCP\Share::getItemShared('folder', $fileId, Test_Share_Backend::FORMAT_SOURCE); + $this->assertNotEmpty($result); + $this->assertEquals(\OCP\Constants::PERMISSION_READ, $result['permissions']); + } + protected function shareUserOneTestFileWithGroupOne() { OC_User::setUserId($this->user1); $this->assertTrue( @@ -766,6 +798,7 @@ class Test_Share extends \Test\TestCase { /** * @param boolean|string $token + * @return array */ protected function getShareByValidToken($token) { $row = OCP\Share::getShareByToken($token); diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index e66dfb13353..76d5662da9d 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -182,6 +182,8 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { static protected function logout() { \OC_Util::tearDownFS(); \OC_User::setUserId(''); + // needed for fully logout + \OC::$server->getUserSession()->setUser(null); } /** diff --git a/tests/settings/controller/userscontrollertest.php b/tests/settings/controller/userscontrollertest.php index 0e34de90914..e70b235f603 100644 --- a/tests/settings/controller/userscontrollertest.php +++ b/tests/settings/controller/userscontrollertest.php @@ -1388,4 +1388,74 @@ class UsersControllerTest extends \Test\TestCase { $this->assertEquals($expectedResult, $result); } + public function setEmailAddressData() { + return [ + ['', true, false, true], + ['foobar@localhost', true, true, false], + ['foo@bar@localhost', false, false, false], + ]; + } + + /** + * @dataProvider setEmailAddressData + * + * @param string $mailAddress + * @param bool $isValid + * @param bool $expectsUpdate + * @param bool $expectsDelete + */ + public function testSetEmailAddress($mailAddress, $isValid, $expectsUpdate, $expectsDelete) { + $this->container['IsAdmin'] = true; + + $user = $this->getMockBuilder('\OC\User\User') + ->disableOriginalConstructor()->getMock(); + $user + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('foo')); + $this->container['UserSession'] + ->expects($this->atLeastOnce()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->container['Mailer'] + ->expects($this->any()) + ->method('validateMailAddress') + ->with($mailAddress) + ->willReturn($isValid); + + if ($isValid) { + $user->expects($this->atLeastOnce()) + ->method('canChangeDisplayName') + ->willReturn(true); + + $this->container['UserManager'] + ->expects($this->atLeastOnce()) + ->method('get') + ->with('foo') + ->will($this->returnValue($user)); + } + + $this->container['Config'] + ->expects(($expectsUpdate) ? $this->once() : $this->never()) + ->method('setUserValue') + ->with( + $this->equalTo($user->getUID()), + $this->equalTo('settings'), + $this->equalTo('email'), + $this->equalTo($mailAddress) + + ); + $this->container['Config'] + ->expects(($expectsDelete) ? $this->once() : $this->never()) + ->method('deleteUserValue') + ->with( + $this->equalTo($user->getUID()), + $this->equalTo('settings'), + $this->equalTo('email') + + ); + + $this->container['UsersController']->setMailAddress($user->getUID(), $mailAddress); + } + } |