Skip to content

Commit

Permalink
Added unit tests for cache of enabled apps
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Petry committed Sep 4, 2014
1 parent 28e1480 commit f268eee
Showing 1 changed file with 74 additions and 10 deletions.
84 changes: 74 additions & 10 deletions tests/lib/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,7 @@ public function testEnabledApps($user, $expectedApps, $forceAll) {

\OC_User::setUserId($user);

$appConfig = $this->getMock(
'\OC\AppConfig',
array('getValues'),
array(\OC_DB::getConnection()),
'',
false
);

$appConfig->expects($this->once())
$this->setupAppConfigMock()->expects($this->once())
->method('getValues')
->will($this->returnValue(
array(
Expand All @@ -358,7 +350,6 @@ public function testEnabledApps($user, $expectedApps, $forceAll) {
)
)
);
$this->registerAppConfig($appConfig);

$apps = \OC_App::getEnabledApps(true, $forceAll);
$this->assertEquals($expectedApps, $apps);
Expand All @@ -377,6 +368,79 @@ public function testEnabledApps($user, $expectedApps, $forceAll) {
$group2->delete();
}

/**
* Test isEnabledApps() with cache, not re-reading the list of
* enabled apps more than once when a user is set.
*/
public function testEnabledAppsCache() {
$userManager = \OC::$server->getUserManager();
$user1 = $userManager->createUser(self::TEST_USER1, self::TEST_USER1);

\OC_User::setUserId(self::TEST_USER1);

$this->setupAppConfigMock()->expects($this->once())
->method('getValues')
->will($this->returnValue(
array(
'app3' => 'yes',
'app2' => 'no',
)
)
);

$apps = \OC_App::getEnabledApps(true);
$this->assertEquals(array('files', 'app3'), $apps);

// mock should not be called again here
$apps = \OC_App::getEnabledApps(false);
$this->assertEquals(array('files', 'app3'), $apps);

$this->restoreAppConfig();
\OC_User::setUserId(null);

$user1->delete();
// clear user cache...
$userManager->delete(self::TEST_USER1);
}

/**
* Tests that the apps list is re-requested (not cached) when
* no user is set.
*/
public function testEnabledAppsNoCache() {
$this->setupAppConfigMock()->expects($this->exactly(2))
->method('getValues')
->will($this->returnValue(
array(
'app3' => 'yes',
'app2' => 'no',
)
)
);

$apps = \OC_App::getEnabledApps(true);
$this->assertEquals(array('files', 'app3'), $apps);

// mock should be called again here
$apps = \OC_App::getEnabledApps(false);
$this->assertEquals(array('files', 'app3'), $apps);

$this->restoreAppConfig();
}

private function setupAppConfigMock() {
$appConfig = $this->getMock(
'\OC\AppConfig',
array('getValues'),
array(\OC_DB::getConnection()),
'',
false
);

$this->registerAppConfig($appConfig);
return $appConfig;
}

/**
* Register an app config mock for testing purposes.
* @param $appConfig app config mock
Expand Down

0 comments on commit f268eee

Please sign in to comment.