Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * XmlTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Utility
15
 * @since         CakePHP(tm) v 1.2.0.5432
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Xml', 'Utility');
20
App::uses('CakeTestModel', 'TestSuite/Fixture');
21
 
22
/**
23
 * Article class
24
 *
25
 * @package       Cake.Test.Case.Utility
26
 */
27
class XmlArticle extends CakeTestModel {
28
 
29
/**
30
 * name property
31
 *
32
 * @var string
33
 */
34
	public $name = 'Article';
35
 
36
/**
37
 * belongsTo property
38
 *
39
 * @var array
40
 */
41
	public $belongsTo = array(
42
		'User' => array(
43
			'className' => 'XmlUser',
44
			'foreignKey' => 'user_id'
45
		)
46
	);
47
}
48
 
49
/**
50
 * User class
51
 *
52
 * @package       Cake.Test.Case.Utility
53
 */
54
class XmlUser extends CakeTestModel {
55
 
56
/**
57
 * name property
58
 *
59
 * @var string
60
 */
61
	public $name = 'User';
62
 
63
/**
64
 * hasMany property
65
 *
66
 * @var array
67
 */
68
	public $hasMany = array(
69
		'Article' => array(
70
			'className' => 'XmlArticle'
71
		)
72
	);
73
}
74
 
75
/**
76
 * XmlTest class
77
 *
78
 * @package       Cake.Test.Case.Utility
79
 */
