CapabilitiesManagerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test;
  8. use OC\CapabilitiesManager;
  9. use OCP\AppFramework\QueryException;
  10. use OCP\Capabilities\ICapability;
  11. use OCP\Capabilities\IPublicCapability;
  12. use Psr\Log\LoggerInterface;
  13. class CapabilitiesManagerTest extends TestCase {
  14. /** @var CapabilitiesManager */
  15. private $manager;
  16. /** @var LoggerInterface */
  17. private $logger;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  21. $this->manager = new CapabilitiesManager($this->logger);
  22. }
  23. /**
  24. * Test no capabilities
  25. */
  26. public function testNoCapabilities() {
  27. $res = $this->manager->getCapabilities();
  28. $this->assertEmpty($res);
  29. }
  30. /**
  31. * Test a valid capabilitie
  32. */
  33. public function testValidCapability() {
  34. $this->manager->registerCapability(function () {
  35. return new SimpleCapability();
  36. });
  37. $res = $this->manager->getCapabilities();
  38. $this->assertEquals(['foo' => 1], $res);
  39. }
  40. /**
  41. * Test a public capabilitie
  42. */
  43. public function testPublicCapability() {
  44. $this->manager->registerCapability(function () {
  45. return new PublicSimpleCapability1();
  46. });
  47. $this->manager->registerCapability(function () {
  48. return new SimpleCapability2();
  49. });
  50. $this->manager->registerCapability(function () {
  51. return new SimpleCapability3();
  52. });
  53. $res = $this->manager->getCapabilities(true);
  54. $this->assertEquals(['foo' => 1], $res);
  55. }
  56. /**
  57. * Test that we need something that implents ICapability
  58. */
  59. public function testNoICapability() {
  60. $this->expectException(\InvalidArgumentException::class);
  61. $this->expectExceptionMessage('The given Capability (Test\\NoCapability) does not implement the ICapability interface');
  62. $this->manager->registerCapability(function () {
  63. return new NoCapability();
  64. });
  65. $res = $this->manager->getCapabilities();
  66. $this->assertEquals([], $res);
  67. }
  68. /**
  69. * Test a bunch of merged Capabilities
  70. */
  71. public function testMergedCapabilities() {
  72. $this->manager->registerCapability(function () {
  73. return new SimpleCapability();
  74. });
  75. $this->manager->registerCapability(function () {
  76. return new SimpleCapability2();
  77. });
  78. $this->manager->registerCapability(function () {
  79. return new SimpleCapability3();
  80. });
  81. $res = $this->manager->getCapabilities();
  82. $expected = [
  83. 'foo' => 1,
  84. 'bar' => [
  85. 'x' => 1,
  86. 'y' => 2
  87. ]
  88. ];
  89. $this->assertEquals($expected, $res);
  90. }
  91. /**
  92. * Test deep identical capabilities
  93. */
  94. public function testDeepIdenticalCapabilities() {
  95. $this->manager->registerCapability(function () {
  96. return new DeepCapability();
  97. });
  98. $this->manager->registerCapability(function () {
  99. return new DeepCapability();
  100. });
  101. $res = $this->manager->getCapabilities();
  102. $expected = [
  103. 'foo' => [
  104. 'bar' => [
  105. 'baz' => true
  106. ]
  107. ]
  108. ];
  109. $this->assertEquals($expected, $res);
  110. }
  111. public function testInvalidCapability() {
  112. $this->manager->registerCapability(function () {
  113. throw new QueryException();
  114. });
  115. $this->logger->expects($this->once())
  116. ->method('error');
  117. $res = $this->manager->getCapabilities();
  118. $this->assertEquals([], $res);
  119. }
  120. }
  121. class SimpleCapability implements ICapability {
  122. public function getCapabilities() {
  123. return [
  124. 'foo' => 1
  125. ];
  126. }
  127. }
  128. class SimpleCapability2 implements ICapability {
  129. public function getCapabilities() {
  130. return [
  131. 'bar' => ['x' => 1]
  132. ];
  133. }
  134. }
  135. class SimpleCapability3 implements ICapability {
  136. public function getCapabilities() {
  137. return [
  138. 'bar' => ['y' => 2]
  139. ];
  140. }
  141. }
  142. class PublicSimpleCapability1 implements IPublicCapability {
  143. public function getCapabilities() {
  144. return [
  145. 'foo' => 1
  146. ];
  147. }
  148. }
  149. class NoCapability {
  150. public function getCapabilities() {
  151. return [
  152. 'baz' => 'z'
  153. ];
  154. }
  155. }
  156. class DeepCapability implements ICapability {
  157. public function getCapabilities() {
  158. return [
  159. 'foo' => [
  160. 'bar' => [
  161. 'baz' => true
  162. ]
  163. ]
  164. ];
  165. }
  166. }