Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 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 boolean
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
 */
557
	public function testFromArrayFail($value) {
558
		try {
559
			Xml::fromArray($value);
560
			$this->fail('No exception.');
561
		} catch (Exception $e) {
562
			$this->assertTrue(true, 'Caught exception.');
563
		}
564
	}
565
 
566
/**
567
 * Test that there are not unterminated errors when building xml
568
 *
569
 * @return void
570
 */
571
	public function testFromArrayUnterminatedError() {
572
		$data = array(
573
			'product_ID' => 'GENERT-DL',
574
			'deeplink' => 'http://example.com/deep',
575
			'image_URL' => 'http://example.com/image',
576
			'thumbnail_image_URL' => 'http://example.com/thumb',
577
			'brand' => 'Malte Lange & Co',
578
			'availability' => 'in stock',
579
			'authors' => array(
580
				'author' => array('Malte Lange & Co')
581
			)
582
		);
583
		$xml = Xml::fromArray(array('products' => $data), 'tags');
584
		$expected = <<<XML
585
<?xml version="1.0" encoding="UTF-8"?>
586
<products>
587
	<product_ID>GENERT-DL</product_ID>
588
	<deeplink>http://example.com/deep</deeplink>
589
	<image_URL>http://example.com/image</image_URL>
590
	<thumbnail_image_URL>http://example.com/thumb</thumbnail_image_URL>
591
	<brand>Malte Lange &amp; Co</brand>
592
	<availability>in stock</availability>
593
	<authors>
594
		<author>Malte Lange &amp; Co</author>
595
	</authors>
596
</products>
597
XML;
598
		$this->assertXmlStringEqualsXmlString($expected, $xml->asXML());
599
	}
600
 
601
/**
602
 * testToArray method
603
 *
604
 * @return void
605
 */
606
	public function testToArray() {
607
		$xml = '<tag>name</tag>';
608
		$obj = Xml::build($xml);
609
		$this->assertEquals(array('tag' => 'name'), Xml::toArray($obj));
610
 
611
		$xml = CAKE . 'Test' . DS . 'Fixture' . DS . 'sample.xml';
612
		$obj = Xml::build($xml);
613
		$expected = array(
614
			'tags' => array(
615
				'tag' => array(
616
					array(
617
						'@id' => '1',
618
						'name' => 'defect'
619
					),
620
					array(
621
						'@id' => '2',
622
						'name' => 'enhancement'
623
					)
624
				)
625
			)
626
		);
627
		$this->assertEquals($expected, Xml::toArray($obj));
628
 
629
		$array = array(
630
			'tags' => array(
631
				'tag' => array(
632
					array(
633
						'id' => '1',
634
						'name' => 'defect'
635
					),
636
					array(
637
						'id' => '2',
638
						'name' => 'enhancement'
639
					)
640
				)
641
			)
642
		);
643
		$this->assertEquals(Xml::toArray(Xml::fromArray($array, 'tags')), $array);
644
 
645
		$expected = array(
646
			'tags' => array(
647
				'tag' => array(
648
					array(
649
						'@id' => '1',
650
						'@name' => 'defect'
651
					),
652
					array(
653
						'@id' => '2',
654
						'@name' => 'enhancement'
655
					)
656
				)
657
			)
658
		);
659
		$this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
660
		$this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument', 'format' => 'attributes'))));
661
		$this->assertEquals(Xml::toArray(Xml::fromArray($array)), $array);
662
		$this->assertEquals(Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument'))), $array);
663
 
664
		$array = array(
665
			'tags' => array(
666
				'tag' => array(
667
					'id' => '1',
668
					'posts' => array(
669
						array('id' => '1'),
670
						array('id' => '2')
671
					)
672
				),
673
				'tagOther' => array(
674
					'subtag' => array(
675
						'id' => '1'
676
					)
677
				)
678
			)
679
		);
680
		$expected = array(
681
			'tags' => array(
682
				'tag' => array(
683
					'@id' => '1',
684
					'posts' => array(
685
						array('@id' => '1'),
686
						array('@id' => '2')
687
					)
688
				),
689
				'tagOther' => array(
690
					'subtag' => array(
691
						'@id' => '1'
692
					)
693
				)
694
			)
695
		);
696
		$this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
697
		$this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('format' => 'attributes', 'return' => 'domdocument'))));
