You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdminTest.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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\Util;
  37. use Test\TestCase;
  38. class AdminTest extends TestCase {
  39. /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $l10nFactory;
  41. /** @var Admin */
  42. private $admin;
  43. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  44. private $config;
  45. /** @var UpdateChecker|\PHPUnit_Framework_MockObject_MockObject */
  46. private $updateChecker;
  47. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  48. private $groupManager;
  49. /** @var IDateTimeFormatter|\PHPUnit_Framework_MockObject_MockObject */
  50. private $dateTimeFormatter;
  51. public function setUp() {
  52. parent::setUp();
  53. $this->config = $this->createMock(IConfig::class);
  54. $this->updateChecker = $this->createMock(UpdateChecker::class);
  55. $this->groupManager = $this->createMock(IGroupManager::class);
  56. $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class);
  57. $this->l10nFactory = $this->createMock(IFactory::class);
  58. $this->admin = new Admin(
  59. $this->config, $this->updateChecker, $this->groupManager, $this->dateTimeFormatter, $this->l10nFactory
  60. );
  61. }
  62. public function testGetFormWithUpdate() {
  63. $channels = [
  64. 'daily',
  65. 'beta',
  66. 'stable',
  67. 'production',
  68. ];
  69. $currentChannel = Util::getChannel();
  70. if ($currentChannel === 'git') {
  71. $channels[] = 'git';
  72. }
  73. $this->config
  74. ->expects($this->exactly(2))
  75. ->method('getAppValue')
  76. ->willReturnMap([
  77. ['core', 'lastupdatedat', '', '12345'],
  78. ['updatenotification', 'notify_groups', '["admin"]', '["admin"]'],
  79. ]);
  80. $this->config
  81. ->expects($this->once())
  82. ->method('getSystemValue')
  83. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  84. ->willReturn('https://updates.nextcloud.com/updater_server/');
  85. $this->dateTimeFormatter
  86. ->expects($this->once())
  87. ->method('formatDateTime')
  88. ->with('12345')
  89. ->willReturn('LastCheckedReturnValue');
  90. $this->updateChecker
  91. ->expects($this->once())
  92. ->method('getUpdateState')
  93. ->willReturn([
  94. 'updateAvailable' => true,
  95. 'updateVersion' => '8.1.2',
  96. 'updateVersionString' => 'Nextcloud 8.1.2',
  97. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  98. 'changes' => [],
  99. 'updaterEnabled' => true,
  100. 'versionIsEol' => false,
  101. ]);
  102. $group = $this->createMock(IGroup::class);
  103. $group->expects($this->any())
  104. ->method('getDisplayName')
  105. ->willReturn('Administrators');
  106. $group->expects($this->any())
  107. ->method('getGID')
  108. ->willReturn('admin');
  109. $this->groupManager->expects($this->once())
  110. ->method('get')
  111. ->with('admin')
  112. ->willReturn($group);
  113. $params = [
  114. 'json' => json_encode([
  115. 'isNewVersionAvailable' => true,
  116. 'isUpdateChecked' => true,
  117. 'lastChecked' => 'LastCheckedReturnValue',
  118. 'currentChannel' => Util::getChannel(),
  119. 'channels' => $channels,
  120. 'newVersion' => '8.1.2',
  121. 'newVersionString' => 'Nextcloud 8.1.2',
  122. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  123. 'changes' => [],
  124. 'updaterEnabled' => true,
  125. 'versionIsEol' => false,
  126. 'isDefaultUpdateServerURL' => true,
  127. 'updateServerURL' => 'https://updates.nextcloud.com/updater_server/',
  128. 'notifyGroups' => [
  129. ['value' => 'admin', 'label' => 'Administrators'],
  130. ],
  131. ]),
  132. ];
  133. $expected = new TemplateResponse('updatenotification', 'admin', $params, '');
  134. $this->assertEquals($expected, $this->admin->getForm());
  135. }
  136. public function testGetSection() {
  137. $this->assertSame('overview', $this->admin->getSection());
  138. }
  139. public function testGetPriority() {
  140. $this->assertSame(11, $this->admin->getPriority());
  141. }
  142. public function changesProvider() {
  143. return [
  144. [ #0, all info, en
  145. [
  146. 'changelogURL' => 'https://go.to.changelog',
  147. 'whatsNew' => [
  148. 'en' => [
  149. 'regular' => ['content'],
  150. ],
  151. 'de' => [
  152. 'regular' => ['inhalt'],
  153. ]
  154. ],
  155. ],
  156. 'en',
  157. [
  158. 'changelogURL' => 'https://go.to.changelog',
  159. 'whatsNew' => [
  160. 'regular' => ['content'],
  161. ],
  162. ]
  163. ],
  164. [ #1, all info, de
  165. [
  166. 'changelogURL' => 'https://go.to.changelog',
  167. 'whatsNew' => [
  168. 'en' => [
  169. 'regular' => ['content'],
  170. ],
  171. 'de' => [
  172. 'regular' => ['inhalt'],
  173. ]
  174. ],
  175. ],
  176. 'de',
  177. [
  178. 'changelogURL' => 'https://go.to.changelog',
  179. 'whatsNew' => [
  180. 'regular' => ['inhalt'],
  181. ]
  182. ],
  183. ],
  184. [ #2, just changelog
  185. [ 'changelogURL' => 'https://go.to.changelog' ],
  186. 'en',
  187. [ 'changelogURL' => 'https://go.to.changelog' ],
  188. ],
  189. [ #3 nothing
  190. [],
  191. 'ru',
  192. []
  193. ]
  194. ];
  195. }
  196. /**
  197. * @dataProvider changesProvider
  198. */
  199. public function testFilterChanges($changes, $userLang, $expectation) {
  200. $iterator = $this->createMock(ILanguageIterator::class);
  201. $iterator->expects($this->any())
  202. ->method('current')
  203. ->willReturnOnConsecutiveCalls('es', $userLang, 'it', 'en');
  204. $iterator->expects($this->any())
  205. ->method('valid')
  206. ->willReturn(true);
  207. $this->l10nFactory->expects($this->atMost(1))
  208. ->method('getLanguageIterator')
  209. ->willReturn($iterator);
  210. $result = $this->invokePrivate($this->admin, 'filterChanges', [$changes]);
  211. $this->assertSame($expectation, $result);
  212. }
  213. }