1
0

UpdateTest.php 6.2 KB

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