698
 
699
		$xml = <<<XML
700
<root>
701
<tag id="1">defect</tag>
702
</root>
703
XML;
704
		$obj = Xml::build($xml);
705
 
706
		$expected = array(
707
			'root' => array(
708
				'tag' => array(
709
					'@id' => 1,
710
					'@' => 'defect'
711
				)
712
			)
713
		);
714
		$this->assertEquals($expected, Xml::toArray($obj));
715
 
716
		$xml = <<<XML
717
<root>
718
	<table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>
719
	<table xmlns="http://www.cakephp.org"><name>CakePHP</name><license>MIT</license></table>
720
	<table>The book is on the table.</table>
721
</root>
722
XML;
723
		$obj = Xml::build($xml);
724
 
725
		$expected = array(
726
			'root' => array(
727
				'table' => array(
728
					array('tr' => array('td' => array('Apples', 'Bananas'))),
729
					array('name' => 'CakePHP', 'license' => 'MIT'),
730
					'The book is on the table.'
731
				)
732
			)
733
		);
734
		$this->assertEquals($expected, Xml::toArray($obj));
735
 
736
		$xml = <<<XML
737
<root xmlns:cake="http://www.cakephp.org/">
738
<tag>defect</tag>
739
<cake:bug>1</cake:bug>
740
</root>
741
XML;
742
		$obj = Xml::build($xml);
743
 
744
		$expected = array(
745
			'root' => array(
746
				'tag' => 'defect',
747
				'cake:bug' => 1
748
			)
749
		);
750
		$this->assertEquals($expected, Xml::toArray($obj));
751
 
752
		$xml = '<tag type="myType">0</tag>';
753
		$obj = Xml::build($xml);
754
		$expected = array(
755
			'tag' => array(
756
				'@type' => 'myType',
757
				'@' => 0
758
			)
759
		);
760
		$this->assertEquals($expected, Xml::toArray($obj));
761
	}
762
 
763
/**
764
 * testRss
765
 *
766
 * @return void
767
 */
768
	public function testRss() {
769
		$rss = file_get_contents(CAKE . 'Test' . DS . 'Fixture' . DS . 'rss.xml');
770
		$rssAsArray = Xml::toArray(Xml::build($rss));
771
		$this->assertEquals('2.0', $rssAsArray['rss']['@version']);
772
		$this->assertEquals(2, count($rssAsArray['rss']['channel']['item']));
773
 
774
		$atomLink = array('@href' => 'http://bakery.cakephp.org/articles/rss', '@rel' => 'self', '@type' => 'application/rss+xml');
775
		$this->assertEquals($rssAsArray['rss']['channel']['atom:link'], $atomLink);
776
		$this->assertEquals('http://bakery.cakephp.org/', $rssAsArray['rss']['channel']['link']);
777
 
778
		$expected = array(
779
			'title' => 'Alertpay automated sales via IPN',
780
			'link' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn',
781
			'description' => 'I\'m going to show you how I implemented a payment module via the Alertpay payment processor.',
782
			'pubDate' => 'Tue, 31 Aug 2010 01:42:00 -0500',
783
			'guid' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn'
784
		);
785
		$this->assertSame($rssAsArray['rss']['channel']['item'][1], $expected);
786
 
787
		$rss = array(
788
			'rss' => array(
789
				'xmlns:atom' => 'http://www.w3.org/2005/Atom',
790
				'@version' => '2.0',
791
				'channel' => array(
792
					'atom:link' => array(
793
						'@href' => 'http://bakery.cakephp.org/articles/rss',
794
						'@rel' => 'self',
795
						'@type' => 'application/rss+xml'
796
					),
797
					'title' => 'The Bakery: ',
798
					'link' => 'http://bakery.cakephp.org/',
799
					'description' => 'Recent  Articles at The Bakery.',
800
					'pubDate' => 'Sun, 12 Sep 2010 04:18:26 -0500',
801
					'item' => array(
802
						array(
803
							'title' => 'CakePHP 1.3.4 released',
804
							'link' => 'http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released'
805
						),
806
						array(
807
							'title' => 'Wizard Component 1.2 Tutorial',
808
							'link' => 'http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial'
809
						)
810
					)
811
				)
812
			)
813
		);
814
		$rssAsSimpleXML = Xml::fromArray($rss);
815
		$xmlText = <<<XML
816
<?xml version="1.0" encoding="UTF-8"?>
817
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
818
<channel>
819
	<atom:link href="http://bakery.cakephp.org/articles/rss" rel="self" type="application/rss+xml"/>
820
	<title>The Bakery: </title>
821
	<link>http://bakery.cakephp.org/</link>
822
	<description>Recent  Articles at The Bakery.</description>
823
	<pubDate>Sun, 12 Sep 2010 04:18:26 -0500</pubDate>
824
	<item>
825
		<title>CakePHP 1.3.4 released</title>
826
		<link>http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released</link>
827
	</item>
828
	<item>
829
		<title>Wizard Component 1.2 Tutorial</title>
830
		<link>http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial</link>
831
	</item>
832
</channel>
833
</rss>
834
XML;
835
		$this->assertXmlStringEqualsXmlString($xmlText, $rssAsSimpleXML->asXML());
836
	}
