2013-05-07 15:37:20 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Christopher Schäpers <kondou@ts.unde.re>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2013-05-07 15:37:20 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
2013-05-07 15:37:20 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2013-05-07 15:37:20 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2013-05-07 15:37:20 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2013-05-07 15:37:20 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-05-07 15:37:20 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-11-11 00:28:12 +03:00
|
|
|
class Test_OC_Files_App_Rename extends \Test\TestCase {
|
2013-11-06 14:57:04 +04:00
|
|
|
private static $user;
|
2013-05-07 15:37:20 +04:00
|
|
|
|
2014-05-02 22:35:03 +04:00
|
|
|
/**
|
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
private $viewMock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCA\Files\App
|
|
|
|
*/
|
|
|
|
private $files;
|
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
private $originalStorage;
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->originalStorage = \OC\Files\Filesystem::getStorage('/');
|
|
|
|
|
2013-05-07 15:37:20 +04:00
|
|
|
// mock OC_L10n
|
2013-11-06 14:57:04 +04:00
|
|
|
if (!self::$user) {
|
|
|
|
self::$user = uniqid();
|
|
|
|
}
|
|
|
|
\OC_User::createUser(self::$user, 'password');
|
|
|
|
\OC_User::setUserId(self::$user);
|
|
|
|
|
|
|
|
\OC\Files\Filesystem::init(self::$user, '/' . self::$user . '/files');
|
|
|
|
|
2013-05-12 15:02:01 +04:00
|
|
|
$l10nMock = $this->getMock('\OC_L10N', array('t'), array(), '', false);
|
2013-05-07 15:37:20 +04:00
|
|
|
$l10nMock->expects($this->any())
|
|
|
|
->method('t')
|
|
|
|
->will($this->returnArgument(0));
|
2014-01-22 20:17:58 +04:00
|
|
|
$viewMock = $this->getMock('\OC\Files\View', array('rename', 'normalizePath', 'getFileInfo', 'file_exists'), array(), '', false);
|
2013-05-07 15:37:20 +04:00
|
|
|
$viewMock->expects($this->any())
|
|
|
|
->method('normalizePath')
|
|
|
|
->will($this->returnArgument(0));
|
|
|
|
$viewMock->expects($this->any())
|
|
|
|
->method('rename')
|
|
|
|
->will($this->returnValue(true));
|
2013-11-28 02:19:57 +04:00
|
|
|
$this->viewMock = $viewMock;
|
2013-05-08 13:56:59 +04:00
|
|
|
$this->files = new \OCA\Files\App($viewMock, $l10nMock);
|
2013-05-07 15:37:20 +04:00
|
|
|
}
|
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
protected function tearDown() {
|
2013-11-06 14:57:04 +04:00
|
|
|
$result = \OC_User::deleteUser(self::$user);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
\OC\Files\Filesystem::tearDown();
|
2014-11-12 17:54:41 +03:00
|
|
|
\OC\Files\Filesystem::mount($this->originalStorage, array(), '/');
|
|
|
|
|
|
|
|
parent::tearDown();
|
2013-11-06 14:57:04 +04:00
|
|
|
}
|
|
|
|
|
2013-05-07 17:24:26 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* test rename of file/folder
|
2013-05-07 17:24:26 +04:00
|
|
|
*/
|
|
|
|
function testRenameFolder() {
|
|
|
|
$dir = '/';
|
|
|
|
$oldname = 'oldname';
|
|
|
|
$newname = 'newname';
|
|
|
|
|
2014-08-11 14:02:33 +04:00
|
|
|
$this->viewMock->expects($this->any())
|
2014-01-22 20:17:58 +04:00
|
|
|
->method('file_exists')
|
2014-08-11 14:02:33 +04:00
|
|
|
->with($this->anything())
|
|
|
|
->will($this->returnValueMap(array(
|
|
|
|
array('/', true),
|
|
|
|
array('/oldname', true)
|
|
|
|
)));
|
|
|
|
|
2014-01-22 20:17:58 +04:00
|
|
|
|
2013-11-28 02:19:57 +04:00
|
|
|
$this->viewMock->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
2013-10-28 23:22:06 +04:00
|
|
|
->will($this->returnValue(new \OC\Files\FileInfo(
|
2014-06-03 17:02:42 +04:00
|
|
|
'/new_name',
|
2014-05-02 22:35:03 +04:00
|
|
|
new \OC\Files\Storage\Local(array('datadir' => '/')),
|
2013-10-28 23:22:06 +04:00
|
|
|
'/',
|
|
|
|
array(
|
2013-11-28 02:19:57 +04:00
|
|
|
'fileid' => 123,
|
|
|
|
'type' => 'dir',
|
|
|
|
'mimetype' => 'httpd/unix-directory',
|
2013-10-28 23:22:06 +04:00
|
|
|
'mtime' => 0,
|
|
|
|
'permissions' => 31,
|
2013-11-28 02:19:57 +04:00
|
|
|
'size' => 18,
|
|
|
|
'etag' => 'abcdef',
|
|
|
|
'directory' => '/',
|
|
|
|
'name' => 'new_name',
|
2014-12-16 16:24:48 +03:00
|
|
|
), null)));
|
2013-11-28 02:19:57 +04:00
|
|
|
|
2013-05-07 17:24:26 +04:00
|
|
|
$result = $this->files->rename($dir, $oldname, $newname);
|
|
|
|
|
2013-11-28 02:19:57 +04:00
|
|
|
$this->assertTrue($result['success']);
|
|
|
|
$this->assertEquals(123, $result['data']['id']);
|
2013-10-28 23:22:06 +04:00
|
|
|
$this->assertEquals('new_name', $result['data']['name']);
|
2013-11-28 02:19:57 +04:00
|
|
|
$this->assertEquals(18, $result['data']['size']);
|
2013-10-28 23:22:06 +04:00
|
|
|
$this->assertEquals('httpd/unix-directory', $result['data']['mimetype']);
|
2013-11-28 02:19:57 +04:00
|
|
|
$this->assertEquals('abcdef', $result['data']['etag']);
|
2015-01-12 16:01:04 +03:00
|
|
|
$this->assertFalse(isset($result['data']['tags']));
|
2015-01-12 16:01:40 +03:00
|
|
|
$this->assertEquals('/', $result['data']['path']);
|
2014-02-20 19:56:09 +04:00
|
|
|
$icon = \OC_Helper::mimetypeIcon('dir');
|
|
|
|
$icon = substr($icon, 0, -3) . 'svg';
|
|
|
|
$this->assertEquals($icon, $result['data']['icon']);
|
2013-05-07 17:24:26 +04:00
|
|
|
}
|
2014-01-22 20:17:58 +04:00
|
|
|
|
2015-01-12 16:01:04 +03:00
|
|
|
/**
|
|
|
|
* test rename of file with tag
|
|
|
|
*/
|
|
|
|
function testRenameFileWithTag() {
|
|
|
|
$taggerMock = $this->getMock('\OCP\ITags');
|
|
|
|
$taggerMock->expects($this->any())
|
|
|
|
->method('getTagsForObjects')
|
|
|
|
->with(array(123))
|
|
|
|
->will($this->returnValue(array(123 => array('tag1', 'tag2'))));
|
|
|
|
$tagManagerMock = $this->getMock('\OCP\ITagManager');
|
|
|
|
$tagManagerMock->expects($this->any())
|
|
|
|
->method('load')
|
|
|
|
->with('files')
|
|
|
|
->will($this->returnValue($taggerMock));
|
|
|
|
$oldTagManager = \OC::$server->query('TagManager');
|
|
|
|
\OC::$server->registerService('TagManager', function ($c) use ($tagManagerMock) {
|
|
|
|
return $tagManagerMock;
|
|
|
|
});
|
|
|
|
|
|
|
|
$dir = '/';
|
|
|
|
$oldname = 'oldname.txt';
|
|
|
|
$newname = 'newname.txt';
|
|
|
|
|
|
|
|
$this->viewMock->expects($this->any())
|
|
|
|
->method('file_exists')
|
|
|
|
->with($this->anything())
|
|
|
|
->will($this->returnValueMap(array(
|
|
|
|
array('/', true),
|
|
|
|
array('/oldname.txt', true)
|
|
|
|
)));
|
|
|
|
|
|
|
|
|
|
|
|
$this->viewMock->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue(new \OC\Files\FileInfo(
|
|
|
|
'/new_name.txt',
|
|
|
|
new \OC\Files\Storage\Local(array('datadir' => '/')),
|
|
|
|
'/',
|
|
|
|
array(
|
|
|
|
'fileid' => 123,
|
|
|
|
'type' => 'file',
|
|
|
|
'mimetype' => 'text/plain',
|
|
|
|
'mtime' => 0,
|
|
|
|
'permissions' => 31,
|
|
|
|
'size' => 18,
|
|
|
|
'etag' => 'abcdef',
|
|
|
|
'directory' => '/',
|
|
|
|
'name' => 'new_name.txt',
|
|
|
|
), null)));
|
|
|
|
|
|
|
|
$result = $this->files->rename($dir, $oldname, $newname);
|
|
|
|
|
|
|
|
$this->assertTrue($result['success']);
|
|
|
|
$this->assertEquals(123, $result['data']['id']);
|
|
|
|
$this->assertEquals('new_name.txt', $result['data']['name']);
|
|
|
|
$this->assertEquals(18, $result['data']['size']);
|
|
|
|
$this->assertEquals('text/plain', $result['data']['mimetype']);
|
|
|
|
$this->assertEquals('abcdef', $result['data']['etag']);
|
|
|
|
$this->assertEquals(array('tag1', 'tag2'), $result['data']['tags']);
|
2015-01-12 16:01:40 +03:00
|
|
|
$this->assertEquals('/', $result['data']['path']);
|
2015-01-12 16:01:04 +03:00
|
|
|
$icon = \OC_Helper::mimetypeIcon('text');
|
|
|
|
$icon = substr($icon, 0, -3) . 'svg';
|
|
|
|
$this->assertEquals($icon, $result['data']['icon']);
|
|
|
|
|
|
|
|
\OC::$server->registerService('TagManager', function ($c) use ($oldTagManager) {
|
|
|
|
return $oldTagManager;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-22 20:17:58 +04:00
|
|
|
/**
|
|
|
|
* Test rename inside a folder that doesn't exist any more
|
|
|
|
*/
|
|
|
|
function testRenameInNonExistingFolder() {
|
|
|
|
$dir = '/unexist';
|
|
|
|
$oldname = 'oldname';
|
|
|
|
$newname = 'newname';
|
|
|
|
|
|
|
|
$this->viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
2014-08-11 14:02:33 +04:00
|
|
|
->with('/unexist/oldname')
|
2014-01-22 20:17:58 +04:00
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->viewMock->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue(array(
|
|
|
|
'fileid' => 123,
|
|
|
|
'type' => 'dir',
|
|
|
|
'mimetype' => 'httpd/unix-directory',
|
|
|
|
'size' => 18,
|
|
|
|
'etag' => 'abcdef',
|
|
|
|
'directory' => '/unexist',
|
|
|
|
'name' => 'new_name',
|
|
|
|
)));
|
|
|
|
|
|
|
|
$result = $this->files->rename($dir, $oldname, $newname);
|
|
|
|
|
2014-08-11 14:02:33 +04:00
|
|
|
$this->assertFalse($result['success']);
|
|
|
|
$this->assertEquals('sourcenotfound', $result['data']['code']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test move to a folder that doesn't exist any more
|
|
|
|
*/
|
|
|
|
function testRenameToNonExistingFolder() {
|
|
|
|
$dir = '/';
|
|
|
|
$oldname = 'oldname';
|
|
|
|
$newname = '/unexist/newname';
|
|
|
|
|
|
|
|
$this->viewMock->expects($this->any())
|
|
|
|
->method('file_exists')
|
|
|
|
->with($this->anything())
|
|
|
|
->will($this->returnValueMap(array(
|
|
|
|
array('/oldname', true),
|
|
|
|
array('/unexist', false)
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->viewMock->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue(array(
|
|
|
|
'fileid' => 123,
|
|
|
|
'type' => 'dir',
|
|
|
|
'mimetype' => 'httpd/unix-directory',
|
|
|
|
'size' => 18,
|
|
|
|
'etag' => 'abcdef',
|
|
|
|
'directory' => '/unexist',
|
|
|
|
'name' => 'new_name',
|
|
|
|
)));
|
|
|
|
|
|
|
|
$result = $this->files->rename($dir, $oldname, $newname);
|
|
|
|
|
2014-01-22 20:17:58 +04:00
|
|
|
$this->assertFalse($result['success']);
|
|
|
|
$this->assertEquals('targetnotfound', $result['data']['code']);
|
|
|
|
}
|
2013-08-18 13:02:08 +04:00
|
|
|
}
|