/tests/Sabre/DAVACL/ExpandPropertiesTest.php

https://github.com/lkneschke/SabreDAV · PHP · 353 lines · 250 code · 94 blank · 9 comment · 5 complexity · 42964c433acacbc57cf07e2f53142b65 MD5 · raw file

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