BackgroundJobTest.php 13 KB

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