837
 
838
/**
839
 * testXmlRpc
840
 *
841
 * @return void
842
 */
843
	public function testXmlRpc() {
844
		$xml = Xml::build('<methodCall><methodName>test</methodName><params /></methodCall>');
845
		$expected = array(
846
			'methodCall' => array(
847
				'methodName' => 'test',
848
				'params' => ''
849
			)
850
		);
851
		$this->assertSame(Xml::toArray($xml), $expected);
852
 
853
		$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>');
854
		$expected = array(
855
			'methodCall' => array(
856
				'methodName' => 'test',
857
				'params' => array(
858
					'param' => array(
859
						'value' => array(
860
							'array' => array(
861
								'data' => array(
862
									'value' => array(
863
										array('int' => '12'),
864
										array('string' => 'Egypt'),
865
										array('boolean' => '0'),
866
										array('int' => '-31')
867
									)
868
								)
869
							)
870
						)
871
					)
872
				)
873
			)
874
		);
875
		$this->assertSame(Xml::toArray($xml), $expected);
876
 
877
		$xmlText = <<<XML
878
<?xml version="1.0" encoding="UTF-8"?>
879
<methodResponse>
880
	<params>
881
		<param>
882
			<value>
883
				<array>
884
					<data>
885
						<value>
886
							<int>1</int>
887
						</value>
888
						<value>
889
							<string>testing</string>
890
						</value>
891
					</data>
892
				</array>
893
			</value>
894
		</param>
895
	</params>
896
</methodResponse>
897
XML;
898
		$xml = Xml::build($xmlText);
899
		$expected = array(
900
			'methodResponse' => array(
901
				'params' => array(
902
					'param' => array(
903
						'value' => array(
904
							'array' => array(
905
								'data' => array(
906
									'value' => array(
907
										array('int' => '1'),
908
										array('string' => 'testing')
909
									)
910
								)
911
							)
912
						)
913
					)
914
				)
915
			)
916
		);
917
		$this->assertSame(Xml::toArray($xml), $expected);
918
 
919
		$xml = Xml::fromArray($expected, 'tags');
920
		$this->assertXmlStringEqualsXmlString($xmlText, $xml->asXML());
921
	}
922
 
923
/**
924
 * testSoap
925
 *
926
 * @return void
927
 */
