AdminTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\UpdateNotification\Tests\Settings;
  8. use OC\User\Backend;
  9. use OCA\UpdateNotification\Settings\Admin;
  10. use OCA\UpdateNotification\UpdateChecker;
  11. use OCP\AppFramework\Http\TemplateResponse;
  12. use OCP\AppFramework\Services\IInitialState;
  13. use OCP\IAppConfig;
  14. use OCP\IConfig;
  15. use OCP\IDateTimeFormatter;
  16. use OCP\IGroup;
  17. use OCP\IGroupManager;
  18. use OCP\IUserManager;
  19. use OCP\L10N\IFactory;
  20. use OCP\L10N\ILanguageIterator;
  21. use OCP\Support\Subscription\IRegistry;
  22. use OCP\User\Backend\ICountUsersBackend;
  23. use OCP\UserInterface;
  24. use OCP\Util;
  25. use Psr\Log\LoggerInterface;
  26. use Test\TestCase;
  27. class AdminTest extends TestCase {
  28. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  29. protected $l10nFactory;
  30. /** @var Admin */
  31. private $admin;
  32. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  33. private $config;
  34. /** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
  35. private $appConfig;
  36. /** @var UpdateChecker|\PHPUnit\Framework\MockObject\MockObject */
  37. private $updateChecker;
  38. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  39. private $groupManager;
  40. /** @var IDateTimeFormatter|\PHPUnit\Framework\MockObject\MockObject */
  41. private $dateTimeFormatter;
  42. /** @var IRegistry|\PHPUnit\Framework\MockObject\MockObject */
  43. private $subscriptionRegistry;
  44. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  45. private $userManager;
  46. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  47. private $logger;
  48. /** IInitialState|\PHPUnit\Framework\MockObject\MockObject */
  49. private $initialState;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->config = $this->createMock(IConfig::class);
  53. $this->appConfig = $this->createMock(IAppConfig::class);
  54. $this->updateChecker = $this->createMock(UpdateChecker::class);
  55. $this->groupManager = $this->createMock(IGroupManager::class);
  56. $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
  57. $this->l10nFactory = $this->createMock(IFactory::class);
  58. $this->subscriptionRegistry = $this->createMock(IRegistry::class);
  59. $this->userManager = $this->createMock(IUserManager::class);
  60. $this->logger = $this->createMock(LoggerInterface::class);
  61. $this->initialState = $this->createMock(IInitialState::class);
  62. $this->admin = new Admin(
  63. $this->config,
  64. $this->appConfig,
  65. $this->updateChecker,
  66. $this->groupManager,
  67. $this->dateTimeFormatter,
  68. $this->l10nFactory,
  69. $this->subscriptionRegistry,
  70. $this->userManager,
  71. $this->logger,
  72. $this->initialState
  73. );
  74. }
  75. public function testGetFormWithUpdate() {
  76. $backend1 = $this->createMock(CountUsersBackend::class);
  77. $backend2 = $this->createMock(CountUsersBackend::class);
  78. $backend3 = $this->createMock(CountUsersBackend::class);
  79. $backend1
  80. ->expects($this->once())
  81. ->method('implementsActions')
  82. ->with(Backend::COUNT_USERS)
  83. ->willReturn(false);
  84. $backend2
  85. ->expects($this->once())
  86. ->method('implementsActions')
  87. ->with(Backend::COUNT_USERS)
  88. ->willReturn(true);
  89. $backend3
  90. ->expects($this->once())
  91. ->method('implementsActions')
  92. ->with(Backend::COUNT_USERS)
  93. ->willReturn(true);
  94. $backend1
  95. ->expects($this->never())
  96. ->method('countUsers');
  97. $backend2
  98. ->expects($this->once())
  99. ->method('countUsers')
  100. ->with()
  101. ->willReturn(false);
  102. $backend3
  103. ->expects($this->once())
  104. ->method('countUsers')
  105. ->with()
  106. ->willReturn(5);
  107. $this->userManager
  108. ->expects($this->once())
  109. ->method('getBackends')
  110. ->with()
  111. ->willReturn([$backend1, $backend2, $backend3]);
  112. $channels = [
  113. 'daily',
  114. 'beta',
  115. 'stable',
  116. 'production',
  117. ];
  118. $currentChannel = Util::getChannel();
  119. if ($currentChannel === 'git') {
  120. $channels[] = 'git';
  121. }
  122. $this->appConfig
  123. ->expects($this->once())
  124. ->method('getValueInt')
  125. ->with('core', 'lastupdatedat', 0)
  126. ->willReturn(12345);
  127. $this->config
  128. ->expects($this->once())
  129. ->method('getAppValue')
  130. ->with('updatenotification', 'notify_groups', '["admin"]')
  131. ->willReturn('["admin"]');
  132. $this->config
  133. ->method('getSystemValue')
  134. ->willReturnMap([
  135. ['updater.server.url', 'https://updates.nextcloud.com/updater_server/', 'https://updates.nextcloud.com/updater_server/'],
  136. ['upgrade.disable-web', false, false],
  137. ]);
  138. $this->dateTimeFormatter
  139. ->expects($this->once())
  140. ->method('formatDateTime')
  141. ->with(12345)
  142. ->willReturn('LastCheckedReturnValue');
  143. $this->updateChecker
  144. ->expects($this->once())
  145. ->method('getUpdateState')
  146. ->willReturn([
  147. 'updateAvailable' => true,
  148. 'updateVersion' => '8.1.2',
  149. 'updateVersionString' => 'Nextcloud 8.1.2',
  150. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  151. 'changes' => [],
  152. 'updaterEnabled' => true,
  153. 'versionIsEol' => false,
  154. ]);
  155. $group = $this->createMock(IGroup::class);
  156. $group->expects($this->any())
  157. ->method('getDisplayName')
  158. ->willReturn('Administrators');
  159. $group->expects($this->any())
  160. ->method('getGID')
  161. ->willReturn('admin');
  162. $this->groupManager->expects($this->once())
  163. ->method('get')
  164. ->with('admin')
  165. ->willReturn($group);
  166. $this->subscriptionRegistry
  167. ->expects($this->once())
  168. ->method('delegateHasValidSubscription')
  169. ->willReturn(true);
  170. $this->initialState->expects($this->once())
  171. ->method('provideInitialState')
  172. ->with('data', [
  173. 'isNewVersionAvailable' => true,
  174. 'isUpdateChecked' => true,
  175. 'lastChecked' => 'LastCheckedReturnValue',
  176. 'currentChannel' => Util::getChannel(),
  177. 'channels' => $channels,
  178. 'newVersion' => '8.1.2',
  179. 'newVersionString' => 'Nextcloud 8.1.2',
  180. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  181. 'changes' => [],
  182. 'webUpdaterEnabled' => true,
  183. 'isWebUpdaterRecommended' => true,
  184. 'updaterEnabled' => true,
  185. 'versionIsEol' => false,
  186. 'isDefaultUpdateServerURL' => true,
  187. 'updateServerURL' => 'https://updates.nextcloud.com/updater_server/',
  188. 'notifyGroups' => [
  189. ['id' => 'admin', 'displayname' => 'Administrators'],
  190. ],
  191. 'hasValidSubscription' => true,
  192. ]);
  193. $expected = new TemplateResponse('updatenotification', 'admin', [], '');
  194. $this->assertEquals($expected, $this->admin->getForm());
  195. }
  196. public function testGetFormWithUpdateAndChangedUpdateServer() {
  197. $backend1 = $this->createMock(CountUsersBackend::class);
  198. $backend2 = $this->createMock(CountUsersBackend::class);
  199. $backend3 = $this->createMock(CountUsersBackend::class);
  200. $backend1
  201. ->expects($this->once())
  202. ->method('implementsActions')
  203. ->with(Backend::COUNT_USERS)
  204. ->willReturn(false);
  205. $backend2
  206. ->expects($this->once())
  207. ->method('implementsActions')
  208. ->with(Backend::COUNT_USERS)
  209. ->willReturn(true);
  210. $backend3
  211. ->expects($this->once())
  212. ->method('implementsActions')
  213. ->with(Backend::COUNT_USERS)
  214. ->willReturn(true);
  215. $backend1
  216. ->expects($this->never())
  217. ->method('countUsers');
  218. $backend2
  219. ->expects($this->once())
  220. ->method('countUsers')
  221. ->with()
  222. ->willReturn(false);
  223. $backend3
  224. ->expects($this->once())
  225. ->method('countUsers')
  226. ->with()
  227. ->willReturn(5);
  228. $this->userManager
  229. ->expects($this->once())
  230. ->method('getBackends')
  231. ->with()
  232. ->willReturn([$backend1, $backend2, $backend3]);
  233. $channels = [
  234. 'daily',
  235. 'beta',
  236. 'stable',
  237. 'production',
  238. ];
  239. $currentChannel = Util::getChannel();
  240. if ($currentChannel === 'git') {
  241. $channels[] = 'git';
  242. }
  243. $this->appConfig
  244. ->expects($this->once())
  245. ->method('getValueInt')
  246. ->with('core', 'lastupdatedat', 0)
  247. ->willReturn(12345);
  248. $this->config
  249. ->expects($this->once())
  250. ->method('getAppValue')
  251. ->with('updatenotification', 'notify_groups', '["admin"]')
  252. ->willReturn('["admin"]');
  253. $this->config
  254. ->method('getSystemValue')
  255. ->willReturnMap([
  256. ['updater.server.url', 'https://updates.nextcloud.com/updater_server/', 'https://updates.nextcloud.com/updater_server_changed/'],
  257. ['upgrade.disable-web', false, true],
  258. ]);
  259. $this->dateTimeFormatter
  260. ->expects($this->once())
  261. ->method('formatDateTime')
  262. ->with('12345')
  263. ->willReturn('LastCheckedReturnValue');
  264. $this->updateChecker
  265. ->expects($this->once())
  266. ->method('getUpdateState')
  267. ->willReturn([
  268. 'updateAvailable' => true,
  269. 'updateVersion' => '8.1.2',
  270. 'updateVersionString' => 'Nextcloud 8.1.2',
  271. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  272. 'changes' => [],
  273. 'updaterEnabled' => true,
  274. 'versionIsEol' => false,
  275. ]);
  276. $group = $this->createMock(IGroup::class);
  277. $group->expects($this->any())
  278. ->method('getDisplayName')
  279. ->willReturn('Administrators');
  280. $group->expects($this->any())
  281. ->method('getGID')
  282. ->willReturn('admin');
  283. $this->groupManager->expects($this->once())
  284. ->method('get')
  285. ->with('admin')
  286. ->willReturn($group);
  287. $this->subscriptionRegistry
  288. ->expects($this->once())
  289. ->method('delegateHasValidSubscription')
  290. ->willReturn(true);
  291. $this->initialState->expects($this->once())
  292. ->method('provideInitialState')
  293. ->with('data', [
  294. 'isNewVersionAvailable' => true,
  295. 'isUpdateChecked' => true,
  296. 'lastChecked' => 'LastCheckedReturnValue',
  297. 'currentChannel' => Util::getChannel(),
  298. 'channels' => $channels,
  299. 'newVersion' => '8.1.2',
  300. 'newVersionString' => 'Nextcloud 8.1.2',
  301. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  302. 'changes' => [],
  303. 'webUpdaterEnabled' => false,
  304. 'isWebUpdaterRecommended' => true,
  305. 'updaterEnabled' => true,
  306. 'versionIsEol' => false,
  307. 'isDefaultUpdateServerURL' => false,
  308. 'updateServerURL' => 'https://updates.nextcloud.com/updater_server_changed/',
  309. 'notifyGroups' => [
  310. ['id' => 'admin', 'displayname' => 'Administrators'],
  311. ],
  312. 'hasValidSubscription' => true,
  313. ]);
  314. $expected = new TemplateResponse('updatenotification', 'admin', [], '');
  315. $this->assertEquals($expected, $this->admin->getForm());
  316. }
  317. public function testGetFormWithUpdateAndCustomersUpdateServer() {
  318. $backend1 = $this->createMock(CountUsersBackend::class);
  319. $backend2 = $this->createMock(CountUsersBackend::class);
  320. $backend3 = $this->createMock(CountUsersBackend::class);
  321. $backend1
  322. ->expects($this->once())
  323. ->method('implementsActions')
  324. ->with(Backend::COUNT_USERS)
  325. ->willReturn(false);
  326. $backend2
  327. ->expects($this->once())
  328. ->method('implementsActions')
  329. ->with(Backend::COUNT_USERS)
  330. ->willReturn(true);
  331. $backend3
  332. ->expects($this->once())
  333. ->method('implementsActions')
  334. ->with(Backend::COUNT_USERS)
  335. ->willReturn(true);
  336. $backend1
  337. ->expects($this->never())
  338. ->method('countUsers');
  339. $backend2
  340. ->expects($this->once())
  341. ->method('countUsers')
  342. ->with()
  343. ->willReturn(false);
  344. $backend3
  345. ->expects($this->once())
  346. ->method('countUsers')
  347. ->with()
  348. ->willReturn(5);
  349. $this->userManager
  350. ->expects($this->once())
  351. ->method('getBackends')
  352. ->with()
  353. ->willReturn([$backend1, $backend2, $backend3]);
  354. $channels = [
  355. 'daily',
  356. 'beta',
  357. 'stable',
  358. 'production',
  359. ];
  360. $currentChannel = Util::getChannel();
  361. if ($currentChannel === 'git') {
  362. $channels[] = 'git';
  363. }
  364. $this->appConfig
  365. ->expects($this->once())
  366. ->method('getValueInt')
  367. ->with('core', 'lastupdatedat', 0)
  368. ->willReturn(12345);
  369. $this->config
  370. ->expects($this->once())
  371. ->method('getAppValue')
  372. ->with('updatenotification', 'notify_groups', '["admin"]')
  373. ->willReturn('["admin"]');
  374. $this->config
  375. ->method('getSystemValue')
  376. ->willReturnMap([
  377. ['updater.server.url', 'https://updates.nextcloud.com/updater_server/', 'https://updates.nextcloud.com/customers/ABC-DEF/'],
  378. ['upgrade.disable-web', false, false],
  379. ]);
  380. $this->dateTimeFormatter
  381. ->expects($this->once())
  382. ->method('formatDateTime')
  383. ->with('12345')
  384. ->willReturn('LastCheckedReturnValue');
  385. $this->updateChecker
  386. ->expects($this->once())
  387. ->method('getUpdateState')
  388. ->willReturn([
  389. 'updateAvailable' => true,
  390. 'updateVersion' => '8.1.2',
  391. 'updateVersionString' => 'Nextcloud 8.1.2',
  392. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  393. 'changes' => [],
  394. 'updaterEnabled' => true,
  395. 'versionIsEol' => false,
  396. ]);
  397. $group = $this->createMock(IGroup::class);
  398. $group->expects($this->any())
  399. ->method('getDisplayName')
  400. ->willReturn('Administrators');
  401. $group->expects($this->any())
  402. ->method('getGID')
  403. ->willReturn('admin');
  404. $this->groupManager->expects($this->once())
  405. ->method('get')
  406. ->with('admin')
  407. ->willReturn($group);
  408. $this->subscriptionRegistry
  409. ->expects($this->once())
  410. ->method('delegateHasValidSubscription')
  411. ->willReturn(true);
  412. $this->initialState->expects($this->once())
  413. ->method('provideInitialState')
  414. ->with('data', [
  415. 'isNewVersionAvailable' => true,
  416. 'isUpdateChecked' => true,
  417. 'lastChecked' => 'LastCheckedReturnValue',
  418. 'currentChannel' => Util::getChannel(),
  419. 'channels' => $channels,
  420. 'newVersion' => '8.1.2',
  421. 'newVersionString' => 'Nextcloud 8.1.2',
  422. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  423. 'changes' => [],
  424. 'webUpdaterEnabled' => true,
  425. 'isWebUpdaterRecommended' => true,
  426. 'updaterEnabled' => true,
  427. 'versionIsEol' => false,
  428. 'isDefaultUpdateServerURL' => true,
  429. 'updateServerURL' => 'https://updates.nextcloud.com/customers/ABC-DEF/',
  430. 'notifyGroups' => [
  431. ['id' => 'admin', 'displayname' => 'Administrators'],
  432. ],
  433. 'hasValidSubscription' => true,
  434. ]);
  435. $expected = new TemplateResponse('updatenotification', 'admin', [], '');
  436. $this->assertEquals($expected, $this->admin->getForm());
  437. }
  438. public function testGetSection() {
  439. $this->assertSame('overview', $this->admin->getSection());
  440. }
  441. public function testGetPriority() {
  442. $this->assertSame(11, $this->admin->getPriority());
  443. }
  444. public function changesProvider() {
  445. return [
  446. [ #0, all info, en
  447. [
  448. 'changelogURL' => 'https://go.to.changelog',
  449. 'whatsNew' => [
  450. 'en' => [
  451. 'regular' => ['content'],
  452. ],
  453. 'de' => [
  454. 'regular' => ['inhalt'],
  455. ]
  456. ],
  457. ],
  458. 'en',
  459. [
  460. 'changelogURL' => 'https://go.to.changelog',
  461. 'whatsNew' => [
  462. 'regular' => ['content'],
  463. ],
  464. ]
  465. ],
  466. [ #1, all info, de
  467. [
  468. 'changelogURL' => 'https://go.to.changelog',
  469. 'whatsNew' => [
  470. 'en' => [
  471. 'regular' => ['content'],
  472. ],
  473. 'de' => [
  474. 'regular' => ['inhalt'],
  475. ]
  476. ],
  477. ],
  478. 'de',
  479. [
  480. 'changelogURL' => 'https://go.to.changelog',
  481. 'whatsNew' => [
  482. 'regular' => ['inhalt'],
  483. ]
  484. ],
  485. ],
  486. [ #2, just changelog
  487. [ 'changelogURL' => 'https://go.to.changelog' ],
  488. 'en',
  489. [ 'changelogURL' => 'https://go.to.changelog' ],
  490. ],
  491. [ #3 nothing
  492. [],
  493. 'ru',
  494. []
  495. ]
  496. ];
  497. }
  498. /**
  499. * @dataProvider changesProvider
  500. */
  501. public function testFilterChanges($changes, $userLang, $expectation) {
  502. $iterator = $this->createMock(ILanguageIterator::class);
  503. $iterator->expects($this->any())
  504. ->method('current')
  505. ->willReturnOnConsecutiveCalls('es', $userLang, 'it', 'en');
  506. $iterator->expects($this->any())
  507. ->method('valid')
  508. ->willReturn(true);
  509. $this->l10nFactory->expects($this->atMost(1))
  510. ->method('getLanguageIterator')
  511. ->willReturn($iterator);
  512. $result = $this->invokePrivate($this->admin, 'filterChanges', [$changes]);
  513. $this->assertSame($expectation, $result);
  514. }
  515. }
  516. abstract class CountUsersBackend implements UserInterface, ICountUsersBackend {
  517. }