123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
- namespace Tests\Core\Command\User;
- use OC\Core\Command\User\Setting;
- use OCP\IConfig;
- use OCP\IDBConnection;
- use OCP\IUserManager;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Test\TestCase;
- class SettingTest extends TestCase {
- /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */
- protected $userManager;
- /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
- protected $config;
- /** @var \OCP\IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
- protected $connection;
- /** @var \Symfony\Component\Console\Input\InputInterface|\PHPUnit\Framework\MockObject\MockObject */
- protected $consoleInput;
- /** @var \Symfony\Component\Console\Output\OutputInterface|\PHPUnit\Framework\MockObject\MockObject */
- protected $consoleOutput;
- protected function setUp(): void {
- parent::setUp();
- $this->userManager = $this->getMockBuilder(IUserManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->config = $this->getMockBuilder(IConfig::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->connection = $this->getMockBuilder(IDBConnection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->consoleInput = $this->getMockBuilder(InputInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- }
- public function getCommand(array $methods = []) {
- if (empty($methods)) {
- return new Setting($this->userManager, $this->config);
- } else {
- $mock = $this->getMockBuilder(Setting::class)
- ->setConstructorArgs([
- $this->userManager,
- $this->config,
- ])
- ->onlyMethods($methods)
- ->getMock();
- return $mock;
- }
- }
- public function dataCheckInput() {
- return [
- [
- [['uid', 'username']],
- [['ignore-missing-user', true]],
- [],
- false,
- false,
- ],
- [
- [['uid', 'username']],
- [['ignore-missing-user', false]],
- [],
- null,
- 'The user "username" does not exist.',
- ],
- [
- [['uid', 'username'], ['key', 'configkey']],
- [['ignore-missing-user', true]],
- [['--default-value', false, true]],
- false,
- false,
- ],
- [
- [['uid', 'username'], ['key', '']],
- [['ignore-missing-user', true]],
- [['--default-value', false, true]],
- false,
- 'The "default-value" option can only be used when specifying a key.',
- ],
- [
- [['uid', 'username'], ['key', 'configkey'], ['value', '']],
- [['ignore-missing-user', true]],
- [],
- false,
- false,
- ],
- [
- [['uid', 'username'], ['key', ''], ['value', '']],
- [['ignore-missing-user', true]],
- [],
- false,
- 'The value argument can only be used when specifying a key.',
- ],
- [
- [['uid', 'username'], ['key', 'configkey'], ['value', '']],
- [['ignore-missing-user', true]],
- [['--default-value', false, true]],
- false,
- 'The value argument can not be used together with "default-value".',
- ],
- [
- [['uid', 'username'], ['key', 'configkey'], ['value', '']],
- [['ignore-missing-user', true], ['update-only', true]],
- [],
- false,
- false,
- ],
- [
- [['uid', 'username'], ['key', 'configkey'], ['value', null]],
- [['ignore-missing-user', true], ['update-only', true]],
- [],
- false,
- 'The "update-only" option can only be used together with "value".',
- ],
- [
- [['uid', 'username'], ['key', 'configkey']],
- [['ignore-missing-user', true], ['delete', true]],
- [],
- false,
- false,
- ],
- [
- [['uid', 'username'], ['key', '']],
- [['ignore-missing-user', true], ['delete', true]],
- [],
- false,
- 'The "delete" option can only be used when specifying a key.',
- ],
- [
- [['uid', 'username'], ['key', 'configkey']],
- [['ignore-missing-user', true], ['delete', true]],
- [['--default-value', false, true]],
- false,
- 'The "delete" option can not be used together with "default-value".',
- ],
- [
- [['uid', 'username'], ['key', 'configkey'], ['value', '']],
- [['ignore-missing-user', true], ['delete', true]],
- [],
- false,
- 'The "delete" option can not be used together with "value".',
- ],
- [
- [['uid', 'username'], ['key', 'configkey']],
- [['ignore-missing-user', true], ['delete', true], ['error-if-not-exists', true]],
- [],
- false,
- false,
- ],
- [
- [['uid', 'username'], ['key', 'configkey']],
- [['ignore-missing-user', true], ['delete', false], ['error-if-not-exists', true]],
- [],
- false,
- 'The "error-if-not-exists" option can only be used together with "delete".',
- ],
- ];
- }
- /**
- * @dataProvider dataCheckInput
- *
- * @param array $arguments
- * @param array $options
- * @param array $parameterOptions
- * @param mixed $user
- * @param string $expectedException
- */
- public function testCheckInput($arguments, $options, $parameterOptions, $user, $expectedException): void {
- $this->consoleInput->expects($this->any())
- ->method('getArgument')
- ->willReturnMap($arguments);
- $this->consoleInput->expects($this->any())
- ->method('getOption')
- ->willReturnMap($options);
- $this->consoleInput->expects($this->any())
- ->method('hasParameterOption')
- ->willReturnCallback(function (string|array $config, bool $default = false) use ($parameterOptions): bool {
- foreach ($parameterOptions as $parameterOption) {
- if ($config === $parameterOption[0]
- // Check the default value if the maps has 3 entries
- && (!isset($parameterOption[2]) || $default === $parameterOption[1])) {
- return end($parameterOption);
- }
- }
- return false;
- });
- if ($user !== false) {
- $this->userManager->expects($this->once())
- ->method('get')
- ->willReturn($user);
- } else {
- $this->userManager->expects($this->never())
- ->method('get');
- }
- $command = $this->getCommand();
- try {
- $this->invokePrivate($command, 'checkInput', [$this->consoleInput]);
- $this->assertFalse($expectedException);
- } catch (\InvalidArgumentException $e) {
- $this->assertEquals($expectedException, $e->getMessage());
- }
- }
- public function testCheckInputExceptionCatch(): void {
- $command = $this->getCommand(['checkInput']);
- $command->expects($this->once())
- ->method('checkInput')
- ->willThrowException(new \InvalidArgumentException('test'));
- $this->consoleOutput->expects($this->once())
- ->method('writeln')
- ->with('<error>test</error>');
- $this->assertEquals(1, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
- }
- public function dataExecuteDelete() {
- return [
- ['config', false, null, 0],
- ['config', true, null, 0],
- [null, false, null, 0],
- [null, true, '<error>The setting does not exist for user "username".</error>', 1],
- ];
- }
- /**
- * @dataProvider dataExecuteDelete
- *
- * @param string|null $value
- * @param bool $errorIfNotExists
- * @param string $expectedLine
- * @param int $expectedReturn
- */
- public function testExecuteDelete($value, $errorIfNotExists, $expectedLine, $expectedReturn): void {
- $command = $this->getCommand([
- 'writeArrayInOutputFormat',
- 'checkInput',
- 'getUserSettings',
- ]);
- $this->consoleInput->expects($this->any())
- ->method('getArgument')
- ->willReturnMap([
- ['uid', 'username'],
- ['app', 'appname'],
- ['key', 'configkey'],
- ]);
- $command->expects($this->once())
- ->method('checkInput');
- $this->config->expects($this->once())
- ->method('getUserValue')
- ->with('username', 'appname', 'configkey', null)
- ->willReturn($value);
- $this->consoleInput->expects($this->atLeastOnce())
- ->method('hasParameterOption')
- ->willReturnMap([
- ['--delete', false, true],
- ['--error-if-not-exists', false, $errorIfNotExists],
- ]);
- if ($expectedLine === null) {
- $this->consoleOutput->expects($this->never())
- ->method('writeln');
- $this->config->expects($this->once())
- ->method('deleteUserValue')
- ->with('username', 'appname', 'configkey');
- } else {
- $this->consoleOutput->expects($this->once())
- ->method('writeln')
- ->with($expectedLine);
- $this->config->expects($this->never())
- ->method('deleteUserValue');
- }
- $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
- }
- public function dataExecuteSet() {
- return [
- ['config', false, null, 0],
- ['config', true, null, 0],
- [null, false, null, 0],
- [null, true, '<error>The setting does not exist for user "username".</error>', 1],
- ];
- }
- /**
- * @dataProvider dataExecuteSet
- *
- * @param string|null $value
- * @param bool $updateOnly
- * @param string $expectedLine
- * @param int $expectedReturn
- */
- public function testExecuteSet($value, $updateOnly, $expectedLine, $expectedReturn): void {
- $command = $this->getCommand([
- 'writeArrayInOutputFormat',
- 'checkInput',
- 'getUserSettings',
- ]);
- $this->consoleInput->expects($this->atLeast(4))
- ->method('getArgument')
- ->willReturnMap([
- ['uid', 'username'],
- ['app', 'appname'],
- ['key', 'configkey'],
- ['value', 'setValue'],
- ]);
- $command->expects($this->once())
- ->method('checkInput');
- $this->config->expects($this->once())
- ->method('getUserValue')
- ->with('username', 'appname', 'configkey', null)
- ->willReturn($value);
- $this->consoleInput->expects($this->atLeastOnce())
- ->method('hasParameterOption')
- ->willReturnMap([
- ['--update-only', false, $updateOnly],
- ]);
- if ($expectedLine === null) {
- $this->consoleOutput->expects($this->never())
- ->method('writeln');
- $this->consoleInput->expects($this->never())
- ->method('getOption');
- $this->config->expects($this->once())
- ->method('setUserValue')
- ->with('username', 'appname', 'configkey', 'setValue');
- } else {
- $this->consoleOutput->expects($this->once())
- ->method('writeln')
- ->with($expectedLine);
- $this->config->expects($this->never())
- ->method('setUserValue');
- }
- $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
- }
- public function dataExecuteGet() {
- return [
- ['config', null, 'config', 0],
- [null, 'config', 'config', 0],
- [null, null, '<error>The setting does not exist for user "username".</error>', 1],
- ];
- }
- /**
- * @dataProvider dataExecuteGet
- *
- * @param string|null $value
- * @param string|null $defaultValue
- * @param string $expectedLine
- * @param int $expectedReturn
- */
- public function testExecuteGet($value, $defaultValue, $expectedLine, $expectedReturn): void {
- $command = $this->getCommand([
- 'writeArrayInOutputFormat',
- 'checkInput',
- 'getUserSettings',
- ]);
- $this->consoleInput->expects($this->any())
- ->method('getArgument')
- ->willReturnMap([
- ['uid', 'username'],
- ['app', 'appname'],
- ['key', 'configkey'],
- ]);
- $command->expects($this->once())
- ->method('checkInput');
- $this->config->expects($this->once())
- ->method('getUserValue')
- ->with('username', 'appname', 'configkey', null)
- ->willReturn($value);
- if ($value === null) {
- if ($defaultValue === null) {
- $this->consoleInput->expects($this->atLeastOnce())
- ->method('hasParameterOption')
- ->willReturn(false);
- } else {
- $this->consoleInput->expects($this->atLeastOnce())
- ->method('hasParameterOption')
- ->willReturnCallback(function (string|array $config, bool $default = false): bool {
- if ($config === '--default-value' && $default === false) {
- return true;
- }
- return false;
- });
- $this->consoleInput->expects($this->once())
- ->method('getOption')
- ->with('default-value')
- ->willReturn($defaultValue);
- }
- }
- $this->consoleOutput->expects($this->once())
- ->method('writeln')
- ->with($expectedLine);
- $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
- }
- public function testExecuteList(): void {
- $command = $this->getCommand([
- 'writeArrayInOutputFormat',
- 'checkInput',
- 'getUserSettings',
- ]);
- $this->consoleInput->expects($this->any())
- ->method('getArgument')
- ->willReturnMap([
- ['uid', 'username'],
- ['app', 'appname'],
- ['key', ''],
- ]);
- $command->expects($this->once())
- ->method('checkInput');
- $command->expects($this->once())
- ->method('getUserSettings')
- ->willReturn(['settings']);
- $command->expects($this->once())
- ->method('writeArrayInOutputFormat')
- ->with($this->consoleInput, $this->consoleOutput, ['settings']);
- $this->assertEquals(0, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
- }
- }
|