You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
6.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Tests\Settings;
  24. use OC\Settings\Admin\Sharing;
  25. use OC\Settings\Manager;
  26. use OC\Settings\Section;
  27. use OCP\Encryption\IManager;
  28. use OCP\IConfig;
  29. use OCP\IDBConnection;
  30. use OCP\IL10N;
  31. use OCP\ILogger;
  32. use OCP\IUserManager;
  33. use OCP\Lock\ILockingProvider;
  34. use Test\TestCase;
  35. class ManagerTest extends TestCase {
  36. /** @var Manager */
  37. private $manager;
  38. /** @var ILogger */
  39. private $logger;
  40. /** @var IDBConnection */
  41. private $dbConnection;
  42. /** @var IL10N */
  43. private $l10n;
  44. /** @var IConfig */
  45. private $config;
  46. /** @var IManager */
  47. private $encryptionManager;
  48. /** @var IUserManager */
  49. private $userManager;
  50. /** @var ILockingProvider */
  51. private $lockingProvider;
  52. public function setUp() {
  53. parent::setUp();
  54. $this->logger = $this->getMockBuilder('\OCP\ILogger')->getMock();
  55. $this->dbConnection = $this->getMockBuilder('\OCP\IDBConnection')->getMock();
  56. $this->l10n = $this->getMockBuilder('\OCP\IL10N')->getMock();
  57. $this->config = $this->getMockBuilder('\OCP\IConfig')->getMock();
  58. $this->encryptionManager = $this->getMockBuilder('\OCP\Encryption\IManager')->getMock();
  59. $this->userManager = $this->getMockBuilder('\OCP\IUserManager')->getMock();
  60. $this->lockingProvider = $this->getMockBuilder('\OCP\Lock\ILockingProvider')->getMock();
  61. $this->manager = new Manager(
  62. $this->logger,
  63. $this->dbConnection,
  64. $this->l10n,
  65. $this->config,
  66. $this->encryptionManager,
  67. $this->userManager,
  68. $this->lockingProvider
  69. );
  70. }
  71. public function testSetupSettings() {
  72. $qb = $this->getMockBuilder('\OCP\DB\QueryBuilder\IQueryBuilder')->getMock();
  73. $qb
  74. ->expects($this->once())
  75. ->method('select')
  76. ->with('class')
  77. ->willReturn($qb);
  78. $this->dbConnection
  79. ->expects($this->at(0))
  80. ->method('getQueryBuilder')
  81. ->willReturn($qb);
  82. $qb
  83. ->expects($this->once())
  84. ->method('from')
  85. ->with('admin_settings')
  86. ->willReturn($qb);
  87. $expressionBuilder = $this->getMockBuilder('\OCP\DB\QueryBuilder\IExpressionBuilder')->getMock();
  88. $qb
  89. ->expects($this->once())
  90. ->method('expr')
  91. ->willReturn($expressionBuilder);
  92. $param = $this->getMockBuilder('\OCP\DB\QueryBuilder\IParameter')->getMock();
  93. $qb
  94. ->expects($this->once())
  95. ->method('createNamedParameter')
  96. ->with('OCA\Files\Settings\Admin')
  97. ->willReturn($param);
  98. $expressionBuilder
  99. ->expects($this->once())
  100. ->method('eq')
  101. ->with('class', $param)
  102. ->willReturn('myString');
  103. $qb
  104. ->expects($this->once())
  105. ->method('where')
  106. ->with('myString')
  107. ->willReturn($qb);
  108. $stmt = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')->getMock();
  109. $qb
  110. ->expects($this->once())
  111. ->method('execute')
  112. ->willReturn($stmt);
  113. $qb1 = $this->getMockBuilder('\OCP\DB\QueryBuilder\IQueryBuilder')->getMock();
  114. $qb1
  115. ->expects($this->once())
  116. ->method('insert')
  117. ->with('admin_settings')
  118. ->willReturn($qb1);
  119. $this->dbConnection
  120. ->expects($this->at(1))
  121. ->method('getQueryBuilder')
  122. ->willReturn($qb1);
  123. $this->manager->setupSettings([
  124. 'admin' => 'OCA\Files\Settings\Admin',
  125. ]);
  126. }
  127. public function testGetAdminSections() {
  128. $qb = $this->getMockBuilder('\OCP\DB\QueryBuilder\IQueryBuilder')->getMock();
  129. $expr = $this->getMockBuilder('OCP\DB\QueryBuilder\IExpressionBuilder')->getMock();
  130. $qb
  131. ->expects($this->once())
  132. ->method('selectDistinct')
  133. ->with('s.class')
  134. ->willReturn($qb);
  135. $qb
  136. ->expects($this->once())
  137. ->method('addSelect')
  138. ->with('s.priority')
  139. ->willReturn($qb);
  140. $qb
  141. ->expects($this->exactly(2))
  142. ->method('from')
  143. ->willReturn($qb);
  144. $qb
  145. ->expects($this->once())
  146. ->method('expr')
  147. ->willReturn($expr);
  148. $qb
  149. ->expects($this->once())
  150. ->method('where')
  151. ->willReturn($qb);
  152. $stmt = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')->getMock();
  153. $qb
  154. ->expects($this->once())
  155. ->method('execute')
  156. ->willReturn($stmt);
  157. $this->dbConnection
  158. ->expects($this->once())
  159. ->method('getQueryBuilder')
  160. ->willReturn($qb);
  161. $this->l10n
  162. ->expects($this->any())
  163. ->method('t')
  164. ->will($this->returnArgument(0));
  165. $this->assertEquals([
  166. 0 => [new Section('server', 'Server settings', 0)],
  167. 5 => [new Section('sharing', 'Sharing', 0)],
  168. 45 => [new Section('encryption', 'Encryption', 0)],
  169. 90 => [new Section('logging', 'Logging', 0)],
  170. 98 => [new Section('additional', 'Additional settings', 0)],
  171. 99 => [new Section('tips-tricks', 'Tips & tricks', 0)],
  172. ], $this->manager->getAdminSections());
  173. }
  174. public function testGetAdminSettings() {
  175. $qb = $this->getMockBuilder('\OCP\DB\QueryBuilder\IQueryBuilder')->getMock();
  176. $qb
  177. ->expects($this->once())
  178. ->method('select')
  179. ->with(['class', 'priority'])
  180. ->willReturn($qb);
  181. $qb
  182. ->expects($this->once())
  183. ->method('from')
  184. ->with('admin_settings')
  185. ->willReturn($qb);
  186. $expressionBuilder = $this->getMockBuilder('\OCP\DB\QueryBuilder\IExpressionBuilder')->getMock();
  187. $qb
  188. ->expects($this->once())
  189. ->method('expr')
  190. ->willReturn($expressionBuilder);
  191. $param = $this->getMockBuilder('\OCP\DB\QueryBuilder\IParameter')->getMock();
  192. $qb
  193. ->expects($this->once())
  194. ->method('createParameter')
  195. ->with('section')
  196. ->willReturn($param);
  197. $expressionBuilder
  198. ->expects($this->once())
  199. ->method('eq')
  200. ->with('section', $param)
  201. ->willReturn('myString');
  202. $qb
  203. ->expects($this->once())
  204. ->method('where')
  205. ->with('myString')
  206. ->willReturn($qb);
  207. $stmt = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')->getMock();
  208. $qb
  209. ->expects($this->once())
  210. ->method('execute')
  211. ->willReturn($stmt);
  212. $this->dbConnection
  213. ->expects($this->exactly(2))
  214. ->method('getQueryBuilder')
  215. ->willReturn($qb);
  216. $this->assertEquals([
  217. 0 => [new Sharing($this->config)],
  218. ], $this->manager->getAdminSettings('sharing'));
  219. }
  220. }