PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Sabre/DAVACL/ExpandPropertiesTest.php

https://github.com/KOLANICH/SabreDAV
PHP | 358 lines | 253 code | 96 blank | 9 comment | 5 complexity | d0c5182f782e7d9a62495b6c6dcb19a2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\DAVACL;
  3. use Sabre\DAV;
  4. use Sabre\HTTP;
  5. require_once 'Sabre/HTTP/ResponseMock.php';
  6. class ExpandPropertiesTest extends \PHPUnit_Framework_TestCase {
  7. function getServer() {
  8. $tree = array(
  9. new MockPropertyNode('node1', array(
  10. '{http://sabredav.org/ns}simple' => 'foo',
  11. '{http://sabredav.org/ns}href' => new DAV\Property\Href('node2'),
  12. '{DAV:}displayname' => 'Node 1',
  13. )),
  14. new MockPropertyNode('node2', array(
  15. '{http://sabredav.org/ns}simple' => 'simple',
  16. '{http://sabredav.org/ns}hreflist' => new DAV\Property\HrefList(array('node1','node3')),
  17. '{DAV:}displayname' => 'Node 2',
  18. )),
  19. new MockPropertyNode('node3', array(
  20. '{http://sabredav.org/ns}simple' => 'simple',
  21. '{DAV:}displayname' => 'Node 3',
  22. )),
  23. );
  24. $fakeServer = new DAV\Server($tree);
  25. $fakeServer->debugExceptions = true;
  26. $fakeServer->httpResponse = new HTTP\ResponseMock();
  27. $plugin = new Plugin();
  28. $plugin->allowAccessToNodesWithoutACL = true;
  29. $this->assertTrue($plugin instanceof Plugin);
  30. $fakeServer->addPlugin($plugin);
  31. $this->assertEquals($plugin, $fakeServer->getPlugin('acl'));
  32. return $fakeServer;
  33. }
  34. function testSimple() {
  35. $xml = '<?xml version="1.0"?>
  36. <d:expand-property xmlns:d="DAV:">
  37. <d:property name="displayname" />
  38. <d:property name="foo" namespace="http://www.sabredav.org/NS/2010/nonexistant" />
  39. <d:property name="simple" namespace="http://sabredav.org/ns" />
  40. <d:property name="href" namespace="http://sabredav.org/ns" />
  41. </d:expand-property>';
  42. $serverVars = array(
  43. 'REQUEST_METHOD' => 'REPORT',
  44. 'HTTP_DEPTH' => '0',
  45. 'REQUEST_URI' => '/node1',
  46. );
  47. $request = new HTTP\Request($serverVars);
  48. $request->setBody($xml);
  49. $server = $this->getServer();
  50. $server->httpRequest = $request;
  51. $server->exec();
  52. $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status,'Incorrect status code received. Full body: ' . $server->httpResponse->body);
  53. $this->assertEquals(array(
  54. 'Content-Type' => 'application/xml; charset=utf-8',
  55. ), $server->httpResponse->headers);
  56. $check = array(
  57. '/d:multistatus',
  58. '/d:multistatus/d:response' => 1,
  59. '/d:multistatus/d:response/d:href' => 1,
  60. '/d:multistatus/d:response/d:propstat' => 2,
  61. '/d:multistatus/d:response/d:propstat/d:prop' => 2,
  62. '/d:multistatus/d:response/d:propstat/d:prop/d:displayname' => 1,
  63. '/d:multistatus/d:response/d:propstat/d:prop/s:simple' => 1,
  64. '/d:multistatus/d:response/d:propstat/d:prop/s:href' => 1,
  65. '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:href' => 1,
  66. );
  67. $xml = simplexml_load_string($server->httpResponse->body);
  68. $xml->registerXPathNamespace('d','DAV:');
  69. $xml->registerXPathNamespace('s','http://sabredav.org/ns');
  70. foreach($check as $v1=>$v2) {
  71. $xpath = is_int($v1)?$v2:$v1;
  72. $result = $xml->xpath($xpath);
  73. $count = 1;
  74. if (!is_int($v1)) $count = $v2;
  75. $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response: ' . $server->httpResponse->body);
  76. }
  77. }
  78. /**
  79. * @depends testSimple
  80. */
  81. function testExpand() {
  82. $xml = '<?xml version="1.0"?>
  83. <d:expand-property xmlns:d="DAV:">
  84. <d:property name="href" namespace="http://sabredav.org/ns">
  85. <d:property name="displayname" />
  86. </d:property>
  87. </d:expand-property>';
  88. $serverVars = array(
  89. 'REQUEST_METHOD' => 'REPORT',
  90. 'HTTP_DEPTH' => '0',
  91. 'REQUEST_URI' => '/node1',
  92. );
  93. $request = new HTTP\Request($serverVars);
  94. $request->setBody($xml);
  95. $server = $this->getServer();
  96. $server->httpRequest = $request;
  97. $server->exec();
  98. $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status, 'Incorrect response status received. Full response body: ' . $server->httpResponse->body);
  99. $this->assertEquals(array(
  100. 'Content-Type' => 'application/xml; charset=utf-8',
  101. ), $server->httpResponse->headers);
  102. $check = array(
  103. '/d:multistatus',
  104. '/d:multistatus/d:response' => 1,
  105. '/d:multistatus/d:response/d:href' => 1,
  106. '/d:multistatus/d:response/d:propstat' => 1,
  107. '/d:multistatus/d:response/d:propstat/d:prop' => 1,
  108. '/d:multistatus/d:response/d:propstat/d:prop/s:href' => 1,
  109. '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response' => 1,
  110. '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:href' => 1,
  111. '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat' => 1,
  112. '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop' => 1,
  113. '/d:multistatus/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop/d:displayname' => 1,
  114. );
  115. $xml = simplexml_load_string($server->httpResponse->body);
  116. $xml->registerXPathNamespace('d','DAV:');
  117. $xml->registerXPathNamespace('s','http://sabredav.org/ns');
  118. foreach($check as $v1=>$v2) {
  119. $xpath = is_int($v1)?$v2:$v1;
  120. $result = $xml->xpath($xpath);
  121. $count = 1;
  122. if (!is_int($v1)) $count = $v2;
  123. $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
  124. }
  125. }
  126. /**
  127. * @depends testSimple
  128. */
  129. function testExpandHrefList() {
  130. $xml = '<?xml version="1.0"?>
  131. <d:expand-property xmlns:d="DAV:">
  132. <d:property name="hreflist" namespace="http://sabredav.org/ns">
  133. <d:property name="displayname" />
  134. </d:property>
  135. </d:expand-property>';
  136. $serverVars = array(
  137. 'REQUEST_METHOD' => 'REPORT',
  138. 'HTTP_DEPTH' => '0',
  139. 'REQUEST_URI' => '/node2',
  140. );
  141. $request = new HTTP\Request($serverVars);
  142. $request->setBody($xml);
  143. $server = $this->getServer();
  144. $server->httpRequest = $request;
  145. $server->exec();
  146. $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status);
  147. $this->assertEquals(array(
  148. 'Content-Type' => 'application/xml; charset=utf-8',
  149. ), $server->httpResponse->headers);
  150. $check = array(
  151. '/d:multistatus',
  152. '/d:multistatus/d:response' => 1,
  153. '/d:multistatus/d:response/d:href' => 1,
  154. '/d:multistatus/d:response/d:propstat' => 1,
  155. '/d:multistatus/d:response/d:propstat/d:prop' => 1,
  156. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist' => 1,
  157. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response' => 2,
  158. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:href' => 2,
  159. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat' => 2,
  160. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop' => 2,
  161. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/d:displayname' => 2,
  162. );
  163. $xml = simplexml_load_string($server->httpResponse->body);
  164. $xml->registerXPathNamespace('d','DAV:');
  165. $xml->registerXPathNamespace('s','http://sabredav.org/ns');
  166. foreach($check as $v1=>$v2) {
  167. $xpath = is_int($v1)?$v2:$v1;
  168. $result = $xml->xpath($xpath);
  169. $count = 1;
  170. if (!is_int($v1)) $count = $v2;
  171. $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
  172. }
  173. }
  174. /**
  175. * @depends testExpand
  176. */
  177. function testExpandDeep() {
  178. $xml = '<?xml version="1.0"?>
  179. <d:expand-property xmlns:d="DAV:">
  180. <d:property name="hreflist" namespace="http://sabredav.org/ns">
  181. <d:property name="href" namespace="http://sabredav.org/ns">
  182. <d:property name="displayname" />
  183. </d:property>
  184. <d:property name="displayname" />
  185. </d:property>
  186. </d:expand-property>';
  187. $serverVars = array(
  188. 'REQUEST_METHOD' => 'REPORT',
  189. 'HTTP_DEPTH' => '0',
  190. 'REQUEST_URI' => '/node2',
  191. );
  192. $request = new HTTP\Request($serverVars);
  193. $request->setBody($xml);
  194. $server = $this->getServer();
  195. $server->httpRequest = $request;
  196. $server->exec();
  197. $this->assertEquals('HTTP/1.1 207 Multi-Status', $server->httpResponse->status);
  198. $this->assertEquals(array(
  199. 'Content-Type' => 'application/xml; charset=utf-8',
  200. ), $server->httpResponse->headers);
  201. $check = array(
  202. '/d:multistatus',
  203. '/d:multistatus/d:response' => 1,
  204. '/d:multistatus/d:response/d:href' => 1,
  205. '/d:multistatus/d:response/d:propstat' => 1,
  206. '/d:multistatus/d:response/d:propstat/d:prop' => 1,
  207. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist' => 1,
  208. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response' => 2,
  209. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:href' => 2,
  210. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat' => 3,
  211. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop' => 3,
  212. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/d:displayname' => 2,
  213. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href' => 2,
  214. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response' => 1,
  215. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:href' => 1,
  216. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat' => 1,
  217. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop' => 1,
  218. '/d:multistatus/d:response/d:propstat/d:prop/s:hreflist/d:response/d:propstat/d:prop/s:href/d:response/d:propstat/d:prop/d:displayname' => 1,
  219. );
  220. $xml = simplexml_load_string($server->httpResponse->body);
  221. $xml->registerXPathNamespace('d','DAV:');
  222. $xml->registerXPathNamespace('s','http://sabredav.org/ns');
  223. foreach($check as $v1=>$v2) {
  224. $xpath = is_int($v1)?$v2:$v1;
  225. $result = $xml->xpath($xpath);
  226. $count = 1;
  227. if (!is_int($v1)) $count = $v2;
  228. $this->assertEquals($count,count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result));
  229. }
  230. }
  231. }
  232. class MockPropertyNode implements DAV\INode, DAV\IProperties {
  233. function __construct($name, array $properties) {
  234. $this->name = $name;
  235. $this->properties = $properties;
  236. }
  237. function getName() {
  238. return $this->name;
  239. }
  240. function getProperties($requestedProperties) {
  241. $returnedProperties = array();
  242. foreach($requestedProperties as $requestedProperty) {
  243. if (isset($this->properties[$requestedProperty])) {
  244. $returnedProperties[$requestedProperty] =
  245. $this->properties[$requestedProperty];
  246. }
  247. }
  248. return $returnedProperties;
  249. }
  250. function delete() {
  251. throw new DAV\Exception('Not implemented');
  252. }
  253. function setName($name) {
  254. throw new DAV\Exception('Not implemented');
  255. }
  256. function getLastModified() {
  257. return null;
  258. }
  259. function updateProperties($properties) {
  260. throw new DAV\Exception('Not implemented');
  261. }
  262. }