UpdateAvailableNotificationsTest.php 13 KB

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