Blame view

sources/apps/files/tests/ajax_rename.php 4.83 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  <?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/>.
   *
   */
  
  class Test_OC_Files_App_Rename extends \PHPUnit_Framework_TestCase {
  	private static $user;
6d9380f96   Cédric Dupont   Update sources OC...
26
27
28
29
30
31
32
33
34
  	/**
  	 * @var PHPUnit_Framework_MockObject_MockObject
  	 */
  	private $viewMock;
  
  	/**
  	 * @var \OCA\Files\App
  	 */
  	private $files;
31b7f2792   Kload   Upgrade to ownclo...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  	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);
  		$l10nMock->expects($this->any())
  			->method('t')
  			->will($this->returnArgument(0));
6d9380f96   Cédric Dupont   Update sources OC...
49
  		$viewMock = $this->getMock('\OC\Files\View', array('rename', 'normalizePath', 'getFileInfo', 'file_exists'), array(), '', false);
31b7f2792   Kload   Upgrade to ownclo...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  		$viewMock->expects($this->any())
  			->method('normalizePath')
  			->will($this->returnArgument(0));
  		$viewMock->expects($this->any())
  			->method('rename')
  			->will($this->returnValue(true));
  		$this->viewMock = $viewMock;
  		$this->files = new \OCA\Files\App($viewMock, $l10nMock);
  	}
  
  	function tearDown() {
  		$result = \OC_User::deleteUser(self::$user);
  		$this->assertTrue($result);
  		\OC\Files\Filesystem::tearDown();
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
67
  	 * test rename of file/folder
31b7f2792   Kload   Upgrade to ownclo...
68
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
69
  	function testRenameFolder() {
31b7f2792   Kload   Upgrade to ownclo...
70
  		$dir = '/';
6d9380f96   Cédric Dupont   Update sources OC...
71
72
  		$oldname = 'oldname';
  		$newname = 'newname';
31b7f2792   Kload   Upgrade to ownclo...
73

f7d878ff1   kload   [enh] Update to 7...
74
  		$this->viewMock->expects($this->any())
6d9380f96   Cédric Dupont   Update sources OC...
75
  			->method('file_exists')
f7d878ff1   kload   [enh] Update to 7...
76
77
78
79
80
  			->with($this->anything())
  			->will($this->returnValueMap(array(
  				array('/', true),
  				array('/oldname', true)
  				)));
31b7f2792   Kload   Upgrade to ownclo...
81
82
83
  
  		$this->viewMock->expects($this->any())
  			->method('getFileInfo')
6d9380f96   Cédric Dupont   Update sources OC...
84
85
86
87
88
  			->will($this->returnValue(new \OC\Files\FileInfo(
  				'/new_name',
  				new \OC\Files\Storage\Local(array('datadir' => '/')),
  				'/',
  				array(
31b7f2792   Kload   Upgrade to ownclo...
89
90
91
  				'fileid' => 123,
  				'type' => 'dir',
  				'mimetype' => 'httpd/unix-directory',
6d9380f96   Cédric Dupont   Update sources OC...
92
93
  				'mtime' => 0,
  				'permissions' => 31,
31b7f2792   Kload   Upgrade to ownclo...
94
95
96
97
  				'size' => 18,
  				'etag' => 'abcdef',
  				'directory' => '/',
  				'name' => 'new_name',
6d9380f96   Cédric Dupont   Update sources OC...
98
  			))));
31b7f2792   Kload   Upgrade to ownclo...
99
100
101
102
103
104
  
  		$result = $this->files->rename($dir, $oldname, $newname);
  
  		$this->assertTrue($result['success']);
  		$this->assertEquals(123, $result['data']['id']);
  		$this->assertEquals('new_name', $result['data']['name']);
31b7f2792   Kload   Upgrade to ownclo...
105
  		$this->assertEquals(18, $result['data']['size']);
6d9380f96   Cédric Dupont   Update sources OC...
106
107
108
109
110
  		$this->assertEquals('httpd/unix-directory', $result['data']['mimetype']);
  		$this->assertEquals('abcdef', $result['data']['etag']);
  		$icon = \OC_Helper::mimetypeIcon('dir');
  		$icon = substr($icon, 0, -3) . 'svg';
  		$this->assertEquals($icon, $result['data']['icon']);
31b7f2792   Kload   Upgrade to ownclo...
111
112
113
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
114
  	 * Test rename inside a folder that doesn't exist any more
31b7f2792   Kload   Upgrade to ownclo...
115
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
116
117
  	function testRenameInNonExistingFolder() {
  		$dir = '/unexist';
31b7f2792   Kload   Upgrade to ownclo...
118
119
  		$oldname = 'oldname';
  		$newname = 'newname';
6d9380f96   Cédric Dupont   Update sources OC...
120
121
  		$this->viewMock->expects($this->at(0))
  			->method('file_exists')
f7d878ff1   kload   [enh] Update to 7...
122
  			->with('/unexist/oldname')
6d9380f96   Cédric Dupont   Update sources OC...
123
  			->will($this->returnValue(false));
31b7f2792   Kload   Upgrade to ownclo...
124
125
126
127
128
129
130
131
  		$this->viewMock->expects($this->any())
  			->method('getFileInfo')
  			->will($this->returnValue(array(
  				'fileid' => 123,
  				'type' => 'dir',
  				'mimetype' => 'httpd/unix-directory',
  				'size' => 18,
  				'etag' => 'abcdef',
6d9380f96   Cédric Dupont   Update sources OC...
132
  				'directory' => '/unexist',
31b7f2792   Kload   Upgrade to ownclo...
133
134
  				'name' => 'new_name',
  			)));
31b7f2792   Kload   Upgrade to ownclo...
135
  		$result = $this->files->rename($dir, $oldname, $newname);
6d9380f96   Cédric Dupont   Update sources OC...
136
  		$this->assertFalse($result['success']);
f7d878ff1   kload   [enh] Update to 7...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  		$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']);
6d9380f96   Cédric Dupont   Update sources OC...
171
  		$this->assertEquals('targetnotfound', $result['data']['code']);
31b7f2792   Kload   Upgrade to ownclo...
172
173
  	}
  }