1
0

CapabilitiesManagerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\ILogger;
  26. class CapabilitiesManagerTest extends TestCase {
  27. /** @var CapabilitiesManager */
  28. private $manager;
  29. /** @var ILogger */
  30. private $logger;
  31. public function setUp() {
  32. $this->logger = $this->getMockBuilder('OCP\ILogger')->getMock();
  33. $this->manager = new CapabilitiesManager($this->logger);
  34. }
  35. /**
  36. * Test no capabilities
  37. */
  38. public function testNoCapabilities() {
  39. $res = $this->manager->getCapabilities();
  40. $this->assertEmpty($res);
  41. }
  42. /**
  43. * Test a valid capabilitie
  44. */
  45. public function testValidCapability() {
  46. $this->manager->registerCapability(function() {
  47. return new SimpleCapability();
  48. });
  49. $res = $this->manager->getCapabilities();
  50. $this->assertEquals(['foo' => 1], $res);
  51. }
  52. /**
  53. * Test that we need something that implents ICapability
  54. * @expectedException \InvalidArgumentException
  55. * @expectedExceptionMessage The given Capability (Test\NoCapability) does not implement the ICapability interface
  56. */
  57. public function testNoICapability() {
  58. $this->manager->registerCapability(function() {
  59. return new NoCapability();
  60. });
  61. $res = $this->manager->getCapabilities();
  62. $this->assertEquals([], $res);
  63. }
  64. /**
  65. * Test a bunch of merged Capabilities
  66. */
  67. public function testMergedCapabilities() {
  68. $this->manager->registerCapability(function() {
  69. return new SimpleCapability();
  70. });
  71. $this->manager->registerCapability(function() {
  72. return new SimpleCapability2();
  73. });
  74. $this->manager->registerCapability(function() {
  75. return new SimpleCapability3();
  76. });
  77. $res = $this->manager->getCapabilities();
  78. $expected = [
  79. 'foo' => 1,
  80. 'bar' => [
  81. 'x' => 1,
  82. 'y' => 2
  83. ]
  84. ];
  85. $this->assertEquals($expected, $res);
  86. }
  87. /**
  88. * Test deep identical capabilities
  89. */
  90. public function testDeepIdenticalCapabilities() {
  91. $this->manager->registerCapability(function() {
  92. return new DeepCapability();
  93. });
  94. $this->manager->registerCapability(function() {
  95. return new DeepCapability();
  96. });
  97. $res = $this->manager->getCapabilities();
  98. $expected = [
  99. 'foo' => [
  100. 'bar' => [
  101. 'baz' => true
  102. ]
  103. ]
  104. ];
  105. $this->assertEquals($expected, $res);
  106. }
  107. public function testInvalidCapability() {
  108. $this->manager->registerCapability(function () {
  109. throw new QueryException();
  110. });
  111. $this->logger->expects($this->once())
  112. ->method('error');
  113. $res = $this->manager->getCapabilities();
  114. $this->assertEquals([], $res);
  115. }
  116. }
  117. class SimpleCapability implements ICapability {
  118. public function getCapabilities() {
  119. return [
  120. 'foo' => 1
  121. ];
  122. }
  123. }
  124. class SimpleCapability2 implements ICapability {
  125. public function getCapabilities() {
  126. return [
  127. 'bar' => ['x' => 1]
  128. ];
  129. }
  130. }
  131. class SimpleCapability3 implements ICapability {
  132. public function getCapabilities() {
  133. return [
  134. 'bar' => ['y' => 2]
  135. ];
  136. }
  137. }
  138. class NoCapability {
  139. public function getCapabilities() {
  140. return [
  141. 'baz' => 'z'
  142. ];
  143. }
  144. }
  145. class DeepCapability implements ICapability {
  146. public function getCapabilities() {
  147. return [
  148. 'foo' => [
  149. 'bar' => [
  150. 'baz' => true
  151. ]
  152. ]
  153. ];
  154. }
  155. }