capabilitiesmanager.php 3.5 KB

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