11888: increase announced own cloud server version
[tine20] / tests / tine20 / Tinebase / WebDav / Plugin / OwnCloudTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  *
5  * @package     Tinebase
6  * @subpackage  Frontend
7  * @license     http://www.gnu.org/licenses/agpl.html
8  * @copyright   Copyright (c) 2013-2017 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Lars Kneschke <l.kneschke@metaways.de>
10  */
11
12
13 /**
14  * Test class for Tinebase_WebDav_Plugin_OwnCloud
15  */
16 class Tinebase_WebDav_Plugin_OwnCloudTest extends Tinebase_WebDav_Plugin_AbstractBaseTest
17 {
18     const REQUEST_BODY = '<?xml version="1.0" encoding="utf-8"?>
19         <propfind xmlns="DAV:">
20             <prop>
21                 <getlastmodified xmlns="DAV:"/>
22                 <getcontentlength xmlns="DAV:"/>
23                 <resourcetype xmlns="DAV:"/>
24                 <getetag xmlns="DAV:"/>
25                 <id xmlns="http://owncloud.org/ns"/>
26             </prop>
27         </propfind>';
28
29     /**
30      * Runs the test methods of this class.
31      *
32      * @access public
33      * @static
34      */
35     public static function main()
36     {
37         $suite = new PHPUnit_Framework_TestSuite('Tine 2.0 Plugin OwnCloud Tests');
38         PHPUnit_TextUI_TestRunner::run($suite);
39     }
40
41     /**
42      * Sets up the fixture.
43      * This method is called before a test is executed.
44      *
45      * @access protected
46      */
47     protected function setUp()
48     {
49         parent::setUp();
50
51         $this->plugin = new Tinebase_WebDav_Plugin_OwnCloud();
52
53         // Create request, there is no in tinebase while running unittests, but the owncloud plugin needs an user agent
54         $request = \Zend\Http\PhpEnvironment\Request::fromString(
55             "POST /index.php HTTP/1.1\r\n" .
56             "Host: localhost\r\n" .
57             "Content-Type: application/json\r\n" .
58             "User-Agent: Mozilla/5.0 (Macintosh) mirall/2.2.4 (build 3709)\r\n"
59         );
60         Tinebase_Core::set('request', $request);
61
62         $this->server->addPlugin($this->plugin);
63     }
64
65     /**
66      * tear down tests
67      */
68     protected function tearDown()
69     {
70         parent::tearDown();
71         Tinebase_Config::getInstance()->set(Tinebase_Config::USE_LOGINNAME_AS_FOLDERNAME, false);
72     }
73
74     /**
75      * test getPluginName method
76      */
77     public function testGetPluginName()
78     {
79         $pluginName = $this->plugin->getPluginName();
80
81         $this->assertEquals('Tinebase_WebDav_Plugin_OwnCloud', $pluginName);
82     }
83
84     /**
85      * test testGetProperties method
86      */
87     public function testGetProperties()
88     {
89         $request = new Sabre\HTTP\Request(array(
90             'REQUEST_METHOD' => 'PROPFIND',
91             'REQUEST_URI' => '/remote.php/webdav/' . Tinebase_Core::getUser()->accountDisplayName,
92             'HTTP_DEPTH' => '0',
93         ));
94         $request->setBody(static::REQUEST_BODY);
95
96         $this->server->httpRequest = $request;
97         $this->server->exec();
98         //var_dump($this->response->body);
99         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
100
101         $responseDoc = new DOMDocument();
102         $responseDoc->loadXML($this->response->body);
103         //$responseDoc->formatOutput = true; echo $responseDoc->saveXML();
104         $xpath = new DomXPath($responseDoc);
105         $xpath->registerNamespace('owncloud', 'http://owncloud.org/ns');
106
107         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/owncloud:id');
108         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
109         $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
110     }
111
112     /**
113      * test testGetProperties method with an invalid client
114      */
115     public function testInvalidOwnCloudVersion()
116     {
117         static::setExpectedException(InvalidArgumentException::class, sprintf(
118             'OwnCloud client min version is "%s"!',
119             Tinebase_WebDav_Plugin_OwnCloud::OWNCLOUD_MIN_VERSION
120         ));
121
122
123         // use old owncloud user agent!
124         $request = \Zend\Http\PhpEnvironment\Request::fromString(
125             "POST /index.php HTTP/1.1\r\n" .
126             "Host: localhost\r\n" .
127             "Content-Type: application/json\r\n" .
128             "User-Agent: Mozilla/5.0 (Macintosh) mirall/1.5.0 (build 3709)\r\n"
129         );
130         Tinebase_Core::set('request', $request);
131
132         $request = new Sabre\HTTP\Request(array(
133             'REQUEST_METHOD' => 'PROPFIND',
134             'REQUEST_URI' => '/remote.php/webdav/' . Tinebase_Core::getUser()->accountDisplayName,
135             'HTTP_DEPTH' => '0',
136         ));
137         $request->setBody(static::REQUEST_BODY);
138
139         $this->server->httpRequest = $request;
140         $this->server->exec();
141     }
142
143     /**
144      * test testGetProperties method with alternate loginname config
145      */
146     public function testGetProperties2()
147     {
148         Tinebase_Config::getInstance()->set(Tinebase_Config::USE_LOGINNAME_AS_FOLDERNAME, true);
149
150         $request = new Sabre\HTTP\Request(array(
151             'REQUEST_METHOD' => 'PROPFIND',
152             'REQUEST_URI' => '/remote.php/webdav/' . Tinebase_Core::getUser()->accountLoginName,
153             'HTTP_DEPTH' => '0',
154         ));
155         $request->setBody(static::REQUEST_BODY);
156
157         $this->server->httpRequest = $request;
158         $this->server->exec();
159         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
160
161         $responseDoc = new DOMDocument();
162         $responseDoc->loadXML($this->response->body);
163         $xpath = new DomXPath($responseDoc);
164         $xpath->registerNamespace('owncloud', 'http://owncloud.org/ns');
165
166         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/owncloud:id');
167         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
168         $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
169     }
170 }