| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183 |
- <?php
- /**
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Utility;
- use Cake\Collection\Collection;
- use Cake\Core\Configure;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\Error\XmlException;
- use Cake\Utility\Xml;
- /**
- * XmlTest class
- *
- */
- class XmlTest extends TestCase {
- /**
- * autoFixtures property
- *
- * @var bool
- */
- public $autoFixtures = false;
- /**
- * fixtures property
- * @var array
- */
- public $fixtures = array(
- 'core.article', 'core.user'
- );
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->_appEncoding = Configure::read('App.encoding');
- Configure::write('App.encoding', 'UTF-8');
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- Configure::write('App.encoding', $this->_appEncoding);
- }
- /**
- * testBuild method
- *
- * @return void
- */
- public function testBuild() {
- $xml = '<tag>value</tag>';
- $obj = Xml::build($xml);
- $this->assertTrue($obj instanceof \SimpleXMLElement);
- $this->assertEquals('tag', (string)$obj->getName());
- $this->assertEquals('value', (string)$obj);
- $xml = '<?xml version="1.0" encoding="UTF-8"?><tag>value</tag>';
- $this->assertEquals($obj, Xml::build($xml));
- $obj = Xml::build($xml, array('return' => 'domdocument'));
- $this->assertTrue($obj instanceof \DOMDocument);
- $this->assertEquals('tag', $obj->firstChild->nodeName);
- $this->assertEquals('value', $obj->firstChild->nodeValue);
- $xml = CORE_TESTS . 'Fixture/sample.xml';
- $obj = Xml::build($xml);
- $this->assertEquals('tags', $obj->getName());
- $this->assertEquals(2, count($obj));
- $this->assertEquals(Xml::build($xml), Xml::build(file_get_contents($xml)));
- $obj = Xml::build($xml, array('return' => 'domdocument'));
- $this->assertEquals('tags', $obj->firstChild->nodeName);
- $this->assertEquals(
- Xml::build($xml, array('return' => 'domdocument')),
- Xml::build(file_get_contents($xml), array('return' => 'domdocument'))
- );
- $xml = array('tag' => 'value');
- $obj = Xml::build($xml);
- $this->assertEquals('tag', $obj->getName());
- $this->assertEquals('value', (string)$obj);
- $obj = Xml::build($xml, array('return' => 'domdocument'));
- $this->assertEquals('tag', $obj->firstChild->nodeName);
- $this->assertEquals('value', $obj->firstChild->nodeValue);
- $obj = Xml::build($xml, array('return' => 'domdocument', 'encoding' => null));
- $this->assertNotRegExp('/encoding/', $obj->saveXML());
- }
- /**
- * Test build() with a Collection instance.
- *
- * @return void
- */
- public function testBuildCollection() {
- $xml = new Collection(['tag' => 'value']);
- $obj = Xml::build($xml);
- $this->assertEquals('tag', $obj->getName());
- $this->assertEquals('value', (string)$obj);
- $xml = new Collection([
- 'response' => [
- 'users' => new Collection(['leonardo', 'raphael'])
- ]
- ]);
- $obj = Xml::build($xml);
- $this->assertContains('<users>leonardo</users>', $obj->saveXML());
- }
- /**
- * data provider function for testBuildInvalidData
- *
- * @return array
- */
- public static function invalidDataProvider() {
- return array(
- array(null),
- array(false),
- array(''),
- array('http://localhost/notthere.xml'),
- );
- }
- /**
- * testBuildInvalidData
- *
- * @dataProvider invalidDataProvider
- * @expectedException \Cake\Error\Exception
- * @return void
- */
- public function testBuildInvalidData($value) {
- $this->assertFalse(defined('HHVM_VERSION') && !empty($value), 'Segfaults HHVM');
- Xml::build($value);
- }
- /**
- * Test that building SimpleXmlElement with invalid XML causes the right exception.
- *
- * @expectedException \Cake\Utility\Error\XmlException
- * @return void
- */
- public function testBuildInvalidDataSimpleXml() {
- $input = '<derp';
- $xml = Xml::build($input, array('return' => 'simplexml'));
- }
- /**
- * test build with a single empty tag
- *
- * @return void
- */
- public function testBuildEmptyTag() {
- try {
- Xml::build('<tag>');
- $this->fail('No exception');
- } catch (\Exception $e) {
- $this->assertTrue(true, 'An exception was raised');
- }
- }
- /**
- * testFromArray method
- *
- * @return void
- */
- public function testFromArray() {
- $xml = array('tag' => 'value');
- $obj = Xml::fromArray($xml);
- $this->assertEquals('tag', $obj->getName());
- $this->assertEquals('value', (string)$obj);
- $xml = array('tag' => null);
- $obj = Xml::fromArray($xml);
- $this->assertEquals('tag', $obj->getName());
- $this->assertEquals('', (string)$obj);
- $xml = array('tag' => array('@' => 'value'));
- $obj = Xml::fromArray($xml);
- $this->assertEquals('tag', $obj->getName());
- $this->assertEquals('value', (string)$obj);
- $xml = array(
- 'tags' => array(
- 'tag' => array(
- array(
- 'id' => '1',
- 'name' => 'defect'
- ),
- array(
- 'id' => '2',
- 'name' => 'enhancement'
- )
- )
- )
- );
- $obj = Xml::fromArray($xml, 'attributes');
- $this->assertTrue($obj instanceof \SimpleXMLElement);
- $this->assertEquals('tags', $obj->getName());
- $this->assertEquals(2, count($obj));
- $xmlText = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <tags>
- <tag id="1" name="defect"/>
- <tag id="2" name="enhancement"/>
- </tags>
- XML;
- $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
- $obj = Xml::fromArray($xml);
- $this->assertTrue($obj instanceof \SimpleXMLElement);
- $this->assertEquals('tags', $obj->getName());
- $this->assertEquals(2, count($obj));
- $xmlText = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <tags>
- <tag>
- <id>1</id>
- <name>defect</name>
- </tag>
- <tag>
- <id>2</id>
- <name>enhancement</name>
- </tag>
- </tags>
- XML;
- $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
- $xml = array(
- 'tags' => array(
- )
- );
- $obj = Xml::fromArray($xml);
- $this->assertEquals('tags', $obj->getName());
- $this->assertEquals('', (string)$obj);
- $xml = array(
- 'tags' => array(
- 'bool' => true,
- 'int' => 1,
- 'float' => 10.2,
- 'string' => 'ok',
- 'null' => null,
- 'array' => array()
- )
- );
- $obj = Xml::fromArray($xml, 'tags');
- $this->assertEquals(6, count($obj));
- $this->assertSame((string)$obj->bool, '1');
- $this->assertSame((string)$obj->int, '1');
- $this->assertSame((string)$obj->float, '10.2');
- $this->assertSame((string)$obj->string, 'ok');
- $this->assertSame((string)$obj->null, '');
- $this->assertSame((string)$obj->array, '');
- $xml = array(
- 'tags' => array(
- 'tag' => array(
- array(
- '@id' => '1',
- 'name' => 'defect'
- ),
- array(
- '@id' => '2',
- 'name' => 'enhancement'
- )
- )
- )
- );
- $obj = Xml::fromArray($xml, 'tags');
- $xmlText = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <tags>
- <tag id="1">
- <name>defect</name>
- </tag>
- <tag id="2">
- <name>enhancement</name>
- </tag>
- </tags>
- XML;
- $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
- $xml = array(
- 'tags' => array(
- 'tag' => array(
- array(
- '@id' => '1',
- 'name' => 'defect',
- '@' => 'Tag 1'
- ),
- array(
- '@id' => '2',
- 'name' => 'enhancement'
- ),
- ),
- '@' => 'All tags'
- )
- );
- $obj = Xml::fromArray($xml, 'tags');
- $xmlText = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <tags>All tags<tag id="1">Tag 1<name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>
- XML;
- $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
- $xml = array(
- 'tags' => array(
- 'tag' => array(
- 'id' => 1,
- '@' => 'defect'
- )
- )
- );
- $obj = Xml::fromArray($xml, 'attributes');
- $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1">defect</tag></tags>';
- $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
- $xml = array(
- 'tag' => array(
- '@' => 0,
- '@test' => 'A test'
- )
- );
- $obj = Xml::fromArray($xml);
- $xmlText = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <tag test="A test">0</tag>
- XML;
- $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
- }
- /**
- * Test non-sequential keys in list types.
- *
- * @return void
- */
- public function testFromArrayNonSequentialKeys() {
- $xmlArray = array(
- 'Event' => array(
- array(
- 'id' => '235',
- 'Attribute' => array(
- 0 => array(
- 'id' => '9646',
- ),
- 2 => array(
- 'id' => '9647',
- )
- )
- )
- )
- );
- $obj = Xml::fromArray($xmlArray);
- $expected = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <Event>
- <id>235</id>
- <Attribute>
- <id>9646</id>
- </Attribute>
- <Attribute>
- <id>9647</id>
- </Attribute>
- </Event>
- XML;
- $this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
- }
- /**
- * testFromArrayPretty method
- *
- * @return void
- */
- public function testFromArrayPretty() {
- $xml = array(
- 'tags' => array(
- 'tag' => array(
- array(
- 'id' => '1',
- 'name' => 'defect'
- ),
- array(
- 'id' => '2',
- 'name' => 'enhancement'
- )
- )
- )
- );
- $expected = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <tags><tag><id>1</id><name>defect</name></tag><tag><id>2</id><name>enhancement</name></tag></tags>
- XML;
- $xmlResponse = Xml::fromArray($xml, array('pretty' => false));
- $this->assertTextEquals($expected, $xmlResponse->asXML());
- $expected = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <tags>
- <tag>
- <id>1</id>
- <name>defect</name>
- </tag>
- <tag>
- <id>2</id>
- <name>enhancement</name>
- </tag>
- </tags>
- XML;
- $xmlResponse = Xml::fromArray($xml, array('pretty' => true));
- $this->assertTextEquals($expected, $xmlResponse->asXML());
- $xml = array(
- 'tags' => array(
- 'tag' => array(
- array(
- 'id' => '1',
- 'name' => 'defect'
- ),
- array(
- 'id' => '2',
- 'name' => 'enhancement'
- )
- )
- )
- );
- $expected = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <tags><tag id="1" name="defect"/><tag id="2" name="enhancement"/></tags>
- XML;
- $xmlResponse = Xml::fromArray($xml, array('pretty' => false, 'format' => 'attributes'));
- $this->assertTextEquals($expected, $xmlResponse->asXML());
- $expected = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <tags>
- <tag id="1" name="defect"/>
- <tag id="2" name="enhancement"/>
- </tags>
- XML;
- $xmlResponse = Xml::fromArray($xml, array('pretty' => true, 'format' => 'attributes'));
- $this->assertTextEquals($expected, $xmlResponse->asXML());
- }
- /**
- * data provider for fromArray() failures
- *
- * @return array
- */
- public static function invalidArrayDataProvider() {
- return array(
- array(''),
- array(null),
- array(false),
- array(array()),
- array(array('numeric key as root')),
- array(array('item1' => '', 'item2' => '')),
- array(array('items' => array('item1', 'item2'))),
- array(array(
- 'tags' => array(
- 'tag' => array(
- array(
- array(
- 'string'
- )
- )
- )
- )
- )),
- array(array(
- 'tags' => array(
- '@tag' => array(
- array(
- '@id' => '1',
- 'name' => 'defect'
- ),
- array(
- '@id' => '2',
- 'name' => 'enhancement'
- )
- )
- )
- )),
- array(new \DateTime())
- );
- }
- /**
- * testFromArrayFail method
- *
- * @dataProvider invalidArrayDataProvider
- * @return void
- */
- public function testFromArrayFail($value) {
- try {
- Xml::fromArray($value);
- $this->fail('No exception.');
- } catch (\Exception $e) {
- $this->assertTrue(true, 'Caught exception.');
- }
- }
- /**
- * Test that there are not unterminated errors when building xml
- *
- * @return void
- */
- public function testFromArrayUnterminatedError() {
- $data = array(
- 'product_ID' => 'GENERT-DL',
- 'deeplink' => 'http://example.com/deep',
- 'image_URL' => 'http://example.com/image',
- 'thumbnail_image_URL' => 'http://example.com/thumb',
- 'brand' => 'Malte Lange & Co',
- 'availability' => 'in stock',
- 'authors' => array(
- 'author' => array('Malte Lange & Co')
- )
- );
- $xml = Xml::fromArray(array('products' => $data), 'tags');
- $expected = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <products>
- <product_ID>GENERT-DL</product_ID>
- <deeplink>http://example.com/deep</deeplink>
- <image_URL>http://example.com/image</image_URL>
- <thumbnail_image_URL>http://example.com/thumb</thumbnail_image_URL>
- <brand>Malte Lange & Co</brand>
- <availability>in stock</availability>
- <authors>
- <author>Malte Lange & Co</author>
- </authors>
- </products>
- XML;
- $this->assertXmlStringEqualsXmlString($expected, $xml->asXML());
- }
- /**
- * testToArray method
- *
- * @return void
- */
- public function testToArray() {
- $xml = '<tag>name</tag>';
- $obj = Xml::build($xml);
- $this->assertEquals(array('tag' => 'name'), Xml::toArray($obj));
- $xml = CORE_TESTS . 'Fixture/sample.xml';
- $obj = Xml::build($xml);
- $expected = array(
- 'tags' => array(
- 'tag' => array(
- array(
- '@id' => '1',
- 'name' => 'defect'
- ),
- array(
- '@id' => '2',
- 'name' => 'enhancement'
- )
- )
- )
- );
- $this->assertEquals($expected, Xml::toArray($obj));
- $array = array(
- 'tags' => array(
- 'tag' => array(
- array(
- 'id' => '1',
- 'name' => 'defect'
- ),
- array(
- 'id' => '2',
- 'name' => 'enhancement'
- )
- )
- )
- );
- $this->assertEquals(Xml::toArray(Xml::fromArray($array, 'tags')), $array);
- $expected = array(
- 'tags' => array(
- 'tag' => array(
- array(
- '@id' => '1',
- '@name' => 'defect'
- ),
- array(
- '@id' => '2',
- '@name' => 'enhancement'
- )
- )
- )
- );
- $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
- $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument', 'format' => 'attributes'))));
- $this->assertEquals(Xml::toArray(Xml::fromArray($array)), $array);
- $this->assertEquals(Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument'))), $array);
- $array = array(
- 'tags' => array(
- 'tag' => array(
- 'id' => '1',
- 'posts' => array(
- array('id' => '1'),
- array('id' => '2')
- )
- ),
- 'tagOther' => array(
- 'subtag' => array(
- 'id' => '1'
- )
- )
- )
- );
- $expected = array(
- 'tags' => array(
- 'tag' => array(
- '@id' => '1',
- 'posts' => array(
- array('@id' => '1'),
- array('@id' => '2')
- )
- ),
- 'tagOther' => array(
- 'subtag' => array(
- '@id' => '1'
- )
- )
- )
- );
- $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, 'attributes')));
- $this->assertEquals($expected, Xml::toArray(Xml::fromArray($array, array('format' => 'attributes', 'return' => 'domdocument'))));
- $xml = <<<XML
- <root>
- <tag id="1">defect</tag>
- </root>
- XML;
- $obj = Xml::build($xml);
- $expected = array(
- 'root' => array(
- 'tag' => array(
- '@id' => 1,
- '@' => 'defect'
- )
- )
- );
- $this->assertEquals($expected, Xml::toArray($obj));
- $xml = <<<XML
- <root>
- <table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>
- <table xmlns="http://www.cakephp.org"><name>CakePHP</name><license>MIT</license></table>
- <table>The book is on the table.</table>
- </root>
- XML;
- $obj = Xml::build($xml);
- $expected = array(
- 'root' => array(
- 'table' => array(
- array('tr' => array('td' => array('Apples', 'Bananas'))),
- array('name' => 'CakePHP', 'license' => 'MIT'),
- 'The book is on the table.'
- )
- )
- );
- $this->assertEquals($expected, Xml::toArray($obj));
- $xml = <<<XML
- <root xmlns:cake="http://www.cakephp.org/">
- <tag>defect</tag>
- <cake:bug>1</cake:bug>
- </root>
- XML;
- $obj = Xml::build($xml);
- $expected = array(
- 'root' => array(
- 'tag' => 'defect',
- 'cake:bug' => 1
- )
- );
- $this->assertEquals($expected, Xml::toArray($obj));
- $xml = '<tag type="myType">0</tag>';
- $obj = Xml::build($xml);
- $expected = array(
- 'tag' => array(
- '@type' => 'myType',
- '@' => 0
- )
- );
- $this->assertEquals($expected, Xml::toArray($obj));
- }
- /**
- * testRss
- *
- * @return void
- */
- public function testRss() {
- $rss = file_get_contents(CORE_TESTS . 'Fixture/rss.xml');
- $rssAsArray = Xml::toArray(Xml::build($rss));
- $this->assertEquals('2.0', $rssAsArray['rss']['@version']);
- $this->assertEquals(2, count($rssAsArray['rss']['channel']['item']));
- $atomLink = array('@href' => 'http://bakery.cakephp.org/articles/rss', '@rel' => 'self', '@type' => 'application/rss+xml');
- $this->assertEquals($rssAsArray['rss']['channel']['atom:link'], $atomLink);
- $this->assertEquals('http://bakery.cakephp.org/', $rssAsArray['rss']['channel']['link']);
- $expected = array(
- 'title' => 'Alertpay automated sales via IPN',
- 'link' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn',
- 'description' => 'I\'m going to show you how I implemented a payment module via the Alertpay payment processor.',
- 'pubDate' => 'Tue, 31 Aug 2010 01:42:00 -0500',
- 'guid' => 'http://bakery.cakephp.org/articles/view/alertpay-automated-sales-via-ipn'
- );
- $this->assertSame($expected, $rssAsArray['rss']['channel']['item'][1]);
- $rss = array(
- 'rss' => array(
- 'xmlns:atom' => 'http://www.w3.org/2005/Atom',
- '@version' => '2.0',
- 'channel' => array(
- 'atom:link' => array(
- '@href' => 'http://bakery.cakephp.org/articles/rss',
- '@rel' => 'self',
- '@type' => 'application/rss+xml'
- ),
- 'title' => 'The Bakery: ',
- 'link' => 'http://bakery.cakephp.org/',
- 'description' => 'Recent Articles at The Bakery.',
- 'pubDate' => 'Sun, 12 Sep 2010 04:18:26 -0500',
- 'item' => array(
- array(
- 'title' => 'CakePHP 1.3.4 released',
- 'link' => 'http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released'
- ),
- array(
- 'title' => 'Wizard Component 1.2 Tutorial',
- 'link' => 'http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial'
- )
- )
- )
- )
- );
- $rssAsSimpleXML = Xml::fromArray($rss);
- $xmlText = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
- <channel>
- <atom:link href="http://bakery.cakephp.org/articles/rss" rel="self" type="application/rss+xml"/>
- <title>The Bakery: </title>
- <link>http://bakery.cakephp.org/</link>
- <description>Recent Articles at The Bakery.</description>
- <pubDate>Sun, 12 Sep 2010 04:18:26 -0500</pubDate>
- <item>
- <title>CakePHP 1.3.4 released</title>
- <link>http://bakery.cakephp.org/articles/view/cakephp-1-3-4-released</link>
- </item>
- <item>
- <title>Wizard Component 1.2 Tutorial</title>
- <link>http://bakery.cakephp.org/articles/view/wizard-component-1-2-tutorial</link>
- </item>
- </channel>
- </rss>
- XML;
- $this->assertXmlStringEqualsXmlString($xmlText, $rssAsSimpleXML->asXML());
- }
- /**
- * testXmlRpc
- *
- * @return void
- */
- public function testXmlRpc() {
- $xml = Xml::build('<methodCall><methodName>test</methodName><params /></methodCall>');
- $expected = array(
- 'methodCall' => array(
- 'methodName' => 'test',
- 'params' => ''
- )
- );
- $this->assertSame($expected, Xml::toArray($xml));
- $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>');
- $expected = array(
- 'methodCall' => array(
- 'methodName' => 'test',
- 'params' => array(
- 'param' => array(
- 'value' => array(
- 'array' => array(
- 'data' => array(
- 'value' => array(
- array('int' => '12'),
- array('string' => 'Egypt'),
- array('boolean' => '0'),
- array('int' => '-31')
- )
- )
- )
- )
- )
- )
- )
- );
- $this->assertSame($expected, Xml::toArray($xml));
- $xmlText = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <methodResponse>
- <params>
- <param>
- <value>
- <array>
- <data>
- <value>
- <int>1</int>
- </value>
- <value>
- <string>testing</string>
- </value>
- </data>
- </array>
- </value>
- </param>
- </params>
- </methodResponse>
- XML;
- $xml = Xml::build($xmlText);
- $expected = array(
- 'methodResponse' => array(
- 'params' => array(
- 'param' => array(
- 'value' => array(
- 'array' => array(
- 'data' => array(
- 'value' => array(
- array('int' => '1'),
- array('string' => 'testing')
- )
- )
- )
- )
- )
- )
- )
- );
- $this->assertSame($expected, Xml::toArray($xml));
- $xml = Xml::fromArray($expected, 'tags');
- $this->assertXmlStringEqualsXmlString($xmlText, $xml->asXML());
- }
- /**
- * testSoap
- *
- * @return void
- */
- public function testSoap() {
- $xmlRequest = Xml::build(CORE_TESTS . 'Fixture/soap_request.xml');
- $expected = array(
- 'Envelope' => array(
- '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
- 'soap:Body' => array(
- 'm:GetStockPrice' => array(
- 'm:StockName' => 'IBM'
- )
- )
- )
- );
- $this->assertEquals($expected, Xml::toArray($xmlRequest));
- $xmlResponse = Xml::build(CORE_TESTS . DS . 'Fixture/soap_response.xml');
- $expected = array(
- 'Envelope' => array(
- '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
- 'soap:Body' => array(
- 'm:GetStockPriceResponse' => array(
- 'm:Price' => '34.5'
- )
- )
- )
- );
- $this->assertEquals($expected, Xml::toArray($xmlResponse));
- $xml = array(
- 'soap:Envelope' => array(
- 'xmlns:soap' => 'http://www.w3.org/2001/12/soap-envelope',
- '@soap:encodingStyle' => 'http://www.w3.org/2001/12/soap-encoding',
- 'soap:Body' => array(
- 'xmlns:m' => 'http://www.example.org/stock',
- 'm:GetStockPrice' => array(
- 'm:StockName' => 'IBM'
- )
- )
- )
- );
- $xmlRequest = Xml::fromArray($xml, array('encoding' => null));
- $xmlText = <<<XML
- <?xml version="1.0"?>
- <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
- <soap:Body xmlns:m="http://www.example.org/stock">
- <m:GetStockPrice><m:StockName>IBM</m:StockName></m:GetStockPrice>
- </soap:Body>
- </soap:Envelope>
- XML;
- $this->assertXmlStringEqualsXmlString($xmlText, $xmlRequest->asXML());
- }
- /**
- * testNamespace
- *
- * @return void
- */
- public function testNamespace() {
- $xml = <<<XML
- <root xmlns:ns="http://cakephp.org">
- <ns:tag id="1">
- <child>good</child>
- <otherchild>bad</otherchild>
- </ns:tag>
- <tag>Tag without ns</tag>
- </root>
- XML;
- $xmlResponse = Xml::build($xml);
- $expected = array(
- 'root' => array(
- 'ns:tag' => array(
- '@id' => '1',
- 'child' => 'good',
- 'otherchild' => 'bad'
- ),
- 'tag' => 'Tag without ns'
- )
- );
- $this->assertEquals($expected, Xml::toArray($xmlResponse));
- $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:tag id="1" /><tag><id>1</id></tag></root>');
- $expected = array(
- 'root' => array(
- 'ns:tag' => array(
- '@id' => '1'
- ),
- 'tag' => array(
- 'id' => '1'
- )
- )
- );
- $this->assertEquals($expected, Xml::toArray($xmlResponse));
- $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:attr>1</ns:attr></root>');
- $expected = array(
- 'root' => array(
- 'ns:attr' => '1'
- )
- );
- $this->assertEquals($expected, Xml::toArray($xmlResponse));
- $xmlResponse = Xml::build('<root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>');
- $this->assertEquals($expected, Xml::toArray($xmlResponse));
- $xml = array(
- 'root' => array(
- 'ns:attr' => array(
- 'xmlns:ns' => 'http://cakephp.org',
- '@' => 1
- )
- )
- );
- $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>';
- $xmlResponse = Xml::fromArray($xml);
- $this->assertEquals($expected, str_replace(array("\r", "\n"), '', $xmlResponse->asXML()));
- $xml = array(
- 'root' => array(
- 'tag' => array(
- 'xmlns:pref' => 'http://cakephp.org',
- 'pref:item' => array(
- 'item 1',
- 'item 2'
- )
- )
- )
- );
- $expected = <<<XML
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <tag xmlns:pref="http://cakephp.org">
- <pref:item>item 1</pref:item>
- <pref:item>item 2</pref:item>
- </tag>
- </root>
- XML;
- $xmlResponse = Xml::fromArray($xml);
- $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
- $xml = array(
- 'root' => array(
- 'tag' => array(
- 'xmlns:' => 'http://cakephp.org'
- )
- )
- );
- $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns="http://cakephp.org"/></root>';
- $xmlResponse = Xml::fromArray($xml);
- $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
- $xml = array(
- 'root' => array(
- 'xmlns:' => 'http://cakephp.org'
- )
- );
- $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns="http://cakephp.org"/>';
- $xmlResponse = Xml::fromArray($xml);
- $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
- $xml = array(
- 'root' => array(
- 'xmlns:ns' => 'http://cakephp.org'
- )
- );
- $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns:ns="http://cakephp.org"/>';
- $xmlResponse = Xml::fromArray($xml);
- $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
- }
- /**
- * test that CDATA blocks don't get screwed up by SimpleXml
- *
- * @return void
- */
- public function testCdata() {
- $xml = '<' . '?xml version="1.0" encoding="UTF-8"?>' .
- '<people><name><![CDATA[ Mark ]]></name></people>';
- $result = Xml::build($xml);
- $this->assertEquals(' Mark ', (string)$result->name);
- }
- /**
- * data provider for toArray() failures
- *
- * @return array
- */
- public static function invalidToArrayDataProvider() {
- return array(
- array(new \DateTime()),
- array(array())
- );
- }
- /**
- * testToArrayFail method
- *
- * @dataProvider invalidToArrayDataProvider
- * @expectedException \Cake\Utility\Error\XmlException
- * @return void
- */
- public function testToArrayFail($value) {
- Xml::toArray($value);
- }
- /**
- * testWithModel method
- *
- * @return void
- */
- public function testWithModel() {
- $this->markTestIncomplete('Models do not work right now');
- $this->loadFixtures('User', 'Article');
- $user = new XmlUser();
- $data = $user->read(null, 1);
- $obj = Xml::build(compact('data'));
- $expected = <<<XML
- <?xml version="1.0" encoding="UTF-8"?><data>
- <User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
- <created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>
- <Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>
- <published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>
- <Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>
- <published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>
- </data>
- XML;
- $this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
- //multiple model results - without a records key it would fatal error
- $data = $user->find('all', array('limit' => 2));
- $data = array('records' => $data);
- $obj = Xml::build(compact('data'));
- $expected = <<<XML
- <?xml version="1.0" encoding="UTF-8"?><data>
- <records>
- <User><id>1</id><user>mariano</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
- <created>2007-03-17 01:16:23</created><updated>2007-03-17 01:18:31</updated></User>
- <Article><id>1</id><user_id>1</user_id><title>First Article</title><body>First Article Body</body>
- <published>Y</published><created>2007-03-18 10:39:23</created><updated>2007-03-18 10:41:31</updated></Article>
- <Article><id>3</id><user_id>1</user_id><title>Third Article</title><body>Third Article Body</body>
- <published>Y</published><created>2007-03-18 10:43:23</created><updated>2007-03-18 10:45:31</updated></Article>
- </records><records><User><id>2</id><user>nate</user><password>5f4dcc3b5aa765d61d8327deb882cf99</password>
- <created>2007-03-17 01:18:23</created><updated>2007-03-17 01:20:31</updated></User><Article/>
- </records>
- </data>
- XML;
- $obj->asXML();
- $this->assertXmlStringEqualsXmlString($expected, $obj->asXML());
- }
- /**
- * Test ampersand in text elements.
- *
- * @return void
- */
- public function testAmpInText() {
- $data = array(
- 'outer' => array(
- 'inner' => array('name' => 'mark & mark')
- )
- );
- $obj = Xml::build($data);
- $result = $obj->asXml();
- $this->assertContains('mark & mark', $result);
- }
- /**
- * Test that entity loading is disabled by default.
- *
- * @return void
- */
- public function testNoEntityLoading() {
- $file = str_replace(' ', '%20', CAKE . 'VERSION.txt');
- $xml = <<<XML
- <!DOCTYPE cakephp [
- <!ENTITY payload SYSTEM "file://$file" >]>
- <request>
- <xxe>&payload;</xxe>
- </request>
- XML;
- try {
- $result = Xml::build($xml);
- $this->assertEquals('', (string)$result->xxe);
- } catch (Exception $e) {
- $this->assertTrue(true, 'A warning was raised meaning external entities were not loaded');
- }
- }
- }
|