RequestTest.php 12 KB

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