1<?php
2
3namespace Sabre\DAV;
4
5use Sabre\DAVServerTest;
6use Sabre\HTTP;
7
8/**
9 * Tests related to the MOVE request.
10 *
11 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
12 * @author Evert Pot (http://evertpot.com/)
13 * @license http://sabre.io/license/ Modified BSD License
14 */
15class HttpMoveTest extends DAVServerTest {
16
17    /**
18     * Sets up the DAV tree.
19     *
20     * @return void
21     */
22    function setUpTree() {
23
24        $this->tree = new Mock\Collection('root', [
25            'file1' => 'content1',
26            'file2' => 'content2',
27        ]);
28
29    }
30
31    function testMoveToSelf() {
32
33        $request = new HTTP\Request('MOVE', '/file1', [
34            'Destination' => '/file1'
35        ]);
36        $response = $this->request($request);
37        $this->assertEquals(403, $response->getStatus());
38        $this->assertEquals('content1', $this->tree->getChild('file1')->get());
39
40    }
41
42    function testMove() {
43
44        $request = new HTTP\Request('MOVE', '/file1', [
45            'Destination' => '/file3'
46        ]);
47        $response = $this->request($request);
48        $this->assertEquals(201, $response->getStatus(), print_r($response,true));
49        $this->assertEquals('content1', $this->tree->getChild('file3')->get());
50        $this->assertFalse($this->tree->childExists('file1'));
51
52    }
53
54    function testMoveToExisting() {
55
56        $request = new HTTP\Request('MOVE', '/file1', [
57            'Destination' => '/file2'
58        ]);
59        $response = $this->request($request);
60        $this->assertEquals(204, $response->getStatus(), print_r($response,true));
61        $this->assertEquals('content1', $this->tree->getChild('file2')->get());
62        $this->assertFalse($this->tree->childExists('file1'));
63
64    }
65
66    function testMoveToExistingOverwriteT() {
67
68        $request = new HTTP\Request('MOVE', '/file1', [
69            'Destination' => '/file2',
70            'Overwrite' => 'T',
71        ]);
72        $response = $this->request($request);
73        $this->assertEquals(204, $response->getStatus(), print_r($response,true));
74        $this->assertEquals('content1', $this->tree->getChild('file2')->get());
75        $this->assertFalse($this->tree->childExists('file1'));
76
77    }
78
79    function testMoveToExistingOverwriteF() {
80
81        $request = new HTTP\Request('MOVE', '/file1', [
82            'Destination' => '/file2',
83            'Overwrite' => 'F',
84        ]);
85        $response = $this->request($request);
86        $this->assertEquals(412, $response->getStatus(), print_r($response,true));
87        $this->assertEquals('content1', $this->tree->getChild('file1')->get());
88        $this->assertEquals('content2', $this->tree->getChild('file2')->get());
89        $this->assertTrue($this->tree->childExists('file1'));
90        $this->assertTrue($this->tree->childExists('file2'));
91
92    }
93
94    /**
95     * If we MOVE to an existing file, but a plugin prevents the original from
96     * being deleted, we need to make sure that the server does not delete
97     * the destination.
98     */
99    function testMoveToExistingBlockedDeleteSource() {
100
101        $this->server->on('beforeUnbind', function($path) {
102
103            if ($path==='file1') {
104                throw new \Sabre\DAV\Exception\Forbidden('uh oh');
105            }
106
107        });
108        $request = new HTTP\Request('MOVE', '/file1', [
109            'Destination' => '/file2'
110        ]);
111        $response = $this->request($request);
112        $this->assertEquals(403, $response->getStatus(), print_r($response,true));
113        $this->assertEquals('content1', $this->tree->getChild('file1')->get());
114        $this->assertEquals('content2', $this->tree->getChild('file2')->get());
115        $this->assertTrue($this->tree->childExists('file1'));
116        $this->assertTrue($this->tree->childExists('file2'));
117
118    }
119}
120