CapabilitiesManagerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * @author Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test;
  22. use OC\CapabilitiesManager;
  23. use OCP\AppFramework\QueryException;
  24. use OCP\Capabilities\ICapability;
  25. use OCP\Capabilities\IPublicCapability;
  26. use OCP\ILogger;
  27. class CapabilitiesManagerTest extends TestCase {
  28. /** @var CapabilitiesManager */
  29. private $manager;
  30. /** @var ILogger */
  31. private $logger;
  32. public function setUp() {
  33. parent::setUp();
  34. $this->logger = $this->getMockBuilder('OCP\ILogger')->getMock();
  35. $this->manager = new CapabilitiesManager($this->logger);
  36. }
  37. /**
  38. * Test no capabilities
  39. */
  40. public function testNoCapabilities() {
  41. $res = $this->manager->getCapabilities();
  42. $this->assertEmpty($res);
  43. }
  44. /**
  45. * Test a valid capabilitie
  46. */
  47. public function testValidCapability() {
  48. $this->manager->registerCapability(function() {
  49. return new SimpleCapability();
  50. });
  51. $res = $this->manager->getCapabilities();
  52. $this->assertEquals(['foo' => 1], $res);
  53. }
  54. /**
  55. * Test a public capabilitie
  56. */
  57. public function testPublicCapability() {
  58. $this->manager->registerCapability(function() {
  59. return new PublicSimpleCapability1();
  60. });
  61. $this->manager->registerCapability(function() {
  62. return new SimpleCapability2();
  63. });
  64. $this->manager->registerCapability(function() {
  65. return new SimpleCapability3();
  66. });
  67. $res = $this->manager->getCapabilities(true);
  68. $this->assertEquals(['foo' => 1], $res);
  69. }
  70. /**
  71. * Test that we need something that implents ICapability
  72. * @expectedException \InvalidArgumentException
  73. * @expectedExceptionMessage The given Capability (Test\NoCapability) does not implement the ICapability interface
  74. */
  75. public function testNoICapability() {
  76. $this->manager->registerCapability(function() {
  77. return new NoCapability();
  78. });
  79. $res = $this->manager->getCapabilities();
  80. $this->assertEquals([], $res);
  81. }
  82. /**
  83. * Test a bunch of merged Capabilities
  84. */
  85. public function testMergedCapabilities() {
  86. $this->manager->registerCapability(function() {
  87. return new SimpleCapability();
  88. });
  89. $this->manager->registerCapability(function() {
  90. return new SimpleCapability2();
  91. });
  92. $this->manager->registerCapability(function() {
  93. return new SimpleCapability3();
  94. });
  95. $res = $this->manager->getCapabilities();
  96. $expected = [
  97. 'foo' => 1,
  98. 'bar' => [
  99. 'x' => 1,
  100. 'y' => 2
  101. ]
  102. ];
  103. $this->assertEquals($expected, $res);
  104. }
  105. /**
  106. * Test deep identical capabilities
  107. */
  108. public function testDeepIdenticalCapabilities() {
  109. $this->manager->registerCapability(function() {
  110. return new DeepCapability();
  111. });
  112. $this->manager->registerCapability(function() {
  113. return new DeepCapability();
  114. });
  115. $res = $this->manager->getCapabilities();
  116. $expected = [
  117. 'foo' => [
  118. 'bar' => [
  119. 'baz' => true
  120. ]
  121. ]
  122. ];
  123. $this->assertEquals($expected, $res);
  124. }
  125. public function testInvalidCapability() {
  126. $this->manager->registerCapability(function () {
  127. throw new QueryException();
  128. });
  129. $this->logger->expects($this->once())
  130. ->method('error');
  131. $res = $this->manager->getCapabilities();
  132. $this->assertEquals([], $res);
  133. }
  134. }
  135. class SimpleCapability implements ICapability {
  136. public function getCapabilities() {
  137. return [
  138. 'foo' => 1
  139. ];
  140. }
  141. }
  142. class SimpleCapability2 implements ICapability {
  143. public function getCapabilities() {
  144. return [
  145. 'bar' => ['x' => 1]
  146. ];
  147. }
  148. }
  149. class SimpleCapability3 implements ICapability {
  150. public function getCapabilities() {
  151. return [
  152. 'bar' => ['y' => 2]
  153. ];
  154. }
  155. }
  156. class PublicSimpleCapability1 implements IPublicCapability {
  157. public function getCapabilities() {
  158. return [
  159. 'foo' => 1
  160. ];
  161. }
  162. }
  163. class NoCapability {
  164. public function getCapabilities() {
  165. return [
  166. 'baz' => 'z'
  167. ];
  168. }
  169. }
  170. class DeepCapability implements ICapability {
  171. public function getCapabilities() {
  172. return [
  173. 'foo' => [
  174. 'bar' => [
  175. 'baz' => true
  176. ]
  177. ]
  178. ];
  179. }
  180. }