XmlTest.php 35 KB

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