AdminTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 testGetSection() {
  146. $this->assertSame('overview', $this->admin->getSection());
  147. }
  148. public function testGetPriority() {
  149. $this->assertSame(11, $this->admin->getPriority());
  150. }
  151. public function changesProvider() {
  152. return [
  153. [ #0, all info, en
  154. [
  155. 'changelogURL' => 'https://go.to.changelog',
  156. 'whatsNew' => [
  157. 'en' => [
  158. 'regular' => ['content'],
  159. ],
  160. 'de' => [
  161. 'regular' => ['inhalt'],
  162. ]
  163. ],
  164. ],
  165. 'en',
  166. [
  167. 'changelogURL' => 'https://go.to.changelog',
  168. 'whatsNew' => [
  169. 'regular' => ['content'],
  170. ],
  171. ]
  172. ],
  173. [ #1, all info, de
  174. [
  175. 'changelogURL' => 'https://go.to.changelog',
  176. 'whatsNew' => [
  177. 'en' => [
  178. 'regular' => ['content'],
  179. ],
  180. 'de' => [
  181. 'regular' => ['inhalt'],
  182. ]
  183. ],
  184. ],
  185. 'de',
  186. [
  187. 'changelogURL' => 'https://go.to.changelog',
  188. 'whatsNew' => [
  189. 'regular' => ['inhalt'],
  190. ]
  191. ],
  192. ],
  193. [ #2, just changelog
  194. [ 'changelogURL' => 'https://go.to.changelog' ],
  195. 'en',
  196. [ 'changelogURL' => 'https://go.to.changelog' ],
  197. ],
  198. [ #3 nothing
  199. [],
  200. 'ru',
  201. []
  202. ]
  203. ];
  204. }
  205. /**
  206. * @dataProvider changesProvider
  207. */
  208. public function testFilterChanges($changes, $userLang, $expectation) {
  209. $iterator = $this->createMock(ILanguageIterator::class);
  210. $iterator->expects($this->any())
  211. ->method('current')
  212. ->willReturnOnConsecutiveCalls('es', $userLang, 'it', 'en');
  213. $iterator->expects($this->any())
  214. ->method('valid')
  215. ->willReturn(true);
  216. $this->l10nFactory->expects($this->atMost(1))
  217. ->method('getLanguageIterator')
  218. ->willReturn($iterator);
  219. $result = $this->invokePrivate($this->admin, 'filterChanges', [$changes]);
  220. $this->assertSame($expectation, $result);
  221. }
  222. }