diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Core/Command/Group/AddUserTest.php | 119 | ||||
-rw-r--r-- | tests/Core/Command/Group/ListCommandTest.php | 127 | ||||
-rw-r--r-- | tests/Core/Command/Group/RemoveUserTest.php | 119 | ||||
-rw-r--r-- | tests/Core/Command/User/DisableTest.php | 94 | ||||
-rw-r--r-- | tests/Core/Command/User/EnableTest.php | 94 | ||||
-rw-r--r-- | tests/Core/Controller/TwoFactorChallengeControllerTest.php | 1 | ||||
-rw-r--r-- | tests/Core/Controller/UserControllerTest.php | 76 | ||||
-rw-r--r-- | tests/lib/Files/FilesystemTest.php | 6 | ||||
-rw-r--r-- | tests/lib/Files/ObjectStore/S3Test.php | 3 | ||||
-rw-r--r-- | tests/lib/Http/Client/ClientTest.php | 71 | ||||
-rw-r--r-- | tests/lib/Http/Client/ResponseTest.php | 27 | ||||
-rw-r--r-- | tests/lib/Settings/ManagerTest.php | 229 | ||||
-rw-r--r-- | tests/lib/Settings/MapperTest.php | 139 | ||||
-rw-r--r-- | tests/preseed-config.php | 17 |
14 files changed, 975 insertions, 147 deletions
diff --git a/tests/Core/Command/Group/AddUserTest.php b/tests/Core/Command/Group/AddUserTest.php new file mode 100644 index 00000000000..af860e244d6 --- /dev/null +++ b/tests/Core/Command/Group/AddUserTest.php @@ -0,0 +1,119 @@ +<?php +/** + * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace Test\Core\Command\Group; + +use OC\Core\Command\Group\AddUser; +use OCP\IGroup; +use OCP\IGroupManager; +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class AddUserTest extends TestCase { + + /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ + private $groupManager; + + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + private $userManager; + + /** @var AddUser */ + private $command; + + /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $input; + + /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $output; + + public function setUp() { + parent::setUp(); + + $this->groupManager = $this->createMock(IGroupManager::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->command = new AddUser($this->userManager, $this->groupManager); + + $this->input = $this->createMock(InputInterface::class); + $this->input->method('getArgument') + ->willReturnCallback(function($arg) { + if ($arg === 'group') { + return 'myGroup'; + } else if ($arg === 'user') { + return 'myUser'; + } + throw new \Exception(); + }); + $this->output = $this->createMock(OutputInterface::class); + } + + public function testNoGroup() { + $this->groupManager->method('get') + ->with('myGroup') + ->willReturn(null); + + $this->output->expects($this->once()) + ->method('writeln') + ->with('<error>group not found</error>'); + + $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]); + } + + public function testNoUser() { + $group = $this->createMock(IGroup::class); + $this->groupManager->method('get') + ->with('myGroup') + ->willReturn($group); + + $this->userManager->method('get') + ->with('myUser') + ->willReturn(null); + + $this->output->expects($this->once()) + ->method('writeln') + ->with('<error>user not found</error>'); + + $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]); + } + + public function testAdd() { + $group = $this->createMock(IGroup::class); + $this->groupManager->method('get') + ->with('myGroup') + ->willReturn($group); + + $user = $this->createMock(IUser::class); + $this->userManager->method('get') + ->with('myUser') + ->willReturn($user); + + $group->expects($this->once()) + ->method('addUser') + ->with($user); + + $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]); + } + + +} diff --git a/tests/Core/Command/Group/ListCommandTest.php b/tests/Core/Command/Group/ListCommandTest.php new file mode 100644 index 00000000000..ac872e74ac8 --- /dev/null +++ b/tests/Core/Command/Group/ListCommandTest.php @@ -0,0 +1,127 @@ +<?php +/** + * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace Test\Core\Command\Group; + +use OC\Core\Command\Group\ListCommand; +use OCP\IGroup; +use OCP\IGroupManager; +use OCP\IUser; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class ListCommandTest extends TestCase { + + /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ + private $groupManager; + + /** @var ListCommand|\PHPUnit_Framework_MockObject_MockObject */ + private $command; + + /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $input; + + /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $output; + + public function setUp() { + parent::setUp(); + + $this->groupManager = $this->createMock(IGroupManager::class); + $this->command = $this->getMockBuilder(ListCommand::class) + ->setConstructorArgs([$this->groupManager]) + ->setMethods(['writeArrayInOutputFormat']) + ->getMock(); + + $this->input = $this->createMock(InputInterface::class); + $this->input->method('getOption') + ->willReturnCallback(function($arg) { + if ($arg === 'limit') { + return '100'; + } else if ($arg === 'offset') { + return '42'; + } + throw new \Exception(); + }); + + + $this->output = $this->createMock(OutputInterface::class); + } + + public function testExecute() { + $group1 = $this->createMock(IGroup::class); + $group1->method('getGID')->willReturn('group1'); + $group2 = $this->createMock(IGroup::class); + $group2->method('getGID')->willReturn('group2'); + $group3 = $this->createMock(IGroup::class); + $group3->method('getGID')->willReturn('group3'); + + $user = $this->createMock(IUser::class); + + $this->groupManager->method('search') + ->with( + '', + 100, + 42 + )->willReturn([$group1, $group2, $group3]); + + $group1->method('getUsers') + ->willReturn([ + 'user1' => $user, + 'user2' => $user, + ]); + + $group2->method('getUsers') + ->willReturn([ + ]); + + $group3->method('getUsers') + ->willReturn([ + 'user1' => $user, + 'user3' => $user, + ]); + + $this->command->expects($this->once()) + ->method('writeArrayInOutputFormat') + ->with( + $this->equalTo($this->input), + $this->equalTo($this->output), + [ + 'group1' => [ + 'user1', + 'user2', + ], + 'group2' => [ + ], + 'group3' => [ + 'user1', + 'user3', + ] + ] + ); + + $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]); + } + + +} diff --git a/tests/Core/Command/Group/RemoveUserTest.php b/tests/Core/Command/Group/RemoveUserTest.php new file mode 100644 index 00000000000..ec7b0f2d714 --- /dev/null +++ b/tests/Core/Command/Group/RemoveUserTest.php @@ -0,0 +1,119 @@ +<?php +/** + * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace Test\Core\Command\Group; + +use OC\Core\Command\Group\RemoveUser; +use OCP\IGroup; +use OCP\IGroupManager; +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class RemoveUserTest extends TestCase { + + /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ + private $groupManager; + + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + private $userManager; + + /** @var RemoveUser */ + private $command; + + /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $input; + + /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $output; + + public function setUp() { + parent::setUp(); + + $this->groupManager = $this->createMock(IGroupManager::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->command = new RemoveUser($this->userManager, $this->groupManager); + + $this->input = $this->createMock(InputInterface::class); + $this->input->method('getArgument') + ->willReturnCallback(function($arg) { + if ($arg === 'group') { + return 'myGroup'; + } else if ($arg === 'user') { + return 'myUser'; + } + throw new \Exception(); + }); + $this->output = $this->createMock(OutputInterface::class); + } + + public function testNoGroup() { + $this->groupManager->method('get') + ->with('myGroup') + ->willReturn(null); + + $this->output->expects($this->once()) + ->method('writeln') + ->with('<error>group not found</error>'); + + $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]); + } + + public function testNoUser() { + $group = $this->createMock(IGroup::class); + $this->groupManager->method('get') + ->with('myGroup') + ->willReturn($group); + + $this->userManager->method('get') + ->with('myUser') + ->willReturn(null); + + $this->output->expects($this->once()) + ->method('writeln') + ->with('<error>user not found</error>'); + + $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]); + } + + public function testAdd() { + $group = $this->createMock(IGroup::class); + $this->groupManager->method('get') + ->with('myGroup') + ->willReturn($group); + + $user = $this->createMock(IUser::class); + $this->userManager->method('get') + ->with('myUser') + ->willReturn($user); + + $group->expects($this->once()) + ->method('removeUser') + ->with($user); + + $this->invokePrivate($this->command, 'execute', [$this->input, $this->output]); + } + + +} diff --git a/tests/Core/Command/User/DisableTest.php b/tests/Core/Command/User/DisableTest.php new file mode 100644 index 00000000000..758020acc71 --- /dev/null +++ b/tests/Core/Command/User/DisableTest.php @@ -0,0 +1,94 @@ +<?php +/** + * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace Tests\Core\Command\User; + + +use OC\Core\Command\User\Disable; +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class DisableTest extends TestCase { + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; + /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var Disable */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $this->userManager = $this->createMock(IUserManager::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); + + $this->command = new Disable($this->userManager); + } + + public function testValidUser() { + $user = $this->createMock(IUser::class); + $user->expects($this->once()) + ->method('setEnabled') + ->with(false); + + $this->userManager + ->method('get') + ->with('user') + ->willReturn($user); + + $this->consoleInput + ->method('getArgument') + ->with('uid') + ->willReturn('user'); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains('The specified user is disabled')); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testInvalidUser() { + $this->userManager->expects($this->once()) + ->method('get') + ->with('user') + ->willReturn(null); + + $this->consoleInput + ->method('getArgument') + ->with('uid') + ->willReturn('user'); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains('User does not exist')); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Command/User/EnableTest.php b/tests/Core/Command/User/EnableTest.php new file mode 100644 index 00000000000..60b415ae79d --- /dev/null +++ b/tests/Core/Command/User/EnableTest.php @@ -0,0 +1,94 @@ +<?php +/** + * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace Tests\Core\Command\User; + + +use OC\Core\Command\User\Enable; +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +class EnableTest extends TestCase { + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; + /** @var InputInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $consoleInput; + /** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $consoleOutput; + + /** @var Disable */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $this->userManager = $this->createMock(IUserManager::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); + + $this->command = new Enable($this->userManager); + } + + public function testValidUser() { + $user = $this->createMock(IUser::class); + $user->expects($this->once()) + ->method('setEnabled') + ->with(true); + + $this->userManager + ->method('get') + ->with('user') + ->willReturn($user); + + $this->consoleInput + ->method('getArgument') + ->with('uid') + ->willReturn('user'); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains('The specified user is enabled')); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + + public function testInvalidUser() { + $this->userManager->expects($this->once()) + ->method('get') + ->with('user') + ->willReturn(null); + + $this->consoleInput + ->method('getArgument') + ->with('uid') + ->willReturn('user'); + + $this->consoleOutput->expects($this->once()) + ->method('writeln') + ->with($this->stringContains('User does not exist')); + + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } +} diff --git a/tests/Core/Controller/TwoFactorChallengeControllerTest.php b/tests/Core/Controller/TwoFactorChallengeControllerTest.php index 8a1cfb9edf1..9421a2537e8 100644 --- a/tests/Core/Controller/TwoFactorChallengeControllerTest.php +++ b/tests/Core/Controller/TwoFactorChallengeControllerTest.php @@ -143,6 +143,7 @@ class TwoFactorChallengeControllerTest extends TestCase { 'backupProvider' => $backupProvider, 'logout_attribute' => 'logoutAttribute', 'template' => '<html/>', + 'redirect_url' => '/re/dir/ect/url', ], 'guest'); $this->assertEquals($expected, $this->controller->showChallenge('myprovider', '/re/dir/ect/url')); diff --git a/tests/Core/Controller/UserControllerTest.php b/tests/Core/Controller/UserControllerTest.php new file mode 100644 index 00000000000..d15bbf7f871 --- /dev/null +++ b/tests/Core/Controller/UserControllerTest.php @@ -0,0 +1,76 @@ +<?php +/** + * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace Test\Core\Controller; + +use OC\Core\Controller\UserController; +use OCP\AppFramework\Http\JSONResponse; +use OCP\IRequest; +use OCP\IUser; +use OCP\IUserManager; +use Test\TestCase; + +class UserControllerTest extends TestCase { + + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + private $userManager; + + /** @var UserController */ + private $controller; + + public function setUp() { + parent::setUp(); + + $this->userManager = $this->createMock(IUserManager::class); + $this->controller = new UserController( + 'core', + $this->createMock(IRequest::class), + $this->userManager + ); + } + + public function testGetDisplayNames() { + $user = $this->createMock(IUser::class); + $user->method('getDisplayName') + ->willReturn('FooDisplay Name'); + + $this->userManager + ->method('get') + ->will($this->returnCallback(function ($uid) use ($user) { + if ($uid === 'foo') { + return $user; + } + return null; + })); + + $expected = new JSONResponse([ + 'users' => [ + 'foo' => 'FooDisplay Name', + 'bar' => 'bar', + ], + 'status' => 'success' + ]); + + $result = $this->controller->getDisplayNames(['foo', 'bar']); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/lib/Files/FilesystemTest.php b/tests/lib/Files/FilesystemTest.php index df77a0855de..1eb6c4fc54b 100644 --- a/tests/lib/Files/FilesystemTest.php +++ b/tests/lib/Files/FilesystemTest.php @@ -414,11 +414,9 @@ class FilesystemTest extends \Test\TestCase { $homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/'); $this->assertTrue($homeMount->instanceOfStorage('\OCP\Files\IHomeStorage')); - if (getenv('RUN_OBJECTSTORE_TESTS')) { - $this->assertTrue($homeMount->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')); + if ($homeMount->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { $this->assertEquals('object::user:' . $userId, $homeMount->getId()); - } else { - $this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Home')); + } else if ($homeMount->instanceOfStorage('\OC\Files\Storage\Home')) { $this->assertEquals('home::' . $userId, $homeMount->getId()); } diff --git a/tests/lib/Files/ObjectStore/S3Test.php b/tests/lib/Files/ObjectStore/S3Test.php index 10afb9a7aa8..b93e9beebdc 100644 --- a/tests/lib/Files/ObjectStore/S3Test.php +++ b/tests/lib/Files/ObjectStore/S3Test.php @@ -23,6 +23,9 @@ namespace Test\Files\ObjectStore; use OC\Files\ObjectStore\S3; +/** + * @group PRIMARY-s3 + */ class S3Test extends ObjectStoreTest { /** * @return \OCP\Files\ObjectStore\IObjectStore diff --git a/tests/lib/Http/Client/ClientTest.php b/tests/lib/Http/Client/ClientTest.php index 4369eab3a54..1b0a51b7395 100644 --- a/tests/lib/Http/Client/ClientTest.php +++ b/tests/lib/Http/Client/ClientTest.php @@ -10,6 +10,7 @@ namespace Test\Http\Client; use GuzzleHttp\Message\Response; use OC\Http\Client\Client; +use OC\Security\CertificateManager; use OCP\ICertificateManager; use OCP\IConfig; @@ -17,11 +18,13 @@ use OCP\IConfig; * Class ClientTest */ class ClientTest extends \Test\TestCase { - /** @var \GuzzleHttp\Client */ + /** @var \GuzzleHttp\Client|\PHPUnit_Framework_MockObject_MockObject */ private $guzzleClient; + /** @var CertificateManager|\PHPUnit_Framework_MockObject_MockObject */ + private $certificateManager; /** @var Client */ private $client; - /** @var IConfig */ + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ private $config; public function setUp() { @@ -30,10 +33,10 @@ class ClientTest extends \Test\TestCase { $this->guzzleClient = $this->getMockBuilder('\GuzzleHttp\Client') ->disableOriginalConstructor() ->getMock(); - $certificateManager = $this->createMock(ICertificateManager::class); + $this->certificateManager = $this->createMock(ICertificateManager::class); $this->client = new Client( $this->config, - $certificateManager, + $this->certificateManager, $this->guzzleClient ); } @@ -109,4 +112,64 @@ class ClientTest extends \Test\TestCase { ->willReturn(new Response(1337)); $this->assertEquals(1337, $this->client->options('http://localhost/', [])->getStatusCode()); } + + public function testHead() { + $this->guzzleClient->method('head') + ->willReturn(new Response(1337)); + $this->assertEquals(1337, $this->client->head('http://localhost/', [])->getStatusCode()); + } + + public function testSetDefaultOptionsWithNotInstalled() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('installed', false) + ->willReturn(false); + $this->certificateManager + ->expects($this->once()) + ->method('listCertificates') + ->willReturn([]); + $this->guzzleClient + ->expects($this->at(0)) + ->method('setDefaultOption') + ->with('verify', \OC::$SERVERROOT . '/resources/config/ca-bundle.crt'); + $this->guzzleClient + ->expects($this->at(1)) + ->method('setDefaultOption') + ->with('headers/User-Agent', 'Nextcloud Server Crawler'); + + self::invokePrivate($this->client, 'setDefaultOptions'); + } + + public function testSetDefaultOptionsWithProxy() { + $this->config + ->expects($this->at(0)) + ->method('getSystemValue') + ->with('proxy', null) + ->willReturn('foo'); + $this->config + ->expects($this->at(1)) + ->method('getSystemValue') + ->with('proxyuserpwd', null) + ->willReturn(null); + $this->certificateManager + ->expects($this->once()) + ->method('getAbsoluteBundlePath') + ->with(null) + ->willReturn('/my/path.crt'); + $this->guzzleClient + ->expects($this->at(0)) + ->method('setDefaultOption') + ->with('verify', '/my/path.crt'); + $this->guzzleClient + ->expects($this->at(1)) + ->method('setDefaultOption') + ->with('headers/User-Agent', 'Nextcloud Server Crawler'); + $this->guzzleClient + ->expects($this->at(2)) + ->method('setDefaultOption') + ->with('proxy', 'foo'); + + self::invokePrivate($this->client, 'setDefaultOptions'); + } } diff --git a/tests/lib/Http/Client/ResponseTest.php b/tests/lib/Http/Client/ResponseTest.php index 685f34a0baf..2e5a47b7f4a 100644 --- a/tests/lib/Http/Client/ResponseTest.php +++ b/tests/lib/Http/Client/ResponseTest.php @@ -8,7 +8,7 @@ namespace Test\Http\Client; -use Guzzle\Stream\Stream; +use GuzzleHttp\Stream\Stream; use GuzzleHttp\Message\Response as GuzzleResponse; use OC\Http\Client\Response; @@ -27,12 +27,33 @@ class ResponseTest extends \Test\TestCase { $this->response = new Response($this->guzzleResponse); } + public function testGetBody() { + $this->guzzleResponse->setBody(Stream::factory('MyResponse')); + $this->assertSame('MyResponse', $this->response->getBody()); + } + public function testGetStatusCode() { - $this->assertEquals(1337, $this->response->getStatusCode()); + $this->assertSame(1337, $this->response->getStatusCode()); } public function testGetHeader() { $this->guzzleResponse->setHeader('bar', 'foo'); - $this->assertEquals('foo', $this->response->getHeader('bar')); + $this->assertSame('foo', $this->response->getHeader('bar')); + } + + public function testGetHeaders() { + $this->guzzleResponse->setHeader('bar', 'foo'); + $this->guzzleResponse->setHeader('x-awesome', 'yes'); + + $expected = [ + 'bar' => [ + 0 => 'foo', + ], + 'x-awesome' => [ + 0 => 'yes', + ], + ]; + $this->assertSame($expected, $this->response->getHeaders()); + $this->assertSame('yes', $this->response->getHeader('x-awesome')); } } diff --git a/tests/lib/Settings/ManagerTest.php b/tests/lib/Settings/ManagerTest.php index 150609499ad..b91331a1d30 100644 --- a/tests/lib/Settings/ManagerTest.php +++ b/tests/lib/Settings/ManagerTest.php @@ -25,6 +25,7 @@ namespace Tests\Settings; use OC\Settings\Admin\Sharing; use OC\Settings\Manager; +use OC\Settings\Mapper; use OC\Settings\Section; use OCP\Encryption\IManager; use OCP\IConfig; @@ -36,22 +37,24 @@ use OCP\Lock\ILockingProvider; use Test\TestCase; class ManagerTest extends TestCase { - /** @var Manager */ + /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */ private $manager; - /** @var ILogger */ + /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ private $logger; - /** @var IDBConnection */ + /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */ private $dbConnection; - /** @var IL10N */ + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ private $l10n; - /** @var IConfig */ + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ private $config; - /** @var IManager */ + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ private $encryptionManager; - /** @var IUserManager */ + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ private $userManager; - /** @var ILockingProvider */ + /** @var ILockingProvider|\PHPUnit_Framework_MockObject_MockObject */ private $lockingProvider; + /** @var Mapper|\PHPUnit_Framework_MockObject_MockObject */ + private $mapper; public function setUp() { parent::setUp(); @@ -63,6 +66,7 @@ class ManagerTest extends TestCase { $this->encryptionManager = $this->getMockBuilder('\OCP\Encryption\IManager')->getMock(); $this->userManager = $this->getMockBuilder('\OCP\IUserManager')->getMock(); $this->lockingProvider = $this->getMockBuilder('\OCP\Lock\ILockingProvider')->getMock(); + $this->mapper = $this->getMockBuilder(Mapper::class)->disableOriginalConstructor()->getMock(); $this->manager = new Manager( $this->logger, @@ -71,63 +75,49 @@ class ManagerTest extends TestCase { $this->config, $this->encryptionManager, $this->userManager, - $this->lockingProvider + $this->lockingProvider, + $this->mapper ); } - public function testSetupSettings() { - $qb = $this->getMockBuilder('\OCP\DB\QueryBuilder\IQueryBuilder')->getMock(); - $qb - ->expects($this->once()) - ->method('select') - ->with('class') - ->willReturn($qb); - $this->dbConnection - ->expects($this->at(0)) - ->method('getQueryBuilder') - ->willReturn($qb); - $qb - ->expects($this->once()) - ->method('from') - ->with('admin_settings') - ->willReturn($qb); - $expressionBuilder = $this->getMockBuilder('\OCP\DB\QueryBuilder\IExpressionBuilder')->getMock(); - $qb - ->expects($this->once()) - ->method('expr') - ->willReturn($expressionBuilder); - $param = $this->getMockBuilder('\OCP\DB\QueryBuilder\IParameter')->getMock(); - $qb - ->expects($this->once()) - ->method('createNamedParameter') - ->with('OCA\Files\Settings\Admin') - ->willReturn($param); - $expressionBuilder - ->expects($this->once()) - ->method('eq') - ->with('class', $param) - ->willReturn('myString'); - $qb - ->expects($this->once()) - ->method('where') - ->with('myString') - ->willReturn($qb); - $stmt = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')->getMock(); - $qb - ->expects($this->once()) - ->method('execute') - ->willReturn($stmt); - - $qb1 = $this->getMockBuilder('\OCP\DB\QueryBuilder\IQueryBuilder')->getMock(); - $qb1 - ->expects($this->once()) - ->method('insert') - ->with('admin_settings') - ->willReturn($qb1); - $this->dbConnection - ->expects($this->at(1)) - ->method('getQueryBuilder') - ->willReturn($qb1); + public function testSetupSettingsUpdate() { + $this->mapper->expects($this->any()) + ->method('has') + ->with('admin_settings', 'OCA\Files\Settings\Admin') + ->will($this->returnValue(true)); + + $this->mapper->expects($this->once()) + ->method('update') + ->with('admin_settings', + 'class', + 'OCA\Files\Settings\Admin', [ + 'section' => 'additional', + 'priority' => 5 + ]); + $this->mapper->expects($this->never()) + ->method('add'); + + $this->manager->setupSettings([ + 'admin' => 'OCA\Files\Settings\Admin', + ]); + } + + public function testSetupSettingsAdd() { + $this->mapper->expects($this->any()) + ->method('has') + ->with('admin_settings', 'OCA\Files\Settings\Admin') + ->will($this->returnValue(false)); + + $this->mapper->expects($this->once()) + ->method('add') + ->with('admin_settings', [ + 'class' => 'OCA\Files\Settings\Admin', + 'section' => 'additional', + 'priority' => 5 + ]); + + $this->mapper->expects($this->never()) + ->method('update'); $this->manager->setupSettings([ 'admin' => 'OCA\Files\Settings\Admin', @@ -135,44 +125,49 @@ class ManagerTest extends TestCase { } public function testGetAdminSections() { - $qb = $this->getMockBuilder('\OCP\DB\QueryBuilder\IQueryBuilder')->getMock(); - $expr = $this->getMockBuilder('OCP\DB\QueryBuilder\IExpressionBuilder')->getMock(); - $qb - ->expects($this->once()) - ->method('selectDistinct') - ->with('s.class') - ->willReturn($qb); - $qb - ->expects($this->once()) - ->method('addSelect') - ->with('s.priority') - ->willReturn($qb); - $qb - ->expects($this->exactly(2)) - ->method('from') - ->willReturn($qb); - $qb - ->expects($this->once()) - ->method('expr') - ->willReturn($expr); - $qb - ->expects($this->once()) - ->method('where') - ->willReturn($qb); - $stmt = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')->getMock(); - $qb - ->expects($this->once()) - ->method('execute') - ->willReturn($stmt); - $this->dbConnection - ->expects($this->once()) - ->method('getQueryBuilder') - ->willReturn($qb); $this->l10n ->expects($this->any()) ->method('t') ->will($this->returnArgument(0)); + $this->mapper->expects($this->once()) + ->method('getAdminSectionsFromDB') + ->will($this->returnValue([ + ['class' => '\OCA\LogReader\Settings\Section', 'priority' => 90] + ])); + + $this->mapper->expects($this->once()) + ->method('getAdminSettingsCountFromDB') + ->will($this->returnValue([ + 'logging' => 1 + ])); + + $this->assertEquals([ + 0 => [new Section('server', 'Server settings', 0)], + 5 => [new Section('sharing', 'Sharing', 0)], + 45 => [new Section('encryption', 'Encryption', 0)], + 90 => [new \OCA\LogReader\Settings\Section(\OC::$server->getL10N('logreader'))], + 98 => [new Section('additional', 'Additional settings', 0)], + 99 => [new Section('tips-tricks', 'Tips & tricks', 0)], + ], $this->manager->getAdminSections()); + } + + public function testGetAdminSectionsEmptySection() { + $this->l10n + ->expects($this->any()) + ->method('t') + ->will($this->returnArgument(0)); + + $this->mapper->expects($this->once()) + ->method('getAdminSectionsFromDB') + ->will($this->returnValue([ + ['class' => '\OCA\LogReader\Settings\Section', 'priority' => 90] + ])); + + $this->mapper->expects($this->once()) + ->method('getAdminSettingsCountFromDB') + ->will($this->returnValue([])); + $this->assertEquals([ 0 => [new Section('server', 'Server settings', 0)], 5 => [new Section('sharing', 'Sharing', 0)], @@ -183,47 +178,9 @@ class ManagerTest extends TestCase { } public function testGetAdminSettings() { - $qb = $this->getMockBuilder('\OCP\DB\QueryBuilder\IQueryBuilder')->getMock(); - $qb - ->expects($this->once()) - ->method('select') - ->with(['class', 'priority']) - ->willReturn($qb); - $qb - ->expects($this->once()) - ->method('from') - ->with('admin_settings') - ->willReturn($qb); - $expressionBuilder = $this->getMockBuilder('\OCP\DB\QueryBuilder\IExpressionBuilder')->getMock(); - $qb - ->expects($this->once()) - ->method('expr') - ->willReturn($expressionBuilder); - $param = $this->getMockBuilder('\OCP\DB\QueryBuilder\IParameter')->getMock(); - $qb - ->expects($this->once()) - ->method('createParameter') - ->with('section') - ->willReturn($param); - $expressionBuilder - ->expects($this->once()) - ->method('eq') - ->with('section', $param) - ->willReturn('myString'); - $qb - ->expects($this->once()) - ->method('where') - ->with('myString') - ->willReturn($qb); - $stmt = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')->getMock(); - $qb - ->expects($this->once()) - ->method('execute') - ->willReturn($stmt); - $this->dbConnection - ->expects($this->exactly(2)) - ->method('getQueryBuilder') - ->willReturn($qb); + $this->mapper->expects($this->any()) + ->method('getAdminSettingsFromDB') + ->will($this->returnValue([])); $this->assertEquals([ 0 => [new Sharing($this->config)], diff --git a/tests/lib/Settings/MapperTest.php b/tests/lib/Settings/MapperTest.php new file mode 100644 index 00000000000..6a648acd5f7 --- /dev/null +++ b/tests/lib/Settings/MapperTest.php @@ -0,0 +1,139 @@ +<?php +/** + * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl> + * + * @author Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Tests\Settings; + +use OC\DB\QueryBuilder\Literal; +use OC\Settings\Mapper; +use Test\TestCase; + +/** + * @group DB + */ +class MapperTest extends TestCase { + const SECTION_PREFIX = 'test_section_'; + + /** @var Mapper */ + private $mapper; + + public function setUp() { + parent::setUp(); + $this->mapper = new Mapper(\OC::$server->getDatabaseConnection()); + } + + public function tearDown() { + parent::tearDown(); + + $db = \OC::$server->getDatabaseConnection(); + $builder = $db->getQueryBuilder(); + + $builder->delete(Mapper::TABLE_ADMIN_SECTIONS) + ->where($builder->expr()->like('id', new Literal(self::SECTION_PREFIX . '%'))); + + $builder->delete(Mapper::TABLE_ADMIN_SETTINGS) + ->where($builder->expr()->like('section', new Literal(self::SECTION_PREFIX . '%'))); + } + + public function testManipulateSettings() { + $this->assertEquals(false, $this->mapper->has(Mapper::TABLE_ADMIN_SETTINGS, '\OC\Dummy')); + $this->assertNotContains('\OC\Dummy', $this->mapper->getClasses(Mapper::TABLE_ADMIN_SETTINGS)); + + $this->mapper->add(Mapper::TABLE_ADMIN_SETTINGS, [ + 'class' => '\OC\Dummy', + 'section' => self::SECTION_PREFIX . '1', + 'priority' => 5 + ]); + + $this->assertEquals(true, $this->mapper->has(Mapper::TABLE_ADMIN_SETTINGS, '\OC\Dummy')); + + $this->assertContains('\OC\Dummy', $this->mapper->getClasses(Mapper::TABLE_ADMIN_SETTINGS)); + + $rows = $this->mapper->getAdminSettingsFromDB(self::SECTION_PREFIX . '1'); + $this->assertEquals([ + ['class' => '\OC\Dummy', 'priority' => 5] + ], $rows); + + $this->mapper->update(Mapper::TABLE_ADMIN_SETTINGS, 'class', '\OC\Dummy', [ + 'section' => self::SECTION_PREFIX . '1', 'priority' => 15 + ]); + + $rows = $this->mapper->getAdminSettingsFromDB(self::SECTION_PREFIX . '1'); + $this->assertEquals([ + ['class' => '\OC\Dummy', 'priority' => 15] + ], $rows); + + $this->mapper->update(Mapper::TABLE_ADMIN_SETTINGS, 'class', '\OC\Dummy', [ + 'section' => self::SECTION_PREFIX . '2', 'priority' => 15 + ]); + + $this->assertEquals([], $this->mapper->getAdminSettingsFromDB(self::SECTION_PREFIX . '1')); + $rows = $this->mapper->getAdminSettingsFromDB(self::SECTION_PREFIX . '2'); + $this->assertEquals([ + ['class' => '\OC\Dummy', 'priority' => 15] + ], $rows); + + $this->mapper->remove(Mapper::TABLE_ADMIN_SETTINGS, '\OC\Dummy'); + + $this->assertEquals(false, $this->mapper->has(Mapper::TABLE_ADMIN_SETTINGS, '\OC\Dummy')); + } + + public function testGetAdminSections() { + $this->assertFalse($this->mapper->has(Mapper::TABLE_ADMIN_SECTIONS, '\OC\Dummy')); + + $this->mapper->add(Mapper::TABLE_ADMIN_SECTIONS, [ + 'id' => self::SECTION_PREFIX . '1', + 'class' => '\OC\Dummy', + 'priority' => 1, + ]); + + $this->assertTrue($this->mapper->has(Mapper::TABLE_ADMIN_SECTIONS, '\OC\Dummy')); + + // until we add a setting for the section it's not returned + $this->assertNotContains([ + 'class' => '\OC\Dummy', + 'priority' => 1, + ], $this->mapper->getAdminSectionsFromDB()); + + $this->mapper->add(Mapper::TABLE_ADMIN_SETTINGS, [ + 'class' => '\OC\Dummy', + 'section' => self::SECTION_PREFIX . '1', + 'priority' => 5 + ]); + + $this->assertContains([ + 'class' => '\OC\Dummy', + 'priority' => 1, + ], $this->mapper->getAdminSectionsFromDB()); + + $this->mapper->remove(Mapper::TABLE_ADMIN_SETTINGS, '\OC\Dummy'); + + $this->assertNotContains([ + 'class' => '\OC\Dummy', + 'priority' => 1, + ], $this->mapper->getAdminSectionsFromDB()); + + $this->mapper->remove(Mapper::TABLE_ADMIN_SECTIONS, '\OC\Dummy'); + + $this->assertFalse($this->mapper->has(Mapper::TABLE_ADMIN_SECTIONS, '\OC\Dummy')); + } +} diff --git a/tests/preseed-config.php b/tests/preseed-config.php index e2c5a55fa58..53579406472 100644 --- a/tests/preseed-config.php +++ b/tests/preseed-config.php @@ -21,3 +21,20 @@ if (is_dir(OC::$SERVERROOT.'/apps2')) { if (substr(strtolower(PHP_OS), 0, 3) === 'win') { $CONFIG['openssl'] = ['config' => OC::$SERVERROOT . '/tests/data/openssl.cnf']; } + +if (getenv('OBJECT_STORE') === 's3') { + $CONFIG['objectstore'] = [ + 'class' => 'OC\\Files\\ObjectStore\\S3', + 'arguments' => array( + 'bucket' => 'nextcloud', + 'autocreate' => true, + 'key' => 'dummy', + 'secret' => 'dummy', + 'hostname' => 'localhost', + 'port' => 4569, + 'use_ssl' => false, + // required for some non amazon s3 implementations + 'use_path_style' => true + ) + ]; +} |