928
	public function testSoap() {
929
		$xmlRequest = Xml::build(CAKE . 'Test' . DS . 'Fixture' . DS . 'soap_request.xml');
930
		$expected = array(
931
			'Envelope' => array(
932
				'@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
933
				'soap:Body' => array(
934
					'm:GetStockPrice' => array(
935
						'm:StockName' => 'IBM'
936
					)
937
				)
938
			)
939
		);
940
		$this->assertEquals($expected, Xml::toArray($xmlRequest));
941
 
942
		$xmlResponse = Xml::build(CAKE . 'Test' . DS . 'Fixture' . DS . 'soap_response.xml');
943
		$expected = array(
944
			'Envelope' => array(
945
				'@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
946
				'soap:Body' => array(
947
					'm:GetStockPriceResponse' => array(
948
						'm:Price' => '34.5'
949
					)
950
				)
951
			)
952
		);
953
		$this->assertEquals($expected, Xml::toArray($xmlResponse));
954
 
955
		$xml = array(
956
			'soap:Envelope' => array(
957
				'xmlns:soap' => 'http://www.w3.org/2001/12/soap-envelope',
958
				'@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
959
				'soap:Body' => array(
960
					'xmlns:m' => 'http://www.example.org/stock',
961
					'm:GetStockPrice' => array(
962
						'm:StockName' => 'IBM'
963
					)
964
				)
965
			)
966
		);
967
		$xmlRequest = Xml::fromArray($xml, array('encoding' => null));
968
		$xmlText = <<<XML
969
<?xml version="1.0"?>
970
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
971
	<soap:Body xmlns:m="http://www.example.org/stock">
972
	<m:GetStockPrice><m:StockName>IBM</m:StockName></m:GetStockPrice>
973
	</soap:Body>
974
</soap:Envelope>
975
XML;
976
		$this->assertXmlStringEqualsXmlString($xmlText, $xmlRequest->asXML());
977
	}
978
 
979
/**
980
 * testNamespace
981
 *
982
 * @return void
983
 */
