XmlTest.php 34 KB

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