XmlTest.php 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. <?php
  2. /**
  3. * XmlTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since 1.2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Utility;
  18. use Cake\Core\Configure;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\Utility\Error\XmlException;
  21. use Cake\Utility\Xml;
  22. /**
  23. * XmlTest class
  24. *
  25. */
  26. class XmlTest extends TestCase {
  27. /**
  28. * autoFixtures property
  29. *
  30. * @var bool
  31. */
  32. public $autoFixtures = false;
  33. /**
  34. * fixtures property
  35. * @var array
  36. */
  37. public $fixtures = array(
  38. 'core.article', 'core.user'
  39. );
  40. /**
  41. * setUp method
  42. *
  43. * @return void
  44. */
  45. public function setUp() {
  46. parent::setUp();
  47. $this->_appEncoding = Configure::read('App.encoding');
  48. Configure::write('App.encoding', 'UTF-8');
  49. }
  50. /**
  51. * tearDown method
  52. *
  53. * @return void
  54. */
  55. public function tearDown() {
  56. parent::tearDown();
  57. Configure::write('App.encoding', $this->_appEncoding);
  58. }
  59. /**
  60. * testBuild method
  61. *
  62. * @return void
  63. */
  64. public function testBuild() {
  65. $xml = '<tag>value</tag>';
  66. $obj = Xml::build($xml);
  67. $this->assertTrue($obj instanceof \SimpleXMLElement);
  68. $this->assertEquals('tag', (string)$obj->getName());
  69. $this->assertEquals('value', (string)$obj);
  70. $xml = '<?xml version="1.0" encoding="UTF-8"?><tag>value</tag>';
  71. $this->assertEquals($obj, Xml::build($xml));
  72. $obj = Xml::build($xml, array('return' => 'domdocument'));
  73. $this->assertTrue($obj instanceof \DOMDocument);
  74. $this->assertEquals('tag', $obj->firstChild->nodeName);
  75. $this->assertEquals('value', $obj->firstChild->nodeValue);
  76. $xml = CORE_TESTS . 'Fixture/sample.xml';
  77. $obj = Xml::build($xml);
  78. $this->assertEquals('tags', $obj->getName());
  79. $this->assertEquals(2, count($obj));
  80. $this->assertEquals(Xml::build($xml), Xml::build(file_get_contents($xml)));
  81. $obj = Xml::build($xml, array('return' => 'domdocument'));
  82. $this->assertEquals('tags', $obj->firstChild->nodeName);
  83. $this->assertEquals(
  84. Xml::build($xml, array('return' => 'domdocument')),
  85. Xml::build(file_get_contents($xml), array('return' => 'domdocument'))
  86. );
  87. $xml = array('tag' => 'value');
  88. $obj = Xml::build($xml);
  89. $this->assertEquals('tag', $obj->getName());
  90. $this->assertEquals('value', (string)$obj);
  91. $obj = Xml::build($xml, array('return' => 'domdocument'));
  92. $this->assertEquals('tag', $obj->firstChild->nodeName);
  93. $this->assertEquals('value', $obj->firstChild->nodeValue);
  94. $obj = Xml::build($xml, array('return' => 'domdocument', 'encoding' => null));
  95. $this->assertNotRegExp('/encoding/', $obj->saveXML());
  96. }
  97. /**
  98. * data provider function for testBuildInvalidData
  99. *
  100. * @return array
  101. */
  102. public static function invalidDataProvider() {
  103. return array(
  104. array(null),
  105. array(false),
  106. array(''),
  107. array('http://localhost/notthere.xml'),
  108. );
  109. }
  110. /**
  111. * testBuildInvalidData
  112. *
  113. * @dataProvider invalidDataProvider
  114. * @expectedException \Cake\Error\Exception
  115. * @return void
  116. */
  117. public function testBuildInvalidData($value) {
  118. $this->assertFalse(defined('HHVM_VERSION') && !empty($value), 'Segfaults HHVM');
  119. Xml::build($value);
  120. }
  121. /**
  122. * Test that building SimpleXmlElement with invalid XML causes the right exception.
  123. *
  124. * @expectedException \Cake\Utility\Error\XmlException
  125. * @return void
  126. */
  127. public function testBuildInvalidDataSimpleXml() {
  128. $input = '<derp';
  129. $xml = Xml::build($input, array('return' => 'simplexml'));
  130. }
  131. /**
  132. * test build with a single empty tag
  133. *
  134. * @return void
  135. */
  136. public function testBuildEmptyTag() {
  137. try {
  138. Xml::build('<tag>');
  139. $this->fail('No exception');
  140. } catch (\Exception $e) {
  141. $this->assertTrue(true, 'An exception was raised');
  142. }
  143. }
  144. /**
  145. * testFromArray method
  146. *
  147. * @return void
  148. */
  149. public function testFromArray() {
  150. $xml = array('tag' => 'value');
  151. $obj = Xml::fromArray($xml);
  152. $this->assertEquals('tag', $obj->getName());
  153. $this->assertEquals('value', (string)$obj);
  154. $xml = array('tag' => null);
  155. $obj = Xml::fromArray($xml);
  156. $this->assertEquals('tag', $obj->getName());
  157. $this->assertEquals('', (string)$obj);
  158. $xml = array('tag' => array('@' => 'value'));
  159. $obj = Xml::fromArray($xml);
  160. $this->assertEquals('tag', $obj->getName());
  161. $this->assertEquals('value', (string)$obj);
  162. $xml = array(
  163. 'tags' => array(
  164. 'tag' => array(
  165. array(
  166. 'id' => '1',
  167. 'name' => 'defect'
  168. ),
  169. array(
  170. 'id' => '2',
  171. 'name' => 'enhancement'
  172. )
  173. )
  174. )
  175. );
  176. $obj = Xml::fromArray($xml, 'attributes');
  177. $this->assertTrue($obj instanceof \SimpleXMLElement);
  178. $this->assertEquals('tags', $obj->getName());
  179. $this->assertEquals(2, count($obj));
  180. $xmlText = <<<XML
  181. <?xml version="1.0" encoding="UTF-8"?>
  182. <tags>
  183. <tag id="1" name="defect"/>
  184. <tag id="2" name="enhancement"/>
  185. </tags>
  186. XML;
  187. $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
  188. $obj = Xml::fromArray($xml);
  189. $this->assertTrue($obj instanceof \SimpleXMLElement);
  190. $this->assertEquals('tags', $obj->getName());
  191. $this->assertEquals(2, count($obj));
  192. $xmlText = <<<XML
  193. <?xml version="1.0" encoding="UTF-8"?>
  194. <tags>
  195. <tag>
  196. <id>1</id>
  197. <name>defect</name>
  198. </tag>
  199. <tag>
  200. <id>2</id>
  201. <name>enhancement</name>
  202. </tag>
  203. </tags>
  204. XML;
  205. $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
  206. $xml = array(
  207. 'tags' => array(
  208. )
  209. );
  210. $obj = Xml::fromArray($xml);
  211. $this->assertEquals('tags', $obj->getName());
  212. $this->assertEquals('', (string)$obj);
  213. $xml = array(
  214. 'tags' => array(
  215. 'bool' => true,
  216. 'int' => 1,
  217. 'float' => 10.2,
  218. 'string' => 'ok',
  219. 'null' => null,
  220. 'array' => array()
  221. )
  222. );
  223. $obj = Xml::fromArray($xml, 'tags');
  224. $this->assertEquals(6, count($obj));
  225. $this->assertSame((string)$obj->bool, '1');
  226. $this->assertSame((string)$obj->int, '1');
  227. $this->assertSame((string)$obj->float, '10.2');
  228. $this->assertSame((string)$obj->string, 'ok');
  229. $this->assertSame((string)$obj->null, '');
  230. $this->assertSame((string)$obj->array, '');
  231. $xml = array(
  232. 'tags' => array(
  233. 'tag' => array(
  234. array(
  235. '@id' => '1',
  236. 'name' => 'defect'
  237. ),
  238. array(
  239. '@id' => '2',
  240. 'name' => 'enhancement'
  241. )
  242. )
  243. )
  244. );
  245. $obj = Xml::fromArray($xml, 'tags');
  246. $xmlText = <<<XML
  247. <?xml version="1.0" encoding="UTF-8"?>
  248. <tags>
  249. <tag id="1">
  250. <name>defect</name>
  251. </tag>
  252. <tag id="2">
  253. <name>enhancement</name>
  254. </tag>
  255. </tags>
  256. XML;
  257. $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
  258. $xml = array(
  259. 'tags' => array(
  260. 'tag' => array(
  261. array(
  262. '@id' => '1',
  263. 'name' => 'defect',
  264. '@' => 'Tag 1'
  265. ),
  266. array(
  267. '@id' => '2',
  268. 'name' => 'enhancement'
  269. ),
  270. ),
  271. '@' => 'All tags'
  272. )
  273. );
  274. $obj = Xml::fromArray($xml, 'tags');
  275. $xmlText = <<<XML
  276. <?xml version="1.0" encoding="UTF-8"?>
  277. <tags>All tags<tag id="1">Tag 1<name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>
  278. XML;
  279. $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
  280. $xml = array(
  281. 'tags' => array(
  282. 'tag' => array(
  283. 'id' => 1,
  284. '@' => 'defect'
  285. )
  286. )
  287. );
  288. $obj = Xml::fromArray($xml, 'attributes');
  289. $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1">defect</tag></tags>';
  290. $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
  291. $xml = array(
  292. 'tag' => array(
  293. '@' => 0,
  294. '@test' => 'A test'
  295. )
  296. );
  297. $obj = Xml::fromArray($xml);
  298. $xmlText = <<<XML
  299. <?xml version="1.0" encoding="UTF-8"?>
  300. <tag test="A test">0</tag>
  301. XML;
  302. $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
  303. }
  304. /**
  305. * Test non-sequential keys in list types.
  306. *
  307. * @return void
  308. */
  309. public function testFromArrayNonSequentialKeys() {
  310. $xmlArray = array(
  311. 'Event' => array(
  312. array(
  313. 'id' => '235',
  314. 'Attribute' => array(
  315. 0 => array(
  316. 'id' => '9646',
  317. ),
  318. 2 => array(
  319. 'id' => '9647',
  320. )
  321. )
  322. )
  323. )
  324. );
  325. $obj = Xml::fromArray($xmlArray);
  326. $expected = <<<XML
  327. <?xml version="1.0" encoding="UTF-8"?>
  328. <Event>
  329. <id>235</id>
  330. <Attribute>
  331. <id>9646</id>
  332. </Attribute>
  333. <Attribute>
  334. <id>9647</id>
  335. </Attribute>
  336. </Event>
  337. XML;
  338. $this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
  339. }
  340. /**
  341. * testFromArrayPretty method
  342. *
  343. * @return void
  344. */
  345. public function testFromArrayPretty() {
  346. $xml = array(
  347. 'tags' => array(
  348. 'tag' => array(
  349. array(
  350. 'id' => '1',
  351. 'name' => 'defect'
  352. ),
  353. array(
  354. 'id' => '2',
  355. 'name' => 'enhancement'
  356. )
  357. )
  358. )
  359. );
  360. $expected = <<<XML
  361. <?xml version="1.0" encoding="UTF-8"?>
  362. <tags><tag><id>1</id><name>defect</name></tag><tag><id>2</id><name>enhancement</name></tag></tags>
  363. XML;
  364. $xmlResponse = Xml::fromArray($xml, array('pretty' => false));
  365. $this->assertTextEquals($expected, $xmlResponse->asXML());
  366. $expected = <<<XML
  367. <?xml version="1.0" encoding="UTF-8"?>
  368. <tags>
  369. <tag>
  370. <id>1</id>
  371. <name>defect</name>
  372. </tag>
  373. <tag>
  374. <id>2</id>
  375. <name>enhancement</name>
  376. </tag>
  377. </tags>
  378. XML;
  379. $xmlResponse = Xml::fromArray($xml, array('pretty' => true));
  380. $this->assertTextEquals($expected, $xmlResponse->asXML());
  381. $xml = array(
  382. 'tags' => array(
  383. 'tag' => array(
  384. array(
  385. 'id' => '1',
  386. 'name' => 'defect'
  387. ),
  388. array(
  389. 'id' => '2',
  390. 'name' => 'enhancement'
  391. )
  392. )
  393. )
  394. );
  395. $expected = <<<XML
  396. <?xml version="1.0" encoding="UTF-8"?>
  397. <tags><tag id="1" name="defect"/><tag id="2" name="enhancement"/></tags>
  398. XML;
  399. $xmlResponse = Xml::fromArray($xml, array('pretty' => false, 'format' => 'attributes'));
  400. $this->assertTextEquals($expected, $xmlResponse->asXML());
  401. $expected = <<<XML
  402. <?xml version="1.0" encoding="UTF-8"?>
  403. <tags>
  404. <tag id="1" name="defect"/>
  405. <tag id="2" name="enhancement"/>
  406. </tags>
  407. XML;
  408. $xmlResponse = Xml::fromArray($xml, array('pretty' => true, 'format' => 'attributes'));
  409. $this->assertTextEquals($expected, $xmlResponse->asXML());
  410. }
  411. /**
  412. * data provider for fromArray() failures
  413. *
  414. * @return array
  415. */
  416. public static function invalidArrayDataProvider() {
  417. return array(
  418. array(''),
  419. array(null),
  420. array(false),
  421. array(array()),
  422. array(array('numeric key as root')),
  423. array(array('item1' => '', 'item2' => '')),
  424. array(array('items' => array('item1', 'item2'))),
  425. array(array(
  426. 'tags' => array(
  427. 'tag' => array(
  428. array(
  429. array(
  430. 'string'
  431. )
  432. )
  433. )
  434. )
  435. )),
  436. array(array(
  437. 'tags' => array(
  438. '@tag' => array(
  439. array(
  440. '@id' => '1',
  441. 'name' => 'defect'
  442. ),
  443. array(
  444. '@id' => '2',
  445. 'name' => 'enhancement'
  446. )
  447. )
  448. )
  449. )),
  450. array(new \DateTime())
  451. );
  452. }
  453. /**
  454. * testFromArrayFail method
  455. *
  456. * @dataProvider invalidArrayDataProvider
  457. * @return void
  458. */
  459. public function testFromArrayFail($value) {
  460. try {
  461. Xml::fromArray($value);
  462. $this->fail('No exception.');
  463. } catch (\Exception $e) {
  464. $this->assertTrue(true, 'Caught exception.');
  465. }
  466. }
  467. /**
  468. * Test that there are not unterminated errors when building xml
  469. *
  470. * @return void
  471. */
  472. public function testFromArrayUnterminatedError() {
  473. $data = array(
  474. 'product_ID' => 'GENERT-DL',
  475. 'deeplink' => 'http://example.com/deep',
  476. 'image_URL' => 'http://example.com/image',
  477. 'thumbnail_image_URL' => 'http://example.com/thumb',
  478. 'brand' => 'Malte Lange & Co',
  479. 'availability' => 'in stock',
  480. 'authors' => array(
  481. 'author' => array('Malte Lange & Co')
  482. )
  483. );
  484. $xml = Xml::fromArray(array('products' => $data), 'tags');
  485. $expected = <<<XML
  486. <?xml version="1.0" encoding="UTF-8"?>
  487. <products>
  488. <product_ID>GENERT-DL</product_ID>
  489. <deeplink>http://example.com/deep</deeplink>
  490. <image_URL>http://example.com/image</image_URL>
  491. <thumbnail_image_URL>http://example.com/thumb</thumbnail_image_URL>
  492. <brand>Malte Lange &amp; Co</brand>
  493. <availability>in stock</availability>
  494. <authors>
  495. <author>Malte Lange &amp; Co</author>
  496. </authors>
  497. </products>
  498. XML;
  499. $this->assertXmlStringEqualsXmlString($expected, $xml->asXML());
  500. }
  501. /**
  502. * testToArray method
  503. *
  504. * @return void
  505. */
  506. public function testToArray() {
  507. $xml = '<tag>name</tag>';
  508. $obj = Xml::build($xml);
  509. $this->assertEquals(array('tag' => 'name'), Xml::toArray($obj));
  510. $xml = CORE_TESTS . 'Fixture/sample.xml';
  511. $obj = Xml::build($xml);
  512. $expected = array(
  513. 'tags' => array(
  514. 'tag' => array(
  515. array(
  516. '@id' => '1',
  517. 'name' => 'defect'
  518. ),
  519. array(
  520. '@id' => '2',
  521. 'name' => 'enhancement'
  522. )
  523. )
  524. )
  525. );
  526. $this->assertEquals($expected, Xml::toArray($obj));
  527. $array = array(
  528. 'tags' => array(
  529. 'tag' => array(
  530. array(
  531. 'id' => '1',
  532. 'name' => 'defect'
  533. ),
  534. array(
  535. 'id' => '2',
  536. 'name' => 'enhancement'
  537. )
  538. )
  539. )
  540. );
  541. $this->assertEquals(Xml::toArray(Xml::fromArray($array, 'tags')), $array);
  542. $expected = array(
  543. 'tags' => array(
  544. 'tag' => array(
  545. array(
  546. '@id' => '1',
  547. '@name' => 'defect'
  548. ),
  549. array(
  550. '@id' => '2',
  551. '@name' => 'enhancement'
  552. )
  553. )
  554. )
  555. );
  556. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
  557. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument', 'format' => 'attributes'))));
  558. $this->assertEquals(Xml::toArray(Xml::fromArray($array)), $array);
  559. $this->assertEquals(Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument'))), $array);
  560. $array = array(
  561. 'tags' => array(
  562. 'tag' => array(
  563. 'id' => '1',
  564. 'posts' => array(
  565. array('id' => '1'),
  566. array('id' => '2')
  567. )
  568. ),
  569. 'tagOther' => array(
  570. 'subtag' => array(
  571. 'id' => '1'
  572. )
  573. )
  574. )
  575. );
  576. $expected = array(
  577. 'tags' => array(
  578. 'tag' => array(
  579. '@id' => '1',
  580. 'posts' => array(
  581. array('@id' => '1'),
  582. array('@id' => '2')
  583. )
  584. ),
  585. 'tagOther' => array(
  586. 'subtag' => array(
  587. '@id' => '1'
  588. )
  589. )
  590. )
  591. );
  592. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
  593. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('format' => 'attributes', 'return' => 'domdocument'))));
  594. $xml = <<<XML
  595. <root>
  596. <tag id="1">defect</tag>
  597. </root>
  598. XML;
  599. $obj = Xml::build($xml);
  600. $expected = array(
  601. 'root' => array(
  602. 'tag' => array(
  603. '@id' => 1,
  604. '@' => 'defect'
  605. )
  606. )
  607. );
  608. $this->assertEquals($expected, Xml::toArray($obj));
  609. $xml = <<<XML
  610. <root>
  611. <table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>
  612. <table xmlns="http://www.cakephp.org"><name>CakePHP</name><license>MIT</license></table>
  613. <table>The book is on the table.</table>
  614. </root>
  615. XML;
  616. $obj = Xml::build($xml);
  617. $expected = array(
  618. 'root' => array(
  619. 'table' => array(
  620. array('tr' => array('td' => array('Apples', 'Bananas'))),
  621. array('name' => 'CakePHP', 'license' => 'MIT'),
  622. 'The book is on the table.'
  623. )
  624. )
  625. );
  626. $this->assertEquals($expected, Xml::toArray($obj));
  627. $xml = <<<XML
  628. <root xmlns:cake="http://www.cakephp.org/">
  629. <tag>defect</tag>
  630. <cake:bug>1</cake:bug>
  631. </root>
  632. XML;
  633. $obj = Xml::build($xml);
  634. $expected = array(
  635. 'root' => array(
  636. 'tag' => 'defect',
  637. 'cake:bug' => 1
  638. )
  639. );
  640. $this->assertEquals($expected, Xml::toArray($obj));
  641. $xml = '<tag type="myType">0</tag>';
  642. $obj = Xml::build($xml);
  643. $expected = array(
  644. 'tag' => array(
  645. '@type' => 'myType',
  646. '@' => 0
  647. )
  648. );
  649. $this->assertEquals($expected, Xml::toArray($obj));
  650. }
  651. /**
  652. * testRss
  653. *
  654. * @return void
  655. */
  656. public function testRss() {
  657. $rss = file_get_contents(CORE_TESTS . 'Fixture/rss.xml');
  658. $rssAsArray = Xml::toArray(Xml::build($rss));
  659. $this->assertEquals('2.0', $rssAsArray['rss']['@version']);
  660. $this->assertEquals(2, count($rssAsArray['rss']['channel']['item']));
  661. $atomLink = array('@href' => 'http://bakery.cakephp.org/articles/rss', '@rel' => 'self', '@type' => 'application/rss+xml');
  662. $this->assertEquals($rssAsArray['rss']['channel']['atom:link'], $atomLink);
  663. $this->assertEquals('http://bakery.cakephp.org/', $rssAsArray['rss']['channel']['link']);
  664. $expected = array(
  665. 'title' => 'Alertpay automated sales via IPN',
  666. 'link' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn',
  667. 'description' => 'I\'m going to show you how I implemented a payment module via the Alertpay payment processor.',
  668. 'pubDate' => 'Tue, 31 Aug 2010 01:42:00 -0500',
  669. 'guid' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn'
  670. );
  671. $this->assertSame($expected, $rssAsArray['rss']['channel']['item'][1]);
  672. $rss = array(
  673. 'rss' => array(
  674. 'xmlns:atom' => 'http://www.w3.org/2005/Atom',
  675. '@version' => '2.0',
  676. 'channel' => array(
  677. 'atom:link' => array(
  678. '@href' => 'http://bakery.cakephp.org/articles/rss',
  679. '@rel' => 'self',
  680. '@type' => 'application/rss+xml'
  681. ),
  682. 'title' => 'The Bakery: ',
  683. 'link' => 'http://bakery.cakephp.org/',
  684. 'description' => 'Recent Articles at The Bakery.',
  685. 'pubDate' => 'Sun, 12 Sep 2010 04:18:26 -0500',
  686. 'item' => array(
  687. array(
  688. 'title' => 'CakePHP 1.3.4 released',
  689. 'link' => 'http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released'
  690. ),
  691. array(
  692. 'title' => 'Wizard Component 1.2 Tutorial',
  693. 'link' => 'http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial'
  694. )
  695. )
  696. )
  697. )
  698. );
  699. $rssAsSimpleXML = Xml::fromArray($rss);
  700. $xmlText = <<<XML
  701. <?xml version="1.0" encoding="UTF-8"?>
  702. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  703. <channel>
  704. <atom:link href="http://bakery.cakephp.org/articles/rss" rel="self" type="application/rss+xml"/>
  705. <title>The Bakery: </title>
  706. <link>http://bakery.cakephp.org/</link>
  707. <description>Recent Articles at The Bakery.</description>
  708. <pubDate>Sun, 12 Sep 2010 04:18:26 -0500</pubDate>
  709. <item>
  710. <title>CakePHP 1.3.4 released</title>
  711. <link>http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released</link>
  712. </item>
  713. <item>
  714. <title>Wizard Component 1.2 Tutorial</title>
  715. <link>http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial</link>
  716. </item>
  717. </channel>
  718. </rss>
  719. XML;
  720. $this->assertXmlStringEqualsXmlString($xmlText, $rssAsSimpleXML->asXML());
  721. }
  722. /**
  723. * testXmlRpc
  724. *
  725. * @return void
  726. */
  727. public function testXmlRpc() {
  728. $xml = Xml::build('<methodCall><methodName>test</methodName><params /></methodCall>');
  729. $expected = array(
  730. 'methodCall' => array(
  731. 'methodName' => 'test',
  732. 'params' => ''
  733. )
  734. );
  735. $this->assertSame($expected, Xml::toArray($xml));
  736. $xml = Xml::build('<methodCall><methodName>test</methodName><params><param><value><array><data><value><int>12</int></value><value><string>Egypt</string></value><value><boolean>0</boolean></value><value><int>-31</int></value></data></array></value></param></params></methodCall>');
  737. $expected = array(
  738. 'methodCall' => array(
  739. 'methodName' => 'test',
  740. 'params' => array(
  741. 'param' => array(
  742. 'value' => array(
  743. 'array' => array(
  744. 'data' => array(
  745. 'value' => array(
  746. array('int' => '12'),
  747. array('string' => 'Egypt'),
  748. array('boolean' => '0'),
  749. array('int' => '-31')
  750. )
  751. )
  752. )
  753. )
  754. )
  755. )
  756. )
  757. );
  758. $this->assertSame($expected, Xml::toArray($xml));
  759. $xmlText = <<<XML
  760. <?xml version="1.0" encoding="UTF-8"?>
  761. <methodResponse>
  762. <params>
  763. <param>
  764. <value>
  765. <array>
  766. <data>
  767. <value>
  768. <int>1</int>
  769. </value>
  770. <value>
  771. <string>testing</string>
  772. </value>
  773. </data>
  774. </array>
  775. </value>
  776. </param>
  777. </params>
  778. </methodResponse>
  779. XML;
  780. $xml = Xml::build($xmlText);
  781. $expected = array(
  782. 'methodResponse' => array(
  783. 'params' => array(
  784. 'param' => array(
  785. 'value' => array(
  786. 'array' => array(
  787. 'data' => array(
  788. 'value' => array(
  789. array('int' => '1'),
  790. array('string' => 'testing')
  791. )
  792. )
  793. )
  794. )
  795. )
  796. )
  797. )
  798. );
  799. $this->assertSame($expected, Xml::toArray($xml));
  800. $xml = Xml::fromArray($expected, 'tags');
  801. $this->assertXmlStringEqualsXmlString($xmlText, $xml->asXML());
  802. }
  803. /**
  804. * testSoap
  805. *
  806. * @return void
  807. */
  808. public function testSoap() {
  809. $xmlRequest = Xml::build(CORE_TESTS . 'Fixture/soap_request.xml');
  810. $expected = array(
  811. 'Envelope' => array(
  812. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  813. 'soap:Body' => array(
  814. 'm:GetStockPrice' => array(
  815. 'm:StockName' => 'IBM'
  816. )
  817. )
  818. )
  819. );
  820. $this->assertEquals($expected, Xml::toArray($xmlRequest));
  821. $xmlResponse = Xml::build(CORE_TESTS . DS . 'Fixture/soap_response.xml');
  822. $expected = array(
  823. 'Envelope' => array(
  824. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  825. 'soap:Body' => array(
  826. 'm:GetStockPriceResponse' => array(
  827. 'm:Price' => '34.5'
  828. )
  829. )
  830. )
  831. );
  832. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  833. $xml = array(
  834. 'soap:Envelope' => array(
  835. 'xmlns:soap' => 'http://www.w3.org/2001/12/soap-envelope',
  836. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  837. 'soap:Body' => array(
  838. 'xmlns:m' => 'http://www.example.org/stock',
  839. 'm:GetStockPrice' => array(
  840. 'm:StockName' => 'IBM'
  841. )
  842. )
  843. )
  844. );
  845. $xmlRequest = Xml::fromArray($xml, array('encoding' => null));
  846. $xmlText = <<<XML
  847. <?xml version="1.0"?>
  848. <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  849. <soap:Body xmlns:m="http://www.example.org/stock">
  850. <m:GetStockPrice><m:StockName>IBM</m:StockName></m:GetStockPrice>
  851. </soap:Body>
  852. </soap:Envelope>
  853. XML;
  854. $this->assertXmlStringEqualsXmlString($xmlText, $xmlRequest->asXML());
  855. }
  856. /**
  857. * testNamespace
  858. *
  859. * @return void
  860. */
  861. public function testNamespace() {
  862. $xml = <<<XML
  863. <root xmlns:ns="http://cakephp.org">
  864. <ns:tag id="1">
  865. <child>good</child>
  866. <otherchild>bad</otherchild>
  867. </ns:tag>
  868. <tag>Tag without ns</tag>
  869. </root>
  870. XML;
  871. $xmlResponse = Xml::build($xml);
  872. $expected = array(
  873. 'root' => array(
  874. 'ns:tag' => array(
  875. '@id' => '1',
  876. 'child' => 'good',
  877. 'otherchild' => 'bad'
  878. ),
  879. 'tag' => 'Tag without ns'
  880. )
  881. );
  882. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  883. $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:tag id="1" /><tag><id>1</id></tag></root>');
  884. $expected = array(
  885. 'root' => array(
  886. 'ns:tag' => array(
  887. '@id' => '1'
  888. ),
  889. 'tag' => array(
  890. 'id' => '1'
  891. )
  892. )
  893. );
  894. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  895. $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:attr>1</ns:attr></root>');
  896. $expected = array(
  897. 'root' => array(
  898. 'ns:attr' => '1'
  899. )
  900. );
  901. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  902. $xmlResponse = Xml::build('<root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>');
  903. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  904. $xml = array(
  905. 'root' => array(
  906. 'ns:attr' => array(
  907. 'xmlns:ns' => 'http://cakephp.org',
  908. '@' => 1
  909. )
  910. )
  911. );
  912. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>';
  913. $xmlResponse = Xml::fromArray($xml);
  914. $this->assertEquals($expected, str_replace(array("\r", "\n"), '', $xmlResponse->asXML()));
  915. $xml = array(
  916. 'root' => array(
  917. 'tag' => array(
  918. 'xmlns:pref' => 'http://cakephp.org',
  919. 'pref:item' => array(
  920. 'item 1',
  921. 'item 2'
  922. )
  923. )
  924. )
  925. );
  926. $expected = <<<XML
  927. <?xml version="1.0" encoding="UTF-8"?>
  928. <root>
  929. <tag xmlns:pref="http://cakephp.org">
  930. <pref:item>item 1</pref:item>
  931. <pref:item>item 2</pref:item>
  932. </tag>
  933. </root>
  934. XML;
  935. $xmlResponse = Xml::fromArray($xml);
  936. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  937. $xml = array(
  938. 'root' => array(
  939. 'tag' => array(
  940. 'xmlns:' => 'http://cakephp.org'
  941. )
  942. )
  943. );
  944. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns="http://cakephp.org"/></root>';
  945. $xmlResponse = Xml::fromArray($xml);
  946. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  947. $xml = array(
  948. 'root' => array(
  949. 'xmlns:' => 'http://cakephp.org'
  950. )
  951. );
  952. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns="http://cakephp.org"/>';
  953. $xmlResponse = Xml::fromArray($xml);
  954. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  955. $xml = array(
  956. 'root' => array(
  957. 'xmlns:ns' => 'http://cakephp.org'
  958. )
  959. );
  960. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns:ns="http://cakephp.org"/>';
  961. $xmlResponse = Xml::fromArray($xml);
  962. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  963. }
  964. /**
  965. * test that CDATA blocks don't get screwed up by SimpleXml
  966. *
  967. * @return void
  968. */
  969. public function testCdata() {
  970. $xml = '<' . '?xml version="1.0" encoding="UTF-8"?>' .
  971. '<people><name><![CDATA[ Mark ]]></name></people>';
  972. $result = Xml::build($xml);
  973. $this->assertEquals(' Mark ', (string)$result->name);
  974. }
  975. /**
  976. * data provider for toArray() failures
  977. *
  978. * @return array
  979. */
  980. public static function invalidToArrayDataProvider() {
  981. return array(
  982. array(new \DateTime()),
  983. array(array())
  984. );
  985. }
  986. /**
  987. * testToArrayFail method
  988. *
  989. * @dataProvider invalidToArrayDataProvider
  990. * @expectedException \Cake\Utility\Error\XmlException
  991. * @return void
  992. */
  993. public function testToArrayFail($value) {
  994. Xml::toArray($value);
  995. }
  996. /**
  997. * testWithModel method
  998. *
  999. * @return void
  1000. */
  1001. public function testWithModel() {
  1002. $this->markTestIncomplete('Models do not work right now');
  1003. $this->loadFixtures('User', 'Article');
  1004. $user = new XmlUser();
  1005. $data = $user->read(null, 1);
  1006. $obj = Xml::build(compact('data'));
  1007. $expected = <<<XML
  1008. <?xml version="1.0" encoding="UTF-8"?><data>
  1009. <User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
  1010. <created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>
  1011. <Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>
  1012. <published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>
  1013. <Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>
  1014. <published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>
  1015. </data>
  1016. XML;
  1017. $this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
  1018. //multiple model results - without a records key it would fatal error
  1019. $data = $user->find('all', array('limit' => 2));
  1020. $data = array('records' => $data);
  1021. $obj = Xml::build(compact('data'));
  1022. $expected = <<<XML
  1023. <?xml version="1.0" encoding="UTF-8"?><data>
  1024. <records>
  1025. <User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
  1026. <created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>
  1027. <Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>
  1028. <published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>
  1029. <Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>
  1030. <published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>
  1031. </records><records><User><id>2</id><user>nate</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
  1032. <created>2007-03-17 01:18:23</created><updated>2007-03-17 01:20:31</updated></User><Article/>
  1033. </records>
  1034. </data>
  1035. XML;
  1036. $obj->asXML();
  1037. $this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
  1038. }
  1039. /**
  1040. * Test ampersand in text elements.
  1041. *
  1042. * @return void
  1043. */
  1044. public function testAmpInText() {
  1045. $data = array(
  1046. 'outer' => array(
  1047. 'inner' => array('name' => 'mark & mark')
  1048. )
  1049. );
  1050. $obj = Xml::build($data);
  1051. $result = $obj->asXml();
  1052. $this->assertContains('mark &amp; mark', $result);
  1053. }
  1054. /**
  1055. * Test that entity loading is disabled by default.
  1056. *
  1057. * @return void
  1058. */
  1059. public function testNoEntityLoading() {
  1060. $file = str_replace(' ', '%20', CAKE . 'VERSION.txt');
  1061. $xml = <<<XML
  1062. <!DOCTYPE cakephp [
  1063. <!ENTITY payload SYSTEM "file://$file" >]>
  1064. <request>
  1065. <xxe>&payload;</xxe>
  1066. </request>
  1067. XML;
  1068. try {
  1069. $result = Xml::build($xml);
  1070. $this->assertEquals('', (string)$result->xxe);
  1071. } catch (Exception $e) {
  1072. $this->assertTrue(true, 'A warning was raised meaning external entities were not loaded');
  1073. }
  1074. }
  1075. }