SecurityTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings\Tests\Settings\Admin;
  7. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  8. use OC\Encryption\Manager;
  9. use OCA\Settings\Settings\Admin\Security;
  10. use OCP\AppFramework\Http\TemplateResponse;
  11. use OCP\AppFramework\Services\IInitialState;
  12. use OCP\IURLGenerator;
  13. use OCP\IUserManager;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use Test\TestCase;
  16. class SecurityTest extends TestCase {
  17. /** @var Security */
  18. private $admin;
  19. /** @var Manager */
  20. private $manager;
  21. /** @var IUserManager */
  22. private $userManager;
  23. /** @var MandatoryTwoFactor|MockObject */
  24. private $mandatoryTwoFactor;
  25. /** @var IInitialState|MockObject */
  26. private $initialState;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->manager = $this->getMockBuilder(Manager::class)->disableOriginalConstructor()->getMock();
  30. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  31. $this->mandatoryTwoFactor = $this->createMock(MandatoryTwoFactor::class);
  32. $this->initialState = $this->createMock(IInitialState::class);
  33. $this->admin = new Security(
  34. $this->manager,
  35. $this->userManager,
  36. $this->mandatoryTwoFactor,
  37. $this->initialState,
  38. $this->createMock(IURLGenerator::class)
  39. );
  40. }
  41. /**
  42. * @return array
  43. */
  44. public function encryptionSettingsProvider() {
  45. return [
  46. [true],
  47. [false],
  48. ];
  49. }
  50. /**
  51. * @dataProvider encryptionSettingsProvider
  52. * @param bool $enabled
  53. */
  54. public function testGetFormWithOnlyOneBackend($enabled) {
  55. $this->manager
  56. ->expects($this->once())
  57. ->method('isEnabled')
  58. ->willReturn($enabled);
  59. $this->manager
  60. ->expects($this->once())
  61. ->method('isReady')
  62. ->willReturn($enabled);
  63. $this->manager
  64. ->expects($this->once())
  65. ->method('getEncryptionModules')
  66. ->willReturn([]);
  67. $this->userManager
  68. ->expects($this->once())
  69. ->method('getBackends')
  70. ->willReturn(['entry']);
  71. $expected = new TemplateResponse(
  72. 'settings',
  73. 'settings/admin/security',
  74. [],
  75. ''
  76. );
  77. $this->assertEquals($expected, $this->admin->getForm());
  78. }
  79. /**
  80. * @dataProvider encryptionSettingsProvider
  81. * @param bool $enabled
  82. */
  83. public function testGetFormWithMultipleBackends($enabled) {
  84. $this->manager
  85. ->expects($this->once())
  86. ->method('isEnabled')
  87. ->willReturn($enabled);
  88. $this->manager
  89. ->expects($this->once())
  90. ->method('isReady')
  91. ->willReturn($enabled);
  92. $this->manager
  93. ->expects($this->once())
  94. ->method('getEncryptionModules')
  95. ->willReturn([]);
  96. $this->userManager
  97. ->expects($this->once())
  98. ->method('getBackends')
  99. ->willReturn(['entry', 'entry']);
  100. $expected = new TemplateResponse(
  101. 'settings',
  102. 'settings/admin/security',
  103. [ ],
  104. ''
  105. );
  106. $this->assertEquals($expected, $this->admin->getForm());
  107. }
  108. public function testGetSection() {
  109. $this->assertSame('security', $this->admin->getSection());
  110. }
  111. public function testGetPriority() {
  112. $this->assertSame(10, $this->admin->getPriority());
  113. }
  114. }