PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/pmjones/php-framework-benchmarks
PHP | 1640 lines | 1114 code | 126 blank | 400 comment | 2 complexity | 4ed1bbb86b91f14f5702b82ffbfe6d96 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file