1
0

AdminTest.php 16 KB

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