984
	public function testNamespace() {
985
		$xml = <<<XML
986
<root xmlns:ns="http://cakephp.org">
987
	<ns:tag id="1">
988
		<child>good</child>
989
		<otherchild>bad</otherchild>
990
	</ns:tag>
991
	<tag>Tag without ns</tag>
992
</root>
993
XML;
994
		$xmlResponse = Xml::build($xml);
995
		$expected = array(
996
			'root' => array(
997
				'ns:tag' => array(
998
					'@id' => '1',
999
					'child' => 'good',
1000
					'otherchild' => 'bad'
1001
				),
1002
				'tag' => 'Tag without ns'
1003
			)
1004
		);
1005
		$this->assertEquals($expected, Xml::toArray($xmlResponse));
1006
 
1007
		$xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:tag id="1" /><tag><id>1</id></tag></root>');
1008
		$expected = array(
1009
			'root' => array(
1010
				'ns:tag' => array(
1011
					'@id' => '1'
1012
				),
1013
				'tag' => array(
1014
					'id' => '1'
1015
				)
1016
			)
1017
		);
1018
		$this->assertEquals($expected, Xml::toArray($xmlResponse));
1019
 
1020
		$xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:attr>1</ns:attr></root>');
1021
		$expected = array(
1022
			'root' => array(
1023
				'ns:attr' => '1'
1024
			)
1025
		);
1026
		$this->assertEquals($expected, Xml::toArray($xmlResponse));
1027
 
1028
		$xmlResponse = Xml::build('<root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>');
1029
		$this->assertEquals($expected, Xml::toArray($xmlResponse));
1030
 
1031
		$xml = array(
1032
			'root' => array(
1033
				'ns:attr' => array(
1034
					'xmlns:ns' => 'http://cakephp.org',
1035
					'@' => 1
1036
				)
1037
			)
1038
		);
1039
		$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>';
1040
		$xmlResponse = Xml::fromArray($xml);
1041
		$this->assertEquals(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
1042
 
1043
		$xml = array(
1044
			'root' => array(
1045
				'tag' => array(
1046
					'xmlns:pref' => 'http://cakephp.org',
1047
					'pref:item' => array(
1048
						'item 1',
1049
						'item 2'
1050
					)
1051
				)
1052
			)
1053
		);
1054
		$expected = <<<XML
1055
<?xml version="1.0" encoding="UTF-8"?>
1056
<root>
1057
	<tag xmlns:pref="http://cakephp.org">
1058
		<pref:item>item 1</pref:item>
1059
		<pref:item>item 2</pref:item>
1060
	</tag>
1061
</root>
1062
XML;
1063
		$xmlResponse = Xml::fromArray($xml);
1064
		$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
1065
 
1066
		$xml = array(
1067
			'root' => array(
1068
				'tag' => array(
1069
					'xmlns:' => 'http://cakephp.org'
1070
				)
1071
			)
1072
		);
1073
		$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns="http://cakephp.org"/></root>';
1074
		$xmlResponse = Xml::fromArray($xml);
1075
		$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
1076
 
1077
		$xml = array(
1078
			'root' => array(
1079
				'xmlns:' => 'http://cakephp.org'
1080
			)
1081
		);
1082
		$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns="http://cakephp.org"/>';
1083
		$xmlResponse = Xml::fromArray($xml);
1084
		$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
1085
 
1086
		$xml = array(
1087
			'root' => array(
1088
				'xmlns:ns' => 'http://cakephp.org'
1089
			)
1090
		);
1091
		$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns:ns="http://cakephp.org"/>';
1092
		$xmlResponse = Xml::fromArray($xml);
1093
		$this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
1094
	}
1095
 
1096
/**
1097
 * test that CDATA blocks don't get screwed up by SimpleXml
1098
 *
1099
 * @return void
1100
 */
1101
	public function testCdata() {
1102
		$xml = '<' . '?xml version="1.0" encoding="UTF-8"?>' .
1103
			'<people><name><![CDATA[ Mark ]]></name></people>';
1104
 
1105
		$result = Xml::build($xml);
1106
		$this->assertEquals(' Mark ', (string)$result->name);
1107
	}
1108
 
1109
/**
1110
 * data provider for toArray() failures
1111
 *
1112
 * @return array
1113
 */
1114
	public static function invalidToArrayDataProvider() {
1115
		return array(
1116
			array(new DateTime()),
1117
			array(array())
1118
		);
1119
	}
1120
 
1121
/**
1122
 * testToArrayFail method
1123
 *
1124
 * @dataProvider invalidToArrayDataProvider
1125
 * @expectedException XmlException
1126
 */
1127
	public function testToArrayFail($value) {
1128
		Xml::toArray($value);
1129
	}
1130
 
1131
/**
1132
 * testWithModel method
1133
 *
1134
 * @return void
1135
 */
1136
	public function testWithModel() {
1137
		$this->loadFixtures('User', 'Article');
1138
 
1139
		$user = new XmlUser();
1140
		$data = $user->read(null, 1);
1141
 
1142
		$obj = Xml::build(compact('data'));
1143
		$expected = <<<XML
1144
<?xml version="1.0" encoding="UTF-8"?><data>
1145
<User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
1146
<created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>
1147
<Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>
1148
<published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>
1149
<Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>
1150
<published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>
1151
</data>
1152
XML;
1153
		$this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
1154
 
1155
		//multiple model results - without a records key it would fatal error
1156
		$data = $user->find('all', array('limit' => 2));
1157
		$data = array('records' => $data);
1158
		$obj = Xml::build(compact('data'));
1159
		$expected = <<<XML
1160
<?xml version="1.0" encoding="UTF-8"?><data>
1161
<records>
1162
<User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
1163
<created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>
1164
<Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>
1165
<published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>
1166
<Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>
1167
<published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>
1168
</records><records><User><id>2</id><user>nate</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
1169
<created>2007-03-17 01:18:23</created><updated>2007-03-17 01:20:31</updated></User><Article/>
1170
</records>
1171
</data>
1172
XML;
1173
		$obj->asXML();
1174
		$this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
1175
	}
1176
 
1177
/**
1178
 * Test ampersand in text elements.
1179
 *
1180
 * @return void
1181
 */
1182
	public function testAmpInText() {
1183
		$data = array(
1184
			'outer' => array(
1185
				'inner' => array('name' => 'mark & mark')
1186
			)
1187
		);
1188
		$obj = Xml::build($data);
1189
		$result = $obj->asXml();
1190
		$this->assertContains('mark &amp; mark', $result);
1191
	}
1192
 
1193
/**
1194
 * Test that entity loading is disabled by default.
1195
 *
1196
 * @return void
1197
 */
1198
	public function testNoEntityLoading() {
1199
		$file = CAKE . 'VERSION.txt';
1200
		$xml = <<<XML
1201
<!DOCTYPE cakephp [
1202
  <!ENTITY payload SYSTEM "file://$file" >]>
1203
<request>
1204
  <xxe>&payload;</xxe>
1205
</request>
1206
XML;
1207
		try {
1208
			$result = Xml::build($xml);
1209
			$this->assertEquals('', (string)$result->xxe);
1210
		} catch (Exception $e) {
1211
			$this->assertTrue(true, 'A warning was raised meaning external entities were not loaded');
1212
		}
1213
	}
1214
 
1215
}