PageRenderTime 73ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 1ms

/cake/tests/cases/libs/xml.test.php

https://bitbucket.org/webpolis/liiv
PHP | 1270 lines | 811 code | 79 blank | 380 comment | 0 complexity | 9bc9970576d470f966a7a694c2d075d0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * XmlTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs
  21. * @since CakePHP(tm) v 1.2.0.5432
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Core', 'Xml');
  28. /**
  29. * XmlTest class
  30. *
  31. * @package cake
  32. * @subpackage cake.tests.cases.libs
  33. */
  34. class XmlTest extends CakeTestCase {
  35. /**
  36. * setUp method
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. function setUp() {
  42. $manager =& new XmlManager();
  43. $manager->namespaces = array();
  44. }
  45. /**
  46. * testRootTagParsing method
  47. *
  48. * @access public
  49. * @return void
  50. */
  51. function testRootTagParsing() {
  52. $input = '<' . '?xml version="1.0" encoding="UTF-8" ?' . '>' . "\n" .
  53. '<plugin id="1" version_id="1" name="my_plugin" title="My Plugin" author="Me" author_email="me@cakephp.org" description="My awesome package" created="2008-01-28 18:21:13" updated="2008-01-28 18:21:13">'
  54. .'<current id="1" plugin_id="1" name="1.0" file="" created="2008-01-28 18:21:13" updated="2008-01-28 18:21:13" />'
  55. .'<version id="1" plugin_id="1" name="1.0" file="" created="2008-01-28 18:21:13" updated="2008-01-28 18:21:13" />'
  56. .'</plugin>';
  57. $xml = new Xml($input);
  58. $this->assertEqual($xml->children[0]->name, 'plugin');
  59. $this->assertEqual($xml->children[0]->children[0]->name, 'current');
  60. $this->assertEqual($xml->toString(true), $input);
  61. }
  62. /**
  63. * testSerialization method
  64. *
  65. * @access public
  66. * @return void
  67. */
  68. function testSerialization() {
  69. $input = array(
  70. array(
  71. 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
  72. 'Style' => array('id' => null, 'name' => null),
  73. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  74. 'Industry' => array('id' => 1, 'name' => 'Financial')
  75. ),
  76. array(
  77. 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
  78. 'Style' => array('id' => null, 'name' => null),
  79. 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
  80. 'Industry' => array('id' => 2, 'name' => 'Education')
  81. )
  82. );
  83. $xml = new Xml($input);
  84. $result = preg_replace("/\n/",'', $xml->toString(false));
  85. $expected = '<project id="1" title="" client_id="1" show="1" is_spotlight="" style_id="0" job_type_id="1" industry_id="1" modified="" created=""><style id="" name="" /><job_type id="1" name="Touch Screen Kiosk" /><industry id="1" name="Financial" /></project><project id="2" title="" client_id="2" show="1" is_spotlight="" style_id="0" job_type_id="2" industry_id="2" modified="2007-11-26 14:48:36" created=""><style id="" name="" /><job_type id="2" name="Awareness Campaign" /><industry id="2" name="Education" /></project>';
  86. $this->assertEqual($result, $expected);
  87. $input = array(
  88. 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
  89. 'Style' => array('id' => null, 'name' => null),
  90. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  91. 'Industry' => array('id' => 1, 'name' => 'Financial')
  92. );
  93. $expected = '<project id="1" title="" client_id="1" show="1" is_spotlight="" style_id="0" job_type_id="1" industry_id="1" modified="" created=""><style id="" name="" /><job_type id="1" name="Touch Screen Kiosk" /><industry id="1" name="Financial" /></project>';
  94. $xml = new Xml($input);
  95. $result = preg_replace("/\n/",'', $xml->toString(false));
  96. $this->assertEqual($result, $expected);
  97. }
  98. /**
  99. * testSerializeOnMultiDimensionalArray method
  100. *
  101. * @access public
  102. * @return void
  103. */
  104. function testSerializeOnMultiDimensionalArray() {
  105. $data = array(
  106. 'Statuses' => array(
  107. array('Status' => array('id' => 1)),
  108. array('Status' => array('id' => 2))
  109. )
  110. );
  111. $result =& new Xml($data, array('format' => 'tags'));
  112. $expected = '<statuses><status><id>1</id></status><status><id>2</id></status></statuses>';
  113. $this->assertIdentical($result->toString(), $expected);
  114. }
  115. /**
  116. * test serialization of boolean and null values. false = 0, true = 1, null = ''
  117. *
  118. * @return void
  119. **/
  120. function testSerializationOfBooleanAndBooleanishValues() {
  121. $xml =& new Xml(array('data' => array('example' => false)));
  122. $result = $xml->toString(false);
  123. $expected = '<data example="0" />';
  124. $this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s');
  125. $xml =& new Xml(array('data' => array('example' => true)));
  126. $result = $xml->toString(false);
  127. $expected = '<data example="1" />';
  128. $this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s');
  129. $xml =& new Xml(array('data' => array('example' => null)));
  130. $result = $xml->toString(false);
  131. $expected = '<data example="" />';
  132. $this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s');
  133. $xml =& new Xml(array('data' => array('example' => 0)));
  134. $result = $xml->toString(false);
  135. $expected = '<data example="0" />';
  136. $this->assertEqual($result, $expected, 'Boolean-ish values incorrectly handled. %s');
  137. $xml =& new Xml(array('data' => array('example' => 1)));
  138. $result = $xml->toString(false);
  139. $expected = '<data example="1" />';
  140. $this->assertEqual($result, $expected, 'Boolean-ish values incorrectly handled. %s');
  141. }
  142. /**
  143. * testSimpleArray method
  144. *
  145. * @access public
  146. * @return void
  147. */
  148. function testSimpleArray() {
  149. $xml = new Xml(array('hello' => 'world'), array('format' => 'tags'));
  150. $result = $xml->toString(false);
  151. $expected = '<hello><![CDATA[world]]></hello>';
  152. $this->assertEqual($expected, $result);
  153. }
  154. /**
  155. * testSimpleObject method
  156. *
  157. * @access public
  158. * @return void
  159. */
  160. function testSimpleObject() {
  161. $input = new StdClass();
  162. $input->hello = 'world';
  163. $xml = new Xml($input, array('format' => 'tags'));
  164. $result = $xml->toString(false);
  165. $expected = '<hello><![CDATA[world]]></hello>';
  166. $this->assertEqual($expected, $result);
  167. }
  168. /**
  169. * testHeader method
  170. *
  171. * @access public
  172. * @return void
  173. */
  174. function testHeader() {
  175. $input = new stdClass();
  176. $input->hello = 'world';
  177. $xml = new Xml($input, array('format' => 'tags'));
  178. $result = $xml->toString(array('header' => true));
  179. $expected = '<'.'?xml version="1.0" encoding="UTF-8" ?'.'>'."\n".'<hello><![CDATA[world]]></hello>';
  180. $this->assertEqual($expected, $result);
  181. }
  182. /**
  183. * testOwnerAssignment method
  184. *
  185. * @access public
  186. * @return void
  187. */
  188. function testOwnerAssignment() {
  189. $xml = new Xml();
  190. $node =& $xml->createElement('hello', 'world');
  191. $owner =& $node->document();
  192. $this->assertTrue($xml === $owner);
  193. $children =& $node->children;
  194. $childOwner =& $children[0]->document();
  195. $this->assertTrue($xml === $childOwner);
  196. }
  197. /**
  198. * testArraySingleSerialization method
  199. *
  200. * @access public
  201. * @return void
  202. */
  203. function testArraySingleSerialization() {
  204. $input = array(
  205. 'Post' => array(
  206. 'id' => '1', 'author_id' => '1', 'title' => 'First Post',
  207. 'body' => 'First Post Body', 'published' => 'Y',
  208. 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  209. ),
  210. 'Author' => array(
  211. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  212. 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'
  213. )
  214. );
  215. $expected = '<post><id>1</id><author_id>1</author_id><title><![CDATA[First Post]]>';
  216. $expected .= '</title><body><![CDATA[First Post Body]]></body><published><![CDATA[Y]]>';
  217. $expected .= '</published><created><![CDATA[2007-03-18 10:39:23]]></created><updated>';
  218. $expected .= '<![CDATA[2007-03-18 10:41:31]]></updated><author><id>1</id><user>';
  219. $expected .= '<![CDATA[mariano]]></user><password><![CDATA[5f4dcc3b5aa765d61d8327deb882';
  220. $expected .= 'cf99]]></password><created><![CDATA[2007-03-17 01:16:23]]></created>';
  221. $expected .= '<updated><![CDATA[2007-03-17 01:18:31]]></updated><test><![CDATA[working]]>';
  222. $expected .= '</test></author></post>';
  223. $xml = new Xml($input, array('format' => 'tags'));
  224. $result = $xml->toString(false);
  225. $this->assertEqual($expected, $result);
  226. }
  227. /**
  228. * testArraySerialization method
  229. *
  230. * @access public
  231. * @return void
  232. */
  233. function testArraySerialization() {
  234. $input = array(
  235. array(
  236. 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
  237. 'Style' => array('id' => null, 'name' => null),
  238. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  239. 'Industry' => array('id' => 1, 'name' => 'Financial')
  240. ),
  241. array(
  242. 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
  243. 'Style' => array('id' => null, 'name' => null),
  244. 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
  245. 'Industry' => array('id' => 2, 'name' => 'Education'),
  246. )
  247. );
  248. $expected = '<project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><id>1</id><name>Financial</name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><id>2</id><name>Education</name></industry></project>';
  249. $xml = new Xml($input, array('format' => 'tags'));
  250. $result = $xml->toString(array('header' => false, 'cdata' => false));
  251. $this->assertEqual($expected, $result);
  252. }
  253. /**
  254. * testNestedArraySerialization method
  255. *
  256. * @access public
  257. * @return void
  258. */
  259. function testNestedArraySerialization() {
  260. $input = array(
  261. array(
  262. 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
  263. 'Style' => array('id' => null, 'name' => null),
  264. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  265. 'Industry' => array('id' => 1, 'name' => 'Financial'),
  266. 'BusinessSolution' => array(array('id' => 6, 'name' => 'Convert Sales')),
  267. 'MediaType' => array(
  268. array('id' => 15, 'name' => 'Print'),
  269. array('id' => 7, 'name' => 'Web Demo'),
  270. array('id' => 6, 'name' => 'CD-ROM')
  271. )
  272. ),
  273. array(
  274. 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
  275. 'Style' => array('id' => null, 'name' => null),
  276. 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
  277. 'Industry' => array('id' => 2, 'name' => 'Education'),
  278. 'BusinessSolution' => array(
  279. array('id' => 4, 'name' => 'Build Relationship'),
  280. array('id' => 6, 'name' => 'Convert Sales')
  281. ),
  282. 'MediaType' => array(
  283. array('id' => 17, 'name' => 'Web'),
  284. array('id' => 6, 'name' => 'CD-ROM')
  285. )
  286. )
  287. );
  288. $expected = '<project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><id>1</id><name>Financial</name></industry><business_solution><id>6</id><name>Convert Sales</name></business_solution><media_type><id>15</id><name>Print</name></media_type><media_type><id>7</id><name>Web Demo</name></media_type><media_type><id>6</id><name>CD-ROM</name></media_type></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><id>2</id><name>Education</name></industry><business_solution><id>4</id><name>Build Relationship</name></business_solution><business_solution><id>6</id><name>Convert Sales</name></business_solution><media_type><id>17</id><name>Web</name></media_type><media_type><id>6</id><name>CD-ROM</name></media_type></project>';
  289. $xml = new Xml($input, array('format' => 'tags'));
  290. $result = $xml->toString(array('header' => false, 'cdata' => false));
  291. $this->assertEqual($expected, $result);
  292. }
  293. /**
  294. * Prove that serialization with a given root node works
  295. * as expected.
  296. *
  297. * @access public
  298. * @return void
  299. * @link https://trac.cakephp.org/ticket/6294
  300. */
  301. function testArraySerializationWithRoot() {
  302. $input = array(
  303. array('Shirt' => array('id' => 1, 'color' => 'green')),
  304. array('Shirt' => array('id' => 2, 'color' => 'blue')),
  305. );
  306. $expected = '<collection><shirt id="1" color="green" />';
  307. $expected .= '<shirt id="2" color="blue" /></collection>';
  308. $Xml = new Xml($input, array('root' => 'collection'));
  309. $result = $Xml->toString(array('header' => false));
  310. $this->assertEqual($expected, $result);
  311. }
  312. /**
  313. * testCloneNode
  314. *
  315. * @access public
  316. * @return void
  317. */
  318. function testCloneNode() {
  319. $node =& new XmlNode('element', 'myValue');
  320. $twin =& $node->cloneNode();
  321. $this->assertEqual($node, $twin);
  322. }
  323. /**
  324. * testNextSibling
  325. *
  326. * @access public
  327. * @return void
  328. */
  329. function testNextSibling() {
  330. $input = array(
  331. array(
  332. 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '1.89', 'industry_id' => '1.56', 'modified' => null, 'created' => null),
  333. 'Style' => array('id' => null, 'name' => null),
  334. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  335. 'Industry' => array('id' => 1, 'name' => 'Financial')
  336. ),
  337. array(
  338. 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '2.2', 'industry_id' => 2.2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
  339. 'Style' => array('id' => null, 'name' => null),
  340. 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
  341. 'Industry' => array('id' => 2, 'name' => 'Education'),
  342. )
  343. );
  344. $xml =& new Xml($input, array('format' => 'tags'));
  345. $node =& $xml->children[0]->children[0];
  346. $nextSibling =& $node->nextSibling();
  347. $this->assertEqual($nextSibling, $xml->children[0]->children[1]);
  348. $nextSibling2 =& $nextSibling->nextSibling();
  349. $this->assertEqual($nextSibling2, $xml->children[0]->children[2]);
  350. $noFriends =& $xml->children[0]->children[12];
  351. $this->assertNull($noFriends->nextSibling());
  352. }
  353. /**
  354. * testPreviousSibling
  355. *
  356. * @access public
  357. * @return void
  358. */
  359. function testPreviousSibling() {
  360. $input = array(
  361. array(
  362. 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '1.89', 'industry_id' => '1.56', 'modified' => null, 'created' => null),
  363. 'Style' => array('id' => null, 'name' => null),
  364. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  365. 'Industry' => array('id' => 1, 'name' => 'Financial')
  366. ),
  367. array(
  368. 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '2.2', 'industry_id' => 2.2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
  369. 'Style' => array('id' => null, 'name' => null),
  370. 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
  371. 'Industry' => array('id' => 2, 'name' => 'Education'),
  372. )
  373. );
  374. $xml =& new Xml($input, array('format' => 'tags'));
  375. $node =& $xml->children[0]->children[1];
  376. $prevSibling =& $node->previousSibling();
  377. $this->assertEqual($prevSibling, $xml->children[0]->children[0]);
  378. $this->assertNull($prevSibling->previousSibling());
  379. }
  380. /**
  381. * testAddAndRemoveAttributes
  382. *
  383. * @access public
  384. * @return void
  385. */
  386. function testAddAndRemoveAttributes() {
  387. $node =& new XmlElement('myElement', 'superValue');
  388. $this->assertTrue(empty($node->attributes));
  389. $attrs = array(
  390. 'id' => 'test',
  391. 'show' => 1,
  392. 'is_spotlight' => 1,
  393. );
  394. $node->addAttribute($attrs);
  395. $this->assertEqual($node->attributes, $attrs);
  396. $node =& new XmlElement('myElement', 'superValue');
  397. $node->addAttribute('test', 'value');
  398. $this->assertTrue(isset($node->attributes['test']));
  399. $node =& new XmlElement('myElement', 'superValue');
  400. $obj =& new StdClass();
  401. $obj->class = 'info';
  402. $obj->id = 'primaryInfoBox';
  403. $node->addAttribute($obj);
  404. $expected = array(
  405. 'class' => 'info',
  406. 'id' => 'primaryInfoBox',
  407. );
  408. $this->assertEqual($node->attributes, $expected);
  409. $result = $node->removeAttribute('class');
  410. $this->assertTrue($result);
  411. $this->assertFalse(isset($node->attributes['class']));
  412. $result = $node->removeAttribute('missing');
  413. $this->assertFalse($result);
  414. }
  415. /**
  416. * Tests that XML documents with non-standard spacing (i.e. leading whitespace, whole document
  417. * on one line) still parse properly.
  418. *
  419. * @return void
  420. */
  421. function testParsingWithNonStandardWhitespace() {
  422. $raw = '<?xml version="1.0" encoding="ISO-8859-1" ?><prices><price>1.0</price></prices>';
  423. $array = array('Prices' => array('price' => 1.0));
  424. $xml = new Xml($raw);
  425. $this->assertEqual($xml->toArray(), $array);
  426. $this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"');
  427. $xml = new Xml(' ' . $raw);
  428. $this->assertEqual($xml->toArray(), $array);
  429. $this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"');
  430. $xml = new Xml("\n" . $raw);
  431. $this->assertEqual($xml->toArray(), $array);
  432. $this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"');
  433. }
  434. /* Not implemented yet */
  435. /* function testChildFilter() {
  436. $input = array(
  437. array(
  438. 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
  439. 'Style' => array('id' => null, 'name' => null),
  440. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  441. 'Industry' => array('id' => 1, 'name' => 'Financial'),
  442. 'BusinessSolution' => array(array('id' => 6, 'name' => 'Convert Sales')),
  443. 'MediaType' => array(
  444. array('id' => 15, 'name' => 'Print'),
  445. array('id' => 7, 'name' => 'Web Demo'),
  446. array('id' => 6, 'name' => 'CD-ROM')
  447. )
  448. ),
  449. array(
  450. 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
  451. 'Style' => array('id' => null, 'name' => null),
  452. 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
  453. 'Industry' => array('id' => 2, 'name' => 'Education'),
  454. 'BusinessSolution' => array(
  455. array('id' => 4, 'name' => 'Build Relationship'),
  456. array('id' => 6, 'name' => 'Convert Sales')
  457. ),
  458. 'MediaType' => array(
  459. array('id' => 17, 'name' => 'Web'),
  460. array('id' => 6, 'name' => 'CD-ROM')
  461. )
  462. )
  463. );
  464. $xml = new Xml($input, array('format' => 'tags', 'tags' => array(
  465. 'MediaType' => array('value' => 'id', 'children' => false),
  466. 'JobType' => array('children' => array()),
  467. 'Industry' => array('children' => array('name')),
  468. 'show' => false
  469. )));
  470. $result = $xml->toString(array('header' => false, 'cdata' => false));
  471. $expected = '<project><id>1</id><title /><client_id>1</client_id><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><name>Financial</name></industry><business_solution><id>6</id><name>Convert Sales</name></business_solution><media_type>15</media_type><media_type>7</media_type><media_type>6</media_type></project><project><id>2</id><title /><client_id>2</client_id><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><name>Education</name></industry><business_solution><id>4</id><name>Build Relationship</name></business_solution><business_solution><id>6</id><name>Convert Sales</name></business_solution><media_type>17</media_type><media_type>6</media_type></project>';
  472. $this->assertEqual($expected, $result);
  473. } */
  474. /* Broken due to a Set class issue */
  475. /* function testMixedArray() {
  476. $input = array('OptionGroup' => array(
  477. array('name' => 'OptA', 'id' => 12, 'OptA 1', 'OptA 2', 'OptA 3', 'OptA 4', 'OptA 5', 'OptA 6'),
  478. array('name' => 'OptB', 'id' => 12, 'OptB 1', 'OptB 2', 'OptB 3', 'OptB 4', 'OptB 5', 'OptB 6')
  479. ));
  480. $expected = '<option_group><name>OptA</name><id>12</id><option_group>OptA 1</option_group><option_group>OptA 2</option_group><option_group>OptA 3</option_group><option_group>OptA 4</option_group><option_group>OptA 5</option_group><option_group>OptA 6</option_group></option_group><option_group><name>OptB</name><id>12</id><option_group>OptB 1</option_group><option_group>OptB 2</option_group><option_group>OptB 3</option_group><option_group>OptB 4</option_group><option_group>OptB 5</option_group><option_group>OptB 6</option_group></option_group>';
  481. $xml = new Xml($input, array('format' => 'tags'));
  482. $result = $xml->toString(array('header' => false, 'cdata' => false));
  483. $this->assertEqual($expected, $result);
  484. } */
  485. /* function testMixedNestedArray() {
  486. $input = array(
  487. 'OptionA' => array(
  488. 'name' => 'OptA',
  489. 'id' => 12,
  490. 'opt' => array('OptA 1', 'OptA 2', 'OptA 3', 'OptA 4', 'OptA 5', 'OptA 6')
  491. ),
  492. 'OptionB' => array(
  493. 'name' => 'OptB',
  494. 'id' => 12,
  495. 'opt' => array('OptB 1', 'OptB 2', 'OptB 3', 'OptB 4', 'OptB 5', 'OptB 6')
  496. )
  497. );
  498. $expected = '<option_a><name>OptA</name><id>12</id><opt>OptA 1</opt><opt>OptA 2</opt><opt>OptA 3</opt><opt>OptA 4</opt><opt>OptA 5</opt><opt>OptA 6</opt></option_a><option_b><name>OptB</name><id>12</id><opt>OptB 1</opt><opt>OptB 2</opt><opt>OptB 3</opt><opt>OptB 4</opt><opt>OptB 5</opt><opt>OptB 6</opt></option_b>';
  499. $xml = new Xml($input, array('format' => 'tags'));
  500. $result = $xml->toString(array('header' => false, 'cdata' => false));
  501. $this->assertEqual($expected, $result);
  502. } */
  503. /* function testMixedArrayAttributes() {
  504. $input = array('OptionGroup' => array(
  505. array(
  506. 'name' => 'OptA',
  507. 'id' => 12,
  508. array('opt' => 'OptA 1'),
  509. array('opt' => 'OptA 2'),
  510. array('opt' => 'OptA 3'),
  511. array('opt' => 'OptA 4'),
  512. array('opt' => 'OptA 5'),
  513. array('opt' => 'OptA 6')
  514. ),
  515. array(
  516. 'name' => 'OptB',
  517. 'id' => 12,
  518. array('opt' => 'OptB 1'),
  519. array('opt' => 'OptB 2'),
  520. array('opt' => 'OptB 3'),
  521. array('opt' => 'OptB 4'),
  522. array('opt' => 'OptB 5'),
  523. array('opt' => 'OptB 6')
  524. )
  525. ));
  526. $expected = '<option_group name="OptA" id="12"><opt>OptA 1</opt><opt>OptA 2</opt><opt>OptA 3</opt><opt>OptA 4</opt><opt>OptA 5</opt><opt>OptA 6</opt></option_group><option_group name="OptB" id="12"><opt>OptB 1</opt><opt>OptB 2</opt><opt>OptB 3</opt><opt>OptB 4</opt><opt>OptB 5</opt><opt>OptB 6</opt></option_group>';
  527. $options = array('tags' => array('option_group' => array('attributes' => array('id', 'name'))));
  528. $xml = new Xml($input, $options);
  529. $result = $xml->toString(false);
  530. $this->assertEqual($expected, $result);
  531. } */
  532. /* Not implemented yet */
  533. /* function testTagMap() {
  534. $input = array(
  535. array(
  536. 'Project' => array('id' => 1, 'title' => null, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
  537. 'Style' => array('id' => null, 'name' => null),
  538. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  539. 'Industry' => array('id' => 1, 'name' => 'Financial')
  540. ),
  541. array(
  542. 'Project' => array('id' => 2, 'title' => null, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
  543. 'Style' => array('id' => null, 'name' => null),
  544. 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
  545. 'Industry' => array('id' => 2, 'name' => 'Education'),
  546. )
  547. );
  548. $expected = '<project id="1"><title /><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style id=""><name /></style><jobtype id="1">Touch Screen Kiosk</jobtype><industry id="1"><name>Financial</name></industry></project><project id="2"><title /><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style id=""><name /></style><jobtype id="2">Awareness Campaign</jobtype><industry id="2"><name>Education</name></industry></project>';
  549. $xml = new Xml($input, array('tags' => array(
  550. 'Project' => array('attributes' => array('id')),
  551. 'style' => array('attributes' => array('id')),
  552. 'JobType' => array('name' => 'jobtype', 'attributes' => array('id'), 'value' => 'name'),
  553. 'Industry' => array('attributes' => array('id'))
  554. )));
  555. $result = $xml->toString(array('header' => false, 'cdata' => false));
  556. $this->assertEqual($expected, $result);
  557. } */
  558. /**
  559. * testAllCData method
  560. *
  561. * @access public
  562. * @return void
  563. */
  564. function testAllCData() {
  565. $input = array(
  566. array(
  567. 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '1.89', 'industry_id' => '1.56', 'modified' => null, 'created' => null),
  568. 'Style' => array('id' => null, 'name' => null),
  569. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  570. 'Industry' => array('id' => 1, 'name' => 'Financial')
  571. ),
  572. array(
  573. 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '2.2', 'industry_id' => 2.2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
  574. 'Style' => array('id' => null, 'name' => null),
  575. 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
  576. 'Industry' => array('id' => 2, 'name' => 'Education'),
  577. )
  578. );
  579. $expected = '<project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1.89</job_type_id><industry_id>1.56</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name><![CDATA[Touch Screen Kiosk]]></name></job_type><industry><id>1</id><name><![CDATA[Financial]]></name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2.2</job_type_id><industry_id>2.2</industry_id><modified><![CDATA[2007-11-26 14:48:36]]></modified><created /><style><id /><name /></style><job_type><id>2</id><name><![CDATA[Awareness Campaign]]></name></job_type><industry><id>2</id><name><![CDATA[Education]]></name></industry></project>';
  580. $xml = new Xml($input, array('format' => 'tags'));
  581. $result = $xml->toString(array('header' => false, 'cdata' => true));
  582. $this->assertEqual($expected, $result);
  583. }
  584. /* PHP-native Unicode support pending */
  585. /* function testConvertEntities() {
  586. $input = array('project' => '&eacute;c&icirc;t');
  587. $xml = new Xml($input);
  588. $result = $xml->toString(array('header' => false, 'cdata' => false, 'convertEntities' => true));
  589. $expected = '<project>&#233;c&#238;t</project>';
  590. $this->assertEqual($result, $expected);
  591. } */
  592. /**
  593. * testWhitespace method
  594. *
  595. * @access public
  596. * @return void
  597. */
  598. function testWhitespace() {
  599. $input = array(
  600. array(
  601. 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
  602. 'Style' => array('id' => null, 'name' => null),
  603. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  604. 'Industry' => array('id' => 1, 'name' => 'Financial')
  605. ),
  606. array(
  607. 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
  608. 'Style' => array('id' => null, 'name' => null),
  609. 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
  610. 'Industry' => array('id' => 2, 'name' => 'Education'),
  611. )
  612. );
  613. $expected = "\n\t<project>\n\t\t<id>\n\t\t\t1\n\t\t</id>\n\t\t<title />\n\t\t<client_id>\n\t\t\t1\n\t\t</client_id>\n\t\t<show>\n\t\t\t1\n\t\t</show>\n\t\t<is_spotlight />\n\t\t<style_id>\n\t\t\t0\n\t\t</style_id>\n\t\t<job_type_id>\n\t\t\t1\n\t\t</job_type_id>\n\t\t<industry_id>\n\t\t\t1\n\t\t</industry_id>\n\t\t<modified />\n\t\t<created />\n\t\t<style>\n\t\t\t<id />\n\t\t\t<name />\n\t\t</style>\n\t\t<job_type>\n\t\t\t<id>\n\t\t\t\t1\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tTouch Screen Kiosk\n\t\t\t</name>\n\t\t</job_type>\n\t\t<industry>\n\t\t\t<id>\n\t\t\t\t1\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tFinancial\n\t\t\t</name>\n\t\t</industry>\n\t</project>\n\t<project>\n\t\t<id>\n\t\t\t2\n\t\t</id>\n\t\t<title />\n\t\t<client_id>\n\t\t\t2\n\t\t</client_id>\n\t\t<show>\n\t\t\t1\n\t\t</show>\n\t\t<is_spotlight />\n\t\t<style_id>\n\t\t\t0\n\t\t</style_id>\n\t\t<job_type_id>\n\t\t\t2\n\t\t</job_type_id>\n\t\t<industry_id>\n\t\t\t2\n\t\t</industry_id>\n\t\t<modified>\n\t\t\t2007-11-26 14:48:36\n\t\t</modified>\n\t\t<created />\n\t\t<style>\n\t\t\t<id />\n\t\t\t<name />\n\t\t</style>\n\t\t<job_type>\n\t\t\t<id>\n\t\t\t\t2\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tAwareness Campaign\n\t\t\t</name>\n\t\t</job_type>\n\t\t<industry>\n\t\t\t<id>\n\t\t\t\t2\n\t\t\t</id>\n\t\t\t<name>\n\t\t\t\tEducation\n\t\t\t</name>\n\t\t</industry>\n\t</project>\n";
  614. $xml = new Xml($input, array('format' => 'tags'));
  615. $result = $xml->toString(array('header' => false, 'cdata' => false, 'whitespace' => true));
  616. $this->assertEqual($expected, $result);
  617. }
  618. /**
  619. * testSetSerialization method
  620. *
  621. * @access public
  622. * @return void
  623. */
  624. function testSetSerialization() {
  625. $input = array(
  626. array(
  627. 'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
  628. 'Style' => array('id' => null, 'name' => null),
  629. 'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
  630. 'Industry' => array('id' => 1, 'name' => 'Financial')
  631. ),
  632. array(
  633. 'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
  634. 'Style' => array('id' => null, 'name' => null),
  635. 'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
  636. 'Industry' => array('id' => 2, 'name' => 'Education'),
  637. )
  638. );
  639. $expected = '<project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><id>1</id><name>Financial</name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><id>2</id><name>Education</name></industry></project>';
  640. $xml = new Xml(Set::map($input), array('format' => 'tags'));
  641. $result = $xml->toString(array('header' => false, 'cdata' => false));
  642. $this->assertEqual($expected, $result);
  643. }
  644. /**
  645. * testSimpleParsing method
  646. *
  647. * @access public
  648. * @return void
  649. */
  650. function testSimpleParsing() {
  651. $source = '<response><hello><![CDATA[happy world]]></hello><goodbye><![CDATA[cruel world]]></goodbye></response>';
  652. $xml = new Xml($source);
  653. $result = $xml->toString();
  654. $this->assertEqual($source, $result);
  655. }
  656. /**
  657. * test that elements with empty tag values do not collapse and corrupt data structures
  658. *
  659. * @access public
  660. * @return void
  661. **/
  662. function testElementCollapsing() {
  663. $xmlDataThatFails = '<resultpackage>
  664. <result qid="46b1c46ed6208"><![CDATA[46b1c46ed3af9]]></result>
  665. <result qid="46b1c46ed332a"><![CDATA[]]></result>
  666. <result qid="46b1c46ed90e6"><![CDATA[46b1c46ed69d8]]></result>
  667. <result qid="46b1c46ed71a7"><![CDATA[46b1c46ed5a38]]></result>
  668. <result qid="46b1c46ed8146"><![CDATA[46b1c46ed98b6]]></result>
  669. <result qid="46b1c46ed7978"><![CDATA[]]></result>
  670. <result qid="46b1c46ed4a98"><![CDATA[]]></result>
  671. <result qid="46b1c46ed42c8"><![CDATA[]]></result>
  672. <result qid="46b1c46ed5268"><![CDATA[46b1c46ed8917]]></result>
  673. </resultpackage>';
  674. $Xml = new Xml();
  675. $Xml->load('<?xml version="1.0" encoding="UTF-8" ?>' . $xmlDataThatFails);
  676. $result = $Xml->toArray(false);
  677. $this->assertTrue(is_array($result));
  678. $expected = array(
  679. 'resultpackage' => array(
  680. 'result' => array(
  681. 0 => array(
  682. 'value' => '46b1c46ed3af9',
  683. 'qid' => '46b1c46ed6208'),
  684. 1 => array(
  685. 'qid' => '46b1c46ed332a'),
  686. 2 => array(
  687. 'value' => '46b1c46ed69d8',
  688. 'qid' => '46b1c46ed90e6'),
  689. 3 => array(
  690. 'value' => '46b1c46ed5a38',
  691. 'qid' => '46b1c46ed71a7'),
  692. 4 => array(
  693. 'value' => '46b1c46ed98b6',
  694. 'qid' => '46b1c46ed8146'),
  695. 5 => array(
  696. 'qid' => '46b1c46ed7978'),
  697. 6 => array(
  698. 'qid' => '46b1c46ed4a98'),
  699. 7 => array(
  700. 'qid' => '46b1c46ed42c8'),
  701. 8 => array(
  702. 'value' => '46b1c46ed8917',
  703. 'qid' => '46b1c46ed5268'),
  704. )
  705. ));
  706. $this->assertEqual(
  707. count($result['resultpackage']['result']), count($expected['resultpackage']['result']),
  708. 'Incorrect array length %s');
  709. $this->assertFalse(
  710. isset($result['resultpackage']['result'][0][0]['qid']), 'Nested array exists, data is corrupt. %s');
  711. $this->assertEqual($result, $expected);
  712. }
  713. /**
  714. * test that empty values do not casefold collapse
  715. *
  716. * @see http://code.cakephp.org/tickets/view/8
  717. * @return void
  718. **/
  719. function testCaseFoldingWithEmptyValues() {
  720. $filledValue = '<method name="set_user_settings">
  721. <title>update user information</title>
  722. <user>1</user>
  723. <User>
  724. <id>1</id>
  725. <name>varchar(45)</name>
  726. </User>
  727. </method>';
  728. $xml =& new XML($filledValue);
  729. $expected = array(
  730. 'Method' => array(
  731. 'name' => 'set_user_settings',
  732. 'title' => 'update user information',
  733. 'user' => '1',
  734. 'User' => array(
  735. 'id' => 1,
  736. 'name' => 'varchar(45)',
  737. ),
  738. )
  739. );
  740. $result = $xml->toArray();
  741. $this->assertEqual($result, $expected);
  742. $emptyValue ='<method name="set_user_settings">
  743. <title>update user information</title>
  744. <user></user>
  745. <User>
  746. <id>1</id>
  747. <name>varchar(45)</name>
  748. </User>
  749. </method>';
  750. $xml =& new XML($emptyValue);
  751. $expected = array(
  752. 'Method' => array(
  753. 'name' => 'set_user_settings',
  754. 'title' => 'update user information',
  755. 'user' => array(),
  756. 'User' => array(
  757. 'id' => 1,
  758. 'name' => 'varchar(45)',
  759. ),
  760. )
  761. );
  762. $result = $xml->toArray();
  763. $this->assertEqual($result, $expected);
  764. }
  765. /**
  766. * testMixedParsing method
  767. *
  768. * @access public
  769. * @return void
  770. */
  771. function testMixedParsing() {
  772. $source = '<response><body><hello><![CDATA[happy world]]></hello><![CDATA[in between]]><goodbye><![CDATA[cruel world]]></goodbye></body></response>';
  773. $xml = new Xml($source);
  774. $result = $xml->toString();
  775. $this->assertEqual($source, $result);
  776. }
  777. /**
  778. * testComplexParsing method
  779. *
  780. * @access public
  781. * @return void
  782. */
  783. function testComplexParsing() {
  784. $source = '<projects><project><id>1</id><title /><client_id>1</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>1</job_type_id><industry_id>1</industry_id><modified /><created /><style><id /><name /></style><job_type><id>1</id><name>Touch Screen Kiosk</name></job_type><industry><id>1</id><name>Financial</name></industry></project><project><id>2</id><title /><client_id>2</client_id><show>1</show><is_spotlight /><style_id>0</style_id><job_type_id>2</job_type_id><industry_id>2</industry_id><modified>2007-11-26 14:48:36</modified><created /><style><id /><name /></style><job_type><id>2</id><name>Awareness Campaign</name></job_type><industry><id>2</id><name>Education</name></industry></project></projects>';
  785. $xml = new Xml($source);
  786. $result = $xml->toString(array('cdata' => false));
  787. $this->assertEqual($source, $result);
  788. }
  789. /**
  790. * testNamespaceParsing method
  791. *
  792. * @access public
  793. * @return void
  794. */
  795. function testNamespaceParsing() {
  796. $source = '<a:container xmlns:a="http://example.com/a" xmlns:b="http://example.com/b" xmlns:c="http://example.com/c" xmlns:d="http://example.com/d" xmlns:e="http://example.com/e"><b:rule test=""><c:result>value</c:result></b:rule><d:rule test=""><e:result>value</e:result></d:rule></a:container>';
  797. $xml = new Xml($source);
  798. $result = $xml->toString(array('cdata' => false));
  799. $this->assertEqual($source, $result);
  800. $children = $xml->children('container');
  801. $this->assertEqual($children[0]->namespace, 'a');
  802. $children = $children[0]->children('rule');
  803. $this->assertEqual($children[0]->namespace, 'b');
  804. }
  805. /**
  806. * testNamespaces method
  807. *
  808. * @access public
  809. * @return void
  810. */
  811. function testNamespaces() {
  812. $source = '<a:container xmlns:a="http://example.com/a" xmlns:b="http://example.com/b" xmlns:c="http://example.com/c" xmlns:d="http://example.com/d" xmlns:e="http://example.com/e"><b:rule test=""><c:result>value</c:result></b:rule><d:rule test=""><e:result>value</e:result></d:rule></a:container>';
  813. $xml = new Xml($source);
  814. $expects = '<a:container xmlns:a="http://example.com/a" xmlns:b="http://example.com/b" xmlns:c="http://example.com/c" xmlns:d="http://example.com/d" xmlns:e="http://example.com/e" xmlns:f="http://example.com/f"><b:rule test=""><c:result>value</c:result></b:rule><d:rule test=""><e:result>value</e:result></d:rule></a:container>';
  815. $_xml = XmlManager::getInstance();
  816. $xml->addNamespace('f', 'http://example.com/f');
  817. $result = $xml->toString(array('cdata' => false));
  818. $this->assertEqual($expects, $result);
  819. }
  820. /**
  821. * testEscapeCharSerialization method
  822. *
  823. * @access public
  824. * @return void
  825. */
  826. function testEscapeCharSerialization() {
  827. $xml = new Xml(array('text' => 'JavaScript & DHTML'), array('attributes' => false, 'format' => 'attributes'));
  828. $result = $xml->toString(false);
  829. $expected = '<std_class text="JavaScript &amp; DHTML" />';
  830. $this->assertEqual($expected, $result);
  831. }
  832. /**
  833. * testCompleteEscapeCharSerialization method
  834. *
  835. * @access public
  836. * @return void
  837. */
  838. function testCompleteEscapeCharSerialization() {
  839. $xml = new Xml(array('text' => '<>&"\''), array('attributes' => false, 'format' => 'attributes'));
  840. $result = $xml->toString(false);
  841. $expected = '<std_class text="&lt;&gt;&amp;&quot;&#039;" />';
  842. $this->assertEqual($expected, $result);
  843. }
  844. /**
  845. * testToArray method
  846. *
  847. * @access public
  848. * @return void
  849. */
  850. function testToArray() {
  851. App::import('Set');
  852. $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  853. <rss version="2.0">
  854. <channel>
  855. <title>Cake PHP Google Group</title>
  856. <link>http://groups.google.com/group/cake-php</link>
  857. <description>Search this group before posting anything. There are over 20,000 posts and it&amp;#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.</description>
  858. <language>en</language>
  859. <item>
  860. <title>constructng result array when using findall</title>
  861. <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
  862. <description>i&#39;m using cakephp to construct a logical data model array that will be &lt;br&gt; passed to a flex app. I have the following model association: &lt;br&gt; ServiceDay-&amp;gt;(hasMany)ServiceTi me-&amp;gt;(hasMany)ServiceTimePrice. So what &lt;br&gt; the current output from my findall is something like this example: &lt;br&gt; &lt;p&gt;Array( &lt;br&gt; [0] =&amp;gt; Array(</description>
  863. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
  864. <author>bmil...@gmail.com(bpscrugs)</author>
  865. <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
  866. </item>
  867. <item>
  868. <title>Re: share views between actions?</title>
  869. <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
  870. <description>Then perhaps you might do us all a favour and refrain from replying to &lt;br&gt; things you do not understand. That goes especially for asinine comments. &lt;br&gt; Indeed. &lt;br&gt; To sum up: &lt;br&gt; No comment. &lt;br&gt; In my day, a simple &amp;quot;RTFM&amp;quot; would suffice. I&#39;ll keep in mind to ignore any &lt;br&gt; further responses from you. &lt;br&gt; You (and I) were referring to the *online documentation*, not other</description>
  871. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
  872. <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
  873. <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
  874. </item>
  875. </channel>
  876. </rss>';
  877. $xml = new Xml($string);
  878. $result = $xml->toArray();
  879. $expected = array('Rss' => array(
  880. 'version' => '2.0',
  881. 'Channel' => array(
  882. 'title' => 'Cake PHP Google Group',
  883. 'link' => 'http://groups.google.com/group/cake-php',
  884. 'description' => 'Search this group before posting anything. There are over 20,000 posts and it&#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
  885. 'language' => 'en',
  886. 'Item' => array(
  887. array(
  888. 'title' => 'constructng result array when using findall',
  889. 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
  890. 'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br><p>Array( <br> [0] =&gt; Array(",
  891. 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
  892. 'author' => 'bmil...@gmail.com(bpscrugs)',
  893. 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
  894. ),
  895. array(
  896. 'title' => 'Re: share views between actions?',
  897. 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
  898. 'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other',
  899. 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
  900. 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
  901. 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
  902. )
  903. )
  904. )
  905. ));
  906. $this->assertEqual($result, $expected);
  907. $string ='<data><post title="Title of this post" description="cool"/></data>';
  908. $xml = new Xml($string);
  909. $result = $xml->toArray();
  910. $expected = array('Data' => array('Post' => array('title' => 'Title of this post', 'description' => 'cool')));
  911. $this->assertEqual($result, $expected);
  912. $xml = new Xml('<example><item><title>An example of a correctly reversed XMLNode</title><desc/></item></example>');
  913. $result = Set::reverse($xml);
  914. $expected = array(
  915. 'Example' => array(
  916. 'Item' => array(
  917. 'title' => 'An example of a correctly reversed XMLNode',
  918. 'desc' => array(),
  919. )
  920. )
  921. );
  922. $this->assertIdentical($result, $expected);
  923. $xml = new Xml('<example><item attr="123"><titles><title>title1</title><title>title2</title></titles></item></example>');
  924. $result = $xml->toArray();
  925. $expected = array(
  926. 'Example' => array(
  927. 'Item' => array(
  928. 'attr' => '123',
  929. 'Titles' => array(
  930. 'Title' => array('title1', 'title2')
  931. )
  932. )
  933. )
  934. );
  935. $this->assertIdentical($result, $expected);
  936. $xml = new Xml('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
  937. $result = $xml->toArray();
  938. $expected = array(
  939. 'Example' => array(
  940. 'attr' => 'ex_attr',
  941. 'Item' => array(
  942. 'attr' => '123',
  943. 'titles' => 'list',
  944. 'value' => 'textforitems'
  945. )
  946. )
  947. );
  948. $this->assertIdentical($result, $expected);
  949. $xml = new Xml('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
  950. $example = $xml->child('example');
  951. $item = $example->child('item');
  952. $result = $item->toArray();
  953. $expected = array(
  954. 'attr' => '123',
  955. 'titles' => 'list',
  956. 'value' => 'textforitems'
  957. );
  958. $this->assertIdentical($result, $expected);
  959. $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  960. <rss version="2.0">
  961. <channel>
  962. <title>Cake PHP Google Group</title>
  963. <link>http://groups.google.com/group/cake-php</link>
  964. <description>Search this group before posting anything. There are over 20,000 posts and it&amp;#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.</description>
  965. <language>en</language>
  966. <item>
  967. <title>constructng result array when using findall</title>
  968. <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
  969. <description>i&#39;m using cakephp to construct a logical data model array that will be &lt;br&gt; passed to a flex app. I have the following model association: &lt;br&gt; ServiceDay-&amp;gt;(hasMany)ServiceTi me-&amp;gt;(hasMany)ServiceTimePrice. So what &lt;br&gt; the current output from my findall is something like this example: &lt;br&gt; &lt;p&gt;Array( &lt;br&gt; [0] =&amp;gt; Array(</description>
  970. <dc:creator>cakephp</dc:creator>
  971. <category><![CDATA[cakephp]]></category>
  972. <category><![CDATA[model]]></category>
  973. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
  974. <author>bmil...@gmail.com(bpscrugs)</author>
  975. <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
  976. </item>
  977. <item>
  978. <title>Re: share views between actions?</title>
  979. <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
  980. <description>Then perhaps you might do us all a favour and refrain from replying to &lt;br&gt; things you do not understand. That goes especially for asinine comments. &lt;br&gt; Indeed. &lt;br&gt; To sum up: &lt;br&gt; No comment. &lt;br&gt; In my day, a simple &amp;quot;RTFM&amp;quot; would suffice. I&#39;ll keep in mind to ignore any &lt;br&gt; further responses from you. &lt;br&gt; You (and I) were referring to the *online documentation*, not other</description>
  981. <dc:creator>cakephp</dc:creator>
  982. <category><![CDATA[cakephp]]></category>
  983. <category><![CDATA[model]]></category>
  984. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
  985. <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
  986. <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
  987. </item>
  988. </channel>
  989. </rss>';
  990. $xml = new Xml($string);
  991. $result = $xml->toArray();
  992. $expected = array('Rss' => array(
  993. 'version' => '2.0',
  994. 'Channel' => array(
  995. 'title' => 'Cake PHP Google Group',
  996. 'link' => 'http://groups.google.com/group/cake-php',
  997. 'description' => 'Search this group before posting anything. There are over 20,000 posts and it&#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
  998. 'language' => 'en',
  999. 'Item' => array(
  1000. array(
  1001. 'title' => 'constructng result array when using findall',
  1002. 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
  1003. 'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br><p>Array( <br> [0] =&gt; Array(",
  1004. 'creator' => 'cakephp',
  1005. 'Category' => array('cakephp', 'model'),
  1006. 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
  1007. 'author' => 'bmil...@gmail.com(bpscrugs)',
  1008. 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
  1009. ),
  1010. array(
  1011. 'title' => 'Re: share views between actions?',
  1012. 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
  1013. 'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other',
  1014. 'creator' => 'cakephp',
  1015. 'Category' => array('cakephp', 'model'),
  1016. 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
  1017. 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
  1018. 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
  1019. )
  1020. )
  1021. )
  1022. ));
  1023. $this->assertEqual($result, $expected);
  1024. $text = "<?xml version='1.0' encoding='utf-8'?>
  1025. <course>
  1026. <comps>
  1027. <comp>1</comp>
  1028. <comp>2</comp>
  1029. <comp>3</comp>
  1030. <comp>4</comp>
  1031. </comps>
  1032. </course>";
  1033. $xml = new Xml($text);
  1034. $result = $xml->toArray();
  1035. $expected = array('Course' => array(
  1036. 'Comps' => array(
  1037. 'Comp' => array(
  1038. 1, 2, 3, 4
  1039. )
  1040. )
  1041. ));
  1042. $this->assertEqual($result, $expected);
  1043. $text = '<?xml version="1.0" encoding="UTF-8"?>
  1044. <XRDS xmlns="xri://$xrds">
  1045. <XRD xml:id="oauth" xmlns="xri://$XRD*($v*2.0)" version="2.0">
  1046. <Type>xri://$xrds*simple</Type>
  1047. <Expires>2008-04-13T07:34:58Z</Expires>
  1048. <Service>
  1049. <Type>http://oauth.net/core/1.0/endpoint/authorize</Type>
  1050. <Type>http://oauth.net/core/1.0/parameters/auth-header</Type>
  1051. <Type>http://oauth.net/core/1.0/parameters/uri-query</Type>
  1052. <URI priority="10">https://ma.gnolia.com/oauth/authorize</URI>
  1053. <URI priority="20">http://ma.gnolia.com/oauth/authorize</URI>
  1054. </Service>
  1055. </XRD>
  1056. <XRD xmlns="xri://$XRD*($v*2.0)" version="2.0">
  1057. <Type>xri://$xrds*simple</Type>
  1058. <Service priority="10">
  1059. <Type>http://oauth.net/discovery/1.0</Type>
  1060. <URI>#oauth</URI>
  1061. </Service>
  1062. </XRD>
  1063. </XRDS>';
  1064. $xml = new Xml($text);
  1065. $result = $xml->toArray();
  1066. $expected = array('XRDS' => array(
  1067. 'xmlns' => 'xri://$xrds',
  1068. 'XRD' => array(
  1069. array(
  1070. 'xml:id' => 'oauth',
  1071. 'xmlns' => 'xri://$XRD*($v*2.0)',
  1072. 'version' => '2.0',
  1073. 'Type' => 'xri://$xrds*simple',
  1074. 'Expires' => '2008-04-13T07:34:58Z',
  1075. 'Service' => array(
  1076. 'Type' => array(
  1077. 'http://oauth.net/core/1.0/endpoint/authorize',
  1078. 'http://oauth.net/core/1.0/parameters/auth-header',
  1079. 'http://oauth.net/core/1.0/parameters/uri-query'
  1080. ),
  1081. 'URI' => array(
  1082. array(
  1083. 'value' => 'https://ma.gnolia.com/oauth/authorize',
  1084. 'priority' => '10',
  1085. ),
  1086. array(
  1087. 'value' => 'http://ma.gnolia.com/oauth/authorize',
  1088. 'priority' => '20'
  1089. )
  1090. )
  1091. )
  1092. ),
  1093. array(
  1094. 'xmlns' => 'xri://$XRD*($v*2.0)',
  1095. 'version' => '2.0',
  1096. 'Type' => 'xri://$xrds*simple',
  1097. 'Service' => array(
  1098. 'priority' => '10',
  1099. 'Type' => 'http://oauth.net/discovery/1.0',
  1100. 'URI' => '#oauth'
  1101. )
  1102. )
  1103. )
  1104. ));
  1105. $this->assertEqual($result, $expected);
  1106. }
  1107. /**
  1108. * testAppend method
  1109. *
  1110. * @access public
  1111. * @return void
  1112. */
  1113. function testAppend() {
  1114. $parentNode = new XmlNode('ourParentNode');
  1115. $parentNode->append( new XmlNode('ourChildNode'));
  1116. $first =& $parentNode->first();
  1117. $this->assertEqual($first->name, 'ourChildNode');
  1118. $string = 'ourChildNode';
  1119. $parentNode = new XmlNode('ourParentNode');
  1120. $parentNode->append($string);
  1121. $last =& $parentNode->last();
  1122. $this->assertEqual($last->name, 'ourChildNode');
  1123. $this->expectError();
  1124. $parentNode->append($parentNode);
  1125. }
  1126. /**
  1127. * testNamespacing method
  1128. *
  1129. * @access public
  1130. * @return void
  1131. */
  1132. function testNamespacing() {
  1133. $node = new Xml('<xml></xml>');
  1134. $node->addNamespace('cake', 'http://cakephp.org');
  1135. $this->assertEqual($node->toString(), '<xml xmlns:cake="http://cakephp.org" />');
  1136. $this->assertTrue($node->removeNamespace('cake'));
  1137. $this->assertEqual($node->toString(), '<xml />');
  1138. $node = new Xml('<xml xmlns:cake="http://cakephp.org" />');
  1139. $this->assertTrue($node->removeNamespace('cake'));
  1140. $this->assertEqual($node->toString(), '<xml />');
  1141. $node->addNamespace('cake', 'http://cakephp.org');
  1142. $this->assertEqual($node->toString(), '<xml xmlns:cake="http://cakephp.org" />');
  1143. }
  1144. /**
  1145. * testCamelize method
  1146. *
  1147. * @access public
  1148. * @return void
  1149. */
  1150. function testCamelize() {
  1151. $xmlString = '<methodCall><methodName>examples.getStateName</methodName>' .
  1152. '<params><param><value><i4>41</i4></value></param></params></methodCall>';
  1153. $Xml = new Xml($xmlString);
  1154. $expected = array(
  1155. 'methodCall' => array(
  1156. 'methodName' => 'examples.getStateName',
  1157. 'params' => array(
  1158. 'param' => array('value' => array('i4' => 41)))));
  1159. $this->assertEqual($expected, $Xml->toArray(false));
  1160. $Xml = new Xml($xmlString);
  1161. $expected = array(
  1162. 'MethodCall' => array(
  1163. 'methodName' => 'examples.getStateName',
  1164. 'Params' => array(
  1165. 'Param' => array('Value' => array('i4' => 41)))));
  1166. $this->assertEqual($expected, $Xml->toArray());
  1167. }
  1168. /**
  1169. * testNumericDataHandling method
  1170. *
  1171. * @access public
  1172. * @return void
  1173. */
  1174. function testNumericDataHandling() {
  1175. $data = '<xml><data>012345</data></xml>';
  1176. $node = new Xml();
  1177. $node->load($data);
  1178. $node->parse();
  1179. $result = $node->first();
  1180. $result = $result->children("data");
  1181. $result = $result[0]->first();
  1182. $this->assertEqual($result->value, '012345');
  1183. }
  1184. }
  1185. ?>