diff options
author | Joas Schilling <coding@schilljs.com> | 2016-10-06 16:57:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-06 16:57:39 +0200 |
commit | a0b34dfd2f5e6ed1ea5e35692816acf73f3ede4e (patch) | |
tree | 335f901a7493d920ad6a382647fdbcb5193af7ce | |
parent | 6d688e89c1457a682fcc07e595e96014ada36add (diff) | |
parent | ed30b1f64537e94fb9874cd5f52be03c5e66899b (diff) | |
download | nextcloud-server-a0b34dfd2f5e6ed1ea5e35692816acf73f3ede4e.tar.gz nextcloud-server-a0b34dfd2f5e6ed1ea5e35692816acf73f3ede4e.zip |
Merge pull request #1629 from nextcloud/cleanup-settings-application
Cleanup settings Application class
-rw-r--r-- | core/Application.php | 2 | ||||
-rw-r--r-- | lib/private/AppFramework/Utility/SimpleContainer.php | 3 | ||||
-rw-r--r-- | settings/Application.php | 231 | ||||
-rw-r--r-- | settings/Controller/AuthSettingsController.php | 6 | ||||
-rw-r--r-- | settings/Controller/EncryptionController.php | 21 | ||||
-rw-r--r-- | settings/Controller/MailSettingsController.php | 25 | ||||
-rw-r--r-- | settings/Controller/UsersController.php | 1 | ||||
-rw-r--r-- | tests/Core/Controller/LostControllerTest.php | 3 | ||||
-rw-r--r-- | tests/Settings/ApplicationTest.php | 123 | ||||
-rw-r--r-- | tests/Settings/Controller/EncryptionControllerTest.php | 1 | ||||
-rw-r--r-- | tests/Settings/Controller/GroupsControllerTest.php | 70 | ||||
-rw-r--r-- | tests/Settings/Controller/LogSettingsControllerTest.php | 30 | ||||
-rw-r--r-- | tests/Settings/Controller/MailSettingsControllerTest.php | 113 | ||||
-rw-r--r-- | tests/Settings/Controller/SecuritySettingsControllerTest.php | 30 | ||||
-rw-r--r-- | tests/Settings/Controller/UsersControllerTest.php | 438 | ||||
-rw-r--r-- | tests/Settings/Middleware/SubadminMiddlewareTest.php | 1 |
16 files changed, 535 insertions, 563 deletions
diff --git a/core/Application.php b/core/Application.php index 97b1e1d37f1..6c6a3768a0f 100644 --- a/core/Application.php +++ b/core/Application.php @@ -30,8 +30,6 @@ namespace OC\Core; -use OC\Authentication\Token\DefaultTokenProvider; -use OC\Authentication\Token\IProvider; use OCP\AppFramework\App; use OCP\Util; diff --git a/lib/private/AppFramework/Utility/SimpleContainer.php b/lib/private/AppFramework/Utility/SimpleContainer.php index 8d5bceb0b87..889f17cbc6a 100644 --- a/lib/private/AppFramework/Utility/SimpleContainer.php +++ b/lib/private/AppFramework/Utility/SimpleContainer.php @@ -68,6 +68,9 @@ class SimpleContainer extends Container implements IContainer { // Service not found, use the default value when available if ($parameter->isDefaultValueAvailable()) { $parameters[] = $parameter->getDefaultValue(); + } else if ($parameterClass !== null) { + $resolveName = $parameter->getName(); + $parameters[] = $this->query($resolveName); } else { throw $e; } diff --git a/settings/Application.php b/settings/Application.php index eceb2b6937c..dd237e40c9d 100644 --- a/settings/Application.php +++ b/settings/Application.php @@ -30,22 +30,11 @@ namespace OC\Settings; -use OC\Files\View; -use OC\Server; -use OC\Settings\Controller\AdminSettingsController; -use OC\Settings\Controller\AppSettingsController; -use OC\Settings\Controller\AuthSettingsController; -use OC\Settings\Controller\CertificateController; -use OC\Settings\Controller\CheckSetupController; -use OC\Settings\Controller\EncryptionController; -use OC\Settings\Controller\GroupsController; -use OC\Settings\Controller\LogSettingsController; -use OC\Settings\Controller\MailSettingsController; -use OC\Settings\Controller\SecuritySettingsController; -use OC\Settings\Controller\UsersController; +use OC\Authentication\Token\IProvider; use OC\Settings\Middleware\SubadminMiddleware; use OCP\AppFramework\App; use OCP\IContainer; +use OCP\Settings\IManager; use OCP\Util; /** @@ -62,171 +51,19 @@ class Application extends App { $container = $this->getContainer(); - /** - * Controllers - */ - $container->registerService('MailSettingsController', function(IContainer $c) { - return new MailSettingsController( - $c->query('AppName'), - $c->query('Request'), - $c->query('L10N'), - $c->query('Config'), - $c->query('UserSession'), - $c->query('Defaults'), - $c->query('Mailer'), - $c->query('DefaultMailAddress') - ); - }); - $container->registerService('EncryptionController', function(IContainer $c) { - return new EncryptionController( - $c->query('AppName'), - $c->query('Request'), - $c->query('L10N'), - $c->query('Config'), - $c->query('DatabaseConnection'), - $c->query('UserManager'), - new View(), - $c->query('Logger') - ); - }); - $container->registerService('AppSettingsController', function(IContainer $c) { - return new AppSettingsController( - $c->query('AppName'), - $c->query('Request'), - $c->query('L10N'), - $c->query('Config'), - $c->query('ICacheFactory'), - $c->query('INavigationManager'), - $c->query('IAppManager'), - $c->query('OcsClient') - ); - }); - $container->registerService('AuthSettingsController', function(IContainer $c) { - return new AuthSettingsController( - $c->query('AppName'), - $c->query('Request'), - $c->query('ServerContainer')->query('OC\Authentication\Token\IProvider'), - $c->query('UserManager'), - $c->query('ServerContainer')->getSession(), - $c->query('ServerContainer')->getSecureRandom(), - $c->query('UserId') - ); - }); - $container->registerService('SecuritySettingsController', function(IContainer $c) { - return new SecuritySettingsController( - $c->query('AppName'), - $c->query('Request'), - $c->query('Config') - ); - }); - $container->registerService('CertificateController', function(IContainer $c) { - return new CertificateController( - $c->query('AppName'), - $c->query('Request'), - $c->query('CertificateManager'), - $c->query('SystemCertificateManager'), - $c->query('L10N'), - $c->query('IAppManager') - ); - }); - $container->registerService('GroupsController', function(IContainer $c) { - return new GroupsController( - $c->query('AppName'), - $c->query('Request'), - $c->query('GroupManager'), - $c->query('UserSession'), - $c->query('IsAdmin'), - $c->query('L10N') - ); - }); - $container->registerService('UsersController', function(IContainer $c) { - return new UsersController( - $c->query('AppName'), - $c->query('Request'), - $c->query('UserManager'), - $c->query('GroupManager'), - $c->query('UserSession'), - $c->query('Config'), - $c->query('IsAdmin'), - $c->query('L10N'), - $c->query('Logger'), - $c->query('Defaults'), - $c->query('Mailer'), - $c->query('DefaultMailAddress'), - $c->query('URLGenerator'), - $c->query('OCP\\App\\IAppManager'), - $c->query('OCP\\IAvatarManager') - ); - }); - $container->registerService('LogSettingsController', function(IContainer $c) { - return new LogSettingsController( - $c->query('AppName'), - $c->query('Request'), - $c->query('Config'), - $c->query('L10N') - ); - }); - $container->registerService('CheckSetupController', function(IContainer $c) { - return new CheckSetupController( - $c->query('AppName'), - $c->query('Request'), - $c->query('Config'), - $c->query('ClientService'), - $c->query('URLGenerator'), - $c->query('Util'), - $c->query('L10N'), - $c->query('Checker'), - $c->query('Logger') - ); - }); - $container->registerService('AdminSettingsController', function(IContainer $c) { - return new AdminSettingsController( - $c->query('AppName'), - $c->query('Request'), - $c->query('INavigationManager'), - $c->query('SettingsManager') - ); - }); - - /** - * Middleware - */ - $container->registerService('SubadminMiddleware', function(IContainer $c){ - return new SubadminMiddleware( - $c->query('ControllerMethodReflector'), - $c->query('IsSubAdmin') - ); - }); - // Execute middlewares - $container->registerMiddleware('SubadminMiddleware'); + // Register Middleware + $container->registerAlias('SubadminMiddleware', SubadminMiddleware::class); + $container->registerMiddleWare('SubadminMiddleware'); /** * Core class wrappers */ - $container->registerService('Config', function(IContainer $c) { - return $c->query('ServerContainer')->getConfig(); - }); - $container->registerService('ICacheFactory', function(IContainer $c) { - return $c->query('ServerContainer')->getMemCacheFactory(); - }); - $container->registerService('L10N', function(IContainer $c) { - return $c->query('ServerContainer')->getL10N('settings'); - }); - $container->registerService('GroupManager', function(IContainer $c) { - return $c->query('ServerContainer')->getGroupManager(); - }); - $container->registerService('UserManager', function(IContainer $c) { - return $c->query('ServerContainer')->getUserManager(); - }); - $container->registerService('UserSession', function(IContainer $c) { - return $c->query('ServerContainer')->getUserSession(); - }); /** FIXME: Remove once OC_User is non-static and mockable */ - $container->registerService('IsAdmin', function(IContainer $c) { + $container->registerService('isAdmin', function() { return \OC_User::isAdminUser(\OC_User::getUser()); }); /** FIXME: Remove once OC_SubAdmin is non-static and mockable */ - $container->registerService('IsSubAdmin', function(IContainer $c) { + $container->registerService('isSubAdmin', function(IContainer $c) { $userObject = \OC::$server->getUserSession()->getUser(); $isSubAdmin = false; if($userObject !== null) { @@ -234,57 +71,19 @@ class Application extends App { } return $isSubAdmin; }); - $container->registerService('Mailer', function(IContainer $c) { - return $c->query('ServerContainer')->getMailer(); - }); - $container->registerService('Defaults', function(IContainer $c) { - return $c->query('ServerContainer')->getThemingDefaults(); - }); - $container->registerService('DefaultMailAddress', function(IContainer $c) { + $container->registerService('fromMailAddress', function() { return Util::getDefaultEmailAddress('no-reply'); }); - $container->registerService('Logger', function(IContainer $c) { - return $c->query('ServerContainer')->getLogger(); - }); - $container->registerService('URLGenerator', function(IContainer $c) { - return $c->query('ServerContainer')->getURLGenerator(); - }); - $container->registerService('ClientService', function(IContainer $c) { - return $c->query('ServerContainer')->getHTTPClientService(); - }); - $container->registerService('INavigationManager', function(IContainer $c) { - return $c->query('ServerContainer')->getNavigationManager(); - }); - $container->registerService('IAppManager', function(IContainer $c) { - return $c->query('ServerContainer')->getAppManager(); - }); - $container->registerService('OcsClient', function(IContainer $c) { - return $c->query('ServerContainer')->getOcsClient(); - }); - $container->registerService('Util', function(IContainer $c) { - return new \OC_Util(); - }); - $container->registerService('DatabaseConnection', function(IContainer $c) { - return $c->query('ServerContainer')->getDatabaseConnection(); - }); - $container->registerService('CertificateManager', function(IContainer $c){ + $container->registerService('userCertificateManager', function(IContainer $c) { return $c->query('ServerContainer')->getCertificateManager(); - }); - $container->registerService('SystemCertificateManager', function (IContainer $c) { + }, false); + $container->registerService('systemCertificateManager', function (IContainer $c) { return $c->query('ServerContainer')->getCertificateManager(null); + }, false); + $container->registerService(IProvider::class, function (IContainer $c) { + return $c->query('ServerContainer')->query(IProvider::class); }); - $container->registerService('Checker', function(IContainer $c) { - /** @var Server $server */ - $server = $c->query('ServerContainer'); - return $server->getIntegrityCodeChecker(); - }); - $container->registerService('EventDispatcher', function (IContainer $c) { - return $c->query('ServerContainer')->getEventDispatcher(); - }); - $container->registerService('EncryptionManager', function (IContainer $c) { - return $c->query('ServerContainer')->getEncryptionManager(); - }); - $container->registerService('SettingsManager', function (IContainer $c) { + $container->registerService(IManager::class, function (IContainer $c) { return $c->query('ServerContainer')->getSettingsManager(); }); } diff --git a/settings/Controller/AuthSettingsController.php b/settings/Controller/AuthSettingsController.php index 5e6ad7859a7..58994f0d59c 100644 --- a/settings/Controller/AuthSettingsController.php +++ b/settings/Controller/AuthSettingsController.php @@ -60,14 +60,14 @@ class AuthSettingsController extends Controller { * @param IUserManager $userManager * @param ISession $session * @param ISecureRandom $random - * @param string $uid + * @param string $userId */ public function __construct($appName, IRequest $request, IProvider $tokenProvider, IUserManager $userManager, - ISession $session, ISecureRandom $random, $uid) { + ISession $session, ISecureRandom $random, $userId) { parent::__construct($appName, $request); $this->tokenProvider = $tokenProvider; $this->userManager = $userManager; - $this->uid = $uid; + $this->uid = $userId; $this->session = $session; $this->random = $random; } diff --git a/settings/Controller/EncryptionController.php b/settings/Controller/EncryptionController.php index b2757b0965f..cda50853fdc 100644 --- a/settings/Controller/EncryptionController.php +++ b/settings/Controller/EncryptionController.php @@ -23,14 +23,15 @@ namespace OC\Settings\Controller; + use OC\Files\View; use OCA\Encryption\Migration; +use OCP\IDBConnection; use OCP\IL10N; use OCP\AppFramework\Controller; use OCP\ILogger; use OCP\IRequest; use OCP\IConfig; -use OC\DB\Connection; use OCP\IUserManager; /** @@ -38,10 +39,10 @@ use OCP\IUserManager; */ class EncryptionController extends Controller { - /** @var \OCP\IL10N */ + /** @var IL10N */ private $l10n; - /** @var Connection */ + /** @var IDBConnection */ private $connection; /** @var IConfig */ @@ -53,15 +54,15 @@ class EncryptionController extends Controller { /** @var View */ private $view; - /** @var ILogger */ + /** @var ILogger */ private $logger; /** * @param string $appName * @param IRequest $request - * @param \OCP\IL10N $l10n - * @param \OCP\IConfig $config - * @param \OC\DB\Connection $connection + * @param IL10N $l10n + * @param IConfig $config + * @param IDBConnection $connection * @param IUserManager $userManager * @param View $view * @param ILogger $logger @@ -70,7 +71,7 @@ class EncryptionController extends Controller { IRequest $request, IL10N $l10n, IConfig $config, - Connection $connection, + IDBConnection $connection, IUserManager $userManager, View $view, ILogger $logger) { @@ -86,13 +87,13 @@ class EncryptionController extends Controller { /** * @param IConfig $config * @param View $view - * @param Connection $connection + * @param IDBConnection $connection * @param ILogger $logger * @return Migration */ protected function getMigration(IConfig $config, View $view, - Connection $connection, + IDBConnection $connection, ILogger $logger) { return new Migration($config, $view, $connection, $logger); } diff --git a/settings/Controller/MailSettingsController.php b/settings/Controller/MailSettingsController.php index 6ff12dec40c..84423aa8e29 100644 --- a/settings/Controller/MailSettingsController.php +++ b/settings/Controller/MailSettingsController.php @@ -24,11 +24,11 @@ namespace OC\Settings\Controller; -use OC\User\Session; -use \OCP\AppFramework\Controller; +use OCP\AppFramework\Controller; use OCP\IRequest; use OCP\IL10N; use OCP\IConfig; +use OCP\IUserSession; use OCP\Mail\IMailer; /** @@ -36,14 +36,12 @@ use OCP\Mail\IMailer; */ class MailSettingsController extends Controller { - /** @var \OCP\IL10N */ + /** @var IL10N */ private $l10n; - /** @var \OCP\IConfig */ + /** @var IConfig */ private $config; - /** @var Session */ + /** @var IUserSession */ private $userSession; - /** @var \OC_Defaults */ - private $defaults; /** @var IMailer */ private $mailer; /** @var string */ @@ -54,26 +52,23 @@ class MailSettingsController extends Controller { * @param IRequest $request * @param IL10N $l10n * @param IConfig $config - * @param Session $userSession - * @param \OC_Defaults $defaults + * @param IUserSession $userSession * @param IMailer $mailer - * @param string $defaultMailAddress + * @param string $fromMailAddress */ public function __construct($appName, IRequest $request, IL10N $l10n, IConfig $config, - Session $userSession, - \OC_Defaults $defaults, + IUserSession $userSession, IMailer $mailer, - $defaultMailAddress) { + $fromMailAddress) { parent::__construct($appName, $request); $this->l10n = $l10n; $this->config = $config; $this->userSession = $userSession; - $this->defaults = $defaults; $this->mailer = $mailer; - $this->defaultMailAddress = $defaultMailAddress; + $this->defaultMailAddress = $fromMailAddress; } /** diff --git a/settings/Controller/UsersController.php b/settings/Controller/UsersController.php index d4831214da2..fde29de3598 100644 --- a/settings/Controller/UsersController.php +++ b/settings/Controller/UsersController.php @@ -96,6 +96,7 @@ class UsersController extends Controller { * @param string $fromMailAddress * @param IURLGenerator $urlGenerator * @param IAppManager $appManager + * @param IAvatarManager $avatarManager */ public function __construct($appName, IRequest $request, diff --git a/tests/Core/Controller/LostControllerTest.php b/tests/Core/Controller/LostControllerTest.php index 177e41061a4..2c9b078908a 100644 --- a/tests/Core/Controller/LostControllerTest.php +++ b/tests/Core/Controller/LostControllerTest.php @@ -40,7 +40,7 @@ use PHPUnit_Framework_MockObject_MockObject; * * @package OC\Core\Controller */ -class LostControllerTest extends \PHPUnit_Framework_TestCase { +class LostControllerTest extends \Test\TestCase { /** @var LostController */ private $lostController; @@ -68,6 +68,7 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { private $request; protected function setUp() { + parent::setUp(); $this->existingUser = $this->getMockBuilder('OCP\IUser') ->disableOriginalConstructor()->getMock(); diff --git a/tests/Settings/ApplicationTest.php b/tests/Settings/ApplicationTest.php new file mode 100644 index 00000000000..32ec4c2273c --- /dev/null +++ b/tests/Settings/ApplicationTest.php @@ -0,0 +1,123 @@ +<?php +/** + * @copyright Copyright (c) 2016, Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Tests\Settings; + + +use OC\Settings\Application; +use OC\Settings\Controller\AdminSettingsController; +use OC\Settings\Controller\AppSettingsController; +use OC\Settings\Controller\AuthSettingsController; +use OC\Settings\Controller\CertificateController; +use OC\Settings\Controller\CheckSetupController; +use OC\Settings\Controller\EncryptionController; +use OC\Settings\Controller\GroupsController; +use OC\Settings\Controller\LogSettingsController; +use OC\Settings\Controller\MailSettingsController; +use OC\Settings\Controller\SecuritySettingsController; +use OC\Settings\Controller\UsersController; +use OC\Settings\Middleware\SubadminMiddleware; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Middleware; +use OCP\IUser; +use OCP\IUserSession; +use Test\TestCase; + +/** + * Class ApplicationTest + * + * @package Tests\Settings + * @group DB + */ +class ApplicationTest extends TestCase { + /** @var \OC\Settings\Application */ + protected $app; + + /** @var \OCP\AppFramework\IAppContainer */ + protected $container; + + protected function setUp() { + parent::setUp(); + $this->app = new Application(); + $this->container = $this->app->getContainer(); + } + + public function testContainerAppName() { + $this->app = new Application(); + $this->assertEquals('settings', $this->container->getAppName()); + } + + public function dataContainerQuery() { + return [ + [AdminSettingsController::class, Controller::class], + [AppSettingsController::class, Controller::class], + [AuthSettingsController::class, Controller::class], + // Needs session: [CertificateController::class, Controller::class], + [CheckSetupController::class, Controller::class], + [EncryptionController::class, Controller::class], + [GroupsController::class, Controller::class], + [LogSettingsController::class, Controller::class], + [MailSettingsController::class, Controller::class], + [SecuritySettingsController::class, Controller::class], + [UsersController::class, Controller::class], + + [SubadminMiddleware::class, Middleware::class], + ]; + } + + /** + * @dataProvider dataContainerQuery + * @param string $service + * @param string $expected + */ + public function testContainerQuery($service, $expected) { + $this->assertTrue($this->container->query($service) instanceof $expected); + } + + public function dataContainerQueryRequiresSession() { + return [ + [CertificateController::class, Controller::class], + ]; + } + + /** + * @dataProvider dataContainerQueryRequiresSession + * @param string $service + * @param string $expected + */ + public function testContainerQueryRequiresSession($service, $expected) { + $user = $this->createMock(IUser::class); + $user->expects($this->once()) + ->method('getUID') + ->willReturn('test'); + + $session = $this->createMock(IUserSession::class); + $session->expects($this->once()) + ->method('getUser') + ->willReturn($user); + + $this->overwriteService('UserSession', $session); + $this->assertTrue($this->container->query($service) instanceof $expected); + $this->restoreService('UserSession'); + } +} diff --git a/tests/Settings/Controller/EncryptionControllerTest.php b/tests/Settings/Controller/EncryptionControllerTest.php index adbbe2cf6a4..bbc2d7b6599 100644 --- a/tests/Settings/Controller/EncryptionControllerTest.php +++ b/tests/Settings/Controller/EncryptionControllerTest.php @@ -55,6 +55,7 @@ class EncryptionControllerTest extends TestCase { private $encryptionController; public function setUp() { + parent::setUp(); $this->request = $this->getMockBuilder('\\OCP\\IRequest') ->disableOriginalConstructor()->getMock(); $this->l10n = $this->getMockBuilder('\\OCP\\IL10N') diff --git a/tests/Settings/Controller/GroupsControllerTest.php b/tests/Settings/Controller/GroupsControllerTest.php index 70cb8282b26..340b39bf9dd 100644 --- a/tests/Settings/Controller/GroupsControllerTest.php +++ b/tests/Settings/Controller/GroupsControllerTest.php @@ -10,42 +10,47 @@ namespace Tests\Settings\Controller; -use OC\Group\Group; use OC\Group\MetaData; -use \OC\Settings\Application; use OC\Settings\Controller\GroupsController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\IGroupManager; +use OCP\IL10N; +use OCP\IRequest; +use OCP\IUserSession; /** * @package Tests\Settings\Controller */ class GroupsControllerTest extends \Test\TestCase { - /** @var \OCP\AppFramework\IAppContainer */ - private $container; + /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ + private $groupManager; + + /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + private $userSession; /** @var GroupsController */ private $groupsController; protected function setUp() { - $app = new Application(); - $this->container = $app->getContainer(); - $this->container['AppName'] = 'settings'; - $this->container['GroupManager'] = $this->getMockBuilder('\OCP\IGroupManager') - ->disableOriginalConstructor()->getMock(); - $this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session') - ->disableOriginalConstructor()->getMock(); - $this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N') - ->disableOriginalConstructor()->getMock(); - $this->container['IsAdmin'] = true; - $this->container['L10N'] - ->expects($this->any()) - ->method('t') - ->will($this->returnCallback(function($text, $parameters = array()) { - return vsprintf($text, $parameters); - })); - $this->groupsController = $this->container['GroupsController']; + parent::setUp(); + + $this->groupManager = $this->createMock(IGroupManager::class); + $this->userSession = $this->createMock(IUserSession::class); + $l = $this->createMock(IL10N::class); + $l->method('t') + ->will($this->returnCallback(function($text, $parameters = []) { + return vsprintf($text, $parameters); + })); + $this->groupsController = new GroupsController( + 'settings', + $this->createMock(IRequest::class), + $this->groupManager, + $this->userSession, + true, + $l + ); } @@ -95,7 +100,7 @@ class GroupsControllerTest extends \Test\TestCase { $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); - $this->container['UserSession'] + $this->userSession ->expects($this->once()) ->method('getUser') ->will($this->returnValue($user)); @@ -103,8 +108,7 @@ class GroupsControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('getUID') ->will($this->returnValue('MyAdminUser')); - $this->container['GroupManager'] - ->method('search') + $this->groupManager->method('search') ->will($this->returnValue($groups)); $expectedResponse = new DataResponse( @@ -188,7 +192,7 @@ class GroupsControllerTest extends \Test\TestCase { $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); - $this->container['UserSession'] + $this->userSession ->expects($this->once()) ->method('getUser') ->will($this->returnValue($user)); @@ -196,7 +200,7 @@ class GroupsControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('getUID') ->will($this->returnValue('MyAdminUser')); - $this->container['GroupManager'] + $this->groupManager ->method('search') ->will($this->returnValue($groups)); @@ -236,7 +240,7 @@ class GroupsControllerTest extends \Test\TestCase { } public function testCreateWithExistingGroup() { - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('groupExists') ->with('ExistingGroup') @@ -253,12 +257,12 @@ class GroupsControllerTest extends \Test\TestCase { } public function testCreateSuccessful() { - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('groupExists') ->with('NewGroup') ->will($this->returnValue(false)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('createGroup') ->with('NewGroup') @@ -275,12 +279,12 @@ class GroupsControllerTest extends \Test\TestCase { } public function testCreateUnsuccessful() { - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('groupExists') ->with('NewGroup') ->will($this->returnValue(false)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('createGroup') ->with('NewGroup') @@ -300,7 +304,7 @@ class GroupsControllerTest extends \Test\TestCase { public function testDestroySuccessful() { $group = $this->getMockBuilder('\OC\Group\Group') ->disableOriginalConstructor()->getMock(); - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('get') ->with('ExistingGroup') @@ -322,7 +326,7 @@ class GroupsControllerTest extends \Test\TestCase { } public function testDestroyUnsuccessful() { - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('get') ->with('ExistingGroup') diff --git a/tests/Settings/Controller/LogSettingsControllerTest.php b/tests/Settings/Controller/LogSettingsControllerTest.php index f296df9903c..27ae9328d1f 100644 --- a/tests/Settings/Controller/LogSettingsControllerTest.php +++ b/tests/Settings/Controller/LogSettingsControllerTest.php @@ -13,25 +13,36 @@ namespace Tests\Settings\Controller; use \OC\Settings\Application; use OC\Settings\Controller\LogSettingsController; use OCP\AppFramework\Http\StreamResponse; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IRequest; /** * @package Tests\Settings\Controller */ class LogSettingsControllerTest extends \Test\TestCase { - /** @var \OCP\AppFramework\IAppContainer */ - private $container; + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $config; /** @var LogSettingsController */ private $logSettingsController; protected function setUp() { - $app = new Application(); - $this->container = $app->getContainer(); - $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') - ->disableOriginalConstructor()->getMock(); - $this->container['AppName'] = 'settings'; - $this->logSettingsController = $this->container['LogSettingsController']; + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + $l = $this->createMock(IL10N::class); + $l->method('t') + ->will($this->returnCallback(function($text, $parameters = []) { + return vsprintf($text, $parameters); + })); + $this->logSettingsController = new LogSettingsController( + 'settings', + $this->createMock(IRequest::class), + $this->config, + $l + ); } /** @@ -39,8 +50,7 @@ class LogSettingsControllerTest extends \Test\TestCase { */ public function testSetLogLevel($level, $inRange) { if ($inRange) { - $this->container['Config'] - ->expects($this->once()) + $this->config->expects($this->once()) ->method('setSystemValue') ->with('loglevel', $level); } diff --git a/tests/Settings/Controller/MailSettingsControllerTest.php b/tests/Settings/Controller/MailSettingsControllerTest.php index 1ac6bae69ea..2012de886c8 100644 --- a/tests/Settings/Controller/MailSettingsControllerTest.php +++ b/tests/Settings/Controller/MailSettingsControllerTest.php @@ -10,75 +10,60 @@ namespace Tests\Settings\Controller; -use OC\Settings\Application; +use OC\Mail\Message; +use OC\Settings\Controller\MailSettingsController; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IRequest; +use OCP\IUserSession; +use OCP\Mail\IMailer; /** * @package Tests\Settings\Controller */ class MailSettingsControllerTest extends \Test\TestCase { - private $container; + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $config; + /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + private $userSession; + /** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */ + private $mailer; + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ + private $l; + + /** @var MailSettingsController */ + private $mailController; protected function setUp() { parent::setUp(); - $app = new Application(); - $this->container = $app->getContainer(); - $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') - ->disableOriginalConstructor()->getMock(); - $this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N') - ->disableOriginalConstructor()->getMock(); - $this->container['AppName'] = 'settings'; - $this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session') - ->disableOriginalConstructor()->getMock(); - $this->container['MailMessage'] = $this->getMockBuilder('\OCP\Mail\IMessage') - ->disableOriginalConstructor()->getMock(); - $this->container['Mailer'] = $this->getMockBuilder('\OC\Mail\Mailer') - ->setMethods(['send']) - ->disableOriginalConstructor()->getMock(); - $this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults') - ->disableOriginalConstructor()->getMock(); - $this->container['DefaultMailAddress'] = 'no-reply@owncloud.com'; + $this->l = $this->createMock(IL10N::class); + $this->config = $this->createMock(IConfig::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->mailer = $this->createMock(IMailer::class); +// $this->mailer = $this->getMockBuilder(IMailer::class) +// ->setMethods(['send']) +// ->getMock(); + $this->mailController = new MailSettingsController( + 'settings', + $this->createMock(IRequest::class), + $this->l, + $this->config, + $this->userSession, + $this->mailer, + 'no-reply@owncloud.com' + ); } public function testSetMailSettings() { - $this->container['L10N'] + $this->l ->expects($this->exactly(2)) ->method('t') ->will($this->returnValue('Saved')); - /** - * FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1 - */ - /* - $this->container['Config'] - ->expects($this->exactly(15)) - ->method('setSystemValue') - ->withConsecutive( - array($this->equalTo('mail_domain'), $this->equalTo('owncloud.com')), - array($this->equalTo('mail_from_address'), $this->equalTo('demo@owncloud.com')), - array($this->equalTo('mail_smtpmode'), $this->equalTo('smtp')), - array($this->equalTo('mail_smtpsecure'), $this->equalTo('ssl')), - array($this->equalTo('mail_smtphost'), $this->equalTo('mx.owncloud.org')), - array($this->equalTo('mail_smtpauthtype'), $this->equalTo('NTLM')), - array($this->equalTo('mail_smtpauth'), $this->equalTo(1)), - array($this->equalTo('mail_smtpport'), $this->equalTo('25')), - array($this->equalTo('mail_domain'), $this->equalTo('owncloud.com')), - array($this->equalTo('mail_from_address'), $this->equalTo('demo@owncloud.com')), - array($this->equalTo('mail_smtpmode'), $this->equalTo('smtp')), - array($this->equalTo('mail_smtpsecure'), $this->equalTo('ssl')), - array($this->equalTo('mail_smtphost'), $this->equalTo('mx.owncloud.org')), - array($this->equalTo('mail_smtpauthtype'), $this->equalTo('NTLM')), - array($this->equalTo('mail_smtpport'), $this->equalTo('25')) - ); - */ - - /** @var \PHPUnit_Framework_MockObject_MockObject $config */ - $config = $this->container['Config']; - $config->expects($this->exactly(2)) - ->method('setSystemValues'); - /** - * FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1 + $this->config->expects($this->exactly(2)) + ->method('setSystemValues') ->withConsecutive( [[ 'mail_domain' => 'owncloud.com', @@ -103,10 +88,9 @@ class MailSettingsControllerTest extends \Test\TestCase { 'mail_smtppassword' => null, ]] ); - */ // With authentication - $response = $this->container['MailSettingsController']->setMailSettings( + $response = $this->mailController->setMailSettings( 'owncloud.com', 'demo@owncloud.com', 'smtp', @@ -120,7 +104,7 @@ class MailSettingsControllerTest extends \Test\TestCase { $this->assertSame($expectedResponse, $response); // Without authentication (testing the deletion of the stored password) - $response = $this->container['MailSettingsController']->setMailSettings( + $response = $this->mailController->setMailSettings( 'owncloud.com', 'demo@owncloud.com', 'smtp', @@ -136,12 +120,12 @@ class MailSettingsControllerTest extends \Test\TestCase { } public function testStoreCredentials() { - $this->container['L10N'] + $this->l ->expects($this->once()) ->method('t') ->will($this->returnValue('Saved')); - $this->container['Config'] + $this->config ->expects($this->once()) ->method('setSystemValues') ->with([ @@ -149,7 +133,7 @@ class MailSettingsControllerTest extends \Test\TestCase { 'mail_smtppassword' => 'PasswordToStore', ]); - $response = $this->container['MailSettingsController']->storeCredentials('UsernameToStore', 'PasswordToStore'); + $response = $this->mailController->storeCredentials('UsernameToStore', 'PasswordToStore'); $expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success'); $this->assertSame($expectedResponse, $response); @@ -166,7 +150,7 @@ class MailSettingsControllerTest extends \Test\TestCase { ->method('getDisplayName') ->will($this->returnValue('Werner Brösel')); - $this->container['L10N'] + $this->l ->expects($this->any()) ->method('t') ->will( @@ -182,22 +166,25 @@ class MailSettingsControllerTest extends \Test\TestCase { 'If you received this email, the settings seem to be correct.') ) )); - $this->container['UserSession'] + $this->userSession ->expects($this->any()) ->method('getUser') ->will($this->returnValue($user)); // Ensure that it fails when no mail address has been specified - $response = $this->container['MailSettingsController']->sendTestMail(); + $response = $this->mailController->sendTestMail(); $expectedResponse = array('data' => array('message' =>'You need to set your user email before being able to send test emails.'), 'status' => 'error'); $this->assertSame($expectedResponse, $response); // If no exception is thrown it should work - $this->container['Config'] + $this->config ->expects($this->any()) ->method('getUserValue') ->will($this->returnValue('mail@example.invalid')); - $response = $this->container['MailSettingsController']->sendTestMail(); + $this->mailer->expects($this->once()) + ->method('createMessage') + ->willReturn($this->createMock(Message::class)); + $response = $this->mailController->sendTestMail(); $expectedResponse = array('data' => array('message' =>'Email sent'), 'status' => 'success'); $this->assertSame($expectedResponse, $response); } diff --git a/tests/Settings/Controller/SecuritySettingsControllerTest.php b/tests/Settings/Controller/SecuritySettingsControllerTest.php index 302dc730fc4..75d580d9f85 100644 --- a/tests/Settings/Controller/SecuritySettingsControllerTest.php +++ b/tests/Settings/Controller/SecuritySettingsControllerTest.php @@ -11,33 +11,37 @@ namespace Tests\Settings\Controller; use \OC\Settings\Application; use OC\Settings\Controller\SecuritySettingsController; +use OCP\IConfig; +use OCP\IRequest; /** * @package Tests\Settings\Controller */ -class SecuritySettingsControllerTest extends \PHPUnit_Framework_TestCase { +class SecuritySettingsControllerTest extends \Test\TestCase { - /** @var \OCP\AppFramework\IAppContainer */ - private $container; + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $config; /** @var SecuritySettingsController */ private $securitySettingsController; protected function setUp() { - $app = new Application(); - $this->container = $app->getContainer(); - $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') - ->disableOriginalConstructor()->getMock(); - $this->container['AppName'] = 'settings'; - $this->securitySettingsController = $this->container['SecuritySettingsController']; + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + $this->securitySettingsController = new SecuritySettingsController( + 'settings', + $this->createMock(IRequest::class), + $this->config + ); } public function testTrustedDomainsWithExistingValues() { - $this->container['Config'] + $this->config ->expects($this->once()) ->method('setSystemValue') ->with('trusted_domains', array('owncloud.org', 'owncloud.com', 'newdomain.com')); - $this->container['Config'] + $this->config ->expects($this->once()) ->method('getSystemValue') ->with('trusted_domains') @@ -50,11 +54,11 @@ class SecuritySettingsControllerTest extends \PHPUnit_Framework_TestCase { } public function testTrustedDomainsEmpty() { - $this->container['Config'] + $this->config ->expects($this->once()) ->method('setSystemValue') ->with('trusted_domains', array('newdomain.com')); - $this->container['Config'] + $this->config ->expects($this->once()) ->method('getSystemValue') ->with($this->equalTo('trusted_domains'), $this->equalTo([])) diff --git a/tests/Settings/Controller/UsersControllerTest.php b/tests/Settings/Controller/UsersControllerTest.php index e036e299e00..03c3a2e2ab4 100644 --- a/tests/Settings/Controller/UsersControllerTest.php +++ b/tests/Settings/Controller/UsersControllerTest.php @@ -10,12 +10,23 @@ namespace Tests\Settings\Controller; -use \OC\Settings\Application; +use OC\Group\Manager; +use OC\Settings\Controller\UsersController; +use OCP\App\IAppManager; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\IAvatar; use OCP\IAvatarManager; +use OCP\IConfig; +use OCP\IGroupManager; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IRequest; +use OCP\IURLGenerator; use OCP\IUser; +use OCP\IUserManager; +use OCP\IUserSession; +use OCP\Mail\IMailer; /** * @group DB @@ -24,68 +35,94 @@ use OCP\IUser; */ class UsersControllerTest extends \Test\TestCase { - /** @var \OCP\AppFramework\IAppContainer */ - private $container; + /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ + private $groupManager; + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + private $userManager; + /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + private $userSession; + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + private $config; + /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ + private $logger; + /** @var \OC_Defaults|\PHPUnit_Framework_MockObject_MockObject */ + private $defaults; + /** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */ + private $mailer; + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ + private $urlGenerator; + /** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */ + private $appManager; + /** @var IAvatarManager|\PHPUnit_Framework_MockObject_MockObject */ + private $avatarManager; + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ + private $l; protected function setUp() { - $app = new Application(); - $this->container = $app->getContainer(); - $this->container['AppName'] = 'settings'; - $this->container['GroupManager'] = $this->getMockBuilder('\OC\Group\Manager') - ->disableOriginalConstructor()->getMock(); - $this->container['UserManager'] = $this->getMockBuilder('\OCP\IUserManager') - ->disableOriginalConstructor()->getMock(); - $this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session') - ->disableOriginalConstructor()->getMock(); - $this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N') - ->disableOriginalConstructor()->getMock(); - $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') - ->disableOriginalConstructor()->getMock(); - $this->container['L10N'] - ->expects($this->any()) - ->method('t') - ->will($this->returnCallback(function($text, $parameters = array()) { + parent::setUp(); + + $this->groupManager = $this->createMock(Manager::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->config = $this->createMock(IConfig::class); + $this->logger = $this->createMock(ILogger::class); + $this->defaults = $this->createMock(\OC_Defaults::class); + $this->mailer = $this->createMock(IMailer::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->appManager = $this->createMock(IAppManager::class); + $this->avatarManager = $this->createMock(IAvatarManager::class); + $this->l = $this->createMock(IL10N::class); + $this->l->method('t') + ->will($this->returnCallback(function ($text, $parameters = []) { return vsprintf($text, $parameters); })); - $this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults') - ->disableOriginalConstructor()->getMock(); - $this->container['Mailer'] = $this->getMockBuilder('\OCP\Mail\IMailer') - ->disableOriginalConstructor()->getMock(); - $this->container['DefaultMailAddress'] = 'no-reply@owncloud.com'; - $this->container['Logger'] = $this->getMockBuilder('\OCP\ILogger') - ->disableOriginalConstructor()->getMock(); - $this->container['URLGenerator'] = $this->getMockBuilder('\OCP\IURLGenerator') - ->disableOriginalConstructor()->getMock(); - $this->container['OCP\\App\\IAppManager'] = $this->getMockBuilder('OCP\\App\\IAppManager') - ->disableOriginalConstructor()->getMock(); - /* - * Set default avtar behaviour for whole testsuite + * Set default avatar behaviour for whole test suite */ - $this->container['OCP\\IAvatarManager'] = $this->createMock(IAvatarManager::class); $avatarExists = $this->createMock(IAvatar::class); $avatarExists->method('exists')->willReturn(true); $avatarNotExists = $this->createMock(IAvatar::class); $avatarNotExists->method('exists')->willReturn(false); - $this->container['OCP\\IAvatarManager'] - ->method('getAvatar') + $this->avatarManager->method('getAvatar') ->will($this->returnValueMap([ ['foo', $avatarExists], ['bar', $avatarExists], ['admin', $avatarNotExists], ])); - $this->container['Config'] - ->method('getSystemValue') + $this->config->method('getSystemValue') ->with('enable_avatars', true) ->willReturn(true); + } + /** + * @param bool $isAdmin + * @return UsersController + */ + protected function getController($isAdmin) { + return new UsersController( + 'settings', + $this->createMock(IRequest::class), + $this->userManager, + $this->groupManager, + $this->userSession, + $this->config, + $isAdmin, + $this->l, + $this->logger, + $this->defaults, + $this->mailer, + 'no-reply@owncloud.com', + $this->urlGenerator, + $this->appManager, + $this->avatarManager + ); } public function testIndexAdmin() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $foo = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -174,26 +211,26 @@ class UsersControllerTest extends \Test\TestCase { ->method('getBackendClassName') ->will($this->returnValue('\Test\Util\User\Dummy')); - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('displayNamesInGroup') ->with('gid', 'pattern') ->will($this->returnValue(array('foo' => 'M. Foo', 'admin' => 'S. Admin', 'bar' => 'B. Ar'))); - $this->container['GroupManager'] + $this->groupManager ->expects($this->exactly(3)) ->method('getUserGroupIds') ->will($this->onConsecutiveCalls(array('Users', 'Support'), array('admins', 'Support'), array('External Users'))); - $this->container['UserManager'] + $this->userManager ->expects($this->at(0)) ->method('get') ->with('foo') ->will($this->returnValue($foo)); - $this->container['UserManager'] + $this->userManager ->expects($this->at(1)) ->method('get') ->with('admin') ->will($this->returnValue($admin)); - $this->container['UserManager'] + $this->userManager ->expects($this->at(2)) ->method('get') ->with('bar') @@ -217,7 +254,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($bar) ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -265,16 +302,16 @@ class UsersControllerTest extends \Test\TestCase { ), ) ); - $response = $this->container['UsersController']->index(0, 10, 'gid', 'pattern'); + $response = $controller->index(0, 10, 'gid', 'pattern'); $this->assertEquals($expectedResponse, $response); } public function testIndexSubAdmin() { - $this->container['IsAdmin'] = false; + $controller = $this->getController(false); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); - $this->container['UserSession'] + $this->userSession ->expects($this->once()) ->method('getUser') ->will($this->returnValue($user)); @@ -366,17 +403,17 @@ class UsersControllerTest extends \Test\TestCase { ->method('getBackendClassName') ->will($this->returnValue('\Test\Util\User\Dummy')); - $this->container['GroupManager'] + $this->groupManager ->expects($this->at(2)) ->method('displayNamesInGroup') ->with('SubGroup2', 'pattern') ->will($this->returnValue(['foo' => 'M. Foo', 'admin' => 'S. Admin'])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->at(1)) ->method('displayNamesInGroup') ->with('SubGroup1', 'pattern') ->will($this->returnValue(['bar' => 'B. Ar'])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->exactly(3)) ->method('getUserGroupIds') ->will($this->onConsecutiveCalls( @@ -384,17 +421,17 @@ class UsersControllerTest extends \Test\TestCase { ['SubGroup2', 'SubGroup1'], ['SubGroup2', 'Foo'] )); - $this->container['UserManager'] + $this->userManager ->expects($this->at(0)) ->method('get') ->with('bar') ->will($this->returnValue($bar)); - $this->container['UserManager'] + $this->userManager ->expects($this->at(1)) ->method('get') ->with('foo') ->will($this->returnValue($foo)); - $this->container['UserManager'] + $this->userManager ->expects($this->at(2)) ->method('get') ->with('admin') @@ -423,7 +460,7 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->any()) ->method('getSubAdminsGroups') ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -472,7 +509,7 @@ class UsersControllerTest extends \Test\TestCase { ] ); - $response = $this->container['UsersController']->index(0, 10, '', 'pattern'); + $response = $controller->index(0, 10, '', 'pattern'); $this->assertEquals($expectedResponse, $response); } @@ -481,7 +518,7 @@ class UsersControllerTest extends \Test\TestCase { * to test for subadmins. Thus the test always assumes you have admin permissions... */ public function testIndexWithSearch() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $foo = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -570,12 +607,12 @@ class UsersControllerTest extends \Test\TestCase { ->method('getBackendClassName') ->will($this->returnValue('\Test\Util\User\Dummy')); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('search') ->with('pattern', 10, 0) ->will($this->returnValue([$foo, $admin, $bar])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->exactly(3)) ->method('getUserGroupIds') ->will($this->onConsecutiveCalls(array('Users', 'Support'), array('admins', 'Support'), array('External Users'))); @@ -586,7 +623,7 @@ class UsersControllerTest extends \Test\TestCase { $subadmin->expects($this->any()) ->method('getSubAdminsGroups') ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -634,12 +671,12 @@ class UsersControllerTest extends \Test\TestCase { ), ) ); - $response = $this->container['UsersController']->index(0, 10, '', 'pattern'); + $response = $controller->index(0, 10, '', 'pattern'); $this->assertEquals($expectedResponse, $response); } public function testIndexWithBackend() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -669,14 +706,14 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('getBackendClassName') ->will($this->returnValue('OC_User_Database')); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('getBackends') ->will($this->returnValue([new \Test\Util\User\Dummy(), new \OC\User\Database()])); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('clearBackends'); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('search') ->with('') @@ -688,7 +725,7 @@ class UsersControllerTest extends \Test\TestCase { $subadmin->expects($this->once()) ->method('getSubAdminsGroups') ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -710,30 +747,30 @@ class UsersControllerTest extends \Test\TestCase { ) ) ); - $response = $this->container['UsersController']->index(0, 10, '','', '\Test\Util\User\Dummy'); + $response = $controller->index(0, 10, '','', '\Test\Util\User\Dummy'); $this->assertEquals($expectedResponse, $response); } public function testIndexWithBackendNoUser() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('getBackends') ->will($this->returnValue([new \Test\Util\User\Dummy(), new \OC\User\Database()])); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('search') ->with('') ->will($this->returnValue([])); $expectedResponse = new DataResponse([]); - $response = $this->container['UsersController']->index(0, 10, '','', '\Test\Util\User\Dummy'); + $response = $controller->index(0, 10, '','', '\Test\Util\User\Dummy'); $this->assertEquals($expectedResponse, $response); } public function testCreateSuccessfulWithoutGroupAdmin() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -748,7 +785,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('getBackendClassName') ->will($this->returnValue('bar')); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('createUser') ->will($this->onConsecutiveCalls($user)); @@ -761,7 +798,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($user) ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -782,15 +819,15 @@ class UsersControllerTest extends \Test\TestCase { ), Http::STATUS_CREATED ); - $response = $this->container['UsersController']->create('foo', 'password', array()); + $response = $controller->create('foo', 'password', array()); $this->assertEquals($expectedResponse, $response); } public function testCreateSuccessfulWithoutGroupSubAdmin() { - $this->container['IsAdmin'] = false; + $controller = $this->getController(false); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); - $this->container['UserSession'] + $this->userSession ->expects($this->once()) ->method('getUser') ->will($this->returnValue($user)); @@ -825,15 +862,15 @@ class UsersControllerTest extends \Test\TestCase { ->method('addUser') ->with($newUser); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('createUser') ->will($this->returnValue($newUser)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->exactly(2)) ->method('get') ->will($this->onConsecutiveCalls($subGroup1, $subGroup2)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('getUserGroupIds') ->with($user) @@ -850,7 +887,7 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->at(1)) ->method('getSubAdminsGroups') ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -871,12 +908,12 @@ class UsersControllerTest extends \Test\TestCase { ), Http::STATUS_CREATED ); - $response = $this->container['UsersController']->create('foo', 'password'); + $response = $controller->create('foo', 'password'); $this->assertEquals($expectedResponse, $response); } public function testCreateSuccessfulWithGroupAdmin() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -906,20 +943,20 @@ class UsersControllerTest extends \Test\TestCase { ->method('addUser') ->with($user); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('createUser') ->will($this->onConsecutiveCalls($user)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->exactly(2)) ->method('get') ->will($this->onConsecutiveCalls(null, $existingGroup)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('createGroup') ->with('NewGroup') ->will($this->onConsecutiveCalls($newGroup)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('getUserGroupIds') ->with($user) @@ -933,7 +970,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($user) ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -954,15 +991,15 @@ class UsersControllerTest extends \Test\TestCase { ), Http::STATUS_CREATED ); - $response = $this->container['UsersController']->create('foo', 'password', array('NewGroup', 'ExistingGroup')); + $response = $controller->create('foo', 'password', array('NewGroup', 'ExistingGroup')); $this->assertEquals($expectedResponse, $response); } public function testCreateSuccessfulWithGroupSubAdmin() { - $this->container['IsAdmin'] = false; + $controller = $this->getController(false); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); - $this->container['UserSession'] + $this->userSession ->expects($this->once()) ->method('getUser') ->will($this->returnValue($user)); @@ -993,26 +1030,26 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('addUser') ->with($user); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('createUser') ->will($this->returnValue($newUser)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->at(0)) ->method('get') ->with('SubGroup1') ->will($this->returnValue($subGroup1)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->at(4)) ->method('get') ->with('SubGroup1') ->will($this->returnValue($subGroup1)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('getUserGroupIds') ->with($user) ->will($this->onConsecutiveCalls(['SubGroup1'])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('getUserGroupIds') ->with($newUser) @@ -1029,7 +1066,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($newUser) ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -1050,14 +1087,14 @@ class UsersControllerTest extends \Test\TestCase { ), Http::STATUS_CREATED ); - $response = $this->container['UsersController']->create('foo', 'password', ['SubGroup1', 'ExistingGroup']); + $response = $controller->create('foo', 'password', ['SubGroup1', 'ExistingGroup']); $this->assertEquals($expectedResponse, $response); } public function testCreateUnsuccessfulAdmin() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); - $this->container['UserManager'] + $this->userManager ->method('createUser') ->will($this->throwException(new \Exception())); @@ -1067,24 +1104,24 @@ class UsersControllerTest extends \Test\TestCase { ), Http::STATUS_FORBIDDEN ); - $response = $this->container['UsersController']->create('foo', 'password', array()); + $response = $controller->create('foo', 'password', array()); $this->assertEquals($expectedResponse, $response); } public function testCreateUnsuccessfulSubAdmin() { - $this->container['IsAdmin'] = false; + $controller = $this->getController(false); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); $user ->expects($this->any()) ->method('getUID') ->will($this->returnValue('username')); - $this->container['UserSession'] + $this->userSession ->expects($this->once()) ->method('getUser') ->will($this->returnValue($user)); - $this->container['UserManager'] + $this->userManager ->method('createUser') ->will($this->throwException(new \Exception())); @@ -1107,7 +1144,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($user) ->will($this->returnValue([$subgroup1, $subgroup2])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -1118,12 +1155,12 @@ class UsersControllerTest extends \Test\TestCase { ], Http::STATUS_FORBIDDEN ); - $response = $this->container['UsersController']->create('foo', 'password', array()); + $response = $controller->create('foo', 'password', array()); $this->assertEquals($expectedResponse, $response); } public function testDestroySelfAdmin() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -1131,7 +1168,7 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('getUID') ->will($this->returnValue('myself')); - $this->container['UserSession'] + $this->userSession ->method('getUser') ->will($this->returnValue($user)); @@ -1144,12 +1181,12 @@ class UsersControllerTest extends \Test\TestCase { ), Http::STATUS_FORBIDDEN ); - $response = $this->container['UsersController']->destroy('myself'); + $response = $controller->destroy('myself'); $this->assertEquals($expectedResponse, $response); } public function testDestroySelfSubadmin() { - $this->container['IsAdmin'] = false; + $controller = $this->getController(false); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -1157,7 +1194,7 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('getUID') ->will($this->returnValue('myself')); - $this->container['UserSession'] + $this->userSession ->method('getUser') ->will($this->returnValue($user)); @@ -1170,12 +1207,12 @@ class UsersControllerTest extends \Test\TestCase { ), Http::STATUS_FORBIDDEN ); - $response = $this->container['UsersController']->destroy('myself'); + $response = $controller->destroy('myself'); $this->assertEquals($expectedResponse, $response); } public function testDestroyAdmin() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -1189,10 +1226,10 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('delete') ->will($this->returnValue(true)); - $this->container['UserSession'] + $this->userSession ->method('getUser') ->will($this->returnValue($user)); - $this->container['UserManager'] + $this->userManager ->method('get') ->with('UserToDelete') ->will($this->returnValue($toDeleteUser)); @@ -1206,19 +1243,19 @@ class UsersControllerTest extends \Test\TestCase { ), Http::STATUS_NO_CONTENT ); - $response = $this->container['UsersController']->destroy('UserToDelete'); + $response = $controller->destroy('UserToDelete'); $this->assertEquals($expectedResponse, $response); } public function testDestroySubAdmin() { - $this->container['IsAdmin'] = false; + $controller = $this->getController(false); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); $user ->expects($this->once()) ->method('getUID') ->will($this->returnValue('myself')); - $this->container['UserSession'] + $this->userSession ->method('getUser') ->will($this->returnValue($user)); @@ -1230,10 +1267,10 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('delete') ->will($this->returnValue(true)); - $this->container['UserSession'] + $this->userSession ->method('getUser') ->will($this->returnValue($user)); - $this->container['UserManager'] + $this->userManager ->method('get') ->with('UserToDelete') ->will($this->returnValue($toDeleteUser)); @@ -1245,7 +1282,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('isUserAccessible') ->with($user, $toDeleteUser) ->will($this->returnValue(true)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -1259,12 +1296,12 @@ class UsersControllerTest extends \Test\TestCase { ], Http::STATUS_NO_CONTENT ); - $response = $this->container['UsersController']->destroy('UserToDelete'); + $response = $controller->destroy('UserToDelete'); $this->assertEquals($expectedResponse, $response); } public function testDestroyUnsuccessfulAdmin() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -1278,10 +1315,10 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('delete') ->will($this->returnValue(false)); - $this->container['UserSession'] + $this->userSession ->method('getUser') ->will($this->returnValue($user)); - $this->container['UserManager'] + $this->userManager ->method('get') ->with('UserToDelete') ->will($this->returnValue($toDeleteUser)); @@ -1295,19 +1332,19 @@ class UsersControllerTest extends \Test\TestCase { ), Http::STATUS_FORBIDDEN ); - $response = $this->container['UsersController']->destroy('UserToDelete'); + $response = $controller->destroy('UserToDelete'); $this->assertEquals($expectedResponse, $response); } public function testDestroyUnsuccessfulSubAdmin() { - $this->container['IsAdmin'] = false; + $controller = $this->getController(false); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); $user ->expects($this->once()) ->method('getUID') ->will($this->returnValue('myself')); - $this->container['UserSession'] + $this->userSession ->method('getUser') ->will($this->returnValue($user)); @@ -1317,10 +1354,10 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('delete') ->will($this->returnValue(false)); - $this->container['UserSession'] + $this->userSession ->method('getUser') ->will($this->returnValue($user)); - $this->container['UserManager'] + $this->userManager ->method('get') ->with('UserToDelete') ->will($this->returnValue($toDeleteUser)); @@ -1332,7 +1369,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('isUserAccessible') ->with($user, $toDeleteUser) ->will($this->returnValue(true)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -1346,12 +1383,12 @@ class UsersControllerTest extends \Test\TestCase { ], Http::STATUS_FORBIDDEN ); - $response = $this->container['UsersController']->destroy('UserToDelete'); + $response = $controller->destroy('UserToDelete'); $this->assertEquals($expectedResponse, $response); } public function testDestroyNotAccessibleToSubAdmin() { - $this->container['IsAdmin'] = false; + $controller = $this->getController(false); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -1359,16 +1396,16 @@ class UsersControllerTest extends \Test\TestCase { ->expects($this->once()) ->method('getUID') ->will($this->returnValue('myself')); - $this->container['UserSession'] + $this->userSession ->method('getUser') ->will($this->returnValue($user)); $toDeleteUser = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); - $this->container['UserSession'] + $this->userSession ->method('getUser') ->will($this->returnValue($user)); - $this->container['UserManager'] + $this->userManager ->method('get') ->with('UserToDelete') ->will($this->returnValue($toDeleteUser)); @@ -1380,7 +1417,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('isUserAccessible') ->with($user, $toDeleteUser) ->will($this->returnValue(false)); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -1394,7 +1431,7 @@ class UsersControllerTest extends \Test\TestCase { ], Http::STATUS_FORBIDDEN ); - $response = $this->container['UsersController']->destroy('UserToDelete'); + $response = $controller->destroy('UserToDelete'); $this->assertEquals($expectedResponse, $response); } @@ -1402,14 +1439,14 @@ class UsersControllerTest extends \Test\TestCase { * test if an invalid mail result in a failure response */ public function testCreateUnsuccessfulWithInvalidEmailAdmin() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $expectedResponse = new DataResponse([ 'message' => 'Invalid mail address', ], Http::STATUS_UNPROCESSABLE_ENTITY ); - $response = $this->container['UsersController']->create('foo', 'password', [], 'invalidMailAdress'); + $response = $controller->create('foo', 'password', [], 'invalidMailAdress'); $this->assertEquals($expectedResponse, $response); } @@ -1417,7 +1454,7 @@ class UsersControllerTest extends \Test\TestCase { * test if a valid mail result in a successful mail send */ public function testCreateSuccessfulWithValidEmailAdmin() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $message = $this->getMockBuilder('\OC\Mail\Message') ->disableOriginalConstructor()->getMock(); $message @@ -1459,16 +1496,16 @@ class UsersControllerTest extends \Test\TestCase { ->method('setFrom') ->with(['no-reply@owncloud.com' => null]); - $this->container['Mailer'] + $this->mailer ->expects($this->at(0)) ->method('validateMailAddress') ->with('validMail@Adre.ss') ->will($this->returnValue(true)); - $this->container['Mailer'] + $this->mailer ->expects($this->at(1)) ->method('createMessage') ->will($this->returnValue($message)); - $this->container['Mailer'] + $this->mailer ->expects($this->at(2)) ->method('send') ->with($message); @@ -1489,7 +1526,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('getBackendClassName') ->will($this->returnValue('bar')); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('createUser') ->will($this->onConsecutiveCalls($user)); @@ -1500,12 +1537,12 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($user) ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); - $response = $this->container['UsersController']->create('foo', 'password', [], 'validMail@Adre.ss'); + $response = $controller->create('foo', 'password', [], 'validMail@Adre.ss'); $this->assertEquals(Http::STATUS_CREATED, $response->getStatus()); } @@ -1550,7 +1587,7 @@ class UsersControllerTest extends \Test\TestCase { } public function testRestorePossibleWithoutEncryption() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); list($user, $expectedResult) = $this->mockUser(); @@ -1561,28 +1598,26 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($user) ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); - $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]); + $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]); $this->assertEquals($expectedResult, $result); } public function testRestorePossibleWithAdminAndUserRestore() { - $this->container['IsAdmin'] = true; - list($user, $expectedResult) = $this->mockUser(); - $this->container['OCP\\App\\IAppManager'] + $this->appManager ->expects($this->once()) ->method('isEnabledForUser') ->with( $this->equalTo('encryption') ) ->will($this->returnValue(true)); - $this->container['Config'] + $this->config ->expects($this->once()) ->method('getAppValue') ->with( @@ -1592,7 +1627,7 @@ class UsersControllerTest extends \Test\TestCase { ) ->will($this->returnValue('1')); - $this->container['Config'] + $this->config ->expects($this->at(1)) ->method('getUserValue') ->with( @@ -1610,21 +1645,20 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($user) ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); - $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]); + $controller = $this->getController(true); + $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]); $this->assertEquals($expectedResult, $result); } public function testRestoreNotPossibleWithoutAdminRestore() { - $this->container['IsAdmin'] = true; - list($user, $expectedResult) = $this->mockUser(); - $this->container['OCP\\App\\IAppManager'] + $this->appManager ->method('isEnabledForUser') ->with( $this->equalTo('encryption') @@ -1640,28 +1674,27 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($user) ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); - $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]); + $controller = $this->getController(true); + $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]); $this->assertEquals($expectedResult, $result); } public function testRestoreNotPossibleWithoutUserRestore() { - $this->container['IsAdmin'] = true; - list($user, $expectedResult) = $this->mockUser(); - $this->container['OCP\\App\\IAppManager'] + $this->appManager ->expects($this->once()) ->method('isEnabledForUser') ->with( $this->equalTo('encryption') ) ->will($this->returnValue(true)); - $this->container['Config'] + $this->config ->expects($this->once()) ->method('getAppValue') ->with( @@ -1671,7 +1704,7 @@ class UsersControllerTest extends \Test\TestCase { ) ->will($this->returnValue('1')); - $this->container['Config'] + $this->config ->expects($this->at(1)) ->method('getUserValue') ->with( @@ -1691,17 +1724,18 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($user) ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); - $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]); + $controller = $this->getController(true); + $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]); $this->assertEquals($expectedResult, $result); } public function testNoAvatar() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); list($user, $expectedResult) = $this->mockUser(); @@ -1712,17 +1746,17 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->with($user) ->will($this->returnValue([])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); - $this->container['OCP\\IAvatarManager'] + $this->avatarManager ->method('getAvatar') ->will($this->throwException(new \OCP\Files\NotFoundException())); $expectedResult['isAvatarAvailable'] = false; - $result = self::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]); + $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]); $this->assertEquals($expectedResult, $result); } @@ -1748,7 +1782,7 @@ class UsersControllerTest extends \Test\TestCase { * @param bool $expectsDelete */ public function testSetEmailAddress($mailAddress, $isValid, $expectsUpdate, $canChangeDisplayName, $responseCode) { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); @@ -1767,11 +1801,11 @@ class UsersControllerTest extends \Test\TestCase { $this->equalTo($mailAddress) ); - $this->container['UserSession'] + $this->userSession ->expects($this->atLeastOnce()) ->method('getUser') ->will($this->returnValue($user)); - $this->container['Mailer'] + $this->mailer ->expects($this->any()) ->method('validateMailAddress') ->with($mailAddress) @@ -1782,22 +1816,22 @@ class UsersControllerTest extends \Test\TestCase { ->method('canChangeDisplayName') ->willReturn(true); - $this->container['UserManager'] + $this->userManager ->expects($this->atLeastOnce()) ->method('get') ->with('foo') ->will($this->returnValue($user)); } - $response = $this->container['UsersController']->setMailAddress($user->getUID(), $mailAddress); + $response = $controller->setMailAddress($user->getUID(), $mailAddress); $this->assertSame($responseCode, $response->getStatus()); } public function testStatsAdmin() { - $this->container['IsAdmin'] = true; + $controller = $this->getController(true); - $this->container['UserManager'] + $this->userManager ->expects($this->at(0)) ->method('countUsers') ->will($this->returnValue([128, 44])); @@ -1807,7 +1841,7 @@ class UsersControllerTest extends \Test\TestCase { 'totalUsers' => 172 ] ); - $response = $this->container['UsersController']->stats(); + $response = $controller->stats(); $this->assertEquals($expectedResponse, $response); } @@ -1816,12 +1850,12 @@ class UsersControllerTest extends \Test\TestCase { * when a user appears in several groups. */ public function testStatsSubAdmin() { - $this->container['IsAdmin'] = false; + $controller = $this->getController(false); $user = $this->getMockBuilder('\OC\User\User') ->disableOriginalConstructor()->getMock(); - $this->container['UserSession'] + $this->userSession ->expects($this->once()) ->method('getUser') ->will($this->returnValue($user)); @@ -1848,7 +1882,7 @@ class UsersControllerTest extends \Test\TestCase { ->method('getSubAdminsGroups') ->will($this->returnValue([$group1, $group2])); - $this->container['GroupManager'] + $this->groupManager ->expects($this->any()) ->method('getSubAdmin') ->will($this->returnValue($subadmin)); @@ -1859,7 +1893,7 @@ class UsersControllerTest extends \Test\TestCase { ] ); - $response = $this->container['UsersController']->stats(); + $response = $controller->stats(); $this->assertEquals($expectedResponse, $response); } @@ -1867,7 +1901,7 @@ class UsersControllerTest extends \Test\TestCase { $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('userName'); - $this->container['UserSession'] + $this->userSession ->expects($this->once()) ->method('getUser') ->willReturn($user); @@ -1880,7 +1914,8 @@ class UsersControllerTest extends \Test\TestCase { ], ] ); - $response = $this->container['UsersController']->setDisplayName(null, 'displayName'); + $controller = $this->getController(true); + $response = $controller->setDisplayName(null, 'displayName'); $this->assertEquals($expectedResponse, $response); } @@ -1924,13 +1959,19 @@ class UsersControllerTest extends \Test\TestCase { /** * @dataProvider dataSetDisplayName + * + * @param IUser|\PHPUnit_Framework_MockObject_MockObject $currentUser + * @param IUser|\PHPUnit_Framework_MockObject_MockObject $editUser + * @param bool $isAdmin + * @param bool $isSubAdmin + * @param bool $valid */ public function testSetDisplayName($currentUser, $editUser, $isAdmin, $isSubAdmin, $valid) { - $this->container['UserSession'] + $this->userSession ->expects($this->once()) ->method('getUser') ->willReturn($currentUser); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('get') ->with($editUser->getUID()) @@ -1944,10 +1985,10 @@ class UsersControllerTest extends \Test\TestCase { ->with($currentUser, $editUser) ->willReturn($isSubAdmin); - $this->container['GroupManager'] + $this->groupManager ->method('getSubAdmin') ->willReturn($subadmin); - $this->container['GroupManager'] + $this->groupManager ->method('isAdmin') ->with($currentUser->getUID()) ->willReturn($isAdmin); @@ -1979,11 +2020,13 @@ class UsersControllerTest extends \Test\TestCase { ); } - $response = $this->container['UsersController']->setDisplayName($editUser->getUID(), 'newDisplayName'); + $controller = $this->getController(true); + $response = $controller->setDisplayName($editUser->getUID(), 'newDisplayName'); $this->assertEquals($expectedResponse, $response); } public function testSetDisplayNameFails() { + /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */ $user = $this->createMock(IUser::class); $user->method('canChangeDisplayname')->willReturn(true); $user->method('getUID')->willReturn('user'); @@ -1993,11 +2036,11 @@ class UsersControllerTest extends \Test\TestCase { ->willReturn(false); $user->method('getDisplayName')->willReturn('oldDisplayName'); - $this->container['UserSession'] + $this->userSession ->expects($this->once()) ->method('getUser') ->willReturn($user); - $this->container['UserManager'] + $this->userManager ->expects($this->once()) ->method('get') ->with($user->getUID()) @@ -2011,10 +2054,10 @@ class UsersControllerTest extends \Test\TestCase { ->with($user, $user) ->willReturn(false); - $this->container['GroupManager'] + $this->groupManager ->method('getSubAdmin') ->willReturn($subadmin); - $this->container['GroupManager'] + $this->groupManager ->expects($this->once()) ->method('isAdmin') ->with($user->getUID()) @@ -2029,7 +2072,8 @@ class UsersControllerTest extends \Test\TestCase { ], ] ); - $response = $this->container['UsersController']->setDisplayName($user->getUID(), 'newDisplayName'); + $controller = $this->getController(true); + $response = $controller->setDisplayName($user->getUID(), 'newDisplayName'); $this->assertEquals($expectedResponse, $response); } } diff --git a/tests/Settings/Middleware/SubadminMiddlewareTest.php b/tests/Settings/Middleware/SubadminMiddlewareTest.php index 652f8b2d151..b2724047750 100644 --- a/tests/Settings/Middleware/SubadminMiddlewareTest.php +++ b/tests/Settings/Middleware/SubadminMiddlewareTest.php @@ -33,6 +33,7 @@ class SubadminMiddlewareTest extends \Test\TestCase { private $controller; protected function setUp() { + parent::setUp(); $this->reflector = $this->getMockBuilder('\OC\AppFramework\Utility\ControllerMethodReflector') ->disableOriginalConstructor()->getMock(); $this->controller = $this->getMockBuilder('\OCP\AppFramework\Controller') |