PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/XmlRpc/RequestTest.php

https://github.com/Thinkscape/zf2
PHP | 343 lines | 237 code | 44 blank | 62 comment | 3 complexity | 7b61e65424eb3e6dc935fc6acb9ae722 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\XmlRpc;
  10. use Zend\XmlRpc\Request;
  11. use Zend\XmlRpc\AbstractValue;
  12. use Zend\XmlRpc\Value;
  13. /**
  14. * @group Zend_XmlRpc
  15. */
  16. class RequestTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * Zend_XmlRpc_Request object
  20. * @var Zend_XmlRpc_Request
  21. */
  22. protected $_request;
  23. /**
  24. * Setup environment
  25. */
  26. public function setUp()
  27. {
  28. $this->_request = new Request();
  29. }
  30. /**
  31. * Teardown environment
  32. */
  33. public function tearDown()
  34. {
  35. unset($this->_request);
  36. }
  37. /**
  38. * get/setMethod() test
  39. */
  40. public function testMethod()
  41. {
  42. $this->assertTrue($this->_request->setMethod('testMethod'));
  43. $this->assertTrue($this->_request->setMethod('testMethod9'));
  44. $this->assertTrue($this->_request->setMethod('test.Method'));
  45. $this->assertTrue($this->_request->setMethod('test_method'));
  46. $this->assertTrue($this->_request->setMethod('test:method'));
  47. $this->assertTrue($this->_request->setMethod('test/method'));
  48. $this->assertFalse($this->_request->setMethod('testMethod-bogus'));
  49. $this->assertEquals('test/method', $this->_request->getMethod());
  50. }
  51. /**
  52. * __construct() test
  53. */
  54. public function testConstructorOptionallySetsMethodAndParams()
  55. {
  56. $r = new Request();
  57. $this->assertEquals('', $r->getMethod());
  58. $this->assertEquals(array(), $r->getParams());
  59. $method = 'foo.bar';
  60. $params = array('baz', 1, array('foo' => 'bar'));
  61. $r = new Request($method, $params);
  62. $this->assertEquals($method, $r->getMethod());
  63. $this->assertEquals($params, $r->getParams());
  64. }
  65. /**
  66. * addParam()/getParams() test
  67. */
  68. public function testAddParam()
  69. {
  70. $this->_request->addParam('string1');
  71. $params = $this->_request->getParams();
  72. $this->assertEquals(1, count($params));
  73. $this->assertEquals('string1', $params[0]);
  74. $this->_request->addParam('string2');
  75. $params = $this->_request->getParams();
  76. $this->assertSame(2, count($params));
  77. $this->assertSame('string1', $params[0]);
  78. $this->assertSame('string2', $params[1]);
  79. $this->_request->addParam(new Value\String('foo'));
  80. $params = $this->_request->getParams();
  81. $this->assertSame(3, count($params));
  82. $this->assertSame('string1', $params[0]);
  83. $this->assertSame('string2', $params[1]);
  84. $this->assertSame('foo', $params[2]->getValue());
  85. }
  86. public function testAddDateParamGeneratesCorrectXml()
  87. {
  88. $time = time();
  89. $this->_request->addParam($time, AbstractValue::XMLRPC_TYPE_DATETIME);
  90. $this->_request->setMethod('foo.bar');
  91. $xml = $this->_request->saveXml();
  92. $sxl = new \SimpleXMLElement($xml);
  93. $param = $sxl->params->param->value;
  94. $type = 'dateTime.iso8601';
  95. $this->assertTrue(isset($param->{$type}), var_export($param, 1));
  96. $this->assertEquals($time, strtotime((string) $param->{$type}));
  97. }
  98. /**
  99. * setParams()/getParams() test
  100. */
  101. public function testSetParams()
  102. {
  103. $params = array(
  104. 'string1',
  105. true,
  106. array('one', 'two')
  107. );
  108. $this->_request->setParams($params);
  109. $returned = $this->_request->getParams();
  110. $this->assertSame($params, $returned);
  111. $params = array(
  112. 'string2',
  113. array('two', 'one')
  114. );
  115. $this->_request->setParams($params);
  116. $returned = $this->_request->getParams();
  117. $this->assertSame($params, $returned);
  118. $params = array(array('value' => 'foobar'));
  119. $this->_request->setParams($params);
  120. $this->assertSame(array('foobar'), $this->_request->getParams());
  121. $this->assertSame(array('string'), $this->_request->getTypes());
  122. $null = new Value\Nil();
  123. $this->_request->setParams('foo', 1, $null);
  124. $this->assertSame(array('foo', 1, $null), $this->_request->getParams());
  125. $this->assertSame(array('string', 'int', 'nil'), $this->_request->getTypes());
  126. $this->assertNull($this->_request->setParams(), 'Call without argument returns null');
  127. }
  128. /**
  129. * loadXml() test
  130. */
  131. public function testLoadXml()
  132. {
  133. $dom = new \DOMDocument('1.0', 'UTF-8');
  134. $mCall = $dom->appendChild($dom->createElement('methodCall'));
  135. $mName = $mCall->appendChild($dom->createElement('methodName', 'do.Something'));
  136. $params = $mCall->appendChild($dom->createElement('params'));
  137. $param1 = $params->appendChild($dom->createElement('param'));
  138. $value1 = $param1->appendChild($dom->createElement('value'));
  139. $value1->appendChild($dom->createElement('string', 'string1'));
  140. $param2 = $params->appendChild($dom->createElement('param'));
  141. $value2 = $param2->appendChild($dom->createElement('value'));
  142. $value2->appendChild($dom->createElement('boolean', 1));
  143. $xml = $dom->saveXml();
  144. $parsed = $this->_request->loadXml($xml);
  145. $this->assertTrue($parsed, $xml);
  146. $this->assertEquals('do.Something', $this->_request->getMethod());
  147. $test = array('string1', true);
  148. $params = $this->_request->getParams();
  149. $this->assertSame($test, $params);
  150. $parsed = $this->_request->loadXml('foo');
  151. $this->assertFalse($parsed, 'Parsed non-XML string?');
  152. }
  153. public function testPassingInvalidTypeToLoadXml()
  154. {
  155. $this->assertFalse($this->_request->loadXml(new \stdClass()));
  156. $this->assertTrue($this->_request->isFault());
  157. $this->assertSame(635, $this->_request->getFault()->getCode());
  158. $this->assertSame('Invalid XML provided to request', $this->_request->getFault()->getMessage());
  159. }
  160. public function testLoadingXmlWithoutMethodNameElement()
  161. {
  162. $this->assertFalse($this->_request->loadXml('<empty/>'));
  163. $this->assertTrue($this->_request->isFault());
  164. $this->assertSame(632, $this->_request->getFault()->getCode());
  165. $this->assertSame("Invalid request, no method passed; request must contain a 'methodName' tag",
  166. $this->_request->getFault()->getMessage());
  167. }
  168. public function testLoadingXmlWithInvalidParams()
  169. {
  170. $this->assertFalse($this->_request->loadXml(
  171. '<methodCall>'
  172. . '<methodName>foo</methodName>'
  173. . '<params><param/><param/><param><foo/></param></params>'
  174. . '</methodCall>'));
  175. $this->assertTrue($this->_request->isFault());
  176. $this->assertSame(633, $this->_request->getFault()->getCode());
  177. $this->assertSame(
  178. 'Param must contain a value',
  179. $this->_request->getFault()->getMessage());
  180. }
  181. public function testExceptionWhileLoadingXmlParamValueIsHandled()
  182. {
  183. $this->assertFalse($this->_request->loadXml(
  184. '<methodCall>'
  185. . '<methodName>foo</methodName>'
  186. . '<params><param><value><foo/></value></param></params>'
  187. . '</methodCall>'));
  188. $this->assertTrue($this->_request->isFault());
  189. $this->assertSame(636, $this->_request->getFault()->getCode());
  190. $this->assertSame(
  191. 'Error creating xmlrpc value',
  192. $this->_request->getFault()->getMessage());
  193. }
  194. /**
  195. * isFault() test
  196. */
  197. public function testIsFault()
  198. {
  199. $this->assertFalse($this->_request->isFault());
  200. $this->_request->loadXml('foo');
  201. $this->assertTrue($this->_request->isFault());
  202. }
  203. /**
  204. * getFault() test
  205. */
  206. public function testGetFault()
  207. {
  208. $fault = $this->_request->getFault();
  209. $this->assertTrue(null === $fault);
  210. $this->_request->loadXml('foo');
  211. $fault = $this->_request->getFault();
  212. $this->assertTrue($fault instanceof \Zend\XmlRpc\Fault);
  213. }
  214. /**
  215. * helper for saveXml() and __toString() tests
  216. *
  217. * @param string $xml
  218. * @return void
  219. */
  220. protected function _testXmlRequest($xml, $argv)
  221. {
  222. $sx = new \SimpleXMLElement($xml);
  223. $result = $sx->xpath('//methodName');
  224. $count = 0;
  225. while (list( , $node) = each($result)) {
  226. ++$count;
  227. }
  228. $this->assertEquals(1, $count, $xml);
  229. $result = $sx->xpath('//params');
  230. $count = 0;
  231. while (list( , $node) = each($result)) {
  232. ++$count;
  233. }
  234. $this->assertEquals(1, $count, $xml);
  235. $methodName = (string) $sx->methodName;
  236. $params = array(
  237. (string) $sx->params->param[0]->value->string,
  238. (bool) $sx->params->param[1]->value->boolean
  239. );
  240. $this->assertEquals('do.Something', $methodName);
  241. $this->assertSame($argv, $params, $xml);
  242. }
  243. /**
  244. * testSaveXML() test
  245. */
  246. public function testSaveXML()
  247. {
  248. $argv = array('string', true);
  249. $this->_request->setMethod('do.Something');
  250. $this->_request->setParams($argv);
  251. $xml = $this->_request->saveXml();
  252. $this->_testXmlRequest($xml, $argv);
  253. }
  254. /**
  255. * __toString() test
  256. */
  257. public function test__toString()
  258. {
  259. $argv = array('string', true);
  260. $this->_request->setMethod('do.Something');
  261. $this->_request->setParams($argv);
  262. $xml = $this->_request->__toString();
  263. $this->_testXmlRequest($xml, $argv);
  264. }
  265. /**
  266. * Test encoding settings
  267. */
  268. public function testSetGetEncoding()
  269. {
  270. $this->assertEquals('UTF-8', $this->_request->getEncoding());
  271. $this->assertEquals('UTF-8', AbstractValue::getGenerator()->getEncoding());
  272. $this->assertSame($this->_request, $this->_request->setEncoding('ISO-8859-1'));
  273. $this->assertEquals('ISO-8859-1', $this->_request->getEncoding());
  274. $this->assertEquals('ISO-8859-1', AbstractValue::getGenerator()->getEncoding());
  275. }
  276. /**
  277. * @group ZF-12293
  278. *
  279. * Test should remain, but is defunct since DOCTYPE presence should return FALSE
  280. * from loadXml()
  281. */
  282. public function testDoesNotAllowExternalEntities()
  283. {
  284. $payload = file_get_contents(dirname(__FILE__) . '/_files/ZF12293-request.xml');
  285. $payload = sprintf($payload, 'file://' . realpath(dirname(__FILE__) . '/_files/ZF12293-payload.txt'));
  286. $this->_request->loadXml($payload);
  287. $method = $this->_request->getMethod();
  288. $this->assertTrue(empty($method));
  289. if (is_string($method)) {
  290. $this->assertNotContains('Local file inclusion', $method);
  291. }
  292. }
  293. public function testShouldDisallowsDoctypeInRequestXmlAndReturnFalseOnLoading()
  294. {
  295. $payload = file_get_contents(dirname(__FILE__) . '/_files/ZF12293-request.xml');
  296. $payload = sprintf($payload, 'file://' . realpath(dirname(__FILE__) . '/_files/ZF12293-payload.txt'));
  297. $this->assertFalse($this->_request->loadXml($payload));
  298. }
  299. }