Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions tests/lib/AppConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function setUp() {
$sql->delete('appconfig');
$sql->execute();

$this->registerAppConfig(new \OC\AppConfig($this->connection));
$this->overwriteService('AppConfig', new \OC\AppConfig($this->connection));

$sql = $this->connection->getQueryBuilder();
$sql->insert('appconfig')
Expand Down Expand Up @@ -130,21 +130,10 @@ public function tearDown() {
$sql->execute();
}

$this->registerAppConfig(new \OC\AppConfig(\OC::$server->getDatabaseConnection()));
$this->restoreService('AppConfig');
parent::tearDown();
}

/**
* Register an app config object for testing purposes.
*
* @param \OCP\IAppConfig $appConfig
*/
protected function registerAppConfig($appConfig) {
\OC::$server->registerService('AppConfig', function () use ($appConfig) {
return $appConfig;
});
}

public function testGetApps() {
$config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());

Expand Down
22 changes: 10 additions & 12 deletions tests/lib/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,24 +489,22 @@ private function setupAppConfigMock() {
* @param IAppConfig $appConfig app config mock
*/
private function registerAppConfig(IAppConfig $appConfig) {
\OC::$server->registerService('AppConfig', function ($c) use ($appConfig) {
return $appConfig;
});
\OC::$server->registerService('AppManager', function (\OC\Server $c) use ($appConfig) {
return new \OC\App\AppManager($c->getUserSession(), $appConfig, $c->getGroupManager(), $c->getMemCacheFactory(), $c->getEventDispatcher());
});
$this->overwriteService('AppConfig', $appConfig);
$this->overwriteService('AppManager', new \OC\App\AppManager(
\OC::$server->getUserSession(),
$appConfig,
\OC::$server->getGroupManager(),
\OC::$server->getMemCacheFactory(),
\OC::$server->getEventDispatcher()
));
}

/**
* Restore the original app config service.
*/
private function restoreAppConfig() {
\OC::$server->registerService('AppConfig', function (\OC\Server $c) {
return new \OC\AppConfig($c->getDatabaseConnection());
});
\OC::$server->registerService('AppManager', function (\OC\Server $c) {
return new \OC\App\AppManager($c->getUserSession(), $c->getAppConfig(), $c->getGroupManager(), $c->getMemCacheFactory(), $c->getEventDispatcher());
});
$this->restoreService('AppConfig');
$this->restoreService('AppManager');

// Remove the cache of the mocked apps list with a forceRefresh
\OC_App::getEnabledApps();
Expand Down
20 changes: 6 additions & 14 deletions tests/lib/Share/ShareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ protected function tearDown() {
parent::tearDown();
}

protected function setHttpHelper($httpHelper) {
\OC::$server->registerService('HTTPHelper', function () use ($httpHelper) {
return $httpHelper;
});
}

public function testShareInvalidShareType() {
$message = 'Share type foobar is not valid for test.txt';
try {
Expand Down Expand Up @@ -1046,11 +1040,10 @@ public function dataRemoteShareUrlCalls() {
* @param string $urlHost
*/
public function testRemoteShareUrlCalls($shareWith, $urlHost) {
$oldHttpHelper = \OC::$server->query('HTTPHelper');
$httpHelperMock = $this->getMockBuilder('OC\HttpHelper')
$httpHelperMock = $this->getMockBuilder('OC\HTTPHelper')
->disableOriginalConstructor()
->getMock();
$this->setHttpHelper($httpHelperMock);
$this->overwriteService('HTTPHelper', $httpHelperMock);

$httpHelperMock->expects($this->at(0))
->method('post')
Expand All @@ -1075,7 +1068,7 @@ public function testRemoteShareUrlCalls($shareWith, $urlHost) {
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);

\OCP\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $shareWith);
$this->setHttpHelper($oldHttpHelper);
$this->restoreService('HTTPHelper');
}

/**
Expand Down Expand Up @@ -1473,11 +1466,10 @@ public function testSetPasswordShareOtherUser() {
* Make sure that a user cannot have multiple identical shares to remote users
*/
public function testOnlyOneRemoteShare() {
$oldHttpHelper = \OC::$server->query('HTTPHelper');
$httpHelperMock = $this->getMockBuilder('OC\HttpHelper')
$httpHelperMock = $this->getMockBuilder('OC\HTTPHelper')
->disableOriginalConstructor()
->getMock();
$this->setHttpHelper($httpHelperMock);
$this->overwriteService('HTTPHelper', $httpHelperMock);

$httpHelperMock->expects($this->at(0))
->method('post')
Expand All @@ -1502,7 +1494,7 @@ public function testOnlyOneRemoteShare() {
->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);

\OCP\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost');
$this->setHttpHelper($oldHttpHelper);
$this->restoreService('HTTPHelper');
}

/**
Expand Down
29 changes: 26 additions & 3 deletions tests/lib/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ public function restoreService($name) {
return false;
}

public function restoreAllServices() {
if (!empty($this->services)) {
if (!empty($this->services)) {
foreach ($this->services as $name => $service) {
$this->restoreService($name);
}
}
}
}

protected function getTestTraits() {
$traits = [];
$class = $this;
Expand Down Expand Up @@ -113,9 +123,7 @@ protected function setUp() {

// overwrite the command bus with one we can run ourselves
$this->commandBus = new QueueBus();
\OC::$server->registerService('AsyncCommandBus', function () {
return $this->commandBus;
});
$this->overwriteService('AsyncCommandBus', $this->commandBus);

$traits = $this->getTestTraits();
foreach ($traits as $trait) {
Expand All @@ -126,7 +134,22 @@ protected function setUp() {
}
}

protected function onNotSuccessfulTest($e) {
$this->restoreAllServices();

// restore database connection
if (!$this->IsDatabaseAccessAllowed()) {
\OC::$server->registerService('DatabaseConnection', function () {
return self::$realDatabase;
});
}

parent::onNotSuccessfulTest($e);
}

protected function tearDown() {
$this->restoreAllServices();

// restore database connection
if (!$this->IsDatabaseAccessAllowed()) {
\OC::$server->registerService('DatabaseConnection', function () {
Expand Down
11 changes: 2 additions & 9 deletions tests/lib/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,17 @@ public function formatDateWithTZFromSessionData() {
function testFormatDateWithTZFromSession($offset, $expected, $expectedTimeZone) {
date_default_timezone_set("UTC");

$oldDateTimeFormatter = \OC::$server->query('DateTimeFormatter');
\OC::$server->getSession()->set('timezone', $offset);

$selectedTimeZone = \OC::$server->getDateTimeZone()->getTimeZone(1350129205);
$this->assertEquals($expectedTimeZone, $selectedTimeZone->getName());
$newDateTimeFormatter = new \OC\DateTimeFormatter($selectedTimeZone, new \OC_L10N('lib', 'en'));
$this->setDateFormatter($newDateTimeFormatter);
$this->overwriteService('DateTimeFormatter', $newDateTimeFormatter);

$result = OC_Util::formatDate(1350129205, false);
$this->assertEquals($expected, $result);

$this->setDateFormatter($oldDateTimeFormatter);
}

protected function setDateFormatter($formatter) {
\OC::$server->registerService('DateTimeFormatter', function ($c) use ($formatter) {
return $formatter;
});
$this->restoreService('DateTimeFormatter');
}

function testSanitizeHTML() {
Expand Down