UpdateTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Encryption;
  22. use OC\Encryption\Update;
  23. use OC\Encryption\Util;
  24. use OC\Files\Mount\Manager;
  25. use OC\Files\View;
  26. use Psr\Log\LoggerInterface;
  27. use Test\TestCase;
  28. use OC\Encryption\File;
  29. use OCP\Encryption\IEncryptionModule;
  30. class UpdateTest extends TestCase {
  31. /** @var \OC\Encryption\Update */
  32. private $update;
  33. /** @var string */
  34. private $uid;
  35. /** @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject */
  36. private $view;
  37. /** @var Util | \PHPUnit\Framework\MockObject\MockObject */
  38. private $util;
  39. /** @var \OC\Files\Mount\Manager | \PHPUnit\Framework\MockObject\MockObject */
  40. private $mountManager;
  41. /** @var \OC\Encryption\Manager | \PHPUnit\Framework\MockObject\MockObject */
  42. private $encryptionManager;
  43. /** @var \OCP\Encryption\IEncryptionModule | \PHPUnit\Framework\MockObject\MockObject */
  44. private $encryptionModule;
  45. /** @var \OC\Encryption\File | \PHPUnit\Framework\MockObject\MockObject */
  46. private $fileHelper;
  47. /** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */
  48. private $logger;
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $this->view = $this->createMock(View::class);
  52. $this->util = $this->createMock(Util::class);
  53. $this->mountManager = $this->createMock(Manager::class);
  54. $this->encryptionManager = $this->createMock(\OC\Encryption\Manager::class);
  55. $this->fileHelper = $this->createMock(File::class);
  56. $this->encryptionModule = $this->createMock(IEncryptionModule::class);
  57. $this->logger = $this->createMock(LoggerInterface::class);
  58. $this->uid = 'testUser1';
  59. $this->update = new Update(
  60. $this->view,
  61. $this->util,
  62. $this->mountManager,
  63. $this->encryptionManager,
  64. $this->fileHelper,
  65. $this->logger,
  66. $this->uid);
  67. }
  68. /**
  69. * @dataProvider dataTestUpdate
  70. *
  71. * @param string $path
  72. * @param boolean $isDir
  73. * @param array $allFiles
  74. * @param integer $numberOfFiles
  75. */
  76. public function testUpdate($path, $isDir, $allFiles, $numberOfFiles) {
  77. $this->encryptionManager->expects($this->once())
  78. ->method('getEncryptionModule')
  79. ->willReturn($this->encryptionModule);
  80. $this->view->expects($this->once())
  81. ->method('is_dir')
  82. ->willReturn($isDir);
  83. if ($isDir) {
  84. $this->util->expects($this->once())
  85. ->method('getAllFiles')
  86. ->willReturn($allFiles);
  87. }
  88. $this->fileHelper->expects($this->exactly($numberOfFiles))
  89. ->method('getAccessList')
  90. ->willReturn(['users' => [], 'public' => false]);
  91. $this->encryptionModule->expects($this->exactly($numberOfFiles))
  92. ->method('update')
  93. ->willReturn(true);
  94. $this->update->update($path);
  95. }
  96. /**
  97. * data provider for testUpdate()
  98. *
  99. * @return array
  100. */
  101. public function dataTestUpdate() {
  102. return [
  103. ['/user/files/foo', true, ['/user/files/foo/file1.txt', '/user/files/foo/file1.txt'], 2],
  104. ['/user/files/test.txt', false, [], 1],
  105. ];
  106. }
  107. /**
  108. * @dataProvider dataTestPostRename
  109. *
  110. * @param string $source
  111. * @param string $target
  112. * @param boolean $encryptionEnabled
  113. */
  114. public function testPostRename($source, $target, $encryptionEnabled) {
  115. $updateMock = $this->getUpdateMock(['update', 'getOwnerPath']);
  116. $this->encryptionManager->expects($this->once())
  117. ->method('isEnabled')
  118. ->willReturn($encryptionEnabled);
  119. if (dirname($source) === dirname($target) || $encryptionEnabled === false) {
  120. $updateMock->expects($this->never())->method('getOwnerPath');
  121. $updateMock->expects($this->never())->method('update');
  122. } else {
  123. $updateMock->expects($this->once())
  124. ->method('getOwnerPath')
  125. ->willReturnCallback(function ($path) use ($target) {
  126. $this->assertSame(
  127. $target,
  128. $path,
  129. 'update needs to be executed for the target destination');
  130. return ['owner', $path];
  131. });
  132. $updateMock->expects($this->once())->method('update');
  133. }
  134. $updateMock->postRename(['oldpath' => $source, 'newpath' => $target]);
  135. }
  136. /**
  137. * test data for testPostRename()
  138. *
  139. * @return array
  140. */
  141. public function dataTestPostRename() {
  142. return [
  143. ['/test.txt', '/testNew.txt', true],
  144. ['/test.txt', '/testNew.txt', false],
  145. ['/folder/test.txt', '/testNew.txt', true],
  146. ['/folder/test.txt', '/testNew.txt', false],
  147. ['/folder/test.txt', '/testNew.txt', true],
  148. ['/test.txt', '/folder/testNew.txt', false],
  149. ];
  150. }
  151. /**
  152. * @dataProvider dataTestPostRestore
  153. *
  154. * @param boolean $encryptionEnabled
  155. */
  156. public function testPostRestore($encryptionEnabled) {
  157. $updateMock = $this->getUpdateMock(['update']);
  158. $this->encryptionManager->expects($this->once())
  159. ->method('isEnabled')
  160. ->willReturn($encryptionEnabled);
  161. if ($encryptionEnabled) {
  162. $updateMock->expects($this->once())->method('update');
  163. } else {
  164. $updateMock->expects($this->never())->method('update');
  165. }
  166. $updateMock->postRestore(['filePath' => '/folder/test.txt']);
  167. }
  168. /**
  169. * test data for testPostRestore()
  170. *
  171. * @return array
  172. */
  173. public function dataTestPostRestore() {
  174. return [
  175. [true],
  176. [false],
  177. ];
  178. }
  179. /**
  180. * create mock of the update method
  181. *
  182. * @param array$methods methods which should be set
  183. * @return \OC\Encryption\Update | \PHPUnit\Framework\MockObject\MockObject
  184. */
  185. protected function getUpdateMock($methods) {
  186. return $this->getMockBuilder('\OC\Encryption\Update')
  187. ->setConstructorArgs(
  188. [
  189. $this->view,
  190. $this->util,
  191. $this->mountManager,
  192. $this->encryptionManager,
  193. $this->fileHelper,
  194. $this->logger,
  195. $this->uid
  196. ]
  197. )->setMethods($methods)->getMock();
  198. }
  199. }