BackgroundJobTest.php 13 KB

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