BackgroundJobTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. $this->config->method('getSystemValueBool')
  100. ->with('debug', false)
  101. ->willReturn(true);
  102. self::invokePrivate($job, 'run', [null]);
  103. }
  104. public function dataCheckCoreUpdate(): array {
  105. return [
  106. ['daily', null, null, null, null],
  107. ['git', null, null, null, null],
  108. ['beta', [], null, null, null],
  109. ['beta', false, false, null, null],
  110. ['beta', false, false, null, 13],
  111. ['beta', [
  112. 'version' => '9.2.0',
  113. 'versionstring' => 'Nextcloud 11.0.0',
  114. ], '9.2.0', 'Nextcloud 11.0.0', null],
  115. ['stable', [], null, null, null],
  116. ['stable', false, false, null, null],
  117. ['stable', false, false, null, 6],
  118. ['stable', [
  119. 'version' => '9.2.0',
  120. 'versionstring' => 'Nextcloud 11.0.0',
  121. ], '9.2.0', 'Nextcloud 11.0.0', null],
  122. ['production', [], null, null, null],
  123. ['production', false, false, null, null],
  124. ['production', false, false, null, 2],
  125. ['production', [
  126. 'version' => '9.2.0',
  127. 'versionstring' => 'Nextcloud 11.0.0',
  128. ], '9.2.0', 'Nextcloud 11.0.0', null],
  129. ];
  130. }
  131. /**
  132. * @dataProvider dataCheckCoreUpdate
  133. *
  134. * @param string $channel
  135. * @param mixed $versionCheck
  136. * @param null|string $version
  137. * @param null|string $readableVersion
  138. * @param null|int $errorDays
  139. */
  140. public function testCheckCoreUpdate(string $channel, $versionCheck, $version, $readableVersion, $errorDays) {
  141. $job = $this->getJob([
  142. 'getChannel',
  143. 'createVersionCheck',
  144. 'createNotifications',
  145. 'clearErrorNotifications',
  146. 'sendErrorNotifications',
  147. ]);
  148. $job->expects($this->once())
  149. ->method('getChannel')
  150. ->willReturn($channel);
  151. if ($versionCheck === null) {
  152. $job->expects($this->never())
  153. ->method('createVersionCheck');
  154. } else {
  155. $check = $this->createMock(VersionCheck::class);
  156. $check->expects($this->once())
  157. ->method('check')
  158. ->willReturn($versionCheck);
  159. $job->expects($this->once())
  160. ->method('createVersionCheck')
  161. ->willReturn($check);
  162. }
  163. if ($version === null) {
  164. $job->expects($this->never())
  165. ->method('createNotifications');
  166. $job->expects($versionCheck === null ? $this->never() : $this->once())
  167. ->method('clearErrorNotifications');
  168. } elseif ($version === false) {
  169. $job->expects($this->never())
  170. ->method('createNotifications');
  171. $job->expects($this->never())
  172. ->method('clearErrorNotifications');
  173. $this->config->expects($this->once())
  174. ->method('getAppValue')
  175. ->willReturn($errorDays);
  176. $this->config->expects($this->once())
  177. ->method('setAppValue')
  178. ->with('updatenotification', 'update_check_errors', $errorDays + 1);
  179. $job->expects($errorDays !== null ? $this->once() : $this->never())
  180. ->method('sendErrorNotifications')
  181. ->with($errorDays + 1);
  182. } else {
  183. $this->config->expects($this->once())
  184. ->method('setAppValue')
  185. ->with('updatenotification', 'update_check_errors', 0);
  186. $job->expects($this->once())
  187. ->method('clearErrorNotifications');
  188. $job->expects($this->once())
  189. ->method('createNotifications')
  190. ->with('core', $version, $readableVersion);
  191. }
  192. self::invokePrivate($job, 'checkCoreUpdate');
  193. }
  194. public function dataCheckAppUpdates(): array {
  195. return [
  196. [
  197. ['app1', 'app2'],
  198. [
  199. ['app1', false],
  200. ['app2', '1.9.2'],
  201. ],
  202. [
  203. ['app2', '1.9.2'],
  204. ],
  205. ],
  206. ];
  207. }
  208. /**
  209. * @dataProvider dataCheckAppUpdates
  210. *
  211. * @param string[] $apps
  212. * @param array $isUpdateAvailable
  213. * @param array $notifications
  214. */
  215. public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array $notifications) {
  216. $job = $this->getJob([
  217. 'isUpdateAvailable',
  218. 'createNotifications',
  219. ]);
  220. $this->appManager->expects($this->once())
  221. ->method('getInstalledApps')
  222. ->willReturn($apps);
  223. $job->expects($this->exactly(\count($apps)))
  224. ->method('isUpdateAvailable')
  225. ->willReturnMap($isUpdateAvailable);
  226. $mockedMethod = $job->expects($this->exactly(\count($notifications)))
  227. ->method('createNotifications');
  228. \call_user_func_array([$mockedMethod, 'withConsecutive'], $notifications);
  229. self::invokePrivate($job, 'checkAppUpdates');
  230. }
  231. public function dataCreateNotifications(): array {
  232. return [
  233. ['app1', '1.0.0', '1.0.0', false, false, null, null],
  234. ['app2', '1.0.1', '1.0.0', '1.0.0', true, ['user1'], [['user1']]],
  235. ['app3', '1.0.1', false, false, true, ['user2', 'user3'], [['user2'], ['user3']]],
  236. ];
  237. }
  238. /**
  239. * @dataProvider dataCreateNotifications
  240. *
  241. * @param string $app
  242. * @param string $version
  243. * @param string|false $lastNotification
  244. * @param string|false $callDelete
  245. * @param bool $createNotification
  246. * @param string[]|null $users
  247. * @param array|null $userNotifications
  248. */
  249. public function testCreateNotifications(string $app, string $version, $lastNotification, $callDelete, $createNotification, $users, $userNotifications) {
  250. $job = $this->getJob([
  251. 'deleteOutdatedNotifications',
  252. 'getUsersToNotify',
  253. ]);
  254. $this->config->expects($this->once())
  255. ->method('getAppValue')
  256. ->with('updatenotification', $app, false)
  257. ->willReturn($lastNotification);
  258. if ($lastNotification !== $version) {
  259. $this->config->expects($this->once())
  260. ->method('setAppValue')
  261. ->with('updatenotification', $app, $version);
  262. }
  263. if ($callDelete === false) {
  264. $job->expects($this->never())
  265. ->method('deleteOutdatedNotifications');
  266. } else {
  267. $job->expects($this->once())
  268. ->method('deleteOutdatedNotifications')
  269. ->with($app, $callDelete);
  270. }
  271. if ($users === null) {
  272. $job->expects($this->never())
  273. ->method('getUsersToNotify');
  274. } else {
  275. $job->expects($this->once())
  276. ->method('getUsersToNotify')
  277. ->willReturn($users);
  278. }
  279. if ($createNotification) {
  280. $notification = $this->createMock(INotification::class);
  281. $notification->expects($this->once())
  282. ->method('setApp')
  283. ->with('updatenotification')
  284. ->willReturnSelf();
  285. $notification->expects($this->once())
  286. ->method('setDateTime')
  287. ->willReturnSelf();
  288. $notification->expects($this->once())
  289. ->method('setObject')
  290. ->with($app, $version)
  291. ->willReturnSelf();
  292. $notification->expects($this->once())
  293. ->method('setSubject')
  294. ->with('update_available')
  295. ->willReturnSelf();
  296. if ($userNotifications !== null) {
  297. $mockedMethod = $notification->expects($this->exactly(\count($userNotifications)))
  298. ->method('setUser')
  299. ->willReturnSelf();
  300. \call_user_func_array([$mockedMethod, 'withConsecutive'], $userNotifications);
  301. $this->notificationManager->expects($this->exactly(\count($userNotifications)))
  302. ->method('notify');
  303. }
  304. $this->notificationManager->expects($this->once())
  305. ->method('createNotification')
  306. ->willReturn($notification);
  307. } else {
  308. $this->notificationManager->expects($this->never())
  309. ->method('createNotification');
  310. }
  311. self::invokePrivate($job, 'createNotifications', [$app, $version]);
  312. }
  313. public function dataGetUsersToNotify(): array {
  314. return [
  315. [['g1', 'g2'], ['g1' => null, 'g2' => ['u1', 'u2']], ['u1', 'u2']],
  316. [['g3', 'g4'], ['g3' => ['u1', 'u2'], 'g4' => ['u2', 'u3']], ['u1', 'u2', 'u3']],
  317. ];
  318. }
  319. /**
  320. * @dataProvider dataGetUsersToNotify
  321. * @param string[] $groups
  322. * @param array $groupUsers
  323. * @param string[] $expected
  324. */
  325. public function testGetUsersToNotify(array $groups, array $groupUsers, array $expected) {
  326. $job = $this->getJob();
  327. $this->config->expects($this->once())
  328. ->method('getAppValue')
  329. ->with('updatenotification', 'notify_groups', '["admin"]')
  330. ->willReturn(json_encode($groups));
  331. $groupMap = [];
  332. foreach ($groupUsers as $gid => $uids) {
  333. if ($uids === null) {
  334. $group = null;
  335. } else {
  336. $group = $this->getGroup($gid);
  337. $group->expects($this->any())
  338. ->method('getUsers')
  339. ->willReturn($this->getUsers($uids));
  340. }
  341. $groupMap[] = [$gid, $group];
  342. }
  343. $this->groupManager->expects($this->exactly(\count($groups)))
  344. ->method('get')
  345. ->willReturnMap($groupMap);
  346. $result = self::invokePrivate($job, 'getUsersToNotify');
  347. $this->assertEquals($expected, $result);
  348. // Test caching
  349. $result = self::invokePrivate($job, 'getUsersToNotify');
  350. $this->assertEquals($expected, $result);
  351. }
  352. public function dataDeleteOutdatedNotifications(): array {
  353. return [
  354. ['app1', '1.1.0'],
  355. ['app2', '1.2.0'],
  356. ];
  357. }
  358. /**
  359. * @dataProvider dataDeleteOutdatedNotifications
  360. * @param string $app
  361. * @param string $version
  362. */
  363. public function testDeleteOutdatedNotifications(string $app, string $version) {
  364. $notification = $this->createMock(INotification::class);
  365. $notification->expects($this->once())
  366. ->method('setApp')
  367. ->with('updatenotification')
  368. ->willReturnSelf();
  369. $notification->expects($this->once())
  370. ->method('setObject')
  371. ->with($app, $version)
  372. ->willReturnSelf();
  373. $this->notificationManager->expects($this->once())
  374. ->method('createNotification')
  375. ->willReturn($notification);
  376. $this->notificationManager->expects($this->once())
  377. ->method('markProcessed')
  378. ->with($notification);
  379. $job = $this->getJob();
  380. self::invokePrivate($job, 'deleteOutdatedNotifications', [$app, $version]);
  381. }
  382. /**
  383. * @param string[] $userIds
  384. * @return IUser[]|\PHPUnit\Framework\MockObject\MockObject[]
  385. */
  386. protected function getUsers(array $userIds): array {
  387. $users = [];
  388. foreach ($userIds as $uid) {
  389. $user = $this->createMock(IUser::class);
  390. $user->expects($this->any())
  391. ->method('getUID')
  392. ->willReturn($uid);
  393. $users[] = $user;
  394. }
  395. return $users;
  396. }
  397. /**
  398. * @param string $gid
  399. * @return \OCP\IGroup|\PHPUnit\Framework\MockObject\MockObject
  400. */
  401. protected function getGroup(string $gid) {
  402. $group = $this->createMock(IGroup::class);
  403. $group->expects($this->any())
  404. ->method('getGID')
  405. ->willReturn($gid);
  406. return $group;
  407. }
  408. }