XmlTest.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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. $xml = [
  346. 'tag' => [
  347. '@' => 0,
  348. '@test' => 'A test'
  349. ]
  350. ];
  351. $obj = Xml::fromArray($xml);
  352. $xmlText = <<<XML
  353. <?xml version="1.0" encoding="UTF-8"?>
  354. <tag test="A test">0</tag>
  355. XML;
  356. $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
  357. }
  358. /**
  359. * Test non-sequential keys in list types.
  360. *
  361. * @return void
  362. */
  363. public function testFromArrayNonSequentialKeys()
  364. {
  365. $xmlArray = [
  366. 'Event' => [
  367. [
  368. 'id' => '235',
  369. 'Attribute' => [
  370. 0 => [
  371. 'id' => '9646',
  372. ],
  373. 2 => [
  374. 'id' => '9647',
  375. ]
  376. ]
  377. ]
  378. ]
  379. ];
  380. $obj = Xml::fromArray($xmlArray);
  381. $expected = <<<XML
  382. <?xml version="1.0" encoding="UTF-8"?>
  383. <Event>
  384. <id>235</id>
  385. <Attribute>
  386. <id>9646</id>
  387. </Attribute>
  388. <Attribute>
  389. <id>9647</id>
  390. </Attribute>
  391. </Event>
  392. XML;
  393. $this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
  394. }
  395. /**
  396. * testFromArrayPretty method
  397. *
  398. * @return void
  399. */
  400. public function testFromArrayPretty()
  401. {
  402. $xml = [
  403. 'tags' => [
  404. 'tag' => [
  405. [
  406. 'id' => '1',
  407. 'name' => 'defect'
  408. ],
  409. [
  410. 'id' => '2',
  411. 'name' => 'enhancement'
  412. ]
  413. ]
  414. ]
  415. ];
  416. $expected = <<<XML
  417. <?xml version="1.0" encoding="UTF-8"?>
  418. <tags><tag><id>1</id><name>defect</name></tag><tag><id>2</id><name>enhancement</name></tag></tags>
  419. XML;
  420. $xmlResponse = Xml::fromArray($xml, ['pretty' => false]);
  421. $this->assertTextEquals($expected, $xmlResponse->asXML());
  422. $expected = <<<XML
  423. <?xml version="1.0" encoding="UTF-8"?>
  424. <tags>
  425. <tag>
  426. <id>1</id>
  427. <name>defect</name>
  428. </tag>
  429. <tag>
  430. <id>2</id>
  431. <name>enhancement</name>
  432. </tag>
  433. </tags>
  434. XML;
  435. $xmlResponse = Xml::fromArray($xml, ['pretty' => true]);
  436. $this->assertTextEquals($expected, $xmlResponse->asXML());
  437. $xml = [
  438. 'tags' => [
  439. 'tag' => [
  440. [
  441. 'id' => '1',
  442. 'name' => 'defect'
  443. ],
  444. [
  445. 'id' => '2',
  446. 'name' => 'enhancement'
  447. ]
  448. ]
  449. ]
  450. ];
  451. $expected = <<<XML
  452. <?xml version="1.0" encoding="UTF-8"?>
  453. <tags><tag id="1" name="defect"/><tag id="2" name="enhancement"/></tags>
  454. XML;
  455. $xmlResponse = Xml::fromArray($xml, ['pretty' => false, 'format' => 'attributes']);
  456. $this->assertTextEquals($expected, $xmlResponse->asXML());
  457. $expected = <<<XML
  458. <?xml version="1.0" encoding="UTF-8"?>
  459. <tags>
  460. <tag id="1" name="defect"/>
  461. <tag id="2" name="enhancement"/>
  462. </tags>
  463. XML;
  464. $xmlResponse = Xml::fromArray($xml, ['pretty' => true, 'format' => 'attributes']);
  465. $this->assertTextEquals($expected, $xmlResponse->asXML());
  466. }
  467. /**
  468. * data provider for fromArray() failures
  469. *
  470. * @return array
  471. */
  472. public static function invalidArrayDataProvider()
  473. {
  474. return [
  475. [''],
  476. [null],
  477. [false],
  478. [[]],
  479. [['numeric key as root']],
  480. [['item1' => '', 'item2' => '']],
  481. [['items' => ['item1', 'item2']]],
  482. [[
  483. 'tags' => [
  484. 'tag' => [
  485. [
  486. [
  487. 'string'
  488. ]
  489. ]
  490. ]
  491. ]
  492. ]],
  493. [[
  494. 'tags' => [
  495. '@tag' => [
  496. [
  497. '@id' => '1',
  498. 'name' => 'defect'
  499. ],
  500. [
  501. '@id' => '2',
  502. 'name' => 'enhancement'
  503. ]
  504. ]
  505. ]
  506. ]],
  507. [new \DateTime()]
  508. ];
  509. }
  510. /**
  511. * testFromArrayFail method
  512. *
  513. * @dataProvider invalidArrayDataProvider
  514. * @expectedException \Exception
  515. * @return void
  516. */
  517. public function testFromArrayFail($value)
  518. {
  519. Xml::fromArray($value);
  520. }
  521. /**
  522. * Test that there are not unterminated errors when building xml
  523. *
  524. * @return void
  525. */
  526. public function testFromArrayUnterminatedError()
  527. {
  528. $data = [
  529. 'product_ID' => 'GENERT-DL',
  530. 'deeplink' => 'http://example.com/deep',
  531. 'image_URL' => 'http://example.com/image',
  532. 'thumbnail_image_URL' => 'http://example.com/thumb',
  533. 'brand' => 'Malte Lange & Co',
  534. 'availability' => 'in stock',
  535. 'authors' => [
  536. 'author' => ['Malte Lange & Co']
  537. ]
  538. ];
  539. $xml = Xml::fromArray(['products' => $data], 'tags');
  540. $expected = <<<XML
  541. <?xml version="1.0" encoding="UTF-8"?>
  542. <products>
  543. <product_ID>GENERT-DL</product_ID>
  544. <deeplink>http://example.com/deep</deeplink>
  545. <image_URL>http://example.com/image</image_URL>
  546. <thumbnail_image_URL>http://example.com/thumb</thumbnail_image_URL>
  547. <brand>Malte Lange &amp; Co</brand>
  548. <availability>in stock</availability>
  549. <authors>
  550. <author>Malte Lange &amp; Co</author>
  551. </authors>
  552. </products>
  553. XML;
  554. $this->assertXmlStringEqualsXmlString($expected, $xml->asXML());
  555. }
  556. /**
  557. * testToArray method
  558. *
  559. * @return void
  560. */
  561. public function testToArray()
  562. {
  563. $xml = '<tag>name</tag>';
  564. $obj = Xml::build($xml);
  565. $this->assertEquals(['tag' => 'name'], Xml::toArray($obj));
  566. $xml = CORE_TESTS . 'Fixture/sample.xml';
  567. $obj = Xml::build($xml);
  568. $expected = [
  569. 'tags' => [
  570. 'tag' => [
  571. [
  572. '@id' => '1',
  573. 'name' => 'defect'
  574. ],
  575. [
  576. '@id' => '2',
  577. 'name' => 'enhancement'
  578. ]
  579. ]
  580. ]
  581. ];
  582. $this->assertEquals($expected, Xml::toArray($obj));
  583. $array = [
  584. 'tags' => [
  585. 'tag' => [
  586. [
  587. 'id' => '1',
  588. 'name' => 'defect'
  589. ],
  590. [
  591. 'id' => '2',
  592. 'name' => 'enhancement'
  593. ]
  594. ]
  595. ]
  596. ];
  597. $this->assertEquals(Xml::toArray(Xml::fromArray($array, 'tags')), $array);
  598. $expected = [
  599. 'tags' => [
  600. 'tag' => [
  601. [
  602. '@id' => '1',
  603. '@name' => 'defect'
  604. ],
  605. [
  606. '@id' => '2',
  607. '@name' => 'enhancement'
  608. ]
  609. ]
  610. ]
  611. ];
  612. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
  613. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, ['return' => 'domdocument', 'format' => 'attributes'])));
  614. $this->assertEquals(Xml::toArray(Xml::fromArray($array)), $array);
  615. $this->assertEquals(Xml::toArray(Xml::fromArray($array, ['return' => 'domdocument'])), $array);
  616. $array = [
  617. 'tags' => [
  618. 'tag' => [
  619. 'id' => '1',
  620. 'posts' => [
  621. ['id' => '1'],
  622. ['id' => '2']
  623. ]
  624. ],
  625. 'tagOther' => [
  626. 'subtag' => [
  627. 'id' => '1'
  628. ]
  629. ]
  630. ]
  631. ];
  632. $expected = [
  633. 'tags' => [
  634. 'tag' => [
  635. '@id' => '1',
  636. 'posts' => [
  637. ['@id' => '1'],
  638. ['@id' => '2']
  639. ]
  640. ],
  641. 'tagOther' => [
  642. 'subtag' => [
  643. '@id' => '1'
  644. ]
  645. ]
  646. ]
  647. ];
  648. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
  649. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, ['format' => 'attributes', 'return' => 'domdocument'])));
  650. $xml = <<<XML
  651. <root>
  652. <tag id="1">defect</tag>
  653. </root>
  654. XML;
  655. $obj = Xml::build($xml);
  656. $expected = [
  657. 'root' => [
  658. 'tag' => [
  659. '@id' => 1,
  660. '@' => 'defect'
  661. ]
  662. ]
  663. ];
  664. $this->assertEquals($expected, Xml::toArray($obj));
  665. $xml = <<<XML
  666. <root>
  667. <table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>
  668. <table xmlns="http://www.cakephp.org"><name>CakePHP</name><license>MIT</license></table>
  669. <table>The book is on the table.</table>
  670. </root>
  671. XML;
  672. $obj = Xml::build($xml);
  673. $expected = [
  674. 'root' => [
  675. 'table' => [
  676. ['tr' => ['td' => ['Apples', 'Bananas']]],
  677. ['name' => 'CakePHP', 'license' => 'MIT'],
  678. 'The book is on the table.'
  679. ]
  680. ]
  681. ];
  682. $this->assertEquals($expected, Xml::toArray($obj));
  683. $xml = <<<XML
  684. <root xmlns:cake="http://www.cakephp.org/">
  685. <tag>defect</tag>
  686. <cake:bug>1</cake:bug>
  687. </root>
  688. XML;
  689. $obj = Xml::build($xml);
  690. $expected = [
  691. 'root' => [
  692. 'tag' => 'defect',
  693. 'cake:bug' => 1
  694. ]
  695. ];
  696. $this->assertEquals($expected, Xml::toArray($obj));
  697. $xml = '<tag type="myType">0</tag>';
  698. $obj = Xml::build($xml);
  699. $expected = [
  700. 'tag' => [
  701. '@type' => 'myType',
  702. '@' => 0
  703. ]
  704. ];
  705. $this->assertEquals($expected, Xml::toArray($obj));
  706. }
  707. /**
  708. * testRss
  709. *
  710. * @return void
  711. */
  712. public function testRss()
  713. {
  714. $rss = file_get_contents(CORE_TESTS . 'Fixture/rss.xml');
  715. $rssAsArray = Xml::toArray(Xml::build($rss));
  716. $this->assertEquals('2.0', $rssAsArray['rss']['@version']);
  717. $this->assertEquals(2, count($rssAsArray['rss']['channel']['item']));
  718. $atomLink = ['@href' => 'http://bakery.cakephp.org/articles/rss', '@rel' => 'self', '@type' => 'application/rss+xml'];
  719. $this->assertEquals($rssAsArray['rss']['channel']['atom:link'], $atomLink);
  720. $this->assertEquals('http://bakery.cakephp.org/', $rssAsArray['rss']['channel']['link']);
  721. $expected = [
  722. 'title' => 'Alertpay automated sales via IPN',
  723. 'link' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn',
  724. 'description' => 'I\'m going to show you how I implemented a payment module via the Alertpay payment processor.',
  725. 'pubDate' => 'Tue, 31 Aug 2010 01:42:00 -0500',
  726. 'guid' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn'
  727. ];
  728. $this->assertSame($expected, $rssAsArray['rss']['channel']['item'][1]);
  729. $rss = [
  730. 'rss' => [
  731. 'xmlns:atom' => 'http://www.w3.org/2005/Atom',
  732. '@version' => '2.0',
  733. 'channel' => [
  734. 'atom:link' => [
  735. '@href' => 'http://bakery.cakephp.org/articles/rss',
  736. '@rel' => 'self',
  737. '@type' => 'application/rss+xml'
  738. ],
  739. 'title' => 'The Bakery: ',
  740. 'link' => 'http://bakery.cakephp.org/',
  741. 'description' => 'Recent Articles at The Bakery.',
  742. 'pubDate' => 'Sun, 12 Sep 2010 04:18:26 -0500',
  743. 'item' => [
  744. [
  745. 'title' => 'CakePHP 1.3.4 released',
  746. 'link' => 'http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released'
  747. ],
  748. [
  749. 'title' => 'Wizard Component 1.2 Tutorial',
  750. 'link' => 'http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial'
  751. ]
  752. ]
  753. ]
  754. ]
  755. ];
  756. $rssAsSimpleXML = Xml::fromArray($rss);
  757. $xmlText = <<<XML
  758. <?xml version="1.0" encoding="UTF-8"?>
  759. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  760. <channel>
  761. <atom:link href="http://bakery.cakephp.org/articles/rss" rel="self" type="application/rss+xml"/>
  762. <title>The Bakery: </title>
  763. <link>http://bakery.cakephp.org/</link>
  764. <description>Recent Articles at The Bakery.</description>
  765. <pubDate>Sun, 12 Sep 2010 04:18:26 -0500</pubDate>
  766. <item>
  767. <title>CakePHP 1.3.4 released</title>
  768. <link>http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released</link>
  769. </item>
  770. <item>
  771. <title>Wizard Component 1.2 Tutorial</title>
  772. <link>http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial</link>
  773. </item>
  774. </channel>
  775. </rss>
  776. XML;
  777. $this->assertXmlStringEqualsXmlString($xmlText, $rssAsSimpleXML->asXML());
  778. }
  779. /**
  780. * testXmlRpc
  781. *
  782. * @return void
  783. */
  784. public function testXmlRpc()
  785. {
  786. $xml = Xml::build('<methodCall><methodName>test</methodName><params /></methodCall>');
  787. $expected = [
  788. 'methodCall' => [
  789. 'methodName' => 'test',
  790. 'params' => ''
  791. ]
  792. ];
  793. $this->assertSame($expected, Xml::toArray($xml));
  794. $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>');
  795. $expected = [
  796. 'methodCall' => [
  797. 'methodName' => 'test',
  798. 'params' => [
  799. 'param' => [
  800. 'value' => [
  801. 'array' => [
  802. 'data' => [
  803. 'value' => [
  804. ['int' => '12'],
  805. ['string' => 'Egypt'],
  806. ['boolean' => '0'],
  807. ['int' => '-31']
  808. ]
  809. ]
  810. ]
  811. ]
  812. ]
  813. ]
  814. ]
  815. ];
  816. $this->assertSame($expected, Xml::toArray($xml));
  817. $xmlText = <<<XML
  818. <?xml version="1.0" encoding="UTF-8"?>
  819. <methodResponse>
  820. <params>
  821. <param>
  822. <value>
  823. <array>
  824. <data>
  825. <value>
  826. <int>1</int>
  827. </value>
  828. <value>
  829. <string>testing</string>
  830. </value>
  831. </data>
  832. </array>
  833. </value>
  834. </param>
  835. </params>
  836. </methodResponse>
  837. XML;
  838. $xml = Xml::build($xmlText);
  839. $expected = [
  840. 'methodResponse' => [
  841. 'params' => [
  842. 'param' => [
  843. 'value' => [
  844. 'array' => [
  845. 'data' => [
  846. 'value' => [
  847. ['int' => '1'],
  848. ['string' => 'testing']
  849. ]
  850. ]
  851. ]
  852. ]
  853. ]
  854. ]
  855. ]
  856. ];
  857. $this->assertSame($expected, Xml::toArray($xml));
  858. $xml = Xml::fromArray($expected, 'tags');
  859. $this->assertXmlStringEqualsXmlString($xmlText, $xml->asXML());
  860. }
  861. /**
  862. * testSoap
  863. *
  864. * @return void
  865. */
  866. public function testSoap()
  867. {
  868. $xmlRequest = Xml::build(CORE_TESTS . 'Fixture/soap_request.xml');
  869. $expected = [
  870. 'Envelope' => [
  871. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  872. 'soap:Body' => [
  873. 'm:GetStockPrice' => [
  874. 'm:StockName' => 'IBM'
  875. ]
  876. ]
  877. ]
  878. ];
  879. $this->assertEquals($expected, Xml::toArray($xmlRequest));
  880. $xmlResponse = Xml::build(CORE_TESTS . DS . 'Fixture/soap_response.xml');
  881. $expected = [
  882. 'Envelope' => [
  883. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  884. 'soap:Body' => [
  885. 'm:GetStockPriceResponse' => [
  886. 'm:Price' => '34.5'
  887. ]
  888. ]
  889. ]
  890. ];
  891. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  892. $xml = [
  893. 'soap:Envelope' => [
  894. 'xmlns:soap' => 'http://www.w3.org/2001/12/soap-envelope',
  895. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  896. 'soap:Body' => [
  897. 'xmlns:m' => 'http://www.example.org/stock',
  898. 'm:GetStockPrice' => [
  899. 'm:StockName' => 'IBM'
  900. ]
  901. ]
  902. ]
  903. ];
  904. $xmlRequest = Xml::fromArray($xml, ['encoding' => null]);
  905. $xmlText = <<<XML
  906. <?xml version="1.0"?>
  907. <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  908. <soap:Body xmlns:m="http://www.example.org/stock">
  909. <m:GetStockPrice>
  910. <m:StockName>IBM</m:StockName>
  911. </m:GetStockPrice>
  912. </soap:Body>
  913. </soap:Envelope>
  914. XML;
  915. $this->assertXmlStringEqualsXmlString($xmlText, $xmlRequest->asXML());
  916. }
  917. /**
  918. * testNamespace
  919. *
  920. * @return void
  921. */
  922. public function testNamespace()
  923. {
  924. $xml = <<<XML
  925. <root xmlns:ns="http://cakephp.org">
  926. <ns:tag id="1">
  927. <child>good</child>
  928. <otherchild>bad</otherchild>
  929. </ns:tag>
  930. <tag>Tag without ns</tag>
  931. </root>
  932. XML;
  933. $xmlResponse = Xml::build($xml);
  934. $expected = [
  935. 'root' => [
  936. 'ns:tag' => [
  937. '@id' => '1',
  938. 'child' => 'good',
  939. 'otherchild' => 'bad'
  940. ],
  941. 'tag' => 'Tag without ns'
  942. ]
  943. ];
  944. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  945. $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:tag id="1" /><tag><id>1</id></tag></root>');
  946. $expected = [
  947. 'root' => [
  948. 'ns:tag' => [
  949. '@id' => '1'
  950. ],
  951. 'tag' => [
  952. 'id' => '1'
  953. ]
  954. ]
  955. ];
  956. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  957. $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:attr>1</ns:attr></root>');
  958. $expected = [
  959. 'root' => [
  960. 'ns:attr' => '1'
  961. ]
  962. ];
  963. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  964. $xmlResponse = Xml::build('<root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>');
  965. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  966. $xml = [
  967. 'root' => [
  968. 'ns:attr' => [
  969. 'xmlns:ns' => 'http://cakephp.org',
  970. '@' => 1
  971. ]
  972. ]
  973. ];
  974. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>';
  975. $xmlResponse = Xml::fromArray($xml);
  976. $this->assertEquals($expected, str_replace(["\r", "\n"], '', $xmlResponse->asXML()));
  977. $xml = [
  978. 'root' => [
  979. 'tag' => [
  980. 'xmlns:pref' => 'http://cakephp.org',
  981. 'pref:item' => [
  982. 'item 1',
  983. 'item 2'
  984. ]
  985. ]
  986. ]
  987. ];
  988. $expected = <<<XML
  989. <?xml version="1.0" encoding="UTF-8"?>
  990. <root>
  991. <tag xmlns:pref="http://cakephp.org">
  992. <pref:item>item 1</pref:item>
  993. <pref:item>item 2</pref:item>
  994. </tag>
  995. </root>
  996. XML;
  997. $xmlResponse = Xml::fromArray($xml);
  998. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  999. $xml = [
  1000. 'root' => [
  1001. 'tag' => [
  1002. 'xmlns:' => 'http://cakephp.org'
  1003. ]
  1004. ]
  1005. ];
  1006. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns="http://cakephp.org"/></root>';
  1007. $xmlResponse = Xml::fromArray($xml);
  1008. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  1009. $xml = [
  1010. 'root' => [
  1011. 'xmlns:' => 'http://cakephp.org'
  1012. ]
  1013. ];
  1014. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns="http://cakephp.org"/>';
  1015. $xmlResponse = Xml::fromArray($xml);
  1016. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  1017. $xml = [
  1018. 'root' => [
  1019. 'xmlns:ns' => 'http://cakephp.org'
  1020. ]
  1021. ];
  1022. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns:ns="http://cakephp.org"/>';
  1023. $xmlResponse = Xml::fromArray($xml);
  1024. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  1025. }
  1026. /**
  1027. * test that CDATA blocks don't get screwed up by SimpleXml
  1028. *
  1029. * @return void
  1030. */
  1031. public function testCdata()
  1032. {
  1033. $xml = '<' . '?xml version="1.0" encoding="UTF-8"?>' .
  1034. '<people><name><![CDATA[ Mark ]]></name></people>';
  1035. $result = Xml::build($xml);
  1036. $this->assertEquals(' Mark ', (string)$result->name);
  1037. }
  1038. /**
  1039. * data provider for toArray() failures
  1040. *
  1041. * @return array
  1042. */
  1043. public static function invalidToArrayDataProvider()
  1044. {
  1045. return [
  1046. [new \DateTime()],
  1047. [[]]
  1048. ];
  1049. }
  1050. /**
  1051. * testToArrayFail method
  1052. *
  1053. * @dataProvider invalidToArrayDataProvider
  1054. * @expectedException \Cake\Utility\Exception\XmlException
  1055. * @return void
  1056. */
  1057. public function testToArrayFail($value)
  1058. {
  1059. Xml::toArray($value);
  1060. }
  1061. /**
  1062. * Test ampersand in text elements.
  1063. *
  1064. * @return void
  1065. */
  1066. public function testAmpInText()
  1067. {
  1068. $data = [
  1069. 'outer' => [
  1070. 'inner' => ['name' => 'mark & mark']
  1071. ]
  1072. ];
  1073. $obj = Xml::build($data);
  1074. $result = $obj->asXml();
  1075. $this->assertContains('mark &amp; mark', $result);
  1076. }
  1077. /**
  1078. * Test that entity loading is disabled by default.
  1079. *
  1080. * @return void
  1081. */
  1082. public function testNoEntityLoading()
  1083. {
  1084. $file = str_replace(' ', '%20', CAKE . 'VERSION.txt');
  1085. $xml = <<<XML
  1086. <!DOCTYPE cakephp [
  1087. <!ENTITY payload SYSTEM "file://$file" >]>
  1088. <request>
  1089. <xxe>&payload;</xxe>
  1090. </request>
  1091. XML;
  1092. $result = Xml::build($xml);
  1093. }
  1094. }