SyncTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\User_LDAP\Tests\Jobs;
  25. use OCA\User_LDAP\Access;
  26. use OCA\User_LDAP\AccessFactory;
  27. use OCA\User_LDAP\Connection;
  28. use OCA\User_LDAP\ConnectionFactory;
  29. use OCA\User_LDAP\Helper;
  30. use OCA\User_LDAP\Jobs\Sync;
  31. use OCA\User_LDAP\LDAP;
  32. use OCA\User_LDAP\Mapping\UserMapping;
  33. use OCA\User_LDAP\User\Manager;
  34. use OCP\IAvatarManager;
  35. use OCP\IConfig;
  36. use OCP\IDBConnection;
  37. use OCP\IUserManager;
  38. use OCP\Notification\IManager;
  39. use Test\TestCase;
  40. class SyncTest extends TestCase {
  41. /** @var array */
  42. protected $arguments;
  43. /** @var Helper|\PHPUnit_Framework_MockObject_MockObject */
  44. protected $helper;
  45. /** @var LDAP|\PHPUnit_Framework_MockObject_MockObject */
  46. protected $ldapWrapper;
  47. /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
  48. protected $userManager;
  49. /** @var UserMapping|\PHPUnit_Framework_MockObject_MockObject */
  50. protected $mapper;
  51. /** @var Sync */
  52. protected $sync;
  53. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  54. protected $config;
  55. /** @var IAvatarManager|\PHPUnit_Framework_MockObject_MockObject */
  56. protected $avatarManager;
  57. /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
  58. protected $dbc;
  59. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  60. protected $ncUserManager;
  61. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  62. protected $notificationManager;
  63. /** @var ConnectionFactory|\PHPUnit_Framework_MockObject_MockObject */
  64. protected $connectionFactory;
  65. /** @var AccessFactory|\PHPUnit_Framework_MockObject_MockObject */
  66. protected $accessFactory;
  67. protected function setUp(): void {
  68. parent::setUp();
  69. $this->helper = $this->createMock(Helper::class);
  70. $this->ldapWrapper = $this->createMock(LDAP::class);
  71. $this->userManager = $this->createMock(Manager::class);
  72. $this->mapper = $this->createMock(UserMapping::class);
  73. $this->config = $this->createMock(IConfig::class);
  74. $this->avatarManager = $this->createMock(IAvatarManager::class);
  75. $this->dbc = $this->createMock(IDBConnection::class);
  76. $this->ncUserManager = $this->createMock(IUserManager::class);
  77. $this->notificationManager = $this->createMock(IManager::class);
  78. $this->connectionFactory = $this->createMock(ConnectionFactory::class);
  79. $this->accessFactory = $this->createMock(AccessFactory::class);
  80. $this->arguments = [
  81. 'helper' => $this->helper,
  82. 'ldapWrapper' => $this->ldapWrapper,
  83. 'userManager' => $this->userManager,
  84. 'mapper' => $this->mapper,
  85. 'config' => $this->config,
  86. 'avatarManager' => $this->avatarManager,
  87. 'dbc' => $this->dbc,
  88. 'ncUserManager' => $this->ncUserManager,
  89. 'notificationManager' => $this->notificationManager,
  90. 'connectionFactory' => $this->connectionFactory,
  91. 'accessFactory' => $this->accessFactory,
  92. ];
  93. $this->sync = new Sync();
  94. }
  95. public function intervalDataProvider() {
  96. return [
  97. [
  98. 0, 1000, 750
  99. ],
  100. [
  101. 22, 0, 50
  102. ],
  103. [
  104. 500, 500, 500
  105. ],
  106. [
  107. 1357, 0, 0
  108. ],
  109. [
  110. 421337, 2000, 3000
  111. ]
  112. ];
  113. }
  114. /**
  115. * @dataProvider intervalDataProvider
  116. */
  117. public function testUpdateInterval($userCount, $pagingSize1, $pagingSize2) {
  118. $this->config->expects($this->once())
  119. ->method('setAppValue')
  120. ->with('user_ldap', 'background_sync_interval', $this->anything())
  121. ->willReturnCallback(function($a, $k, $interval) {
  122. $this->assertTrue($interval >= SYNC::MIN_INTERVAL);
  123. $this->assertTrue($interval <= SYNC::MAX_INTERVAL);
  124. return true;
  125. });
  126. $this->config->expects($this->atLeastOnce())
  127. ->method('getAppKeys')
  128. ->willReturn([
  129. 'blabla',
  130. 'ldap_paging_size',
  131. 's07blabla',
  132. 'installed',
  133. 's07ldap_paging_size'
  134. ]);
  135. $this->config->expects($this->exactly(2))
  136. ->method('getAppValue')
  137. ->willReturnOnConsecutiveCalls($pagingSize1, $pagingSize2);
  138. $this->mapper->expects($this->atLeastOnce())
  139. ->method('count')
  140. ->willReturn($userCount);
  141. $this->sync->setArgument($this->arguments);
  142. $this->sync->updateInterval();
  143. }
  144. public function moreResultsProvider() {
  145. return [
  146. [ 3, 3, true ],
  147. [ 3, 5, true ],
  148. [ 3, 2, false],
  149. [ 0, 4, false],
  150. [ null, 4, false]
  151. ];
  152. }
  153. /**
  154. * @dataProvider moreResultsProvider
  155. */
  156. public function testMoreResults($pagingSize, $results, $expected) {
  157. $connection = $this->createMock(Connection::class);
  158. $this->connectionFactory->expects($this->any())
  159. ->method('get')
  160. ->willReturn($connection);
  161. $connection->expects($this->any())
  162. ->method('__get')
  163. ->willReturnCallback(function ($key) use ($pagingSize) {
  164. if($key === 'ldapPagingSize') {
  165. return $pagingSize;
  166. }
  167. return null;
  168. });
  169. /** @var Access|\PHPUnit_Framework_MockObject_MockObject $access */
  170. $access = $this->createMock(Access::class);
  171. $this->accessFactory->expects($this->any())
  172. ->method('get')
  173. ->with($connection)
  174. ->willReturn($access);
  175. $access->expects($this->once())
  176. ->method('fetchListOfUsers')
  177. ->willReturn(array_pad([], $results, 'someUser'));
  178. $access->connection = $connection;
  179. $access->userManager = $this->userManager;
  180. $this->sync->setArgument($this->arguments);
  181. $hasMoreResults = $this->sync->runCycle(['prefix' => 's01', 'offset' => 100]);
  182. $this->assertSame($expected, $hasMoreResults);
  183. }
  184. public function cycleDataProvider() {
  185. $lastCycle = ['prefix' => 's01', 'offset' => 1000];
  186. $lastCycle2 = ['prefix' => '', 'offset' => 1000];
  187. return [
  188. [ null, ['s01'], ['prefix' => 's01', 'offset' => 0] ],
  189. [ null, [''], ['prefix' => '', 'offset' => 0] ],
  190. [ $lastCycle, ['s01', 's02'], ['prefix' => 's02', 'offset' => 0] ],
  191. [ $lastCycle, [''], ['prefix' => '', 'offset' => 0] ],
  192. [ $lastCycle2, ['', 's01'], ['prefix' => 's01', 'offset' => 0] ],
  193. [ $lastCycle, [], null ],
  194. ];
  195. }
  196. /**
  197. * @dataProvider cycleDataProvider
  198. */
  199. public function testDetermineNextCycle($cycleData, $prefixes, $expectedCycle) {
  200. $this->helper->expects($this->any())
  201. ->method('getServerConfigurationPrefixes')
  202. ->with(true)
  203. ->willReturn($prefixes);
  204. if(is_array($expectedCycle)) {
  205. $this->config->expects($this->exactly(2))
  206. ->method('setAppValue')
  207. ->withConsecutive(
  208. ['user_ldap', 'background_sync_prefix', $expectedCycle['prefix']],
  209. ['user_ldap', 'background_sync_offset', $expectedCycle['offset']]
  210. );
  211. } else {
  212. $this->config->expects($this->never())
  213. ->method('setAppValue');
  214. }
  215. $this->sync->setArgument($this->arguments);
  216. $nextCycle = $this->sync->determineNextCycle($cycleData);
  217. if($expectedCycle === null) {
  218. $this->assertNull($nextCycle);
  219. } else {
  220. $this->assertSame($expectedCycle['prefix'], $nextCycle['prefix']);
  221. $this->assertSame($expectedCycle['offset'], $nextCycle['offset']);
  222. }
  223. }
  224. public function testQualifiesToRun() {
  225. $cycleData = ['prefix' => 's01'];
  226. $this->config->expects($this->exactly(2))
  227. ->method('getAppValue')
  228. ->willReturnOnConsecutiveCalls(time() - 60*40, time() - 60*20);
  229. $this->sync->setArgument($this->arguments);
  230. $this->assertTrue($this->sync->qualifiesToRun($cycleData));
  231. $this->assertFalse($this->sync->qualifiesToRun($cycleData));
  232. }
  233. public function runDataProvider() {
  234. return [
  235. #0 - one LDAP server, reset
  236. [[
  237. 'prefixes' => [''],
  238. 'scheduledCycle' => ['prefix' => '', 'offset' => '4500'],
  239. 'pagingSize' => 500,
  240. 'usersThisCycle' => 0,
  241. 'expectedNextCycle' => ['prefix' => '', 'offset' => '0'],
  242. 'mappedUsers' => 123,
  243. ]],
  244. #1 - 2 LDAP servers, next prefix
  245. [[
  246. 'prefixes' => ['', 's01'],
  247. 'scheduledCycle' => ['prefix' => '', 'offset' => '4500'],
  248. 'pagingSize' => 500,
  249. 'usersThisCycle' => 0,
  250. 'expectedNextCycle' => ['prefix' => 's01', 'offset' => '0'],
  251. 'mappedUsers' => 123,
  252. ]],
  253. #2 - 2 LDAP servers, rotate prefix
  254. [[
  255. 'prefixes' => ['', 's01'],
  256. 'scheduledCycle' => ['prefix' => 's01', 'offset' => '4500'],
  257. 'pagingSize' => 500,
  258. 'usersThisCycle' => 0,
  259. 'expectedNextCycle' => ['prefix' => '', 'offset' => '0'],
  260. 'mappedUsers' => 123,
  261. ]],
  262. ];
  263. }
  264. /**
  265. * @dataProvider runDataProvider
  266. */
  267. public function testRun($runData) {
  268. $this->config->expects($this->any())
  269. ->method('getAppValue')
  270. ->willReturnCallback(function($app, $key, $default) use ($runData) {
  271. if($app === 'core' && $key === 'backgroundjobs_mode') {
  272. return 'cron';
  273. }
  274. if($app = 'user_ldap') {
  275. // for getCycle()
  276. if($key === 'background_sync_prefix') {
  277. return $runData['scheduledCycle']['prefix'];
  278. }
  279. if($key === 'background_sync_offset') {
  280. return $runData['scheduledCycle']['offset'];
  281. }
  282. // for qualifiesToRun()
  283. if($key === $runData['scheduledCycle']['prefix'] . '_lastChange') {
  284. return time() - 60*40;
  285. }
  286. // for getMinPagingSize
  287. if($key === $runData['scheduledCycle']['prefix'] . 'ldap_paging_size') {
  288. return $runData['pagingSize'];
  289. }
  290. }
  291. return $default;
  292. });
  293. $this->config->expects($this->exactly(3))
  294. ->method('setAppValue')
  295. ->withConsecutive(
  296. ['user_ldap', 'background_sync_prefix', $runData['expectedNextCycle']['prefix']],
  297. ['user_ldap', 'background_sync_offset', $runData['expectedNextCycle']['offset']],
  298. ['user_ldap', 'background_sync_interval', $this->anything()]
  299. );
  300. $this->config->expects($this->any())
  301. ->method('getAppKeys')
  302. ->with('user_ldap')
  303. ->willReturn([$runData['scheduledCycle']['prefix'] . 'ldap_paging_size']);
  304. $this->helper->expects($this->any())
  305. ->method('getServerConfigurationPrefixes')
  306. ->with(true)
  307. ->willReturn($runData['prefixes']);
  308. $connection = $this->createMock(Connection::class);
  309. $this->connectionFactory->expects($this->any())
  310. ->method('get')
  311. ->willReturn($connection);
  312. $connection->expects($this->any())
  313. ->method('__get')
  314. ->willReturnCallback(function ($key) use ($runData) {
  315. if($key === 'ldapPagingSize') {
  316. return $runData['pagingSize'];
  317. }
  318. return null;
  319. });
  320. /** @var Access|\PHPUnit_Framework_MockObject_MockObject $access */
  321. $access = $this->createMock(Access::class);
  322. $this->accessFactory->expects($this->any())
  323. ->method('get')
  324. ->with($connection)
  325. ->willReturn($access);
  326. $access->expects($this->once())
  327. ->method('fetchListOfUsers')
  328. ->willReturn(array_pad([], $runData['usersThisCycle'], 'someUser'));
  329. $access->connection = $connection;
  330. $access->userManager = $this->userManager;
  331. $this->mapper->expects($this->any())
  332. ->method('count')
  333. ->willReturn($runData['mappedUsers']);
  334. $this->sync->run($this->arguments);
  335. }
  336. }