AdminTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\UpdateNotification\Tests\Settings;
  26. use OCA\UpdateNotification\Settings\Admin;
  27. use OCA\UpdateNotification\UpdateChecker;
  28. use OCP\AppFramework\Http\TemplateResponse;
  29. use OCP\IConfig;
  30. use OCP\IDateTimeFormatter;
  31. use OCP\IGroup;
  32. use OCP\IGroupManager;
  33. use OCP\IUserSession;
  34. use OCP\L10N\IFactory;
  35. use OCP\L10N\ILanguageIterator;
  36. use OCP\Support\Subscription\IRegistry;
  37. use OCP\Util;
  38. use Test\TestCase;
  39. class AdminTest extends TestCase {
  40. /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
  41. protected $l10nFactory;
  42. /** @var Admin */
  43. private $admin;
  44. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  45. private $config;
  46. /** @var UpdateChecker|\PHPUnit_Framework_MockObject_MockObject */
  47. private $updateChecker;
  48. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  49. private $groupManager;
  50. /** @var IDateTimeFormatter|\PHPUnit_Framework_MockObject_MockObject */
  51. private $dateTimeFormatter;
  52. /** @var IRegistry|\PHPUnit_Framework_MockObject_MockObject */
  53. private $subscriptionRegistry;
  54. public function setUp() {
  55. parent::setUp();
  56. $this->config = $this->createMock(IConfig::class);
  57. $this->updateChecker = $this->createMock(UpdateChecker::class);
  58. $this->groupManager = $this->createMock(IGroupManager::class);
  59. $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
  60. $this->l10nFactory = $this->createMock(IFactory::class);
  61. $this->subscriptionRegistry = $this->createMock(IRegistry::class);
  62. $this->admin = new Admin(
  63. $this->config, $this->updateChecker, $this->groupManager, $this->dateTimeFormatter, $this->l10nFactory, $this->subscriptionRegistry
  64. );
  65. }
  66. public function testGetFormWithUpdate() {
  67. $channels = [
  68. 'daily',
  69. 'beta',
  70. 'stable',
  71. 'production',
  72. ];
  73. $currentChannel = Util::getChannel();
  74. if ($currentChannel === 'git') {
  75. $channels[] = 'git';
  76. }
  77. $this->config
  78. ->expects($this->exactly(2))
  79. ->method('getAppValue')
  80. ->willReturnMap([
  81. ['core', 'lastupdatedat', '', '12345'],
  82. ['updatenotification', 'notify_groups', '["admin"]', '["admin"]'],
  83. ]);
  84. $this->config
  85. ->expects($this->once())
  86. ->method('getSystemValue')
  87. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  88. ->willReturn('https://updates.nextcloud.com/updater_server/');
  89. $this->dateTimeFormatter
  90. ->expects($this->once())
  91. ->method('formatDateTime')
  92. ->with('12345')
  93. ->willReturn('LastCheckedReturnValue');
  94. $this->updateChecker
  95. ->expects($this->once())
  96. ->method('getUpdateState')
  97. ->willReturn([
  98. 'updateAvailable' => true,
  99. 'updateVersion' => '8.1.2',
  100. 'updateVersionString' => 'Nextcloud 8.1.2',
  101. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  102. 'changes' => [],
  103. 'updaterEnabled' => true,
  104. 'versionIsEol' => false,
  105. ]);
  106. $group = $this->createMock(IGroup::class);
  107. $group->expects($this->any())
  108. ->method('getDisplayName')
  109. ->willReturn('Administrators');
  110. $group->expects($this->any())
  111. ->method('getGID')
  112. ->willReturn('admin');
  113. $this->groupManager->expects($this->once())
  114. ->method('get')
  115. ->with('admin')
  116. ->willReturn($group);
  117. $this->subscriptionRegistry
  118. ->expects($this->once())
  119. ->method('delegateHasValidSubscription')
  120. ->willReturn(true);
  121. $params = [
  122. 'json' => json_encode([
  123. 'isNewVersionAvailable' => true,
  124. 'isUpdateChecked' => true,
  125. 'lastChecked' => 'LastCheckedReturnValue',
  126. 'currentChannel' => Util::getChannel(),
  127. 'channels' => $channels,
  128. 'newVersion' => '8.1.2',
  129. 'newVersionString' => 'Nextcloud 8.1.2',
  130. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  131. 'changes' => [],
  132. 'updaterEnabled' => true,
  133. 'versionIsEol' => false,
  134. 'isDefaultUpdateServerURL' => true,
  135. 'updateServerURL' => 'https://updates.nextcloud.com/updater_server/',
  136. 'notifyGroups' => [
  137. ['value' => 'admin', 'label' => 'Administrators'],
  138. ],
  139. 'hasValidSubscription' => true,
  140. ]),
  141. ];
  142. $expected = new TemplateResponse('updatenotification', 'admin', $params, '');
  143. $this->assertEquals($expected, $this->admin->getForm());
  144. }
  145. public function testGetFormWithUpdateAndChangedUpdateServer() {
  146. $channels = [
  147. 'daily',
  148. 'beta',
  149. 'stable',
  150. 'production',
  151. ];
  152. $currentChannel = Util::getChannel();
  153. if ($currentChannel === 'git') {
  154. $channels[] = 'git';
  155. }
  156. $this->config
  157. ->expects($this->exactly(2))
  158. ->method('getAppValue')
  159. ->willReturnMap([
  160. ['core', 'lastupdatedat', '', '12345'],
  161. ['updatenotification', 'notify_groups', '["admin"]', '["admin"]'],
  162. ]);
  163. $this->config
  164. ->expects($this->once())
  165. ->method('getSystemValue')
  166. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  167. ->willReturn('https://updates.nextcloud.com/updater_server_changed/');
  168. $this->dateTimeFormatter
  169. ->expects($this->once())
  170. ->method('formatDateTime')
  171. ->with('12345')
  172. ->willReturn('LastCheckedReturnValue');
  173. $this->updateChecker
  174. ->expects($this->once())
  175. ->method('getUpdateState')
  176. ->willReturn([
  177. 'updateAvailable' => true,
  178. 'updateVersion' => '8.1.2',
  179. 'updateVersionString' => 'Nextcloud 8.1.2',
  180. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  181. 'changes' => [],
  182. 'updaterEnabled' => true,
  183. 'versionIsEol' => false,
  184. ]);
  185. $group = $this->createMock(IGroup::class);
  186. $group->expects($this->any())
  187. ->method('getDisplayName')
  188. ->willReturn('Administrators');
  189. $group->expects($this->any())
  190. ->method('getGID')
  191. ->willReturn('admin');
  192. $this->groupManager->expects($this->once())
  193. ->method('get')
  194. ->with('admin')
  195. ->willReturn($group);
  196. $this->subscriptionRegistry
  197. ->expects($this->once())
  198. ->method('delegateHasValidSubscription')
  199. ->willReturn(true);
  200. $params = [
  201. 'json' => json_encode([
  202. 'isNewVersionAvailable' => true,
  203. 'isUpdateChecked' => true,
  204. 'lastChecked' => 'LastCheckedReturnValue',
  205. 'currentChannel' => Util::getChannel(),
  206. 'channels' => $channels,
  207. 'newVersion' => '8.1.2',
  208. 'newVersionString' => 'Nextcloud 8.1.2',
  209. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  210. 'changes' => [],
  211. 'updaterEnabled' => true,
  212. 'versionIsEol' => false,
  213. 'isDefaultUpdateServerURL' => false,
  214. 'updateServerURL' => 'https://updates.nextcloud.com/updater_server_changed/',
  215. 'notifyGroups' => [
  216. ['value' => 'admin', 'label' => 'Administrators'],
  217. ],
  218. 'hasValidSubscription' => true,
  219. ]),
  220. ];
  221. $expected = new TemplateResponse('updatenotification', 'admin', $params, '');
  222. $this->assertEquals($expected, $this->admin->getForm());
  223. }
  224. public function testGetFormWithUpdateAndCustomersUpdateServer() {
  225. $channels = [
  226. 'daily',
  227. 'beta',
  228. 'stable',
  229. 'production',
  230. ];
  231. $currentChannel = Util::getChannel();
  232. if ($currentChannel === 'git') {
  233. $channels[] = 'git';
  234. }
  235. $this->config
  236. ->expects($this->exactly(2))
  237. ->method('getAppValue')
  238. ->willReturnMap([
  239. ['core', 'lastupdatedat', '', '12345'],
  240. ['updatenotification', 'notify_groups', '["admin"]', '["admin"]'],
  241. ]);
  242. $this->config
  243. ->expects($this->once())
  244. ->method('getSystemValue')
  245. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  246. ->willReturn('https://updates.nextcloud.com/customers/ABC-DEF/');
  247. $this->dateTimeFormatter
  248. ->expects($this->once())
  249. ->method('formatDateTime')
  250. ->with('12345')
  251. ->willReturn('LastCheckedReturnValue');
  252. $this->updateChecker
  253. ->expects($this->once())
  254. ->method('getUpdateState')
  255. ->willReturn([
  256. 'updateAvailable' => true,
  257. 'updateVersion' => '8.1.2',
  258. 'updateVersionString' => 'Nextcloud 8.1.2',
  259. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  260. 'changes' => [],
  261. 'updaterEnabled' => true,
  262. 'versionIsEol' => false,
  263. ]);
  264. $group = $this->createMock(IGroup::class);
  265. $group->expects($this->any())
  266. ->method('getDisplayName')
  267. ->willReturn('Administrators');
  268. $group->expects($this->any())
  269. ->method('getGID')
  270. ->willReturn('admin');
  271. $this->groupManager->expects($this->once())
  272. ->method('get')
  273. ->with('admin')
  274. ->willReturn($group);
  275. $this->subscriptionRegistry
  276. ->expects($this->once())
  277. ->method('delegateHasValidSubscription')
  278. ->willReturn(true);
  279. $params = [
  280. 'json' => json_encode([
  281. 'isNewVersionAvailable' => true,
  282. 'isUpdateChecked' => true,
  283. 'lastChecked' => 'LastCheckedReturnValue',
  284. 'currentChannel' => Util::getChannel(),
  285. 'channels' => $channels,
  286. 'newVersion' => '8.1.2',
  287. 'newVersionString' => 'Nextcloud 8.1.2',
  288. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  289. 'changes' => [],
  290. 'updaterEnabled' => true,
  291. 'versionIsEol' => false,
  292. 'isDefaultUpdateServerURL' => true,
  293. 'updateServerURL' => 'https://updates.nextcloud.com/customers/ABC-DEF/',
  294. 'notifyGroups' => [
  295. ['value' => 'admin', 'label' => 'Administrators'],
  296. ],
  297. 'hasValidSubscription' => true,
  298. ]),
  299. ];
  300. $expected = new TemplateResponse('updatenotification', 'admin', $params, '');
  301. $this->assertEquals($expected, $this->admin->getForm());
  302. }
  303. public function testGetSection() {
  304. $this->assertSame('overview', $this->admin->getSection());
  305. }
  306. public function testGetPriority() {
  307. $this->assertSame(11, $this->admin->getPriority());
  308. }
  309. public function changesProvider() {
  310. return [
  311. [ #0, all info, en
  312. [
  313. 'changelogURL' => 'https://go.to.changelog',
  314. 'whatsNew' => [
  315. 'en' => [
  316. 'regular' => ['content'],
  317. ],
  318. 'de' => [
  319. 'regular' => ['inhalt'],
  320. ]
  321. ],
  322. ],
  323. 'en',
  324. [
  325. 'changelogURL' => 'https://go.to.changelog',
  326. 'whatsNew' => [
  327. 'regular' => ['content'],
  328. ],
  329. ]
  330. ],
  331. [ #1, all info, de
  332. [
  333. 'changelogURL' => 'https://go.to.changelog',
  334. 'whatsNew' => [
  335. 'en' => [
  336. 'regular' => ['content'],
  337. ],
  338. 'de' => [
  339. 'regular' => ['inhalt'],
  340. ]
  341. ],
  342. ],
  343. 'de',
  344. [
  345. 'changelogURL' => 'https://go.to.changelog',
  346. 'whatsNew' => [
  347. 'regular' => ['inhalt'],
  348. ]
  349. ],
  350. ],
  351. [ #2, just changelog
  352. [ 'changelogURL' => 'https://go.to.changelog' ],
  353. 'en',
  354. [ 'changelogURL' => 'https://go.to.changelog' ],
  355. ],
  356. [ #3 nothing
  357. [],
  358. 'ru',
  359. []
  360. ]
  361. ];
  362. }
  363. /**
  364. * @dataProvider changesProvider
  365. */
  366. public function testFilterChanges($changes, $userLang, $expectation) {
  367. $iterator = $this->createMock(ILanguageIterator::class);
  368. $iterator->expects($this->any())
  369. ->method('current')
  370. ->willReturnOnConsecutiveCalls('es', $userLang, 'it', 'en');
  371. $iterator->expects($this->any())
  372. ->method('valid')
  373. ->willReturn(true);
  374. $this->l10nFactory->expects($this->atMost(1))
  375. ->method('getLanguageIterator')
  376. ->willReturn($iterator);
  377. $result = $this->invokePrivate($this->admin, 'filterChanges', [$changes]);
  378. $this->assertSame($expectation, $result);
  379. }
  380. }