1
0

AdminTest.php 16 KB

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