Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add occ command to reset for all users #83

Merged
merged 1 commit into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ The First run wizard can be customized to meet specific design goals, or to chan
<owncloud min-version="10" max-version="11" />
</dependencies>
<version>1.1.1</version>
<namespace>FirstRunWizard</namespace>
<default_enable/>
<commands>
<command>OCA\FirstRunWizard\Command\ResetAll</command>
</commands>
</info>
80 changes: 80 additions & 0 deletions lib/Command/ResetAll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* @author Tom Needham <tom@owncloud.com>
*
* @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 <http://www.gnu.org/licenses/>
*
*/

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("<info>Done</info>");
}
}
35 changes: 34 additions & 1 deletion lib/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* ownCloud - firstrunwizard App
*
Expand All @@ -20,14 +19,27 @@
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\FirstRunWizard;

use OCP\IConfig;
use OCP\IUserSession;

/**
* Class Config
*
* @package OCA\FirstRunWizard
*/
class Config {

/**
* @var IConfig
*/
protected $config;

/**
* @var IUserSession
*/
protected $userSession;

/**
Expand Down Expand Up @@ -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);
}
}
}
86 changes: 80 additions & 6 deletions tests/lib/configtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -92,6 +113,9 @@ public function testDisable($isUserAvailable) {
}
}

/**
* @return array
*/
public function enableDisableData() {
return [
[true],
Expand All @@ -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')
Expand Down Expand Up @@ -158,11 +190,53 @@ public function testIsEnabled($isEnabled) {
}
}

/**
* @return array
*/
public function isEnabledData() {
return [
[true],
[false],
[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']);
}
}