PageRenderTime 66ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

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