1
0

BackgroundJobTest.php 13 KB

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