80
class XmlTest extends CakeTestCase {
81
 
82
/**
83
 * autoFixtures property
84
 *
85
 * @var bool
86
 */
87
	public $autoFixtures = false;
88
 
89
/**
90
 * fixtures property
91
 * @var array
92
 */
93
	public $fixtures = array(
94
		'core.article', 'core.user'
95
	);
96
 
97
/**
98
 * setUp method
99
 *
100
 * @return void
101
 */
102
	public function setUp() {
103
		parent::setUp();
104
		$this->_appEncoding = Configure::read('App.encoding');
105
		Configure::write('App.encoding', 'UTF-8');
106
	}
107
 
108
/**
109
 * tearDown method
110
 *
111
 * @return void
112
 */
113
	public function tearDown() {
114
		parent::tearDown();
115
		Configure::write('App.encoding', $this->_appEncoding);
116
	}
117
 
118
/**
119
 * testBuild method
120
 *
121
 * @return void
122
 */
123
	public function testBuild() {
124
		$xml = '<tag>value</tag>';
125
		$obj = Xml::build($xml);
126
		$this->assertTrue($obj instanceof SimpleXMLElement);
127
		$this->assertEquals('tag', (string)$obj->getName());
128
		$this->assertEquals('value', (string)$obj);
129
 
130
		$xml = '<?xml version="1.0" encoding="UTF-8"?><tag>value</tag>';
131
		$this->assertEquals($obj, Xml::build($xml));
132
 
133
		$obj = Xml::build($xml, array('return' => 'domdocument'));
134
		$this->assertTrue($obj instanceof DOMDocument);
135
		$this->assertEquals('tag', $obj->firstChild->nodeName);
136
		$this->assertEquals('value', $obj->firstChild->nodeValue);
137
 
138
		$xml = CAKE . 'Test' . DS . 'Fixture' . DS . 'sample.xml';
139
		$obj = Xml::build($xml);
140
		$this->assertEquals('tags', $obj->getName());
141
		$this->assertEquals(2, count($obj));
142
 
143
		$this->assertEquals(Xml::build($xml), Xml::build(file_get_contents($xml)));
144
 
145
		$obj = Xml::build($xml, array('return' => 'domdocument'));
146
		$this->assertEquals('tags', $obj->firstChild->nodeName);
147
 
148
		$this->assertEquals(
149
			Xml::build($xml, array('return' => 'domdocument')),
150
			Xml::build(file_get_contents($xml), array('return' => 'domdocument'))
151
		);
152
		$this->assertEquals(
153
			Xml::build($xml, array('return' => 'simplexml')),
154
			Xml::build($xml, 'simplexml')
155
		);
156
 
157
		$xml = array('tag' => 'value');
158
		$obj = Xml::build($xml);
159
		$this->assertEquals('tag', $obj->getName());
160
		$this->assertEquals('value', (string)$obj);
161
 
162
		$obj = Xml::build($xml, array('return' => 'domdocument'));
163
		$this->assertEquals('tag', $obj->firstChild->nodeName);
164
		$this->assertEquals('value', $obj->firstChild->nodeValue);
165
 
166
		$obj = Xml::build($xml, array('return' => 'domdocument', 'encoding' => null));
167
		$this->assertNotRegExp('/encoding/', $obj->saveXML());
168
	}
169
 
170
/**
171
 * data provider function for testBuildInvalidData
172
 *
173
 * @return array
174
 */
175
	public static function invalidDataProvider() {
176
		return array(
177
			array(null),
178
			array(false),
179
			array(''),
180
			array('http://localhost/notthere.xml'),
181
		);
182
	}
183
 
184
/**
185
 * testBuildInvalidData
186
 *
187
 * @dataProvider invalidDataProvider
188
 * @expectedException XmlException
189
 * @return void
190
 */
191
	public function testBuildInvalidData($value) {
192
		Xml::build($value);
193
	}
194
 
195
/**
196
 * Test that building SimpleXmlElement with invalid XML causes the right exception.
197
 *
198
 * @expectedException XmlException
199
 * @return void
200
 */
201
	public function testBuildInvalidDataSimpleXml() {
202
		$input = '<derp';
203
		$xml = Xml::build($input, array('return' => 'simplexml'));
204
	}
205
 
206
/**
207
 * test build with a single empty tag
208
 *
209
 * @return void
210
 */
211
	public function testBuildEmptyTag() {
212
		try {
213
			Xml::build('<tag>');
214
			$this->fail('No exception');
215
		} catch (Exception $e) {
216
			$this->assertTrue(true, 'An exception was raised');
217
		}
218
	}
219
 
220
/**
221
 * testFromArray method
222
 *
223
 * @return void
224
 */
225
	public function testFromArray() {
226
		$xml = array('tag' => 'value');
227
		$obj = Xml::fromArray($xml);
228
		$this->assertEquals('tag', $obj->getName());
229
		$this->assertEquals('value', (string)$obj);
230
 
231
		$xml = array('tag' => null);
232
		$obj = Xml::fromArray($xml);
233
		$this->assertEquals('tag', $obj->getName());
234
		$this->assertEquals('', (string)$obj);
235
 
236
		$xml = array('tag' => array('@' => 'value'));
237
		$obj = Xml::fromArray($xml);
238
		$this->assertEquals('tag', $obj->getName());
239
		$this->assertEquals('value', (string)$obj);
240
 
241
		$xml = array(
242
			'tags' => array(
243
				'tag' => array(
244
					array(
245
						'id' => '1',
246
						'name' => 'defect'
247
					),
248
					array(
249
						'id' => '2',
250
						'name' => 'enhancement'
251
					)
252
				)
253
			)
254
		);
255
		$obj = Xml::fromArray($xml, 'attributes');
256
		$this->assertTrue($obj instanceof SimpleXMLElement);
257
		$this->assertEquals('tags', $obj->getName());
258
		$this->assertEquals(2, count($obj));
259
		$xmlText = <<<XML
260
<?xml version="1.0" encoding="UTF-8"?>
261
<tags>
262
	<tag id="1" name="defect"/>
263
	<tag id="2" name="enhancement"/>
264
</tags>
265
XML;
266
		$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
267
 
268
		$obj = Xml::fromArray($xml);
269
		$this->assertTrue($obj instanceof SimpleXMLElement);
270
		$this->assertEquals('tags', $obj->getName());
271
		$this->assertEquals(2, count($obj));
272
		$xmlText = <<<XML
273
<?xml version="1.0" encoding="UTF-8"?>
274
<tags>
275
	<tag>
276
		<id>1</id>
277
		<name>defect</name>
278
	</tag>
279
	<tag>
280
		<id>2</id>
281
		<name>enhancement</name>
282
	</tag>
283
</tags>
284
XML;
285
		$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
286
 
287
		$xml = array(
288
			'tags' => array(
289
			)
290
		);
291
		$obj = Xml::fromArray($xml);
292
		$this->assertEquals('tags', $obj->getName());
293
		$this->assertEquals('', (string)$obj);
294
 
295
		$xml = array(
296
			'tags' => array(
297
				'bool' => true,
298
				'int' => 1,
299
				'float' => 10.2,
300
				'string' => 'ok',
301
				'null' => null,
302
				'array' => array()
303
			)
304
		);
305
		$obj = Xml::fromArray($xml, 'tags');
306
		$this->assertEquals(6, count($obj));
307
		$this->assertSame((string)$obj->bool, '1');
308
		$this->assertSame((string)$obj->int, '1');
309
		$this->assertSame((string)$obj->float, '10.2');
310
		$this->assertSame((string)$obj->string, 'ok');
311
		$this->assertSame((string)$obj->null, '');
312
		$this->assertSame((string)$obj->array, '');
313
 
314
		$xml = array(
315
			'tags' => array(
316
				'tag' => array(
317
					array(
318
						'@id' => '1',
319
						'name' => 'defect'
320
					),
321
					array(
322
						'@id' => '2',
323
						'name' => 'enhancement'
324
					)
325
				)
326
			)
327
		);
328
		$obj = Xml::fromArray($xml, 'tags');
329
		$xmlText = <<<XML
330
<?xml version="1.0" encoding="UTF-8"?>
331
<tags>
332
	<tag id="1">
333
		<name>defect</name>
334
	</tag>
335
	<tag id="2">
336
		<name>enhancement</name>
337
	</tag>
338
</tags>
339
XML;
340
		$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
341
 
342
		$xml = array(
343
			'tags' => array(
344
				'tag' => array(
345
					array(
346
						'@id' => '1',
347
						'name' => 'defect',
348
						'@' => 'Tag 1'
349
					),
350
					array(
351
						'@id' => '2',
352
						'name' => 'enhancement'
353
					),
354
				),
355
				'@' => 'All tags'
356
			)
357
		);
358
		$obj = Xml::fromArray($xml, 'tags');
359
		$xmlText = <<<XML
360
<?xml version="1.0" encoding="UTF-8"?>
361
<tags>All tags<tag id="1">Tag 1<name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>
362
XML;
363
		$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
364
 
365
		$xml = array(
366
			'tags' => array(
367
				'tag' => array(
368
					'id' => 1,
369
					'@' => 'defect'
370
				)
371
			)
372
		);
373
		$obj = Xml::fromArray($xml, 'attributes');
374
		$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1">defect</tag></tags>';
375
		$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
376
 
377
		$xml = array(
378
			'tag' => array(
379
				'@' => 0,
380
				'@test' => 'A test'
381
			)
382
		);
383
		$obj = Xml::fromArray($xml);
384
		$xmlText = <<<XML
385
<?xml version="1.0" encoding="UTF-8"?>
386
<tag test="A test">0</tag>
387
XML;
388
		$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
389
	}
390
 
391
/**
392
 * Test non-sequential keys in list types.
393
 *
394
 * @return void
395
 */
396
	public function testFromArrayNonSequentialKeys() {
397
		$xmlArray = array(
398
			'Event' => array(
399
				array(
400
					'id' => '235',
401
					'Attribute' => array(
402
 
403
							'id' => '9646',
404
						),
405
						2 => array(
406
							'id' => '9647',
407
						)
408
					)
409
				)
410
			)
411
		);
412
		$obj = Xml::fromArray($xmlArray);
413
		$expected = <<<XML
414
<?xml version="1.0" encoding="UTF-8"?>
415
<Event>
416
	<id>235</id>
417
	<Attribute>
418
		<id>9646</id>
419
	</Attribute>
420
	<Attribute>
421
		<id>9647</id>
422
	</Attribute>
423
</Event>
424
XML;
425
		$this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
426
	}
