XmlTest.php 36 KB

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