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