427
 
428
/**
429
 * testFromArrayPretty method
430
 *
431
 * @return void
432
 */
433
	public function testFromArrayPretty() {
434
		$xml = array(
435
			'tags' => array(
436
				'tag' => array(
437
					array(
438
						'id' => '1',
439
						'name' => 'defect'
440
					),
441
					array(
442
						'id' => '2',
443
						'name' => 'enhancement'
444
					)
445
				)
446
			)
447
		);
448
 
449
		$expected = <<<XML
450
<?xml version="1.0" encoding="UTF-8"?>
451
<tags><tag><id>1</id><name>defect</name></tag><tag><id>2</id><name>enhancement</name></tag></tags>
452
 
453
XML;
454
		$xmlResponse = Xml::fromArray($xml, array('pretty' => false));
455
		$this->assertTextEquals($expected, $xmlResponse->asXML());
456
 
457
		$expected = <<<XML
458
<?xml version="1.0" encoding="UTF-8"?>
459
<tags>
460
  <tag>
461
    <id>1</id>
462
    <name>defect</name>
463
  </tag>
464
  <tag>
465
    <id>2</id>
466
    <name>enhancement</name>
467
  </tag>
468
</tags>
469
 
470
XML;
471
		$xmlResponse = Xml::fromArray($xml, array('pretty' => true));
472
		$this->assertTextEquals($expected, $xmlResponse->asXML());
473
 
474
				$xml = array(
475
			'tags' => array(
476
				'tag' => array(
477
					array(
478
						'id' => '1',
479
						'name' => 'defect'
480
					),
481
					array(
482
						'id' => '2',
483
						'name' => 'enhancement'
484
					)
485
				)
486
			)
487
		);
488
 
489
		$expected = <<<XML
490
<?xml version="1.0" encoding="UTF-8"?>
491
<tags><tag id="1" name="defect"/><tag id="2" name="enhancement"/></tags>
492
 
493
XML;
494
		$xmlResponse = Xml::fromArray($xml, array('pretty' => false, 'format' => 'attributes'));
495
		$this->assertTextEquals($expected, $xmlResponse->asXML());
496
 
497
		$expected = <<<XML
498
<?xml version="1.0" encoding="UTF-8"?>
499
<tags>
500
  <tag id="1" name="defect"/>
501
  <tag id="2" name="enhancement"/>
502
</tags>
503
 
504
XML;
505
		$xmlResponse = Xml::fromArray($xml, array('pretty' => true, 'format' => 'attributes'));
506
		$this->assertTextEquals($expected, $xmlResponse->asXML());
507
	}
