From e4e4527ff9e37155f3c73480cd084a47dff46dbc Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Fri, 9 Nov 2018 13:03:47 +0000 Subject: [PATCH] Add occ command to reset for all users --- appinfo/info.xml | 4 ++ lib/Command/ResetAll.php | 80 +++++++++++++++++++++++++++++++++++++ lib/config.php | 35 +++++++++++++++- tests/lib/configtest.php | 86 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 198 insertions(+), 7 deletions(-) create mode 100644 lib/Command/ResetAll.php diff --git a/appinfo/info.xml b/appinfo/info.xml index c22dde0eee6..54e9ebbda1e 100755 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -11,5 +11,9 @@ The First run wizard can be customized to meet specific design goals, or to chan 1.1.1 + FirstRunWizard + + OCA\FirstRunWizard\Command\ResetAll + diff --git a/lib/Command/ResetAll.php b/lib/Command/ResetAll.php new file mode 100644 index 00000000000..3af548f6cdf --- /dev/null +++ b/lib/Command/ResetAll.php @@ -0,0 +1,80 @@ + +* +* @copyright Copyright (c) 2019, ownCloud GmbH +* @license AGPL-3.0 +* +* This code is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License, version 3, +* as published by the Free Software Foundation. +* +* 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, version 3, +* along with this program. If not, see +* +*/ + +namespace OCA\FirstRunWizard\Command; + +use OCA\FirstRunWizard\Config; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\ProgressBar; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * Class ResetAll + * + * @package OCA\FirstRunWizard\Command + */ +class ResetAll extends Command { + /** + * @var Config + */ + protected $config; + + /** + * ResetAll constructor. + * + * @param Config $config + */ + public function __construct(Config $config) { + parent::__construct(); + $this->config = $config; + } + + /** + * Setup the command + * + * @return int|null|void + */ + public function configure() { + $this + ->setName('firstrunwizard:reset-all') + ->setDescription('Reset the first run wizard for all users'); + } + + /** + * Execute the command + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int|null|void + */ + public function execute(InputInterface $input, OutputInterface $output) { + $output->writeln('Resetting firstrunwizard for all users'); + $progress = new ProgressBar($output); + $this->config->resetAllUsers(function ($user) use ($progress) { + $progress->advance(); + }); + $progress->finish(); + $output->writeln(""); + $output->writeln("Done"); + } +} diff --git a/lib/config.php b/lib/config.php index 88c565b4b59..42e622e3bee 100755 --- a/lib/config.php +++ b/lib/config.php @@ -1,5 +1,4 @@ . * */ + namespace OCA\FirstRunWizard; use OCP\IConfig; use OCP\IUserSession; +/** + * Class Config + * + * @package OCA\FirstRunWizard + */ class Config { + + /** + * @var IConfig + */ protected $config; + /** + * @var IUserSession + */ protected $userSession; /** @@ -72,4 +84,25 @@ public function isEnabled() { return false; } } + + /** + * Enables the firstrunwizard for all users again that had it disabled + * + * @param callable $callback called with the current user being set + * + * @return void + * + * @throws \OCP\PreConditionNotMetException + */ + public function resetAllUsers(callable $callback) { + $users = $this->config->getUsersForUserValue( + 'firstrunwizard', + 'show', + 0 + ); + foreach ($users as $user) { + \call_user_func($callback, $user); + $this->config->setUserValue($user, 'firstrunwizard', 'show', 1); + } + } } diff --git a/tests/lib/configtest.php b/tests/lib/configtest.php index 703545bd7b5..937288d3d21 100644 --- a/tests/lib/configtest.php +++ b/tests/lib/configtest.php @@ -6,16 +6,29 @@ use OCP\IConfig; use OCP\IUserSession; +/** + * Class ConfigTest + * + * @package OCA\FirstRunWizard\Tests + */ class ConfigTest extends \PHPUnit_Framework_TestCase { /** + * @param bool $isUserAvailable + * * @dataProvider enableDisableData + * + * @return void */ public function testEnable($isUserAvailable) { - /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config */ + /** + * @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config + */ $config = $this->getMockBuilder('\OCP\IConfig') ->disableOriginalConstructor()->getMock(); - /** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject $userSession */ + /** + * @var IUserSession | \PHPUnit_Framework_MockObject_MockObject $userSession + */ $userSession = $this->getMockBuilder('\OCP\IUserSession') ->disableOriginalConstructor()->getMock(); $user = $this->getMockBuilder('\OCP\IUser') @@ -51,13 +64,21 @@ public function testEnable($isUserAvailable) { } /** + * @param bool $isUserAvailable + * + * @return void + * * @dataProvider enableDisableData */ public function testDisable($isUserAvailable) { - /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config */ + /** + * @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config + */ $config = $this->getMockBuilder('\OCP\IConfig') ->disableOriginalConstructor()->getMock(); - /** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject $userSession */ + /** + * @var IUserSession | \PHPUnit_Framework_MockObject_MockObject $userSession + */ $userSession = $this->getMockBuilder('\OCP\IUserSession') ->disableOriginalConstructor()->getMock(); $user = $this->getMockBuilder('\OCP\IUser') @@ -92,6 +113,9 @@ public function testDisable($isUserAvailable) { } } + /** + * @return array + */ public function enableDisableData() { return [ [true], @@ -100,13 +124,21 @@ public function enableDisableData() { } /** + * @param bool $isEnabled + * + * @return void + * * @dataProvider isEnabledData */ public function testIsEnabled($isEnabled) { - /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config */ + /** + * @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config + */ $config = $this->getMockBuilder('\OCP\IConfig') ->disableOriginalConstructor()->getMock(); - /** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject $userSession */ + /** + * @var IUserSession | \PHPUnit_Framework_MockObject_MockObject $userSession + */ $userSession = $this->getMockBuilder('\OCP\IUserSession') ->disableOriginalConstructor()->getMock(); $user = $this->getMockBuilder('\OCP\IUser') @@ -158,6 +190,9 @@ public function testIsEnabled($isEnabled) { } } + /** + * @return array + */ public function isEnabledData() { return [ [true], @@ -165,4 +200,43 @@ public function isEnabledData() { [null], ]; } + + /** + * Test that the config is reset for all users + * + * @return void + * + * @throws \OCP\PreConditionNotMetException + */ + public function testResetAllUsers() { + $users = []; + $users[] = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + $users[] = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + /** + * @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config + */ + $config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + $config->method('getUsersForUserValue') + ->with('firstrunwizard', 'show', 0) + ->willReturn($users); + $config->expects($this->exactly(\count($users))) + ->method('setUserValue'); + /** + * @var IUserSession | \PHPUnit_Framework_MockObject_MockObject $userSession + */ + $userSession = $this->getMockBuilder('\OCP\IUserSession') + ->disableOriginalConstructor()->getMock(); + $c = new Config($config, $userSession); + // Create a fake callback to check it gets called + $mock = $this->getMockBuilder('stdClass'); + $mock->setMethods(['callback']); + $mock = $mock->getMock(); + $mock->expects($this->exactly(\count($users))) + ->method('callback') + ->will($this->returnValue(true)); + $c->resetAllUsers([$mock, 'callback']); + } }