PageRenderTime 65ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/unsl/listop
PHP | 1338 lines | 867 code | 80 blank | 391 comment | 0 complexity | d19d39c29c3a37120bee4ef25bd2fabc MD5 | raw file
Possible License(s): GPL-3.0

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

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

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