Fix tests

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2016-10-06 14:05:52 +02:00
parent bc8186e4c5
commit ccc29a3da2
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
8 changed files with 368 additions and 286 deletions

View File

@ -96,6 +96,7 @@ class UsersController extends Controller {
* @param string $fromMailAddress * @param string $fromMailAddress
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param IAppManager $appManager * @param IAppManager $appManager
* @param IAvatarManager $avatarManager
*/ */
public function __construct($appName, public function __construct($appName,
IRequest $request, IRequest $request,

View File

@ -55,6 +55,7 @@ class EncryptionControllerTest extends TestCase {
private $encryptionController; private $encryptionController;
public function setUp() { public function setUp() {
parent::setUp();
$this->request = $this->getMockBuilder('\\OCP\\IRequest') $this->request = $this->getMockBuilder('\\OCP\\IRequest')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->l10n = $this->getMockBuilder('\\OCP\\IL10N') $this->l10n = $this->getMockBuilder('\\OCP\\IL10N')

View File

@ -10,42 +10,47 @@
namespace Tests\Settings\Controller; namespace Tests\Settings\Controller;
use OC\Group\Group;
use OC\Group\MetaData; use OC\Group\MetaData;
use \OC\Settings\Application;
use OC\Settings\Controller\GroupsController; use OC\Settings\Controller\GroupsController;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserSession;
/** /**
* @package Tests\Settings\Controller * @package Tests\Settings\Controller
*/ */
class GroupsControllerTest extends \Test\TestCase { class GroupsControllerTest extends \Test\TestCase {
/** @var \OCP\AppFramework\IAppContainer */ /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
private $container; private $groupManager;
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
private $userSession;
/** @var GroupsController */ /** @var GroupsController */
private $groupsController; private $groupsController;
protected function setUp() { protected function setUp() {
$app = new Application(); parent::setUp();
$this->container = $app->getContainer();
$this->container['AppName'] = 'settings'; $this->groupManager = $this->createMock(IGroupManager::class);
$this->container['GroupManager'] = $this->getMockBuilder('\OCP\IGroupManager') $this->userSession = $this->createMock(IUserSession::class);
->disableOriginalConstructor()->getMock(); $l = $this->createMock(IL10N::class);
$this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session') $l->method('t')
->disableOriginalConstructor()->getMock(); ->will($this->returnCallback(function($text, $parameters = []) {
$this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N') return vsprintf($text, $parameters);
->disableOriginalConstructor()->getMock(); }));
$this->container['IsAdmin'] = true; $this->groupsController = new GroupsController(
$this->container['L10N'] 'settings',
->expects($this->any()) $this->createMock(IRequest::class),
->method('t') $this->groupManager,
->will($this->returnCallback(function($text, $parameters = array()) { $this->userSession,
return vsprintf($text, $parameters); true,
})); $l
$this->groupsController = $this->container['GroupsController']; );
} }
@ -95,7 +100,7 @@ class GroupsControllerTest extends \Test\TestCase {
$user = $this->getMockBuilder('\OC\User\User') $user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->container['UserSession'] $this->userSession
->expects($this->once()) ->expects($this->once())
->method('getUser') ->method('getUser')
->will($this->returnValue($user)); ->will($this->returnValue($user));
@ -103,8 +108,7 @@ class GroupsControllerTest extends \Test\TestCase {
->expects($this->once()) ->expects($this->once())
->method('getUID') ->method('getUID')
->will($this->returnValue('MyAdminUser')); ->will($this->returnValue('MyAdminUser'));
$this->container['GroupManager'] $this->groupManager->method('search')
->method('search')
->will($this->returnValue($groups)); ->will($this->returnValue($groups));
$expectedResponse = new DataResponse( $expectedResponse = new DataResponse(
@ -188,7 +192,7 @@ class GroupsControllerTest extends \Test\TestCase {
$user = $this->getMockBuilder('\OC\User\User') $user = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->container['UserSession'] $this->userSession
->expects($this->once()) ->expects($this->once())
->method('getUser') ->method('getUser')
->will($this->returnValue($user)); ->will($this->returnValue($user));
@ -196,7 +200,7 @@ class GroupsControllerTest extends \Test\TestCase {
->expects($this->once()) ->expects($this->once())
->method('getUID') ->method('getUID')
->will($this->returnValue('MyAdminUser')); ->will($this->returnValue('MyAdminUser'));
$this->container['GroupManager'] $this->groupManager
->method('search') ->method('search')
->will($this->returnValue($groups)); ->will($this->returnValue($groups));
@ -236,7 +240,7 @@ class GroupsControllerTest extends \Test\TestCase {
} }
public function testCreateWithExistingGroup() { public function testCreateWithExistingGroup() {
$this->container['GroupManager'] $this->groupManager
->expects($this->once()) ->expects($this->once())
->method('groupExists') ->method('groupExists')
->with('ExistingGroup') ->with('ExistingGroup')
@ -253,12 +257,12 @@ class GroupsControllerTest extends \Test\TestCase {
} }
public function testCreateSuccessful() { public function testCreateSuccessful() {
$this->container['GroupManager'] $this->groupManager
->expects($this->once()) ->expects($this->once())
->method('groupExists') ->method('groupExists')
->with('NewGroup') ->with('NewGroup')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$this->container['GroupManager'] $this->groupManager
->expects($this->once()) ->expects($this->once())
->method('createGroup') ->method('createGroup')
->with('NewGroup') ->with('NewGroup')
@ -275,12 +279,12 @@ class GroupsControllerTest extends \Test\TestCase {
} }
public function testCreateUnsuccessful() { public function testCreateUnsuccessful() {
$this->container['GroupManager'] $this->groupManager
->expects($this->once()) ->expects($this->once())
->method('groupExists') ->method('groupExists')
->with('NewGroup') ->with('NewGroup')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$this->container['GroupManager'] $this->groupManager
->expects($this->once()) ->expects($this->once())
->method('createGroup') ->method('createGroup')
->with('NewGroup') ->with('NewGroup')
@ -300,7 +304,7 @@ class GroupsControllerTest extends \Test\TestCase {
public function testDestroySuccessful() { public function testDestroySuccessful() {
$group = $this->getMockBuilder('\OC\Group\Group') $group = $this->getMockBuilder('\OC\Group\Group')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->container['GroupManager'] $this->groupManager
->expects($this->once()) ->expects($this->once())
->method('get') ->method('get')
->with('ExistingGroup') ->with('ExistingGroup')
@ -322,7 +326,7 @@ class GroupsControllerTest extends \Test\TestCase {
} }
public function testDestroyUnsuccessful() { public function testDestroyUnsuccessful() {
$this->container['GroupManager'] $this->groupManager
->expects($this->once()) ->expects($this->once())
->method('get') ->method('get')
->with('ExistingGroup') ->with('ExistingGroup')

View File

@ -13,25 +13,36 @@ namespace Tests\Settings\Controller;
use \OC\Settings\Application; use \OC\Settings\Application;
use OC\Settings\Controller\LogSettingsController; use OC\Settings\Controller\LogSettingsController;
use OCP\AppFramework\Http\StreamResponse; use OCP\AppFramework\Http\StreamResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
/** /**
* @package Tests\Settings\Controller * @package Tests\Settings\Controller
*/ */
class LogSettingsControllerTest extends \Test\TestCase { class LogSettingsControllerTest extends \Test\TestCase {
/** @var \OCP\AppFramework\IAppContainer */ /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
private $container; private $config;
/** @var LogSettingsController */ /** @var LogSettingsController */
private $logSettingsController; private $logSettingsController;
protected function setUp() { protected function setUp() {
$app = new Application(); parent::setUp();
$this->container = $app->getContainer();
$this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') $this->config = $this->createMock(IConfig::class);
->disableOriginalConstructor()->getMock(); $l = $this->createMock(IL10N::class);
$this->container['AppName'] = 'settings'; $l->method('t')
$this->logSettingsController = $this->container['LogSettingsController']; ->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) { public function testSetLogLevel($level, $inRange) {
if ($inRange) { if ($inRange) {
$this->container['Config'] $this->config->expects($this->once())
->expects($this->once())
->method('setSystemValue') ->method('setSystemValue')
->with('loglevel', $level); ->with('loglevel', $level);
} }

View File

@ -10,39 +10,54 @@
namespace Tests\Settings\Controller; 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 * @package Tests\Settings\Controller
*/ */
class MailSettingsControllerTest extends \Test\TestCase { 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() { protected function setUp() {
parent::setUp(); parent::setUp();
$app = new Application(); $this->l = $this->createMock(IL10N::class);
$this->container = $app->getContainer(); $this->config = $this->createMock(IConfig::class);
$this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') $this->userSession = $this->createMock(IUserSession::class);
->disableOriginalConstructor()->getMock(); $this->mailer = $this->createMock(IMailer::class);
$this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N') // $this->mailer = $this->getMockBuilder(IMailer::class)
->disableOriginalConstructor()->getMock(); // ->setMethods(['send'])
$this->container['AppName'] = 'settings'; // ->getMock();
$this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session') $this->mailController = new MailSettingsController(
->disableOriginalConstructor()->getMock(); 'settings',
$this->container['MailMessage'] = $this->getMockBuilder('\OCP\Mail\IMessage') $this->createMock(IRequest::class),
->disableOriginalConstructor()->getMock(); $this->l,
$this->container['Mailer'] = $this->getMockBuilder('\OC\Mail\Mailer') $this->config,
->setMethods(['send']) $this->userSession,
->disableOriginalConstructor()->getMock(); $this->mailer,
$this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults') 'no-reply@owncloud.com'
->disableOriginalConstructor()->getMock(); );
$this->container['DefaultMailAddress'] = 'no-reply@owncloud.com';
} }
public function testSetMailSettings() { public function testSetMailSettings() {
$this->container['L10N'] $this->l
->expects($this->exactly(2)) ->expects($this->exactly(2))
->method('t') ->method('t')
->will($this->returnValue('Saved')); ->will($this->returnValue('Saved'));
@ -51,7 +66,7 @@ class MailSettingsControllerTest extends \Test\TestCase {
* FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1 * FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1
*/ */
/* /*
$this->container['Config'] $this->config
->expects($this->exactly(15)) ->expects($this->exactly(15))
->method('setSystemValue') ->method('setSystemValue')
->withConsecutive( ->withConsecutive(
@ -74,8 +89,7 @@ class MailSettingsControllerTest extends \Test\TestCase {
*/ */
/** @var \PHPUnit_Framework_MockObject_MockObject $config */ /** @var \PHPUnit_Framework_MockObject_MockObject $config */
$config = $this->container['Config']; $this->config->expects($this->exactly(2))
$config->expects($this->exactly(2))
->method('setSystemValues'); ->method('setSystemValues');
/** /**
* FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1 * FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1
@ -106,7 +120,7 @@ class MailSettingsControllerTest extends \Test\TestCase {
*/ */
// With authentication // With authentication
$response = $this->container['MailSettingsController']->setMailSettings( $response = $this->mailController->setMailSettings(
'owncloud.com', 'owncloud.com',
'demo@owncloud.com', 'demo@owncloud.com',
'smtp', 'smtp',
@ -120,7 +134,7 @@ class MailSettingsControllerTest extends \Test\TestCase {
$this->assertSame($expectedResponse, $response); $this->assertSame($expectedResponse, $response);
// Without authentication (testing the deletion of the stored password) // Without authentication (testing the deletion of the stored password)
$response = $this->container['MailSettingsController']->setMailSettings( $response = $this->mailController->setMailSettings(
'owncloud.com', 'owncloud.com',
'demo@owncloud.com', 'demo@owncloud.com',
'smtp', 'smtp',
@ -136,12 +150,12 @@ class MailSettingsControllerTest extends \Test\TestCase {
} }
public function testStoreCredentials() { public function testStoreCredentials() {
$this->container['L10N'] $this->l
->expects($this->once()) ->expects($this->once())
->method('t') ->method('t')
->will($this->returnValue('Saved')); ->will($this->returnValue('Saved'));
$this->container['Config'] $this->config
->expects($this->once()) ->expects($this->once())
->method('setSystemValues') ->method('setSystemValues')
->with([ ->with([
@ -149,7 +163,7 @@ class MailSettingsControllerTest extends \Test\TestCase {
'mail_smtppassword' => 'PasswordToStore', 'mail_smtppassword' => 'PasswordToStore',
]); ]);
$response = $this->container['MailSettingsController']->storeCredentials('UsernameToStore', 'PasswordToStore'); $response = $this->mailController->storeCredentials('UsernameToStore', 'PasswordToStore');
$expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success'); $expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success');
$this->assertSame($expectedResponse, $response); $this->assertSame($expectedResponse, $response);
@ -166,7 +180,7 @@ class MailSettingsControllerTest extends \Test\TestCase {
->method('getDisplayName') ->method('getDisplayName')
->will($this->returnValue('Werner Brösel')); ->will($this->returnValue('Werner Brösel'));
$this->container['L10N'] $this->l
->expects($this->any()) ->expects($this->any())
->method('t') ->method('t')
->will( ->will(
@ -182,22 +196,25 @@ class MailSettingsControllerTest extends \Test\TestCase {
'If you received this email, the settings seem to be correct.') 'If you received this email, the settings seem to be correct.')
) )
)); ));
$this->container['UserSession'] $this->userSession
->expects($this->any()) ->expects($this->any())
->method('getUser') ->method('getUser')
->will($this->returnValue($user)); ->will($this->returnValue($user));
// Ensure that it fails when no mail address has been specified // 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'); $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); $this->assertSame($expectedResponse, $response);
// If no exception is thrown it should work // If no exception is thrown it should work
$this->container['Config'] $this->config
->expects($this->any()) ->expects($this->any())
->method('getUserValue') ->method('getUserValue')
->will($this->returnValue('mail@example.invalid')); ->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'); $expectedResponse = array('data' => array('message' =>'Email sent'), 'status' => 'success');
$this->assertSame($expectedResponse, $response); $this->assertSame($expectedResponse, $response);
} }

View File

@ -11,33 +11,37 @@ namespace Tests\Settings\Controller;
use \OC\Settings\Application; use \OC\Settings\Application;
use OC\Settings\Controller\SecuritySettingsController; use OC\Settings\Controller\SecuritySettingsController;
use OCP\IConfig;
use OCP\IRequest;
/** /**
* @package Tests\Settings\Controller * @package Tests\Settings\Controller
*/ */
class SecuritySettingsControllerTest extends \PHPUnit_Framework_TestCase { class SecuritySettingsControllerTest extends \Test\TestCase {
/** @var \OCP\AppFramework\IAppContainer */ /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
private $container; private $config;
/** @var SecuritySettingsController */ /** @var SecuritySettingsController */
private $securitySettingsController; private $securitySettingsController;
protected function setUp() { protected function setUp() {
$app = new Application(); parent::setUp();
$this->container = $app->getContainer();
$this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') $this->config = $this->createMock(IConfig::class);
->disableOriginalConstructor()->getMock(); $this->securitySettingsController = new SecuritySettingsController(
$this->container['AppName'] = 'settings'; 'settings',
$this->securitySettingsController = $this->container['SecuritySettingsController']; $this->createMock(IRequest::class),
$this->config
);
} }
public function testTrustedDomainsWithExistingValues() { public function testTrustedDomainsWithExistingValues() {
$this->container['Config'] $this->config
->expects($this->once()) ->expects($this->once())
->method('setSystemValue') ->method('setSystemValue')
->with('trusted_domains', array('owncloud.org', 'owncloud.com', 'newdomain.com')); ->with('trusted_domains', array('owncloud.org', 'owncloud.com', 'newdomain.com'));
$this->container['Config'] $this->config
->expects($this->once()) ->expects($this->once())
->method('getSystemValue') ->method('getSystemValue')
->with('trusted_domains') ->with('trusted_domains')
@ -50,11 +54,11 @@ class SecuritySettingsControllerTest extends \PHPUnit_Framework_TestCase {
} }
public function testTrustedDomainsEmpty() { public function testTrustedDomainsEmpty() {
$this->container['Config'] $this->config
->expects($this->once()) ->expects($this->once())
->method('setSystemValue') ->method('setSystemValue')
->with('trusted_domains', array('newdomain.com')); ->with('trusted_domains', array('newdomain.com'));
$this->container['Config'] $this->config
->expects($this->once()) ->expects($this->once())
->method('getSystemValue') ->method('getSystemValue')
->with($this->equalTo('trusted_domains'), $this->equalTo([])) ->with($this->equalTo('trusted_domains'), $this->equalTo([]))

File diff suppressed because it is too large Load Diff

View File

@ -33,6 +33,7 @@ class SubadminMiddlewareTest extends \Test\TestCase {
private $controller; private $controller;
protected function setUp() { protected function setUp() {
parent::setUp();
$this->reflector = $this->getMockBuilder('\OC\AppFramework\Utility\ControllerMethodReflector') $this->reflector = $this->getMockBuilder('\OC\AppFramework\Utility\ControllerMethodReflector')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->controller = $this->getMockBuilder('\OCP\AppFramework\Controller') $this->controller = $this->getMockBuilder('\OCP\AppFramework\Controller')