1
0

AdminTest.php 15 KB

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