508
 
509
/**
510
 * data provider for fromArray() failures
511
 *
512
 * @return array
513
 */
514
	public static function invalidArrayDataProvider() {
515
		return array(
516
			array(''),
517
			array(null),
518
			array(false),
519
			array(array()),
520
			array(array('numeric key as root')),
521
			array(array('item1' => '', 'item2' => '')),
522
			array(array('items' => array('item1', 'item2'))),
523
			array(array(
524
				'tags' => array(
525
					'tag' => array(
526
						array(
527
							array(
528
								'string'
529
							)
530
						)
531
					)
532
				)
533
			)),
534
			array(array(
535
				'tags' => array(
536
					'@tag' => array(
537
						array(
538
							'@id' => '1',
539
							'name' => 'defect'
540
						),
541
						array(
542
							'@id' => '2',
543
							'name' => 'enhancement'
544
						)
545
					)
546
				)
547
			)),
548
			array(new DateTime())
549
		);
550
	}
551
 
552
/**
553
 * testFromArrayFail method
554
 *
555
 * @dataProvider invalidArrayDataProvider
556
 * @return void
557
 */
558
	public function testFromArrayFail($value) {
559
		try {
560
			Xml::fromArray($value);
561
			$this->fail('No exception.');
562
		} catch (Exception $e) {
563
			$this->assertTrue(true, 'Caught exception.');
564
		}
565
	}
566
 
567
/**
568
 * Test that there are not unterminated errors when building xml
569
 *
570
 * @return void
571
 */
572
	public function testFromArrayUnterminatedError() {
573
		$data = array(
574
			'product_ID' => 'GENERT-DL',
575
			'deeplink' => 'http://example.com/deep',
576
			'image_URL' => 'http://example.com/image',
577
			'thumbnail_image_URL' => 'http://example.com/thumb',
578
			'brand' => 'Malte Lange & Co',
579
			'availability' => 'in stock',
580
			'authors' => array(
581
				'author' => array('Malte Lange & Co')
582
			)
583
		);
584
		$xml = Xml::fromArray(array('products' => $data), 'tags');
585
		$expected = <<<XML
586
<?xml version="1.0" encoding="UTF-8"?>
587
<products>
588
	<product_ID>GENERT-DL</product_ID>
589
	<deeplink>http://example.com/deep</deeplink>
590
	<image_URL>http://example.com/image</image_URL>
591
	<thumbnail_image_URL>http://example.com/thumb</thumbnail_image_URL>
592
	<brand>Malte Lange &amp; Co</brand>
593
	<availability>in stock</availability>
594
	<authors>
595
		<author>Malte Lange &amp; Co</author>
596
	</authors>
597
</products>
598
XML;
599
		$this->assertXmlStringEqualsXmlString($expected, $xml->asXML());
600
	}
601
 
602
/**
603
 * testToArray method
604
 *
605
 * @return void
606
 */
