2015-07-28 16:12:41 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-19 12:17:01 +03:00
|
|
|
namespace Tests\Settings\Controller;
|
2015-07-28 16:12:41 +03:00
|
|
|
|
|
|
|
use OC\DB\Connection;
|
|
|
|
use OC\Files\View;
|
2016-05-19 12:17:01 +03:00
|
|
|
use OC\Settings\Controller\EncryptionController;
|
2017-10-24 00:20:50 +03:00
|
|
|
use OCP\App\IAppManager;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCA\Encryption\Migration;
|
2015-07-28 16:12:41 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserManager;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\UserInterface;
|
2015-07-28 16:12:41 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class EncryptionControllerTest
|
|
|
|
*
|
2016-05-19 12:17:01 +03:00
|
|
|
* @package Tests\Settings\Controller
|
2015-07-28 16:12:41 +03:00
|
|
|
*/
|
|
|
|
class EncryptionControllerTest extends TestCase {
|
|
|
|
/** @var IRequest */
|
|
|
|
private $request;
|
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
/** @var Connection */
|
|
|
|
private $connection;
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
/** @var View */
|
|
|
|
private $view;
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2017-10-24 00:20:50 +03:00
|
|
|
/** @var IAppManager */
|
|
|
|
private $appManager;
|
2015-07-28 16:12:41 +03:00
|
|
|
/** @var EncryptionController */
|
|
|
|
private $encryptionController;
|
|
|
|
|
|
|
|
public function setUp() {
|
2016-10-06 15:05:52 +03:00
|
|
|
parent::setUp();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->request = $this->getMockBuilder(IRequest::class)
|
2015-07-28 16:12:41 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->l10n = $this->getMockBuilder(IL10N::class)
|
2015-07-28 16:12:41 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->l10n->expects($this->any())
|
|
|
|
->method('t')
|
|
|
|
->will($this->returnCallback(function($message, array $replace) {
|
|
|
|
return vsprintf($message, $replace);
|
|
|
|
}));
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->config = $this->getMockBuilder(IConfig::class)
|
2015-07-28 16:12:41 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->connection = $this->getMockBuilder(Connection::class)
|
2015-07-28 16:12:41 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->userManager = $this->getMockBuilder(IUserManager::class)
|
2015-07-28 16:12:41 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->view = $this->getMockBuilder(View::class)
|
2015-07-28 16:12:41 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->logger = $this->getMockBuilder(ILogger::class)
|
2015-07-28 16:12:41 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 00:20:50 +03:00
|
|
|
$this->appManager = $this->getMockBuilder('\\OCP\\App\\IAppManager')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-07-28 16:12:41 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->encryptionController = $this->getMockBuilder(EncryptionController::class)
|
2015-07-28 16:12:41 +03:00
|
|
|
->setConstructorArgs([
|
|
|
|
'settings',
|
|
|
|
$this->request,
|
|
|
|
$this->l10n,
|
|
|
|
$this->config,
|
|
|
|
$this->connection,
|
|
|
|
$this->userManager,
|
|
|
|
$this->view,
|
|
|
|
$this->logger,
|
2017-10-24 00:20:50 +03:00
|
|
|
$this->appManager,
|
2015-07-28 16:12:41 +03:00
|
|
|
])
|
|
|
|
->setMethods(['getMigration'])
|
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStartMigrationSuccessful() {
|
2016-01-18 14:16:13 +03:00
|
|
|
// we need to be able to autoload the class we're mocking
|
2016-05-10 12:49:55 +03:00
|
|
|
\OC_App::registerAutoloading('encryption', \OC_App::getAppPath('encryption'));
|
2016-01-18 14:16:13 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$migration = $this->getMockBuilder(Migration::class)
|
2015-07-28 16:12:41 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->encryptionController
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getMigration')
|
|
|
|
->with($this->config, $this->view, $this->connection, $this->logger)
|
|
|
|
->will($this->returnValue($migration));
|
|
|
|
$migration
|
|
|
|
->expects($this->once())
|
|
|
|
->method('reorganizeSystemFolderStructure');
|
|
|
|
$migration
|
|
|
|
->expects($this->once())
|
|
|
|
->method('updateDB');
|
2017-10-24 16:26:53 +03:00
|
|
|
$backend = $this->getMockBuilder(UserInterface::class)
|
2015-07-28 16:12:41 +03:00
|
|
|
->getMock();
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getBackends')
|
|
|
|
->will($this->returnValue([$backend]));
|
|
|
|
$backend
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUsers')
|
|
|
|
->will($this->returnValue(['User 1', 'User 2']));
|
|
|
|
$migration
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('reorganizeFolderStructureForUser')
|
|
|
|
->withConsecutive(
|
|
|
|
['User 1'],
|
|
|
|
['User 2']
|
|
|
|
);
|
|
|
|
$migration
|
|
|
|
->expects($this->once())
|
|
|
|
->method('finalCleanUp');
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'message' => 'Migration Completed',
|
|
|
|
],
|
|
|
|
'status' => 'success',
|
|
|
|
];
|
|
|
|
$this->assertSame($expected, $this->encryptionController->startMigration());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStartMigrationException() {
|
|
|
|
$this->encryptionController
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getMigration')
|
|
|
|
->with($this->config, $this->view, $this->connection, $this->logger)
|
|
|
|
->will($this->throwException(new \Exception('My error message')));
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'message' => 'A problem occurred, please check your log files (Error: My error message)',
|
|
|
|
],
|
|
|
|
'status' => 'error',
|
|
|
|
];
|
|
|
|
$this->assertSame($expected, $this->encryptionController->startMigration());
|
|
|
|
}
|
|
|
|
}
|