2013-05-07 15:37:20 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ownCloud - Core
|
|
|
|
*
|
|
|
|
* @author Morris Jobke
|
|
|
|
* @copyright 2013 Morris Jobke morris.jobke@gmail.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-05-08 13:56:59 +04:00
|
|
|
class Test_OC_Files_App_Rename extends \PHPUnit_Framework_TestCase {
|
2013-11-06 14:57:04 +04:00
|
|
|
private static $user;
|
2013-05-07 15:37:20 +04:00
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2013-11-06 14:57:04 +04:00
|
|
|
function tearDown() {
|
|
|
|
$result = \OC_User::deleteUser(self::$user);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
\OC\Files\Filesystem::tearDown();
|
|
|
|
}
|
|
|
|
|
2013-05-07 15:37:20 +04:00
|
|
|
/**
|
|
|
|
* @brief test rename of file/folder named "Shared"
|
|
|
|
*/
|
|
|
|
function testRenameSharedFolder() {
|
|
|
|
$dir = '/';
|
|
|
|
$oldname = 'Shared';
|
|
|
|
$newname = 'new_name';
|
|
|
|
|
2014-01-22 20:17:58 +04:00
|
|
|
$this->viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
|
|
|
->with('/')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2013-05-07 15:37:20 +04:00
|
|
|
$result = $this->files->rename($dir, $oldname, $newname);
|
|
|
|
$expected = array(
|
|
|
|
'success' => false,
|
2013-05-03 02:15:28 +04:00
|
|
|
'data' => array('message' => '%s could not be renamed')
|
2013-05-07 15:37:20 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test rename of file/folder named "Shared"
|
|
|
|
*/
|
|
|
|
function testRenameSharedFolderInSubdirectory() {
|
|
|
|
$dir = '/test';
|
|
|
|
$oldname = 'Shared';
|
|
|
|
$newname = 'new_name';
|
|
|
|
|
2014-01-22 20:17:58 +04:00
|
|
|
$this->viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
|
|
|
->with('/test')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2013-11-28 02:19:57 +04:00
|
|
|
$this->viewMock->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue(array(
|
|
|
|
'fileid' => 123,
|
|
|
|
'type' => 'dir',
|
|
|
|
'mimetype' => 'httpd/unix-directory',
|
|
|
|
'size' => 18,
|
|
|
|
'etag' => 'abcdef',
|
|
|
|
'directory' => '/',
|
|
|
|
'name' => 'new_name',
|
|
|
|
)));
|
|
|
|
|
2013-05-07 15:37:20 +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']);
|
|
|
|
$this->assertEquals('new_name', $result['data']['name']);
|
|
|
|
$this->assertEquals('/test', $result['data']['directory']);
|
|
|
|
$this->assertEquals(18, $result['data']['size']);
|
|
|
|
$this->assertEquals('httpd/unix-directory', $result['data']['mime']);
|
|
|
|
$this->assertEquals(\OC_Helper::mimetypeIcon('dir'), $result['data']['icon']);
|
|
|
|
$this->assertFalse($result['data']['isPreviewAvailable']);
|
2013-05-07 15:37:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test rename of file/folder to "Shared"
|
|
|
|
*/
|
|
|
|
function testRenameFolderToShared() {
|
|
|
|
$dir = '/';
|
|
|
|
$oldname = 'oldname';
|
|
|
|
$newname = 'Shared';
|
|
|
|
|
|
|
|
$result = $this->files->rename($dir, $oldname, $newname);
|
|
|
|
$expected = array(
|
|
|
|
'success' => false,
|
2013-10-29 00:42:48 +04:00
|
|
|
'data' => array('message' => "Invalid folder name. Usage of 'Shared' is reserved.")
|
2013-05-07 15:37:20 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2013-05-07 17:24:26 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test rename of file/folder
|
|
|
|
*/
|
|
|
|
function testRenameFolder() {
|
|
|
|
$dir = '/';
|
|
|
|
$oldname = 'oldname';
|
|
|
|
$newname = 'newname';
|
|
|
|
|
2014-01-22 20:17:58 +04:00
|
|
|
$this->viewMock->expects($this->at(0))
|
|
|
|
->method('file_exists')
|
|
|
|
->with('/')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2013-11-28 02:19:57 +04:00
|
|
|
$this->viewMock->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue(array(
|
|
|
|
'fileid' => 123,
|
|
|
|
'type' => 'dir',
|
|
|
|
'mimetype' => 'httpd/unix-directory',
|
|
|
|
'size' => 18,
|
|
|
|
'etag' => 'abcdef',
|
|
|
|
'directory' => '/',
|
|
|
|
'name' => 'new_name',
|
|
|
|
)));
|
|
|
|
|
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']);
|
|
|
|
$this->assertEquals('newname', $result['data']['name']);
|
|
|
|
$this->assertEquals('/', $result['data']['directory']);
|
|
|
|
$this->assertEquals(18, $result['data']['size']);
|
|
|
|
$this->assertEquals('httpd/unix-directory', $result['data']['mime']);
|
|
|
|
$this->assertEquals('abcdef', $result['data']['etag']);
|
|
|
|
$this->assertEquals(\OC_Helper::mimetypeIcon('dir'), $result['data']['icon']);
|
|
|
|
$this->assertFalse($result['data']['isPreviewAvailable']);
|
2013-05-07 17:24:26 +04:00
|
|
|
}
|
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')
|
|
|
|
->with('/unexist')
|
|
|
|
->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);
|
|
|
|
|
|
|
|
$this->assertFalse($result['success']);
|
|
|
|
$this->assertEquals('targetnotfound', $result['data']['code']);
|
|
|
|
}
|
2013-08-18 13:02:08 +04:00
|
|
|
}
|