607
	public function testToArray() {
608
		$xml = '<tag>name</tag>';
609
		$obj = Xml::build($xml);
610
		$this->assertEquals(array('tag' => 'name'), Xml::toArray($obj));
611
 
612
		$xml = CAKE . 'Test' . DS . 'Fixture' . DS . 'sample.xml';
613
		$obj = Xml::build($xml);
614
		$expected = array(
615
			'tags' => array(
616
				'tag' => array(
617
					array(
618
						'@id' => '1',
619
						'name' => 'defect'
620
					),
621
					array(
622
						'@id' => '2',
623
						'name' => 'enhancement'
624
					)
625
				)
626
			)
627
		);
628
		$this->assertEquals($expected, Xml::toArray($obj));
629
 
630
		$array = array(
631
			'tags' => array(
632
				'tag' => array(
633
					array(
634
						'id' => '1',
635
						'name' => 'defect'
636
					),
637
					array(
638
						'id' => '2',
639
						'name' => 'enhancement'
640
					)
641
				)
642
			)
643
		);
644
		$this->assertEquals(Xml::toArray(Xml::fromArray($array, 'tags')), $array);
645
 
646
		$expected = array(
647
			'tags' => array(
648
				'tag' => array(
649
					array(
650
						'@id' => '1',
651
						'@name' => 'defect'
652
					),
653
					array(
654
						'@id' => '2',
655
						'@name' => 'enhancement'
656
					)
657
				)
658
			)
659
		);
660
		$this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
661
		$this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument', 'format' => 'attributes'))));
662
		$this->assertEquals(Xml::toArray(Xml::fromArray($array)), $array);
663
		$this->assertEquals(Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument'))), $array);
664
 
665
		$array = array(
666
			'tags' => array(
667
				'tag' => array(
668
					'id' => '1',
669
					'posts' => array(
670
						array('id' => '1'),
671
						array('id' => '2')
672
					)
673
				),
674
				'tagOther' => array(
675
					'subtag' => array(
676
						'id' => '1'
677
					)
678
				)
679
			)
680
		);
681
		$expected = array(
682
			'tags' => array(
683
				'tag' => array(
684
					'@id' => '1',
685
					'posts' => array(
686
						array('@id' => '1'),
687
						array('@id' => '2')
688
					)
689
				),
690
				'tagOther' => array(
691
					'subtag' => array(
692
						'@id' => '1'
693
					)
694
				)
695
			)
696
		);
697
		$this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
698
		$this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('format' => 'attributes', 'return' => 'domdocument'))));
699
 
700
		$xml = <<<XML
701
<root>
702
<tag id="1">defect</tag>
703
</root>
704
XML;
705
		$obj = Xml::build($xml);
706
 
707
		$expected = array(
708
			'root' => array(
709
				'tag' => array(
710
					'@id' => 1,
711
					'@' => 'defect'
712
				)
713
			)
714
		);
715
		$this->assertEquals($expected, Xml::toArray($obj));
716
 
717
		$xml = <<<XML
718
<root>
719
	<table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>
720
	<table xmlns="http://www.cakephp.org"><name>CakePHP</name><license>MIT</license></table>
721
	<table>The book is on the table.</table>
722
</root>
723
XML;
724
		$obj = Xml::build($xml);
725
 
726
		$expected = array(
727
			'root' => array(
728
				'table' => array(
729
					array('tr' => array('td' => array('Apples', 'Bananas'))),
730
					array('name' => 'CakePHP', 'license' => 'MIT'),
731
					'The book is on the table.'
732
				)
733
			)
734
		);
735
		$this->assertEquals($expected, Xml::toArray($obj));
736
 
737
		$xml = <<<XML
738
<root xmlns:cake="http://www.cakephp.org/">
739
<tag>defect</tag>
740
<cake:bug>1</cake:bug>
741
</root>
742
XML;
743
		$obj = Xml::build($xml);
744
 
745
		$expected = array(
746
			'root' => array(
747
				'tag' => 'defect',
748
				'cake:bug' => 1
749
			)
750
		);
751
		$this->assertEquals($expected, Xml::toArray($obj));
752
 
753
		$xml = '<tag type="myType">0</tag>';
754
		$obj = Xml::build($xml);
755
		$expected = array(
756
			'tag' => array(
757
				'@type' => 'myType',
758
				'@' => 0
759
			)
760
		);
761
		$this->assertEquals($expected, Xml::toArray($obj));
762
	}
763
 
764
/**
765
 * testRss
766
 *
767
 * @return void
768
 */
