XmlTest.php 27 KB

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