XmlTest.php 28 KB

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