manager.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\App;
  9. use OC\Group\Group;
  10. use OC\User\User;
  11. class Manager extends \PHPUnit_Framework_TestCase {
  12. /**
  13. * @return \OCP\IAppConfig | \PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected function getAppConfig() {
  16. $appConfig = array();
  17. $config = $this->getMockBuilder('\OCP\IAppConfig')
  18. ->disableOriginalConstructor()
  19. ->getMock();
  20. $config->expects($this->any())
  21. ->method('getValue')
  22. ->will($this->returnCallback(function ($app, $key, $default) use (&$appConfig) {
  23. return (isset($appConfig[$app]) and isset($appConfig[$app][$key])) ? $appConfig[$app][$key] : $default;
  24. }));
  25. $config->expects($this->any())
  26. ->method('setValue')
  27. ->will($this->returnCallback(function ($app, $key, $value) use (&$appConfig) {
  28. if (!isset($appConfig[$app])) {
  29. $appConfig[$app] = array();
  30. }
  31. $appConfig[$app][$key] = $value;
  32. }));
  33. $config->expects($this->any())
  34. ->method('getValues')
  35. ->will($this->returnCallback(function ($app, $key) use (&$appConfig) {
  36. if ($app) {
  37. return $appConfig[$app];
  38. } else {
  39. $values = array();
  40. foreach ($appConfig as $app => $appData) {
  41. if (isset($appData[$key])) {
  42. $values[$app] = $appData[$key];
  43. }
  44. }
  45. return $values;
  46. }
  47. }));
  48. return $config;
  49. }
  50. /** @var \OCP\IUserSession */
  51. protected $userSession;
  52. /** @var \OCP\IGroupManager */
  53. protected $groupManager;
  54. /** @var \OCP\IAppConfig */
  55. protected $appConfig;
  56. /** @var \OCP\ICache */
  57. protected $cache;
  58. /** @var \OCP\ICacheFactory */
  59. protected $cacheFactory;
  60. /** @var \OCP\App\IAppManager */
  61. protected $manager;
  62. protected function setUp() {
  63. parent::setUp();
  64. $this->userSession = $this->getMock('\OCP\IUserSession');
  65. $this->groupManager = $this->getMock('\OCP\IGroupManager');
  66. $this->appConfig = $this->getAppConfig();
  67. $this->cacheFactory = $this->getMock('\OCP\ICacheFactory');
  68. $this->cache = $this->getMock('\OCP\ICache');
  69. $this->cacheFactory->expects($this->any())
  70. ->method('create')
  71. ->with('settings')
  72. ->willReturn($this->cache);
  73. $this->manager = new \OC\App\AppManager($this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory);
  74. }
  75. protected function expectClearCache() {
  76. $this->cache->expects($this->once())
  77. ->method('clear')
  78. ->with('listApps');
  79. }
  80. public function testEnableApp() {
  81. $this->expectClearCache();
  82. $this->manager->enableApp('test');
  83. $this->assertEquals('yes', $this->appConfig->getValue('test', 'enabled', 'no'));
  84. }
  85. public function testDisableApp() {
  86. $this->expectClearCache();
  87. $this->manager->disableApp('test');
  88. $this->assertEquals('no', $this->appConfig->getValue('test', 'enabled', 'no'));
  89. }
  90. public function testEnableAppForGroups() {
  91. $groups = array(
  92. new Group('group1', array(), null),
  93. new Group('group2', array(), null)
  94. );
  95. $this->expectClearCache();
  96. $this->manager->enableAppForGroups('test', $groups);
  97. $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no'));
  98. }
  99. public function testIsInstalledEnabled() {
  100. $this->appConfig->setValue('test', 'enabled', 'yes');
  101. $this->assertTrue($this->manager->isInstalled('test'));
  102. }
  103. public function testIsInstalledDisabled() {
  104. $this->appConfig->setValue('test', 'enabled', 'no');
  105. $this->assertFalse($this->manager->isInstalled('test'));
  106. }
  107. public function testIsInstalledEnabledForGroups() {
  108. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  109. $this->assertTrue($this->manager->isInstalled('test'));
  110. }
  111. public function testIsEnabledForUserEnabled() {
  112. $this->appConfig->setValue('test', 'enabled', 'yes');
  113. $user = new User('user1', null);
  114. $this->assertTrue($this->manager->isEnabledForUser('test', $user));
  115. }
  116. public function testIsEnabledForUserDisabled() {
  117. $this->appConfig->setValue('test', 'enabled', 'no');
  118. $user = new User('user1', null);
  119. $this->assertFalse($this->manager->isEnabledForUser('test', $user));
  120. }
  121. public function testIsEnabledForUserEnabledForGroup() {
  122. $user = new User('user1', null);
  123. $this->groupManager->expects($this->once())
  124. ->method('getUserGroupIds')
  125. ->with($user)
  126. ->will($this->returnValue(array('foo', 'bar')));
  127. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  128. $this->assertTrue($this->manager->isEnabledForUser('test', $user));
  129. }
  130. public function testIsEnabledForUserDisabledForGroup() {
  131. $user = new User('user1', null);
  132. $this->groupManager->expects($this->once())
  133. ->method('getUserGroupIds')
  134. ->with($user)
  135. ->will($this->returnValue(array('bar')));
  136. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  137. $this->assertFalse($this->manager->isEnabledForUser('test', $user));
  138. }
  139. public function testIsEnabledForUserLoggedOut() {
  140. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  141. $this->assertFalse($this->manager->IsEnabledForUser('test'));
  142. }
  143. public function testIsEnabledForUserLoggedIn() {
  144. $user = new User('user1', null);
  145. $this->userSession->expects($this->once())
  146. ->method('getUser')
  147. ->will($this->returnValue($user));
  148. $this->groupManager->expects($this->once())
  149. ->method('getUserGroupIds')
  150. ->with($user)
  151. ->will($this->returnValue(array('foo', 'bar')));
  152. $this->appConfig->setValue('test', 'enabled', '["foo"]');
  153. $this->assertTrue($this->manager->isEnabledForUser('test'));
  154. }
  155. public function testGetInstalledApps() {
  156. $this->appConfig->setValue('test1', 'enabled', 'yes');
  157. $this->appConfig->setValue('test2', 'enabled', 'no');
  158. $this->appConfig->setValue('test3', 'enabled', '["foo"]');
  159. $this->assertEquals(['test1', 'test3'], $this->manager->getInstalledApps());
  160. }
  161. public function testGetAppsForUser() {
  162. $user = new User('user1', null);
  163. $this->groupManager->expects($this->any())
  164. ->method('getUserGroupIds')
  165. ->with($user)
  166. ->will($this->returnValue(array('foo', 'bar')));
  167. $this->appConfig->setValue('test1', 'enabled', 'yes');
  168. $this->appConfig->setValue('test2', 'enabled', 'no');
  169. $this->appConfig->setValue('test3', 'enabled', '["foo"]');
  170. $this->appConfig->setValue('test4', 'enabled', '["asd"]');
  171. $this->assertEquals(['test1', 'test3'], $this->manager->getEnabledAppsForUser($user));
  172. }
  173. }