UtilTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Clark Tomlinson <fallen013@gmail.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Encryption\Tests;
  25. use OC\Files\View;
  26. use OCA\Encryption\Util;
  27. use OCP\Files\Mount\IMountPoint;
  28. use OCP\IConfig;
  29. use OCP\ILogger;
  30. use OCP\IUserManager;
  31. use Test\TestCase;
  32. class UtilTest extends TestCase {
  33. private static $tempStorage = [];
  34. /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
  35. private $configMock;
  36. /** @var \OC\Files\View|\PHPUnit_Framework_MockObject_MockObject */
  37. private $filesMock;
  38. /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  39. private $userManagerMock;
  40. /** @var \OCP\Files\Mount\IMountPoint|\PHPUnit_Framework_MockObject_MockObject */
  41. private $mountMock;
  42. /** @var Util */
  43. private $instance;
  44. public function testSetRecoveryForUser() {
  45. $this->instance->setRecoveryForUser('1');
  46. $this->assertArrayHasKey('recoveryEnabled', self::$tempStorage);
  47. }
  48. public function testIsRecoveryEnabledForUser() {
  49. $this->assertTrue($this->instance->isRecoveryEnabledForUser('admin'));
  50. // Assert recovery will return default value if not set
  51. unset(self::$tempStorage['recoveryEnabled']);
  52. $this->assertEquals(0, $this->instance->isRecoveryEnabledForUser('admin'));
  53. }
  54. public function testUserHasFiles() {
  55. $this->filesMock->expects($this->once())
  56. ->method('file_exists')
  57. ->will($this->returnValue(true));
  58. $this->assertTrue($this->instance->userHasFiles('admin'));
  59. }
  60. protected function setUp() {
  61. parent::setUp();
  62. $this->mountMock = $this->createMock(IMountPoint::class);
  63. $this->filesMock = $this->createMock(View::class);
  64. $this->userManagerMock = $this->createMock(IUserManager::class);
  65. /** @var \OCA\Encryption\Crypto\Crypt $cryptMock */
  66. $cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. /** @var \OCP\ILogger $loggerMock */
  70. $loggerMock = $this->createMock(ILogger::class);
  71. /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSessionMock */
  72. $userSessionMock = $this->getMockBuilder('OCP\IUserSession')
  73. ->disableOriginalConstructor()
  74. ->setMethods([
  75. 'isLoggedIn',
  76. 'getUID',
  77. 'login',
  78. 'logout',
  79. 'setUser',
  80. 'getUser'
  81. ])
  82. ->getMock();
  83. $userSessionMock->method('isLoggedIn')->will($this->returnValue(true));
  84. $userSessionMock->method('getUID')->will($this->returnValue('admin'));
  85. $userSessionMock->expects($this->any())
  86. ->method($this->anything())
  87. ->will($this->returnSelf());
  88. $this->configMock = $this->createMock(IConfig::class);
  89. $this->configMock->expects($this->any())
  90. ->method('getUserValue')
  91. ->will($this->returnCallback([$this, 'getValueTester']));
  92. $this->configMock->expects($this->any())
  93. ->method('setUserValue')
  94. ->will($this->returnCallback([$this, 'setValueTester']));
  95. $this->instance = new Util($this->filesMock, $cryptMock, $loggerMock, $userSessionMock, $this->configMock, $this->userManagerMock);
  96. }
  97. /**
  98. * @param $userId
  99. * @param $app
  100. * @param $key
  101. * @param $value
  102. */
  103. public function setValueTester($userId, $app, $key, $value) {
  104. self::$tempStorage[$key] = $value;
  105. }
  106. /**
  107. * @param $userId
  108. * @param $app
  109. * @param $key
  110. * @param $default
  111. * @return mixed
  112. */
  113. public function getValueTester($userId, $app, $key, $default) {
  114. if (!empty(self::$tempStorage[$key])) {
  115. return self::$tempStorage[$key];
  116. }
  117. return $default ?: null;
  118. }
  119. /**
  120. * @dataProvider dataTestIsMasterKeyEnabled
  121. *
  122. * @param string $value
  123. * @param bool $expect
  124. */
  125. public function testIsMasterKeyEnabled($value, $expect) {
  126. $this->configMock->expects($this->once())->method('getAppValue')
  127. ->with('encryption', 'useMasterKey', '0')->willReturn($value);
  128. $this->assertSame($expect,
  129. $this->instance->isMasterKeyEnabled()
  130. );
  131. }
  132. public function dataTestIsMasterKeyEnabled() {
  133. return [
  134. ['0', false],
  135. ['1', true]
  136. ];
  137. }
  138. /**
  139. * @dataProvider dataTestShouldEncryptHomeStorage
  140. * @param string $returnValue return value from getAppValue()
  141. * @param bool $expected
  142. */
  143. public function testShouldEncryptHomeStorage($returnValue, $expected) {
  144. $this->configMock->expects($this->once())->method('getAppValue')
  145. ->with('encryption', 'encryptHomeStorage', '1')
  146. ->willReturn($returnValue);
  147. $this->assertSame($expected,
  148. $this->instance->shouldEncryptHomeStorage());
  149. }
  150. public function dataTestShouldEncryptHomeStorage() {
  151. return [
  152. ['1', true],
  153. ['0', false]
  154. ];
  155. }
  156. /**
  157. * @dataProvider dataTestSetEncryptHomeStorage
  158. * @param $value
  159. * @param $expected
  160. */
  161. public function testSetEncryptHomeStorage($value, $expected) {
  162. $this->configMock->expects($this->once())->method('setAppValue')
  163. ->with('encryption', 'encryptHomeStorage', $expected);
  164. $this->instance->setEncryptHomeStorage($value);
  165. }
  166. public function dataTestSetEncryptHomeStorage() {
  167. return [
  168. [true, '1'],
  169. [false, '0']
  170. ];
  171. }
  172. public function testGetStorage() {
  173. $return = $this->getMockBuilder('OC\Files\Storage\Storage')
  174. ->disableOriginalConstructor()
  175. ->getMock();
  176. $path = '/foo/bar.txt';
  177. $this->filesMock->expects($this->once())->method('getMount')->with($path)
  178. ->willReturn($this->mountMock);
  179. $this->mountMock->expects($this->once())->method('getStorage')->willReturn($return);
  180. $this->assertEquals($return, $this->instance->getStorage($path));
  181. }
  182. }