XmlTest.php 34 KB

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