ResponseTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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-2012 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/Response.php';
  23. /**
  24. * Test case for Zend_XmlRpc_Response
  25. *
  26. * @category Zend
  27. * @package Zend_XmlRpc
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @group Zend_XmlRpc
  32. */
  33. class Zend_XmlRpc_ResponseTest extends PHPUnit_Framework_TestCase
  34. {
  35. /**
  36. * Zend_XmlRpc_Response object
  37. * @var Zend_XmlRpc_Response
  38. */
  39. protected $_response;
  40. /**
  41. * @var bool
  42. */
  43. protected $_errorOccured = false;
  44. /**
  45. * Setup environment
  46. */
  47. public function setUp()
  48. {
  49. $this->_response = new Zend_XmlRpc_Response();
  50. }
  51. /**
  52. * Teardown environment
  53. */
  54. public function tearDown()
  55. {
  56. unset($this->_response);
  57. }
  58. /**
  59. * __construct() test
  60. */
  61. public function test__construct()
  62. {
  63. $this->assertTrue($this->_response instanceof Zend_XmlRpc_Response);
  64. }
  65. /**
  66. * get/setReturnValue() test
  67. */
  68. public function testReturnValue()
  69. {
  70. $this->_response->setReturnValue('string');
  71. $this->assertEquals('string', $this->_response->getReturnValue());
  72. $this->_response->setReturnValue(array('one', 'two'));
  73. $this->assertSame(array('one', 'two'), $this->_response->getReturnValue());
  74. }
  75. /**
  76. * isFault() test
  77. *
  78. * Call as method call
  79. *
  80. * Returns: boolean
  81. */
  82. public function testIsFault()
  83. {
  84. $this->assertFalse($this->_response->isFault());
  85. $this->_response->loadXml('foo');
  86. $this->assertTrue($this->_response->isFault());
  87. }
  88. /**
  89. * Tests getFault() returns NULL (no fault) or the fault object
  90. */
  91. public function testGetFault()
  92. {
  93. $this->assertNull($this->_response->getFault());
  94. $this->_response->loadXml('foo');
  95. $this->assertType('Zend_XmlRpc_Fault', $this->_response->getFault());
  96. }
  97. /**
  98. * loadXml() test
  99. *
  100. * Call as method call
  101. *
  102. * Expects:
  103. * - response:
  104. *
  105. * Returns: boolean
  106. */
  107. public function testLoadXml()
  108. {
  109. $dom = new DOMDocument('1.0', 'UTF-8');
  110. $response = $dom->appendChild($dom->createElement('methodResponse'));
  111. $params = $response->appendChild($dom->createElement('params'));
  112. $param = $params->appendChild($dom->createElement('param'));
  113. $value = $param->appendChild($dom->createElement('value'));
  114. $value->appendChild($dom->createElement('string', 'Return value'));
  115. $xml = $dom->saveXml();
  116. $parsed = $this->_response->loadXml($xml);
  117. $this->assertTrue($parsed, $xml);
  118. $this->assertEquals('Return value', $this->_response->getReturnValue());
  119. }
  120. public function testLoadXmlWithInvalidValue()
  121. {
  122. $this->assertFalse($this->_response->loadXml(new stdClass()));
  123. $this->assertTrue($this->_response->isFault());
  124. $this->assertSame(650, $this->_response->getFault()->getCode());
  125. }
  126. /**
  127. * @group ZF-9039
  128. */
  129. public function testExceptionIsThrownWhenInvalidXmlIsReturnedByServer()
  130. {
  131. set_error_handler(array($this, 'trackError'));
  132. $invalidResponse = 'foo';
  133. $response = new Zend_XmlRpc_Response();
  134. $this->assertFalse($this->_errorOccured);
  135. $this->assertFalse($response->loadXml($invalidResponse));
  136. $this->assertFalse($this->_errorOccured);
  137. }
  138. /**
  139. * @group ZF-5404
  140. */
  141. public function testNilResponseFromXmlRpcServer()
  142. {
  143. $rawResponse = <<<EOD
  144. <methodResponse><params><param><value><array><data><value><struct><member><name>id</name><value><string>1</string></value></member><member><name>name</name><value><string>birdy num num!</string></value></member><member><name>description</name><value><nil/></value></member></struct></value></data></array></value></param></params></methodResponse>
  145. EOD;
  146. try {
  147. $response = new Zend_XmlRpc_Response();
  148. $ret = $response->loadXml($rawResponse);
  149. } catch(Exception $e) {
  150. $this->fail("Parsing the response should not throw an exception.");
  151. }
  152. $this->assertTrue($ret);
  153. $this->assertEquals(array(
  154. 0 => array(
  155. 'id' => 1,
  156. 'name' => 'birdy num num!',
  157. 'description' => null,
  158. )
  159. ), $response->getReturnValue());
  160. }
  161. /**
  162. * helper for saveXml() and __toString() tests
  163. *
  164. * @param string $xml
  165. * @return void
  166. */
  167. protected function _testXmlResponse($xml)
  168. {
  169. try {
  170. $sx = new SimpleXMLElement($xml);
  171. } catch (Exception $e) {
  172. $this->fail('Invalid XML returned');
  173. }
  174. $this->assertTrue($sx->params ? true : false);
  175. $this->assertTrue($sx->params->param ? true : false);
  176. $this->assertTrue($sx->params->param->value ? true : false);
  177. $this->assertTrue($sx->params->param->value->string ? true : false);
  178. $this->assertEquals('return value', (string) $sx->params->param->value->string);
  179. }
  180. /**
  181. * saveXml() test
  182. */
  183. public function testSaveXML()
  184. {
  185. $this->_response->setReturnValue('return value');
  186. $xml = $this->_response->saveXml();
  187. $this->_testXmlResponse($xml);
  188. }
  189. /**
  190. * __toString() test
  191. */
  192. public function test__toString()
  193. {
  194. $this->_response->setReturnValue('return value');
  195. $xml = $this->_response->__toString();
  196. $this->_testXmlResponse($xml);
  197. }
  198. /**
  199. * Test encoding settings
  200. */
  201. public function testSetGetEncoding()
  202. {
  203. $this->assertEquals('UTF-8', $this->_response->getEncoding());
  204. $this->assertEquals('UTF-8', Zend_XmlRpc_Value::getGenerator()->getEncoding());
  205. $this->assertSame($this->_response, $this->_response->setEncoding('ISO-8859-1'));
  206. $this->assertEquals('ISO-8859-1', $this->_response->getEncoding());
  207. $this->assertEquals('ISO-8859-1', Zend_XmlRpc_Value::getGenerator()->getEncoding());
  208. }
  209. public function testLoadXmlThrowsExceptionWithMissingNodes()
  210. {
  211. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><params><param>foo</param></params></methodResponse>');
  212. $this->_loadXml($sxl->asXML());
  213. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><params>foo</params></methodResponse>');
  214. $this->_loadXml($sxl->asXML());
  215. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><bar>foo</bar></methodResponse>');
  216. $this->_loadXml($sxl->asXML());
  217. }
  218. protected function _loadXml($xml)
  219. {
  220. try {
  221. $this->_response->loadXml($xml);
  222. $this->fail('Invalid XML-RPC response should raise an exception');
  223. } catch (Exception $e) {
  224. }
  225. }
  226. public function trackError($error)
  227. {
  228. $this->_errorOccured = true;
  229. }
  230. /**
  231. * @group ZF-12293
  232. */
  233. public function testDoesNotAllowExternalEntities()
  234. {
  235. $payload = file_get_contents(dirname(__FILE__) . '/_files/ZF12293-response.xml');
  236. $payload = sprintf($payload, 'file://' . realpath(dirname(__FILE__) . '/_files/ZF12293-payload.txt'));
  237. $this->_response->loadXml($payload);
  238. $value = $this->_response->getReturnValue();
  239. $this->assertTrue(empty($value));
  240. if (is_string($value)) {
  241. $this->assertNotContains('Local file inclusion', $value);
  242. }
  243. }
  244. public function testShouldDisallowsDoctypeInRequestXmlAndReturnFalseOnLoading()
  245. {
  246. $payload = file_get_contents(dirname(__FILE__) . '/_files/ZF12293-response.xml');
  247. $payload = sprintf($payload, 'file://' . realpath(dirname(__FILE__) . '/_files/ZF12293-payload.txt'));
  248. $this->assertFalse($this->_response->loadXml($payload));
  249. }
  250. }