1
0

BackgroundJobTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  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, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UpdateNotification\Tests\Notification;
  24. use OC\Installer;
  25. use OCA\UpdateNotification\Notification\BackgroundJob;
  26. use OCP\App\IAppManager;
  27. use OCP\Http\Client\IClientService;
  28. use OCP\IConfig;
  29. use OCP\IGroupManager;
  30. use OCP\IUser;
  31. use OCP\Notification\IManager;
  32. use Test\TestCase;
  33. use OC\Updater\VersionCheck;
  34. use OCP\Notification\INotification;
  35. use OCP\IGroup;
  36. class BackgroundJobTest extends TestCase {
  37. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $config;
  39. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $notificationManager;
  41. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  42. protected $groupManager;
  43. /** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
  44. protected $appManager;
  45. /** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */
  46. protected $client;
  47. /** @var Installer|\PHPUnit_Framework_MockObject_MockObject */
  48. protected $installer;
  49. public function setUp() {
  50. parent::setUp();
  51. $this->config = $this->createMock(IConfig::class);
  52. $this->notificationManager = $this->createMock(IManager::class);
  53. $this->groupManager = $this->createMock(IGroupManager::class);
  54. $this->appManager = $this->createMock(IAppManager::class);
  55. $this->client = $this->createMock(IClientService::class);
  56. $this->installer = $this->createMock(Installer::class);
  57. }
  58. /**
  59. * @param array $methods
  60. * @return BackgroundJob|\PHPUnit_Framework_MockObject_MockObject
  61. */
  62. protected function getJob(array $methods = []) {
  63. if (empty($methods)) {
  64. return new BackgroundJob(
  65. $this->config,
  66. $this->notificationManager,
  67. $this->groupManager,
  68. $this->appManager,
  69. $this->client,
  70. $this->installer
  71. );
  72. } {
  73. return $this->getMockBuilder(BackgroundJob::class)
  74. ->setConstructorArgs([
  75. $this->config,
  76. $this->notificationManager,
  77. $this->groupManager,
  78. $this->appManager,
  79. $this->client,
  80. $this->installer,
  81. ])
  82. ->setMethods($methods)
  83. ->getMock();
  84. }
  85. }
  86. public function testRun() {
  87. $job = $this->getJob([
  88. 'checkCoreUpdate',
  89. 'checkAppUpdates',
  90. ]);
  91. $job->expects($this->once())
  92. ->method('checkCoreUpdate');
  93. $job->expects($this->once())
  94. ->method('checkAppUpdates');
  95. self::invokePrivate($job, 'run', [null]);
  96. }
  97. public function dataCheckCoreUpdate(): array {
  98. return [
  99. ['daily', null, null, null, null],
  100. ['git', null, null, null, null],
  101. ['beta', [], null, null, null],
  102. ['beta', false, false, null, null],
  103. ['beta', false, false, null, 13],
  104. ['beta', [
  105. 'version' => '9.2.0',
  106. 'versionstring' => 'Nextcloud 11.0.0',
  107. ], '9.2.0', 'Nextcloud 11.0.0', null],
  108. ['stable', [], null, null, null],
  109. ['stable', false, false, null, null],
  110. ['stable', false, false, null, 6],
  111. ['stable', [
  112. 'version' => '9.2.0',
  113. 'versionstring' => 'Nextcloud 11.0.0',
  114. ], '9.2.0', 'Nextcloud 11.0.0', null],
  115. ['production', [], null, null, null],
  116. ['production', false, false, null, null],
  117. ['production', false, false, null, 2],
  118. ['production', [
  119. 'version' => '9.2.0',
  120. 'versionstring' => 'Nextcloud 11.0.0',
  121. ], '9.2.0', 'Nextcloud 11.0.0', null],
  122. ];
  123. }
  124. /**
  125. * @dataProvider dataCheckCoreUpdate
  126. *
  127. * @param string $channel
  128. * @param mixed $versionCheck
  129. * @param null|string $version
  130. * @param null|string $readableVersion
  131. * @param null|int $errorDays
  132. */
  133. public function testCheckCoreUpdate(string $channel, $versionCheck, $version, $readableVersion, $errorDays) {
  134. $job = $this->getJob([
  135. 'getChannel',
  136. 'createVersionCheck',
  137. 'createNotifications',
  138. 'clearErrorNotifications',
  139. 'sendErrorNotifications',
  140. ]);
  141. $job->expects($this->once())
  142. ->method('getChannel')
  143. ->willReturn($channel);
  144. if ($versionCheck === null) {
  145. $job->expects($this->never())
  146. ->method('createVersionCheck');
  147. } else {
  148. $check = $this->createMock(VersionCheck::class);
  149. $check->expects($this->once())
  150. ->method('check')
  151. ->willReturn($versionCheck);
  152. $job->expects($this->once())
  153. ->method('createVersionCheck')
  154. ->willReturn($check);
  155. }
  156. if ($version === null) {
  157. $job->expects($this->never())
  158. ->method('createNotifications');
  159. $job->expects($versionCheck === null ? $this->never() : $this->once())
  160. ->method('clearErrorNotifications');
  161. } else if ($version === false) {
  162. $job->expects($this->never())
  163. ->method('createNotifications');
  164. $job->expects($this->never())
  165. ->method('clearErrorNotifications');
  166. $this->config->expects($this->once())
  167. ->method('getAppValue')
  168. ->willReturn($errorDays);
  169. $this->config->expects($this->once())
  170. ->method('setAppValue')
  171. ->with('updatenotification', 'update_check_errors', $errorDays + 1);
  172. $job->expects($errorDays !== null ? $this->once() : $this->never())
  173. ->method('sendErrorNotifications')
  174. ->with($errorDays + 1);
  175. } else {
  176. $this->config->expects($this->once())
  177. ->method('setAppValue')
  178. ->with('updatenotification', 'update_check_errors', 0);
  179. $job->expects($this->once())
  180. ->method('clearErrorNotifications');
  181. $job->expects($this->once())
  182. ->method('createNotifications')
  183. ->with('core', $version, $readableVersion);
  184. }
  185. self::invokePrivate($job, 'checkCoreUpdate');
  186. }
  187. public function dataCheckAppUpdates(): array {
  188. return [
  189. [
  190. ['app1', 'app2'],
  191. [
  192. ['app1', false],
  193. ['app2', '1.9.2'],
  194. ],
  195. [
  196. ['app2', '1.9.2'],
  197. ],
  198. ],
  199. ];
  200. }
  201. /**
  202. * @dataProvider dataCheckAppUpdates
  203. *
  204. * @param string[] $apps
  205. * @param array $isUpdateAvailable
  206. * @param array $notifications
  207. */
  208. public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array $notifications) {
  209. $job = $this->getJob([
  210. 'isUpdateAvailable',
  211. 'createNotifications',
  212. ]);
  213. $this->appManager->expects($this->once())
  214. ->method('getInstalledApps')
  215. ->willReturn($apps);
  216. $job->expects($this->exactly(\count($apps)))
  217. ->method('isUpdateAvailable')
  218. ->willReturnMap($isUpdateAvailable);
  219. $mockedMethod = $job->expects($this->exactly(\count($notifications)))
  220. ->method('createNotifications');
  221. \call_user_func_array([$mockedMethod, 'withConsecutive'], $notifications);
  222. self::invokePrivate($job, 'checkAppUpdates');
  223. }
  224. public function dataCreateNotifications(): array {
  225. return [
  226. ['app1', '1.0.0', '1.0.0', false, false, null, null],
  227. ['app2', '1.0.1', '1.0.0', '1.0.0', true, ['user1'], [['user1']]],
  228. ['app3', '1.0.1', false, false, true, ['user2', 'user3'], [['user2'], ['user3']]],
  229. ];
  230. }
  231. /**
  232. * @dataProvider dataCreateNotifications
  233. *
  234. * @param string $app
  235. * @param string $version
  236. * @param string|false $lastNotification
  237. * @param string|false $callDelete
  238. * @param bool $createNotification
  239. * @param string[]|null $users
  240. * @param array|null $userNotifications
  241. */
  242. public function testCreateNotifications(string $app, string $version, $lastNotification, $callDelete, $createNotification, $users, $userNotifications) {
  243. $job = $this->getJob([
  244. 'deleteOutdatedNotifications',
  245. 'getUsersToNotify',
  246. ]);
  247. $this->config->expects($this->once())
  248. ->method('getAppValue')
  249. ->with('updatenotification', $app, false)
  250. ->willReturn($lastNotification);
  251. if ($lastNotification !== $version) {
  252. $this->config->expects($this->once())
  253. ->method('setAppValue')
  254. ->with('updatenotification', $app, $version);
  255. }
  256. if ($callDelete === false) {
  257. $job->expects($this->never())
  258. ->method('deleteOutdatedNotifications');
  259. } else {
  260. $job->expects($this->once())
  261. ->method('deleteOutdatedNotifications')
  262. ->with($app, $callDelete);
  263. }
  264. if ($users === null) {
  265. $job->expects($this->never())
  266. ->method('getUsersToNotify');
  267. } else {
  268. $job->expects($this->once())
  269. ->method('getUsersToNotify')
  270. ->willReturn($users);
  271. }
  272. if ($createNotification) {
  273. $notification = $this->createMock(INotification::class);
  274. $notification->expects($this->once())
  275. ->method('setApp')
  276. ->with('updatenotification')
  277. ->willReturnSelf();
  278. $notification->expects($this->once())
  279. ->method('setDateTime')
  280. ->willReturnSelf();
  281. $notification->expects($this->once())
  282. ->method('setObject')
  283. ->with($app, $version)
  284. ->willReturnSelf();
  285. $notification->expects($this->once())
  286. ->method('setSubject')
  287. ->with('update_available')
  288. ->willReturnSelf();
  289. if ($userNotifications !== null) {
  290. $mockedMethod = $notification->expects($this->exactly(\count($userNotifications)))
  291. ->method('setUser')
  292. ->willReturnSelf();
  293. \call_user_func_array([$mockedMethod, 'withConsecutive'], $userNotifications);
  294. $this->notificationManager->expects($this->exactly(\count($userNotifications)))
  295. ->method('notify')
  296. ->willReturn($notification);
  297. }
  298. $this->notificationManager->expects($this->once())
  299. ->method('createNotification')
  300. ->willReturn($notification);
  301. } else {
  302. $this->notificationManager->expects($this->never())
  303. ->method('createNotification');
  304. }
  305. self::invokePrivate($job, 'createNotifications', [$app, $version]);
  306. }
  307. public function dataGetUsersToNotify(): array {
  308. return [
  309. [['g1', 'g2'], ['g1' => null, 'g2' => ['u1', 'u2']], ['u1', 'u2']],
  310. [['g3', 'g4'], ['g3' => ['u1', 'u2'], 'g4' => ['u2', 'u3']], ['u1', 'u2', 'u3']],
  311. ];
  312. }
  313. /**
  314. * @dataProvider dataGetUsersToNotify
  315. * @param string[] $groups
  316. * @param array $groupUsers
  317. * @param string[] $expected
  318. */
  319. public function testGetUsersToNotify(array $groups, array $groupUsers, array $expected) {
  320. $job = $this->getJob();
  321. $this->config->expects($this->once())
  322. ->method('getAppValue')
  323. ->with('updatenotification', 'notify_groups', '["admin"]')
  324. ->willReturn(json_encode($groups));
  325. $groupMap = [];
  326. foreach ($groupUsers as $gid => $uids) {
  327. if ($uids === null) {
  328. $group = null;
  329. } else {
  330. $group = $this->getGroup($gid);
  331. $group->expects($this->any())
  332. ->method('getUsers')
  333. ->willReturn($this->getUsers($uids));
  334. }
  335. $groupMap[] = [$gid, $group];
  336. }
  337. $this->groupManager->expects($this->exactly(\count($groups)))
  338. ->method('get')
  339. ->willReturnMap($groupMap);
  340. $result = self::invokePrivate($job, 'getUsersToNotify');
  341. $this->assertEquals($expected, $result);
  342. // Test caching
  343. $result = self::invokePrivate($job, 'getUsersToNotify');
  344. $this->assertEquals($expected, $result);
  345. }
  346. public function dataDeleteOutdatedNotifications(): array {
  347. return [
  348. ['app1', '1.1.0'],
  349. ['app2', '1.2.0'],
  350. ];
  351. }
  352. /**
  353. * @dataProvider dataDeleteOutdatedNotifications
  354. * @param string $app
  355. * @param string $version
  356. */
  357. public function testDeleteOutdatedNotifications(string $app, string $version) {
  358. $notification = $this->createMock(INotification::class);
  359. $notification->expects($this->once())
  360. ->method('setApp')
  361. ->with('updatenotification')
  362. ->willReturnSelf();
  363. $notification->expects($this->once())
  364. ->method('setObject')
  365. ->with($app, $version)
  366. ->willReturnSelf();
  367. $this->notificationManager->expects($this->once())
  368. ->method('createNotification')
  369. ->willReturn($notification);
  370. $this->notificationManager->expects($this->once())
  371. ->method('markProcessed')
  372. ->with($notification);
  373. $job = $this->getJob();
  374. self::invokePrivate($job, 'deleteOutdatedNotifications', [$app, $version]);
  375. }
  376. /**
  377. * @param string[] $userIds
  378. * @return IUser[]|\PHPUnit_Framework_MockObject_MockObject[]
  379. */
  380. protected function getUsers(array $userIds): array {
  381. $users = [];
  382. foreach ($userIds as $uid) {
  383. $user = $this->createMock(IUser::class);
  384. $user->expects($this->any())
  385. ->method('getUID')
  386. ->willReturn($uid);
  387. $users[] = $user;
  388. }
  389. return $users;
  390. }
  391. /**
  392. * @param string $gid
  393. * @return \OCP\IGroup|\PHPUnit_Framework_MockObject_MockObject
  394. */
  395. protected function getGroup(string $gid) {
  396. $group = $this->createMock(IGroup::class);
  397. $group->expects($this->any())
  398. ->method('getGID')
  399. ->willReturn($gid);
  400. return $group;
  401. }
  402. }