769
	public function testRss() {
770
		$rss = file_get_contents(CAKE . 'Test' . DS . 'Fixture' . DS . 'rss.xml');
771
		$rssAsArray = Xml::toArray(Xml::build($rss));
772
		$this->assertEquals('2.0', $rssAsArray['rss']['@version']);
773
		$this->assertEquals(2, count($rssAsArray['rss']['channel']['item']));
774
 
775
		$atomLink = array('@href' => 'http://bakery.cakephp.org/articles/rss', '@rel' => 'self', '@type' => 'application/rss+xml');
776
		$this->assertEquals($rssAsArray['rss']['channel']['atom:link'], $atomLink);
777
		$this->assertEquals('http://bakery.cakephp.org/', $rssAsArray['rss']['channel']['link']);
778
 
779
		$expected = array(
780
			'title' => 'Alertpay automated sales via IPN',
781
			'link' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn',
782
			'description' => 'I\'m going to show you how I implemented a payment module via the Alertpay payment processor.',
783
			'pubDate' => 'Tue, 31 Aug 2010 01:42:00 -0500',
784
			'guid' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn'
785
		);
786
		$this->assertSame($expected, $rssAsArray['rss']['channel']['item'][1]);
787
 
788
		$rss = array(
789
			'rss' => array(
790
				'xmlns:atom' => 'http://www.w3.org/2005/Atom',
791
				'@version' => '2.0',
792
				'channel' => array(
793
					'atom:link' => array(
794
						'@href' => 'http://bakery.cakephp.org/articles/rss',
795
						'@rel' => 'self',
796
						'@type' => 'application/rss+xml'
797
					),
798
					'title' => 'The Bakery: ',
799
					'link' => 'http://bakery.cakephp.org/',
800
					'description' => 'Recent  Articles at The Bakery.',
801
					'pubDate' => 'Sun, 12 Sep 2010 04:18:26 -0500',
802
					'item' => array(
803
						array(
804
							'title' => 'CakePHP 1.3.4 released',
805
							'link' => 'http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released'
806
						),
807
						array(
808
							'title' => 'Wizard Component 1.2 Tutorial',
809
							'link' => 'http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial'
810
						)
811
					)
812
				)
813
			)
814
		);
815
		$rssAsSimpleXML = Xml::fromArray($rss);
816
		$xmlText = <<<XML
817
<?xml version="1.0" encoding="UTF-8"?>
818
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
819
<channel>
820
	<atom:link href="http://bakery.cakephp.org/articles/rss" rel="self" type="application/rss+xml"/>
821
	<title>The Bakery: </title>
822
	<link>http://bakery.cakephp.org/</link>
823
	<description>Recent  Articles at The Bakery.</description>
824
	<pubDate>Sun, 12 Sep 2010 04:18:26 -0500</pubDate>
825
	<item>
826
		<title>CakePHP 1.3.4 released</title>
827
		<link>http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released</link>
828
	</item>
829
	<item>
830
		<title>Wizard Component 1.2 Tutorial</title>
831
		<link>http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial</link>
832
	</item>
833
</channel>
834
</rss>
835
XML;
836
		$this->assertXmlStringEqualsXmlString($xmlText, $rssAsSimpleXML->asXML());
837
	}
838
 
839
/**
840
 * testXmlRpc
841
 *
842
 * @return void
843
 */
844
	public function testXmlRpc() {
845
		$xml = Xml::build('<methodCall><methodName>test</methodName><params /></methodCall>');
846
		$expected = array(
847
			'methodCall' => array(
848
				'methodName' => 'test',
849
				'params' => ''
850
			)
851
		);
852
		$this->assertSame($expected, Xml::toArray($xml));
853
 
854
		$xml = Xml::build('<methodCall><methodName>test</methodName><params><param><value><array><data><value><int>12</int></value><value><string>Egypt</string></value><value><boolean>0</boolean></value><value><int>-31</int></value></data></array></value></param></params></methodCall>');
855
		$expected = array(
856
			'methodCall' => array(
857
				'methodName' => 'test',
858
				'params' => array(
859
					'param' => array(
860
						'value' => array(
861
							'array' => array(
862
								'data' => array(
863
									'value' => array(
864
										array('int' => '12'),
865
										array('string' => 'Egypt'),
866
										array('boolean' => '0'),
867
										array('int' => '-31')
868
									)
869
								)
870
							)
871
						)
872
					)
873
				)
874
			)
875
		);
876
		$this->assertSame($expected, Xml::toArray($xml));
877
 
878
		$xmlText = <<<XML
879
<?xml version="1.0" encoding="UTF-8"?>
880
<methodResponse>
881
	<params>
882
		<param>
883
			<value>
884
				<array>
885
					<data>
886
						<value>
887
							<int>1</int>
888
						</value>
889
						<value>
890
							<string>testing</string>
891
						</value>
892
					</data>
893
				</array>
894
			</value>
895
		</param>
896
	</params>
897
</methodResponse>
898
XML;
899
		$xml = Xml::build($xmlText);
900
		$expected = array(
901
			'methodResponse' => array(
902
				'params' => array(
903
					'param' => array(
904
						'value' => array(
905
							'array' => array(
906
								'data' => array(
907
									'value' => array(
908
										array('int' => '1'),
909
										array('string' => 'testing')
910
									)
911
								)
912
							)
913
						)
914
					)
915
				)
916
			)
917
		);
918
		$this->assertSame($expected, Xml::toArray($xml));
919
 
920
		$xml = Xml::fromArray($expected, 'tags');
921
		$this->assertXmlStringEqualsXmlString($xmlText, $xml->asXML());
922
	}
923
 
924
/**
925
 * testSoap
926
 *
927
 * @return void
928
 */
