PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

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

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