UpdateTest.php 6.2 KB

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