929
	public function testSoap() {
930
		$xmlRequest = Xml::build(CAKE . 'Test' . DS . 'Fixture' . DS . 'soap_request.xml');
931
		$expected = array(
932
			'Envelope' => array(
933
				'@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
934
				'soap:Body' => array(
935
					'm:GetStockPrice' => array(
936
						'm:StockName' => 'IBM'
937
					)
938
				)
939
			)
940
		);
941
		$this->assertEquals($expected, Xml::toArray($xmlRequest));
942
 
943
		$xmlResponse = Xml::build(CAKE . 'Test' . DS . 'Fixture' . DS . 'soap_response.xml');
944
		$expected = array(
945
			'Envelope' => array(
946
				'@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
947
				'soap:Body' => array(
948
					'm:GetStockPriceResponse' => array(
949
						'm:Price' => '34.5'
950
					)
951
				)
952
			)
953
		);
954
		$this->assertEquals($expected, Xml::toArray($xmlResponse));
955
 
956
		$xml = array(
957
			'soap:Envelope' => array(
958
				'xmlns:soap' => 'http://www.w3.org/2001/12/soap-envelope',
959
				'@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
960
				'soap:Body' => array(
961
					'xmlns:m' => 'http://www.example.org/stock',
962
					'm:GetStockPrice' => array(
963
						'm:StockName' => 'IBM'
964
					)
965
				)
966
			)
967
		);
968
		$xmlRequest = Xml::fromArray($xml, array('encoding' => null));
969
		$xmlText = <<<XML
970
<?xml version="1.0"?>
971
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
972
	<soap:Body xmlns:m="http://www.example.org/stock">
973
	<m:GetStockPrice><m:StockName>IBM</m:StockName></m:GetStockPrice>
974
	</soap:Body>
975
</soap:Envelope>
976
XML;
977
		$this->assertXmlStringEqualsXmlString($xmlText, $xmlRequest->asXML());
978
	}
979
 
980
/**
981
 * testNamespace
982
 *
983
 * @return void
984
 */
985
	public function testNamespace() {
986
		$xml = <<<XML
987
<root xmlns:ns="http://cakephp.org">
988
	<ns:tag id="1">
989
		<child>good</child>
990
		<otherchild>bad</otherchild>
991
	</ns:tag>
992
	<tag>Tag without ns</tag>
993
</root>
994
XML;
995
		$xmlResponse = Xml::build($xml);
996
		$expected = array(
997
			'root' => array(
998
				'ns:tag' => array(
999
					'@id' => '1',
1000
					'child' => 'good',
1001
					'otherchild' => 'bad'
1002
				),
1003
				'tag' => 'Tag without ns'
1004
			)
1005
		);
1006
		$this->assertEquals($expected, Xml::toArray($xmlResponse));
1007
 
1008
		$xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:tag id="1" /><tag><id>1</id></tag></root>');
1009
		$expected = array(
1010
			'root' => array(
1011
				'ns:tag' => array(
1012
					'@id' => '1'
1013
				),
1014
				'tag' => array(
1015
					'id' => '1'
1016
				)
1017
			)
1018
		);
1019
		$this->assertEquals($expected, Xml::toArray($xmlResponse));
1020
 
1021
		$xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:attr>1</ns:attr></root>');
1022
		$expected = array(
1023
			'root' => array(
1024
				'ns:attr' => '1'
1025
			)
1026
		);
1027
		$this->assertEquals($expected, Xml::toArray($xmlResponse));
1028
 
1029
		$xmlResponse = Xml::build('<root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>');
1030
		$this->assertEquals($expected, Xml::toArray($xmlResponse));
1031
 
1032
		$xml = array(
1033
			'root' => array(
1034
				'ns:attr' => array(
1035
					'xmlns:ns' => 'http://cakephp.org',
1036
					'@' => 1
1037
				)
1038
			)
1039
		);
1040
		$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>';
1041
		$xmlResponse = Xml::fromArray($xml);
1042
		$this->assertEquals($expected, str_replace(array("\r", "\n"), '', $xmlResponse->asXML()));
1043
 
1044
		$xml = array(
1045
			'root' => array(
1046
				'tag' => array(
1047
					'xmlns:pref' => 'http://cakephp.org',
1048
					'pref:item' => array(
1049
						'item 1',
1050
						'item 2'
1051
					)
1052
				)
1053
			)
1054
		);
1055
		$expected = <<<XML
1056
<?xml version="1.0" encoding="UTF-8"?>
1057
<root>
1058
	<tag xmlns:pref="http://cakephp.org">
1059
		<pref:item>item 1</pref:item>
1060
		<pref:item>item 2</pref:item>
1061
	</tag>
1062
</root>
1063
XML;
1064
		$xmlResponse = Xml::fromArray($xml);
1065
		$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
1066
 
1067
		$xml = array(
1068
			'root' => array(
1069
				'tag' => array(
1070
					'xmlns:' => 'http://cakephp.org'
1071
				)
1072
			)
1073
		);
1074
		$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns="http://cakephp.org"/></root>';
1075
		$xmlResponse = Xml::fromArray($xml);
1076
		$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
1077
 
1078
		$xml = array(
1079
			'root' => array(
1080
				'xmlns:' => 'http://cakephp.org'
1081
			)
1082
		);
1083
		$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns="http://cakephp.org"/>';
1084
		$xmlResponse = Xml::fromArray($xml);
1085
		$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
1086
 
1087
		$xml = array(
1088
			'root' => array(
1089
				'xmlns:ns' => 'http://cakephp.org'
1090
			)
1091
		);
1092
		$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns:ns="http://cakephp.org"/>';
1093
		$xmlResponse = Xml::fromArray($xml);
1094
		$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
1095
	}
