XmlTest.php 27 KB

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