1
0

UpdateTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Encryption;
  8. use OC\Encryption\File;
  9. use OC\Encryption\Update;
  10. use OC\Encryption\Util;
  11. use OC\Files\Mount\Manager;
  12. use OC\Files\View;
  13. use OCP\Encryption\IEncryptionModule;
  14. use Psr\Log\LoggerInterface;
  15. use Test\TestCase;
  16. class UpdateTest extends TestCase {
  17. /** @var \OC\Encryption\Update */
  18. private $update;
  19. /** @var string */
  20. private $uid;
  21. /** @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject */
  22. private $view;
  23. /** @var Util | \PHPUnit\Framework\MockObject\MockObject */
  24. private $util;
  25. /** @var \OC\Files\Mount\Manager | \PHPUnit\Framework\MockObject\MockObject */
  26. private $mountManager;
  27. /** @var \OC\Encryption\Manager | \PHPUnit\Framework\MockObject\MockObject */
  28. private $encryptionManager;
  29. /** @var \OCP\Encryption\IEncryptionModule | \PHPUnit\Framework\MockObject\MockObject */
  30. private $encryptionModule;
  31. /** @var \OC\Encryption\File | \PHPUnit\Framework\MockObject\MockObject */
  32. private $fileHelper;
  33. /** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */
  34. private $logger;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->view = $this->createMock(View::class);
  38. $this->util = $this->createMock(Util::class);
  39. $this->mountManager = $this->createMock(Manager::class);
  40. $this->encryptionManager = $this->createMock(\OC\Encryption\Manager::class);
  41. $this->fileHelper = $this->createMock(File::class);
  42. $this->encryptionModule = $this->createMock(IEncryptionModule::class);
  43. $this->logger = $this->createMock(LoggerInterface::class);
  44. $this->uid = 'testUser1';
  45. $this->update = new Update(
  46. $this->view,
  47. $this->util,
  48. $this->mountManager,
  49. $this->encryptionManager,
  50. $this->fileHelper,
  51. $this->logger,
  52. $this->uid);
  53. }
  54. /**
  55. * @dataProvider dataTestUpdate
  56. *
  57. * @param string $path
  58. * @param boolean $isDir
  59. * @param array $allFiles
  60. * @param integer $numberOfFiles
  61. */
  62. public function testUpdate($path, $isDir, $allFiles, $numberOfFiles): void {
  63. $this->encryptionManager->expects($this->once())
  64. ->method('getEncryptionModule')
  65. ->willReturn($this->encryptionModule);
  66. $this->view->expects($this->once())
  67. ->method('is_dir')
  68. ->willReturn($isDir);
  69. if ($isDir) {
  70. $this->util->expects($this->once())
  71. ->method('getAllFiles')
  72. ->willReturn($allFiles);
  73. }
  74. $this->fileHelper->expects($this->exactly($numberOfFiles))
  75. ->method('getAccessList')
  76. ->willReturn(['users' => [], 'public' => false]);
  77. $this->encryptionModule->expects($this->exactly($numberOfFiles))
  78. ->method('update')
  79. ->willReturn(true);
  80. $this->update->update($path);
  81. }
  82. /**
  83. * data provider for testUpdate()
  84. *
  85. * @return array
  86. */
  87. public function dataTestUpdate() {
  88. return [
  89. ['/user/files/foo', true, ['/user/files/foo/file1.txt', '/user/files/foo/file1.txt'], 2],
  90. ['/user/files/test.txt', false, [], 1],
  91. ];
  92. }
  93. /**
  94. * @dataProvider dataTestPostRename
  95. *
  96. * @param string $source
  97. * @param string $target
  98. * @param boolean $encryptionEnabled
  99. */
  100. public function testPostRename($source, $target, $encryptionEnabled): void {
  101. $updateMock = $this->getUpdateMock(['update', 'getOwnerPath']);
  102. $this->encryptionManager->expects($this->once())
  103. ->method('isEnabled')
  104. ->willReturn($encryptionEnabled);
  105. if (dirname($source) === dirname($target) || $encryptionEnabled === false) {
  106. $updateMock->expects($this->never())->method('getOwnerPath');
  107. $updateMock->expects($this->never())->method('update');
  108. } else {
  109. $updateMock->expects($this->once())
  110. ->method('getOwnerPath')
  111. ->willReturnCallback(function ($path) use ($target) {
  112. $this->assertSame(
  113. $target,
  114. $path,
  115. 'update needs to be executed for the target destination');
  116. return ['owner', $path];
  117. });
  118. $updateMock->expects($this->once())->method('update');
  119. }
  120. $updateMock->postRename(['oldpath' => $source, 'newpath' => $target]);
  121. }
  122. /**
  123. * test data for testPostRename()
  124. *
  125. * @return array
  126. */
  127. public function dataTestPostRename() {
  128. return [
  129. ['/test.txt', '/testNew.txt', true],
  130. ['/test.txt', '/testNew.txt', false],
  131. ['/folder/test.txt', '/testNew.txt', true],
  132. ['/folder/test.txt', '/testNew.txt', false],
  133. ['/folder/test.txt', '/testNew.txt', true],
  134. ['/test.txt', '/folder/testNew.txt', false],
  135. ];
  136. }
  137. /**
  138. * @dataProvider dataTestPostRestore
  139. *
  140. * @param boolean $encryptionEnabled
  141. */
  142. public function testPostRestore($encryptionEnabled): void {
  143. $updateMock = $this->getUpdateMock(['update']);
  144. $this->encryptionManager->expects($this->once())
  145. ->method('isEnabled')
  146. ->willReturn($encryptionEnabled);
  147. if ($encryptionEnabled) {
  148. $updateMock->expects($this->once())->method('update');
  149. } else {
  150. $updateMock->expects($this->never())->method('update');
  151. }
  152. $updateMock->postRestore(['filePath' => '/folder/test.txt']);
  153. }
  154. /**
  155. * test data for testPostRestore()
  156. *
  157. * @return array
  158. */
  159. public function dataTestPostRestore() {
  160. return [
  161. [true],
  162. [false],
  163. ];
  164. }
  165. /**
  166. * create mock of the update method
  167. *
  168. * @param array $methods methods which should be set
  169. * @return \OC\Encryption\Update | \PHPUnit\Framework\MockObject\MockObject
  170. */
  171. protected function getUpdateMock($methods) {
  172. return $this->getMockBuilder('\OC\Encryption\Update')
  173. ->setConstructorArgs(
  174. [
  175. $this->view,
  176. $this->util,
  177. $this->mountManager,
  178. $this->encryptionManager,
  179. $this->fileHelper,
  180. $this->logger,
  181. $this->uid
  182. ]
  183. )->setMethods($methods)->getMock();
  184. }
  185. }