XmlTest.php 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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\Error\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 \Cake\Error\Exception
  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\Error\XmlException
  159. * @return void
  160. */
  161. public function testBuildInvalidDataSimpleXml() {
  162. $input = '<derp';
  163. $xml = 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. * @return void
  492. */
  493. public function testFromArrayFail($value) {
  494. try {
  495. Xml::fromArray($value);
  496. $this->fail('No exception.');
  497. } catch (\Exception $e) {
  498. $this->assertTrue(true, 'Caught exception.');
  499. }
  500. }
  501. /**
  502. * Test that there are not unterminated errors when building xml
  503. *
  504. * @return void
  505. */
  506. public function testFromArrayUnterminatedError() {
  507. $data = array(
  508. 'product_ID' => 'GENERT-DL',
  509. 'deeplink' => 'http://example.com/deep',
  510. 'image_URL' => 'http://example.com/image',
  511. 'thumbnail_image_URL' => 'http://example.com/thumb',
  512. 'brand' => 'Malte Lange & Co',
  513. 'availability' => 'in stock',
  514. 'authors' => array(
  515. 'author' => array('Malte Lange & Co')
  516. )
  517. );
  518. $xml = Xml::fromArray(array('products' => $data), 'tags');
  519. $expected = <<<XML
  520. <?xml version="1.0" encoding="UTF-8"?>
  521. <products>
  522. <product_ID>GENERT-DL</product_ID>
  523. <deeplink>http://example.com/deep</deeplink>
  524. <image_URL>http://example.com/image</image_URL>
  525. <thumbnail_image_URL>http://example.com/thumb</thumbnail_image_URL>
  526. <brand>Malte Lange &amp; Co</brand>
  527. <availability>in stock</availability>
  528. <authors>
  529. <author>Malte Lange &amp; Co</author>
  530. </authors>
  531. </products>
  532. XML;
  533. $this->assertXmlStringEqualsXmlString($expected, $xml->asXML());
  534. }
  535. /**
  536. * testToArray method
  537. *
  538. * @return void
  539. */
  540. public function testToArray() {
  541. $xml = '<tag>name</tag>';
  542. $obj = Xml::build($xml);
  543. $this->assertEquals(array('tag' => 'name'), Xml::toArray($obj));
  544. $xml = CORE_TESTS . 'Fixture/sample.xml';
  545. $obj = Xml::build($xml);
  546. $expected = array(
  547. 'tags' => array(
  548. 'tag' => array(
  549. array(
  550. '@id' => '1',
  551. 'name' => 'defect'
  552. ),
  553. array(
  554. '@id' => '2',
  555. 'name' => 'enhancement'
  556. )
  557. )
  558. )
  559. );
  560. $this->assertEquals($expected, Xml::toArray($obj));
  561. $array = array(
  562. 'tags' => array(
  563. 'tag' => array(
  564. array(
  565. 'id' => '1',
  566. 'name' => 'defect'
  567. ),
  568. array(
  569. 'id' => '2',
  570. 'name' => 'enhancement'
  571. )
  572. )
  573. )
  574. );
  575. $this->assertEquals(Xml::toArray(Xml::fromArray($array, 'tags')), $array);
  576. $expected = array(
  577. 'tags' => array(
  578. 'tag' => array(
  579. array(
  580. '@id' => '1',
  581. '@name' => 'defect'
  582. ),
  583. array(
  584. '@id' => '2',
  585. '@name' => 'enhancement'
  586. )
  587. )
  588. )
  589. );
  590. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
  591. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument', 'format' => 'attributes'))));
  592. $this->assertEquals(Xml::toArray(Xml::fromArray($array)), $array);
  593. $this->assertEquals(Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument'))), $array);
  594. $array = array(
  595. 'tags' => array(
  596. 'tag' => array(
  597. 'id' => '1',
  598. 'posts' => array(
  599. array('id' => '1'),
  600. array('id' => '2')
  601. )
  602. ),
  603. 'tagOther' => array(
  604. 'subtag' => array(
  605. 'id' => '1'
  606. )
  607. )
  608. )
  609. );
  610. $expected = array(
  611. 'tags' => array(
  612. 'tag' => array(
  613. '@id' => '1',
  614. 'posts' => array(
  615. array('@id' => '1'),
  616. array('@id' => '2')
  617. )
  618. ),
  619. 'tagOther' => array(
  620. 'subtag' => array(
  621. '@id' => '1'
  622. )
  623. )
  624. )
  625. );
  626. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
  627. $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('format' => 'attributes', 'return' => 'domdocument'))));
  628. $xml = <<<XML
  629. <root>
  630. <tag id="1">defect</tag>
  631. </root>
  632. XML;
  633. $obj = Xml::build($xml);
  634. $expected = array(
  635. 'root' => array(
  636. 'tag' => array(
  637. '@id' => 1,
  638. '@' => 'defect'
  639. )
  640. )
  641. );
  642. $this->assertEquals($expected, Xml::toArray($obj));
  643. $xml = <<<XML
  644. <root>
  645. <table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>
  646. <table xmlns="http://www.cakephp.org"><name>CakePHP</name><license>MIT</license></table>
  647. <table>The book is on the table.</table>
  648. </root>
  649. XML;
  650. $obj = Xml::build($xml);
  651. $expected = array(
  652. 'root' => array(
  653. 'table' => array(
  654. array('tr' => array('td' => array('Apples', 'Bananas'))),
  655. array('name' => 'CakePHP', 'license' => 'MIT'),
  656. 'The book is on the table.'
  657. )
  658. )
  659. );
  660. $this->assertEquals($expected, Xml::toArray($obj));
  661. $xml = <<<XML
  662. <root xmlns:cake="http://www.cakephp.org/">
  663. <tag>defect</tag>
  664. <cake:bug>1</cake:bug>
  665. </root>
  666. XML;
  667. $obj = Xml::build($xml);
  668. $expected = array(
  669. 'root' => array(
  670. 'tag' => 'defect',
  671. 'cake:bug' => 1
  672. )
  673. );
  674. $this->assertEquals($expected, Xml::toArray($obj));
  675. $xml = '<tag type="myType">0</tag>';
  676. $obj = Xml::build($xml);
  677. $expected = array(
  678. 'tag' => array(
  679. '@type' => 'myType',
  680. '@' => 0
  681. )
  682. );
  683. $this->assertEquals($expected, Xml::toArray($obj));
  684. }
  685. /**
  686. * testRss
  687. *
  688. * @return void
  689. */
  690. public function testRss() {
  691. $rss = file_get_contents(CORE_TESTS . 'Fixture/rss.xml');
  692. $rssAsArray = Xml::toArray(Xml::build($rss));
  693. $this->assertEquals('2.0', $rssAsArray['rss']['@version']);
  694. $this->assertEquals(2, count($rssAsArray['rss']['channel']['item']));
  695. $atomLink = array('@href' => 'http://bakery.cakephp.org/articles/rss', '@rel' => 'self', '@type' => 'application/rss+xml');
  696. $this->assertEquals($rssAsArray['rss']['channel']['atom:link'], $atomLink);
  697. $this->assertEquals('http://bakery.cakephp.org/', $rssAsArray['rss']['channel']['link']);
  698. $expected = array(
  699. 'title' => 'Alertpay automated sales via IPN',
  700. 'link' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn',
  701. 'description' => 'I\'m going to show you how I implemented a payment module via the Alertpay payment processor.',
  702. 'pubDate' => 'Tue, 31 Aug 2010 01:42:00 -0500',
  703. 'guid' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn'
  704. );
  705. $this->assertSame($expected, $rssAsArray['rss']['channel']['item'][1]);
  706. $rss = array(
  707. 'rss' => array(
  708. 'xmlns:atom' => 'http://www.w3.org/2005/Atom',
  709. '@version' => '2.0',
  710. 'channel' => array(
  711. 'atom:link' => array(
  712. '@href' => 'http://bakery.cakephp.org/articles/rss',
  713. '@rel' => 'self',
  714. '@type' => 'application/rss+xml'
  715. ),
  716. 'title' => 'The Bakery: ',
  717. 'link' => 'http://bakery.cakephp.org/',
  718. 'description' => 'Recent Articles at The Bakery.',
  719. 'pubDate' => 'Sun, 12 Sep 2010 04:18:26 -0500',
  720. 'item' => array(
  721. array(
  722. 'title' => 'CakePHP 1.3.4 released',
  723. 'link' => 'http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released'
  724. ),
  725. array(
  726. 'title' => 'Wizard Component 1.2 Tutorial',
  727. 'link' => 'http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial'
  728. )
  729. )
  730. )
  731. )
  732. );
  733. $rssAsSimpleXML = Xml::fromArray($rss);
  734. $xmlText = <<<XML
  735. <?xml version="1.0" encoding="UTF-8"?>
  736. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  737. <channel>
  738. <atom:link href="http://bakery.cakephp.org/articles/rss" rel="self" type="application/rss+xml"/>
  739. <title>The Bakery: </title>
  740. <link>http://bakery.cakephp.org/</link>
  741. <description>Recent Articles at The Bakery.</description>
  742. <pubDate>Sun, 12 Sep 2010 04:18:26 -0500</pubDate>
  743. <item>
  744. <title>CakePHP 1.3.4 released</title>
  745. <link>http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released</link>
  746. </item>
  747. <item>
  748. <title>Wizard Component 1.2 Tutorial</title>
  749. <link>http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial</link>
  750. </item>
  751. </channel>
  752. </rss>
  753. XML;
  754. $this->assertXmlStringEqualsXmlString($xmlText, $rssAsSimpleXML->asXML());
  755. }
  756. /**
  757. * testXmlRpc
  758. *
  759. * @return void
  760. */
  761. public function testXmlRpc() {
  762. $xml = Xml::build('<methodCall><methodName>test</methodName><params /></methodCall>');
  763. $expected = array(
  764. 'methodCall' => array(
  765. 'methodName' => 'test',
  766. 'params' => ''
  767. )
  768. );
  769. $this->assertSame($expected, Xml::toArray($xml));
  770. $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>');
  771. $expected = array(
  772. 'methodCall' => array(
  773. 'methodName' => 'test',
  774. 'params' => array(
  775. 'param' => array(
  776. 'value' => array(
  777. 'array' => array(
  778. 'data' => array(
  779. 'value' => array(
  780. array('int' => '12'),
  781. array('string' => 'Egypt'),
  782. array('boolean' => '0'),
  783. array('int' => '-31')
  784. )
  785. )
  786. )
  787. )
  788. )
  789. )
  790. )
  791. );
  792. $this->assertSame($expected, Xml::toArray($xml));
  793. $xmlText = <<<XML
  794. <?xml version="1.0" encoding="UTF-8"?>
  795. <methodResponse>
  796. <params>
  797. <param>
  798. <value>
  799. <array>
  800. <data>
  801. <value>
  802. <int>1</int>
  803. </value>
  804. <value>
  805. <string>testing</string>
  806. </value>
  807. </data>
  808. </array>
  809. </value>
  810. </param>
  811. </params>
  812. </methodResponse>
  813. XML;
  814. $xml = Xml::build($xmlText);
  815. $expected = array(
  816. 'methodResponse' => array(
  817. 'params' => array(
  818. 'param' => array(
  819. 'value' => array(
  820. 'array' => array(
  821. 'data' => array(
  822. 'value' => array(
  823. array('int' => '1'),
  824. array('string' => 'testing')
  825. )
  826. )
  827. )
  828. )
  829. )
  830. )
  831. )
  832. );
  833. $this->assertSame($expected, Xml::toArray($xml));
  834. $xml = Xml::fromArray($expected, 'tags');
  835. $this->assertXmlStringEqualsXmlString($xmlText, $xml->asXML());
  836. }
  837. /**
  838. * testSoap
  839. *
  840. * @return void
  841. */
  842. public function testSoap() {
  843. $xmlRequest = Xml::build(CORE_TESTS . 'Fixture/soap_request.xml');
  844. $expected = array(
  845. 'Envelope' => array(
  846. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  847. 'soap:Body' => array(
  848. 'm:GetStockPrice' => array(
  849. 'm:StockName' => 'IBM'
  850. )
  851. )
  852. )
  853. );
  854. $this->assertEquals($expected, Xml::toArray($xmlRequest));
  855. $xmlResponse = Xml::build(CORE_TESTS . DS . 'Fixture/soap_response.xml');
  856. $expected = array(
  857. 'Envelope' => array(
  858. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  859. 'soap:Body' => array(
  860. 'm:GetStockPriceResponse' => array(
  861. 'm:Price' => '34.5'
  862. )
  863. )
  864. )
  865. );
  866. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  867. $xml = array(
  868. 'soap:Envelope' => array(
  869. 'xmlns:soap' => 'http://www.w3.org/2001/12/soap-envelope',
  870. '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
  871. 'soap:Body' => array(
  872. 'xmlns:m' => 'http://www.example.org/stock',
  873. 'm:GetStockPrice' => array(
  874. 'm:StockName' => 'IBM'
  875. )
  876. )
  877. )
  878. );
  879. $xmlRequest = Xml::fromArray($xml, array('encoding' => null));
  880. $xmlText = <<<XML
  881. <?xml version="1.0"?>
  882. <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  883. <soap:Body xmlns:m="http://www.example.org/stock">
  884. <m:GetStockPrice><m:StockName>IBM</m:StockName></m:GetStockPrice>
  885. </soap:Body>
  886. </soap:Envelope>
  887. XML;
  888. $this->assertXmlStringEqualsXmlString($xmlText, $xmlRequest->asXML());
  889. }
  890. /**
  891. * testNamespace
  892. *
  893. * @return void
  894. */
  895. public function testNamespace() {
  896. $xml = <<<XML
  897. <root xmlns:ns="http://cakephp.org">
  898. <ns:tag id="1">
  899. <child>good</child>
  900. <otherchild>bad</otherchild>
  901. </ns:tag>
  902. <tag>Tag without ns</tag>
  903. </root>
  904. XML;
  905. $xmlResponse = Xml::build($xml);
  906. $expected = array(
  907. 'root' => array(
  908. 'ns:tag' => array(
  909. '@id' => '1',
  910. 'child' => 'good',
  911. 'otherchild' => 'bad'
  912. ),
  913. 'tag' => 'Tag without ns'
  914. )
  915. );
  916. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  917. $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:tag id="1" /><tag><id>1</id></tag></root>');
  918. $expected = array(
  919. 'root' => array(
  920. 'ns:tag' => array(
  921. '@id' => '1'
  922. ),
  923. 'tag' => array(
  924. 'id' => '1'
  925. )
  926. )
  927. );
  928. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  929. $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:attr>1</ns:attr></root>');
  930. $expected = array(
  931. 'root' => array(
  932. 'ns:attr' => '1'
  933. )
  934. );
  935. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  936. $xmlResponse = Xml::build('<root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>');
  937. $this->assertEquals($expected, Xml::toArray($xmlResponse));
  938. $xml = array(
  939. 'root' => array(
  940. 'ns:attr' => array(
  941. 'xmlns:ns' => 'http://cakephp.org',
  942. '@' => 1
  943. )
  944. )
  945. );
  946. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>';
  947. $xmlResponse = Xml::fromArray($xml);
  948. $this->assertEquals($expected, str_replace(array("\r", "\n"), '', $xmlResponse->asXML()));
  949. $xml = array(
  950. 'root' => array(
  951. 'tag' => array(
  952. 'xmlns:pref' => 'http://cakephp.org',
  953. 'pref:item' => array(
  954. 'item 1',
  955. 'item 2'
  956. )
  957. )
  958. )
  959. );
  960. $expected = <<<XML
  961. <?xml version="1.0" encoding="UTF-8"?>
  962. <root>
  963. <tag xmlns:pref="http://cakephp.org">
  964. <pref:item>item 1</pref:item>
  965. <pref:item>item 2</pref:item>
  966. </tag>
  967. </root>
  968. XML;
  969. $xmlResponse = Xml::fromArray($xml);
  970. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  971. $xml = array(
  972. 'root' => array(
  973. 'tag' => array(
  974. 'xmlns:' => 'http://cakephp.org'
  975. )
  976. )
  977. );
  978. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns="http://cakephp.org"/></root>';
  979. $xmlResponse = Xml::fromArray($xml);
  980. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  981. $xml = array(
  982. 'root' => array(
  983. 'xmlns:' => 'http://cakephp.org'
  984. )
  985. );
  986. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns="http://cakephp.org"/>';
  987. $xmlResponse = Xml::fromArray($xml);
  988. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  989. $xml = array(
  990. 'root' => array(
  991. 'xmlns:ns' => 'http://cakephp.org'
  992. )
  993. );
  994. $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns:ns="http://cakephp.org"/>';
  995. $xmlResponse = Xml::fromArray($xml);
  996. $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
  997. }
  998. /**
  999. * test that CDATA blocks don't get screwed up by SimpleXml
  1000. *
  1001. * @return void
  1002. */
  1003. public function testCdata() {
  1004. $xml = '<' . '?xml version="1.0" encoding="UTF-8"?>' .
  1005. '<people><name><![CDATA[ Mark ]]></name></people>';
  1006. $result = Xml::build($xml);
  1007. $this->assertEquals(' Mark ', (string)$result->name);
  1008. }
  1009. /**
  1010. * data provider for toArray() failures
  1011. *
  1012. * @return array
  1013. */
  1014. public static function invalidToArrayDataProvider() {
  1015. return array(
  1016. array(new \DateTime()),
  1017. array(array())
  1018. );
  1019. }
  1020. /**
  1021. * testToArrayFail method
  1022. *
  1023. * @dataProvider invalidToArrayDataProvider
  1024. * @expectedException \Cake\Utility\Error\XmlException
  1025. * @return void
  1026. */
  1027. public function testToArrayFail($value) {
  1028. Xml::toArray($value);
  1029. }
  1030. /**
  1031. * Test ampersand in text elements.
  1032. *
  1033. * @return void
  1034. */
  1035. public function testAmpInText() {
  1036. $data = array(
  1037. 'outer' => array(
  1038. 'inner' => array('name' => 'mark & mark')
  1039. )
  1040. );
  1041. $obj = Xml::build($data);
  1042. $result = $obj->asXml();
  1043. $this->assertContains('mark &amp; mark', $result);
  1044. }
  1045. /**
  1046. * Test that entity loading is disabled by default.
  1047. *
  1048. * @return void
  1049. */
  1050. public function testNoEntityLoading() {
  1051. $file = str_replace(' ', '%20', CAKE . 'VERSION.txt');
  1052. $xml = <<<XML
  1053. <!DOCTYPE cakephp [
  1054. <!ENTITY payload SYSTEM "file://$file" >]>
  1055. <request>
  1056. <xxe>&payload;</xxe>
  1057. </request>
  1058. XML;
  1059. try {
  1060. $result = Xml::build($xml);
  1061. $this->assertEquals('', (string)$result->xxe);
  1062. } catch (Exception $e) {
  1063. $this->assertTrue(true, 'A warning was raised meaning external entities were not loaded');
  1064. }
  1065. }
  1066. }