1096
 
1097
/**
1098
 * test that CDATA blocks don't get screwed up by SimpleXml
1099
 *
1100
 * @return void
1101
 */
1102
	public function testCdata() {
1103
		$xml = '<' . '?xml version="1.0" encoding="UTF-8"?>' .
1104
			'<people><name><![CDATA[ Mark ]]></name></people>';
1105
 
1106
		$result = Xml::build($xml);
1107
		$this->assertEquals(' Mark ', (string)$result->name);
1108
	}
1109
 
1110
/**
1111
 * data provider for toArray() failures
1112
 *
1113
 * @return array
1114
 */
1115
	public static function invalidToArrayDataProvider() {
1116
		return array(
1117
			array(new DateTime()),
1118
			array(array())
1119
		);
1120
	}
1121
 
1122
/**
1123
 * testToArrayFail method
1124
 *
1125
 * @dataProvider invalidToArrayDataProvider
1126
 * @expectedException XmlException
1127
 * @return void
1128
 */
1129
	public function testToArrayFail($value) {
1130
		Xml::toArray($value);
1131
	}
1132
 
1133
/**
1134
 * testWithModel method
1135
 *
1136
 * @return void
1137
 */
1138
	public function testWithModel() {
1139
		$this->loadFixtures('User', 'Article');
1140
 
1141
		$user = new XmlUser();
1142
		$data = $user->read(null, 1);
1143
 
1144
		$obj = Xml::build(compact('data'));
1145
		$expected = <<<XML
1146
<?xml version="1.0" encoding="UTF-8"?><data>
1147
<User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
1148
<created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>
1149
<Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>
1150
<published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>
1151
<Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>
1152
<published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>
1153
</data>
1154
XML;
1155
		$this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
1156
 
1157
		//multiple model results - without a records key it would fatal error
1158
		$data = $user->find('all', array('limit' => 2));
1159
		$data = array('records' => $data);
1160
		$obj = Xml::build(compact('data'));
1161
		$expected = <<<XML
1162
<?xml version="1.0" encoding="UTF-8"?><data>
1163
<records>
1164
<User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
1165
<created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>
1166
<Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>
1167
<published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>
1168
<Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>
1169
<published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>
1170
</records><records><User><id>2</id><user>nate</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
1171
<created>2007-03-17 01:18:23</created><updated>2007-03-17 01:20:31</updated></User><Article/>
1172
</records>
1173
</data>
1174
XML;
1175
		$obj->asXML();
1176
		$this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
1177
	}
1178
 
1179
/**
1180
 * Test ampersand in text elements.
1181
 *
1182
 * @return void
1183
 */
1184
	public function testAmpInText() {
1185
		$data = array(
1186
			'outer' => array(
1187
				'inner' => array('name' => 'mark & mark')
1188
			)
1189
		);
1190
		$obj = Xml::build($data);
1191
		$result = $obj->asXml();
1192
		$this->assertContains('mark &amp; mark', $result);
1193
	}
1194
 
1195
/**
1196
 * Test that entity loading is disabled by default.
1197
 *
1198
 * @return void
1199
 */
1200
	public function testNoEntityLoading() {
1201
		$file = CAKE . 'VERSION.txt';
1202
		$xml = <<<XML
1203
<!DOCTYPE cakephp [
1204
  <!ENTITY payload SYSTEM "file://$file" >]>
1205
<request>
1206
  <xxe>&payload;</xxe>
1207
</request>
1208
XML;
1209
		try {
1210
			$result = Xml::build($xml);
1211
			$this->assertEquals('', (string)$result->xxe);
1212
		} catch (Exception $e) {
1213
			$this->assertTrue(true, 'A warning was raised meaning external entities were not loaded');
1214
		}
1215
	}
1216
 
1217
}