server/apps/files/tests/ajax_rename.php

181 lines
4.8 KiB
PHP
Raw Normal View History

2013-05-07 11:37:20 +00: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 09:56:59 +00:00
class Test_OC_Files_App_Rename extends \PHPUnit_Framework_TestCase {
private static $user;
2013-05-07 11:37:20 +00:00
2014-05-02 18:35:03 +00:00
/**
* @var PHPUnit_Framework_MockObject_MockObject
*/
private $viewMock;
/**
* @var \OCA\Files\App
*/
private $files;
2013-05-07 11:37:20 +00:00
function setUp() {
// mock OC_L10n
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');
$l10nMock = $this->getMock('\OC_L10N', array('t'), array(), '', false);
2013-05-07 11:37:20 +00:00
$l10nMock->expects($this->any())
->method('t')
->will($this->returnArgument(0));
$viewMock = $this->getMock('\OC\Files\View', array('rename', 'normalizePath', 'getFileInfo', 'file_exists'), array(), '', false);
2013-05-07 11:37:20 +00:00
$viewMock->expects($this->any())
->method('normalizePath')
->will($this->returnArgument(0));
$viewMock->expects($this->any())
->method('rename')
->will($this->returnValue(true));
2013-11-27 22:19:57 +00:00
$this->viewMock = $viewMock;
2013-05-08 09:56:59 +00:00
$this->files = new \OCA\Files\App($viewMock, $l10nMock);
2013-05-07 11:37:20 +00:00
}
function tearDown() {
$result = \OC_User::deleteUser(self::$user);
$this->assertTrue($result);
\OC\Files\Filesystem::tearDown();
}
/**
* test rename of file/folder
*/
function testRenameFolder() {
$dir = '/';
$oldname = 'oldname';
$newname = 'newname';
2014-08-11 10:02:33 +00:00
$this->viewMock->expects($this->any())
->method('file_exists')
2014-08-11 10:02:33 +00:00
->with($this->anything())
->will($this->returnValueMap(array(
array('/', true),
array('/oldname', true)
)));
2013-11-27 22:19:57 +00:00
$this->viewMock->expects($this->any())
->method('getFileInfo')
->will($this->returnValue(new \OC\Files\FileInfo(
2014-06-03 13:02:42 +00:00
'/new_name',
2014-05-02 18:35:03 +00:00
new \OC\Files\Storage\Local(array('datadir' => '/')),
'/',
array(
2013-11-27 22:19:57 +00:00
'fileid' => 123,
'type' => 'dir',
'mimetype' => 'httpd/unix-directory',
'mtime' => 0,
'permissions' => 31,
2013-11-27 22:19:57 +00:00
'size' => 18,
'etag' => 'abcdef',
'directory' => '/',
'name' => 'new_name',
))));
2013-11-27 22:19:57 +00:00
$result = $this->files->rename($dir, $oldname, $newname);
2013-11-27 22:19:57 +00:00
$this->assertTrue($result['success']);
$this->assertEquals(123, $result['data']['id']);
$this->assertEquals('new_name', $result['data']['name']);
2013-11-27 22:19:57 +00:00
$this->assertEquals(18, $result['data']['size']);
$this->assertEquals('httpd/unix-directory', $result['data']['mimetype']);
2013-11-27 22:19:57 +00:00
$this->assertEquals('abcdef', $result['data']['etag']);
2014-02-20 15:56:09 +00:00
$icon = \OC_Helper::mimetypeIcon('dir');
$icon = substr($icon, 0, -3) . 'svg';
$this->assertEquals($icon, $result['data']['icon']);
}
/**
* 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 10:02:33 +00:00
->with('/unexist/oldname')
->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 10:02:33 +00: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);
$this->assertFalse($result['success']);
$this->assertEquals('targetnotfound', $result['data']['code']);
}
}