ajax_rename.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Joas Schilling <nickvergessen@owncloud.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. class Test_OC_Files_App_Rename extends \Test\TestCase {
  28. private static $user;
  29. /**
  30. * @var PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $viewMock;
  33. /**
  34. * @var \OCA\Files\App
  35. */
  36. private $files;
  37. protected function setUp() {
  38. parent::setUp();
  39. // mock OC_L10n
  40. if (!self::$user) {
  41. self::$user = uniqid();
  42. }
  43. \OC_User::createUser(self::$user, 'password');
  44. $this->loginAsUser(self::$user);
  45. $l10nMock = $this->getMock('\OC_L10N', array('t'), array(), '', false);
  46. $l10nMock->expects($this->any())
  47. ->method('t')
  48. ->will($this->returnArgument(0));
  49. $viewMock = $this->getMock('\OC\Files\View', array('rename', 'normalizePath', 'getFileInfo', 'file_exists'), array(), '', false);
  50. $viewMock->expects($this->any())
  51. ->method('normalizePath')
  52. ->will($this->returnArgument(0));
  53. $viewMock->expects($this->any())
  54. ->method('rename')
  55. ->will($this->returnValue(true));
  56. $this->viewMock = $viewMock;
  57. $this->files = new \OCA\Files\App($viewMock, $l10nMock);
  58. }
  59. protected function tearDown() {
  60. $result = \OC_User::deleteUser(self::$user);
  61. $this->assertTrue($result);
  62. $this->logout();
  63. parent::tearDown();
  64. }
  65. /**
  66. * test rename of file/folder
  67. */
  68. function testRenameFolder() {
  69. $dir = '/';
  70. $oldname = 'oldname';
  71. $newname = 'newname';
  72. $this->viewMock->expects($this->any())
  73. ->method('file_exists')
  74. ->with($this->anything())
  75. ->will($this->returnValueMap(array(
  76. array('/', true),
  77. array('/oldname', true)
  78. )));
  79. $this->viewMock->expects($this->any())
  80. ->method('getFileInfo')
  81. ->will($this->returnValue(new \OC\Files\FileInfo(
  82. '/new_name',
  83. new \OC\Files\Storage\Local(array('datadir' => '/')),
  84. '/',
  85. array(
  86. 'fileid' => 123,
  87. 'type' => 'dir',
  88. 'mimetype' => 'httpd/unix-directory',
  89. 'mtime' => 0,
  90. 'permissions' => 31,
  91. 'size' => 18,
  92. 'etag' => 'abcdef',
  93. 'directory' => '/',
  94. 'name' => 'new_name',
  95. ), null)));
  96. $result = $this->files->rename($dir, $oldname, $newname);
  97. $this->assertTrue($result['success']);
  98. $this->assertEquals(123, $result['data']['id']);
  99. $this->assertEquals('new_name', $result['data']['name']);
  100. $this->assertEquals(18, $result['data']['size']);
  101. $this->assertEquals('httpd/unix-directory', $result['data']['mimetype']);
  102. $this->assertEquals('abcdef', $result['data']['etag']);
  103. $this->assertFalse(isset($result['data']['tags']));
  104. $this->assertEquals('/', $result['data']['path']);
  105. $icon = \OC_Helper::mimetypeIcon('dir');
  106. $icon = substr($icon, 0, -3) . 'svg';
  107. $this->assertEquals($icon, $result['data']['icon']);
  108. }
  109. /**
  110. * test rename of file with tag
  111. */
  112. function testRenameFileWithTag() {
  113. $taggerMock = $this->getMock('\OCP\ITags');
  114. $taggerMock->expects($this->any())
  115. ->method('getTagsForObjects')
  116. ->with(array(123))
  117. ->will($this->returnValue(array(123 => array('tag1', 'tag2'))));
  118. $tagManagerMock = $this->getMock('\OCP\ITagManager');
  119. $tagManagerMock->expects($this->any())
  120. ->method('load')
  121. ->with('files')
  122. ->will($this->returnValue($taggerMock));
  123. $oldTagManager = \OC::$server->query('TagManager');
  124. \OC::$server->registerService('TagManager', function ($c) use ($tagManagerMock) {
  125. return $tagManagerMock;
  126. });
  127. $dir = '/';
  128. $oldname = 'oldname.txt';
  129. $newname = 'newname.txt';
  130. $this->viewMock->expects($this->any())
  131. ->method('file_exists')
  132. ->with($this->anything())
  133. ->will($this->returnValueMap(array(
  134. array('/', true),
  135. array('/oldname.txt', true)
  136. )));
  137. $this->viewMock->expects($this->any())
  138. ->method('getFileInfo')
  139. ->will($this->returnValue(new \OC\Files\FileInfo(
  140. '/new_name.txt',
  141. new \OC\Files\Storage\Local(array('datadir' => '/')),
  142. '/',
  143. array(
  144. 'fileid' => 123,
  145. 'type' => 'file',
  146. 'mimetype' => 'text/plain',
  147. 'mtime' => 0,
  148. 'permissions' => 31,
  149. 'size' => 18,
  150. 'etag' => 'abcdef',
  151. 'directory' => '/',
  152. 'name' => 'new_name.txt',
  153. ), null)));
  154. $result = $this->files->rename($dir, $oldname, $newname);
  155. $this->assertTrue($result['success']);
  156. $this->assertEquals(123, $result['data']['id']);
  157. $this->assertEquals('new_name.txt', $result['data']['name']);
  158. $this->assertEquals(18, $result['data']['size']);
  159. $this->assertEquals('text/plain', $result['data']['mimetype']);
  160. $this->assertEquals('abcdef', $result['data']['etag']);
  161. $this->assertEquals(array('tag1', 'tag2'), $result['data']['tags']);
  162. $this->assertEquals('/', $result['data']['path']);
  163. $icon = \OC_Helper::mimetypeIcon('text');
  164. $icon = substr($icon, 0, -3) . 'svg';
  165. $this->assertEquals($icon, $result['data']['icon']);
  166. \OC::$server->registerService('TagManager', function ($c) use ($oldTagManager) {
  167. return $oldTagManager;
  168. });
  169. }
  170. /**
  171. * Test rename inside a folder that doesn't exist any more
  172. */
  173. function testRenameInNonExistingFolder() {
  174. $dir = '/unexist';
  175. $oldname = 'oldname';
  176. $newname = 'newname';
  177. $this->viewMock->expects($this->at(0))
  178. ->method('file_exists')
  179. ->with('/unexist/oldname')
  180. ->will($this->returnValue(false));
  181. $this->viewMock->expects($this->any())
  182. ->method('getFileInfo')
  183. ->will($this->returnValue(array(
  184. 'fileid' => 123,
  185. 'type' => 'dir',
  186. 'mimetype' => 'httpd/unix-directory',
  187. 'size' => 18,
  188. 'etag' => 'abcdef',
  189. 'directory' => '/unexist',
  190. 'name' => 'new_name',
  191. )));
  192. $result = $this->files->rename($dir, $oldname, $newname);
  193. $this->assertFalse($result['success']);
  194. $this->assertEquals('sourcenotfound', $result['data']['code']);
  195. }
  196. /**
  197. * Test move to a folder that doesn't exist any more
  198. */
  199. function testRenameToNonExistingFolder() {
  200. $dir = '/';
  201. $oldname = 'oldname';
  202. $newname = '/unexist/newname';
  203. $this->viewMock->expects($this->any())
  204. ->method('file_exists')
  205. ->with($this->anything())
  206. ->will($this->returnValueMap(array(
  207. array('/oldname', true),
  208. array('/unexist', false)
  209. )));
  210. $this->viewMock->expects($this->any())
  211. ->method('getFileInfo')
  212. ->will($this->returnValue(array(
  213. 'fileid' => 123,
  214. 'type' => 'dir',
  215. 'mimetype' => 'httpd/unix-directory',
  216. 'size' => 18,
  217. 'etag' => 'abcdef',
  218. 'directory' => '/unexist',
  219. 'name' => 'new_name',
  220. )));
  221. $result = $this->files->rename($dir, $oldname, $newname);
  222. $this->assertFalse($result['success']);
  223. $this->assertEquals('targetnotfound', $result['data']['code']);
  224. }
  225. }