UtilTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\Tests;
  8. use OC\Files\View;
  9. use OCA\Encryption\Crypto\Crypt;
  10. use OCA\Encryption\Util;
  11. use OCP\Files\Mount\IMountPoint;
  12. use OCP\Files\Storage\IStorage;
  13. use OCP\IConfig;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. use OCP\IUserSession;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use Test\TestCase;
  19. class UtilTest extends TestCase {
  20. private static $tempStorage = [];
  21. /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  22. private $configMock;
  23. /** @var \OC\Files\View|\PHPUnit\Framework\MockObject\MockObject */
  24. private $filesMock;
  25. /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  26. private $userManagerMock;
  27. /** @var \OCP\Files\Mount\IMountPoint|\PHPUnit\Framework\MockObject\MockObject */
  28. private $mountMock;
  29. /** @var Util */
  30. private $instance;
  31. public function testSetRecoveryForUser(): void {
  32. $this->instance->setRecoveryForUser('1');
  33. $this->assertArrayHasKey('recoveryEnabled', self::$tempStorage);
  34. }
  35. public function testIsRecoveryEnabledForUser(): void {
  36. $this->assertTrue($this->instance->isRecoveryEnabledForUser('admin'));
  37. // Assert recovery will return default value if not set
  38. unset(self::$tempStorage['recoveryEnabled']);
  39. $this->assertEquals(0, $this->instance->isRecoveryEnabledForUser('admin'));
  40. }
  41. public function testUserHasFiles(): void {
  42. $this->filesMock->expects($this->once())
  43. ->method('file_exists')
  44. ->willReturn(true);
  45. $this->assertTrue($this->instance->userHasFiles('admin'));
  46. }
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->mountMock = $this->createMock(IMountPoint::class);
  50. $this->filesMock = $this->createMock(View::class);
  51. $this->userManagerMock = $this->createMock(IUserManager::class);
  52. /** @var \OCA\Encryption\Crypto\Crypt $cryptMock */
  53. $cryptMock = $this->getMockBuilder(Crypt::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $user = $this->createMock(IUser::class);
  57. $user->expects($this->any())
  58. ->method('getUID')
  59. ->willReturn('admin');
  60. /** @var IUserSession|MockObject $userSessionMock */
  61. $userSessionMock = $this->createMock(IUserSession::class);
  62. $userSessionMock->expects($this->any())
  63. ->method('getUser')
  64. ->willReturn($user);
  65. $userSessionMock->expects($this->any())
  66. ->method('isLoggedIn')
  67. ->willReturn(true);
  68. $this->configMock = $this->createMock(IConfig::class);
  69. $this->configMock->expects($this->any())
  70. ->method('getUserValue')
  71. ->willReturnCallback([$this, 'getValueTester']);
  72. $this->configMock->expects($this->any())
  73. ->method('setUserValue')
  74. ->willReturnCallback([$this, 'setValueTester']);
  75. $this->instance = new Util($this->filesMock, $cryptMock, $userSessionMock, $this->configMock, $this->userManagerMock);
  76. }
  77. /**
  78. * @param $userId
  79. * @param $app
  80. * @param $key
  81. * @param $value
  82. */
  83. public function setValueTester($userId, $app, $key, $value) {
  84. self::$tempStorage[$key] = $value;
  85. }
  86. /**
  87. * @param $userId
  88. * @param $app
  89. * @param $key
  90. * @param $default
  91. * @return mixed
  92. */
  93. public function getValueTester($userId, $app, $key, $default) {
  94. if (!empty(self::$tempStorage[$key])) {
  95. return self::$tempStorage[$key];
  96. }
  97. return $default ?: null;
  98. }
  99. /**
  100. * @dataProvider dataTestIsMasterKeyEnabled
  101. *
  102. * @param string $value
  103. * @param bool $expect
  104. */
  105. public function testIsMasterKeyEnabled($value, $expect): void {
  106. $this->configMock->expects($this->once())->method('getAppValue')
  107. ->with('encryption', 'useMasterKey', '1')->willReturn($value);
  108. $this->assertSame($expect,
  109. $this->instance->isMasterKeyEnabled()
  110. );
  111. }
  112. public function dataTestIsMasterKeyEnabled() {
  113. return [
  114. ['0', false],
  115. ['1', true]
  116. ];
  117. }
  118. /**
  119. * @dataProvider dataTestShouldEncryptHomeStorage
  120. * @param string $returnValue return value from getAppValue()
  121. * @param bool $expected
  122. */
  123. public function testShouldEncryptHomeStorage($returnValue, $expected): void {
  124. $this->configMock->expects($this->once())->method('getAppValue')
  125. ->with('encryption', 'encryptHomeStorage', '1')
  126. ->willReturn($returnValue);
  127. $this->assertSame($expected,
  128. $this->instance->shouldEncryptHomeStorage());
  129. }
  130. public function dataTestShouldEncryptHomeStorage() {
  131. return [
  132. ['1', true],
  133. ['0', false]
  134. ];
  135. }
  136. /**
  137. * @dataProvider dataTestSetEncryptHomeStorage
  138. * @param $value
  139. * @param $expected
  140. */
  141. public function testSetEncryptHomeStorage($value, $expected): void {
  142. $this->configMock->expects($this->once())->method('setAppValue')
  143. ->with('encryption', 'encryptHomeStorage', $expected);
  144. $this->instance->setEncryptHomeStorage($value);
  145. }
  146. public function dataTestSetEncryptHomeStorage() {
  147. return [
  148. [true, '1'],
  149. [false, '0']
  150. ];
  151. }
  152. public function testGetStorage(): void {
  153. $return = $this->getMockBuilder(IStorage::class)
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $path = '/foo/bar.txt';
  157. $this->filesMock->expects($this->once())->method('getMount')->with($path)
  158. ->willReturn($this->mountMock);
  159. $this->mountMock->expects($this->once())->method('getStorage')->willReturn($return);
  160. $this->assertEquals($return, $this->instance->getStorage($path));
  161. }
  162. }