PageRenderTime 47ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

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

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