Skip to content

Commit

Permalink
Add occ command to reset for all users
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Needham committed Feb 12, 2019
1 parent c17a10f commit aafcbd1
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 7 deletions.
3 changes: 3 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ The First run wizard can be customized to meet specific design goals, or to chan
</dependencies>
<version>1.1.1</version>
<default_enable/>
<commands>
<command>OCA\FirstRunWizard\Command\ResetAll</command>
</commands>
</info>
62 changes: 62 additions & 0 deletions lib/Command/ResetAll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

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']);
}
}

0 comments on commit aafcbd1

Please sign in to comment.