XmlTest.php 34 KB

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