Skip to content

Commit

Permalink
Merge pull request #30281 from owncloud/stable10-c41dda7205aa68f62ff8…
Browse files Browse the repository at this point in the history
…a5ae3d780715b51f3edd

[stable10] When executing the data-fingerprint command a log entry is…
  • Loading branch information
DeepDiver1975 authored Jan 26, 2018
2 parents df23b6e + ce1481e commit d41cd59
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 24 deletions.
34 changes: 30 additions & 4 deletions core/Command/Maintenance/DataFingerprint.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @author Roeland Jago Douma <[email protected]>
* @author Thomas Müller <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
Expand All @@ -22,22 +23,30 @@

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\ILogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;


class DataFingerprint extends Command {

/** @var IConfig */
protected $config;
private $config;
/** @var ITimeFactory */
protected $timeFactory;
private $timeFactory;
/** @var ILogger */
private $logger;

public function __construct(IConfig $config,
ITimeFactory $timeFactory) {
ITimeFactory $timeFactory,
ILogger $logger ) {
$this->config = $config;
$this->timeFactory = $timeFactory;
$this->logger = $logger;
parent::__construct();
}

Expand All @@ -48,6 +57,23 @@ protected function configure() {
}

protected function execute(InputInterface $input, OutputInterface $output) {
$this->config->setSystemValue('data-fingerprint', md5($this->timeFactory->getTime()));
$io = new SymfonyStyle($input, $output);
$warning = <<<EOD
Setting the data fingerprint will notify desktop and mobile clients that a server backup has been restored.
Users will be prompted to resolve any conflicts between newer and older file versions.
Setting the data fingerprint has a large impact on all connected clients!
Only perform this operation if you really know what you are doing.
EOD;

$io->warning($warning);
if ($io->confirm('Do you want to set the data fingerprint?', false)) {
$osUser = get_current_user();
$server = gethostname();
$fingerprint = md5($this->timeFactory->getTime());
$this->config->setSystemValue('data-fingerprint', $fingerprint);
$this->logger->info("Data fingerprint was set by $osUser@$server to $fingerprint");
}
}
}
2 changes: 1 addition & 1 deletion core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
);
$application->add(new OC\Core\Command\Encryption\ShowKeyStorageRoot($util));

$application->add(new OC\Core\Command\Maintenance\DataFingerprint(\OC::$server->getConfig(), new \OC\AppFramework\Utility\TimeFactory()));
$application->add(new OC\Core\Command\Maintenance\DataFingerprint(\OC::$server->getConfig(), new \OC\AppFramework\Utility\TimeFactory(), \OC::$server->getLogger()));
$application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateDB(\OC::$server->getMimeTypeDetector(), \OC::$server->getMimeTypeLoader()));
$application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->getMimeTypeDetector()));
$application->add(new OC\Core\Command\Maintenance\Mode(\OC::$server->getConfig()));
Expand Down
66 changes: 47 additions & 19 deletions tests/Core/Command/Maintenance/DataFingerprintTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @author Roeland Jago Douma <[email protected]>
* @author Thomas Müller <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
Expand All @@ -24,41 +25,68 @@
use OC\Core\Command\Maintenance\DataFingerprint;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\ILogger;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;

class DataFingerprintTest extends TestCase {
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
protected $config;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $consoleInput;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $consoleOutput;
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $timeFactory;

/** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
private $config;
/** @var ITimeFactory | \PHPUnit_Framework_MockObject_MockObject */
private $timeFactory;
/** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */
private $logger;
/** @var \Symfony\Component\Console\Command\Command */
protected $command;
private $command;
/** @var CommandTester */
private $commandTester;

public function providesAnswers() {
return [
'yes' => [true, 'yes'],
'no' => [false, 'no'],
];
}

protected function setUp() {
parent::setUp();

$this->config = $this->createMock('OCP\IConfig');
$this->timeFactory = $this->createMock('OCP\AppFramework\Utility\ITimeFactory');
$this->consoleInput = $this->createMock('Symfony\Component\Console\Input\InputInterface');
$this->consoleOutput = $this->createMock('Symfony\Component\Console\Output\OutputInterface');
$this->config = $this->createMock(IConfig::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->logger = $this->createMock(ILogger::class);

$this->command = new DataFingerprint($this->config, $this->timeFactory, $this->logger);
$this->commandTester = new CommandTester($this->command);

/** @var \OCP\IConfig $config */
$this->command = new DataFingerprint($this->config, $this->timeFactory);
}

public function testSetFingerPrint() {
$this->timeFactory->expects($this->once())
/**
* @dataProvider providesAnswers
* @param $expectToLog
* @param $answer
*/
public function testSetFingerPrint($expectToLog, $answer) {
$expects = $expectToLog ? $this->any() : $this->never();
$this->timeFactory->expects($expects)
->method('getTime')
->willReturn(42);
$this->config->expects($this->once())
$this->config->expects($expects)
->method('setSystemValue')
->with('data-fingerprint', md5(42));

self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
$osUser = get_current_user();
$server = gethostname();

$this->logger->expects($expects)
->method('info')
->with("Data fingerprint was set by $osUser@$server to a1d0c6e83f027327d8461063f4ac58a6");

$this->commandTester->setInputs([$answer]);
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
$this->assertContains("Do you want to set the data fingerprint?", $output);


}
}

0 comments on commit d41cd59

Please sign in to comment.