* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs
* @since CakePHP(tm) v 1.2.0.5432
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', 'Xml');
/**
* XmlTest class
*
* @package cake
* @subpackage cake.tests.cases.libs
*/
class XmlTest extends CakeTestCase {
/**
* setUp method
*
* @access public
* @return void
*/
function setUp() {
$manager =& new XmlManager();
$manager->namespaces = array();
}
/**
* testRootTagParsing method
*
* @access public
* @return void
*/
function testRootTagParsing() {
$input = '<' . '?xml version="1.0" encoding="UTF-8" ?' . '>' . "\n" .
''
.' '
.' '
.' ';
$xml = new Xml($input);
$this->assertEqual($xml->children[0]->name, 'plugin');
$this->assertEqual($xml->children[0]->children[0]->name, 'current');
$this->assertEqual($xml->toString(true), $input);
}
/**
* testSerialization method
*
* @access public
* @return void
*/
function testSerialization() {
$input = array(
array(
'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial')
),
array(
'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
'Industry' => array('id' => 2, 'name' => 'Education')
)
);
$xml = new Xml($input);
$result = preg_replace("/\n/",'', $xml->toString(false));
$expected = ' ';
$this->assertEqual($result, $expected);
$input = array(
'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial')
);
$expected = ' ';
$xml = new Xml($input);
$result = preg_replace("/\n/",'', $xml->toString(false));
$this->assertEqual($result, $expected);
}
/**
* testSerializeOnMultiDimensionalArray method
*
* @access public
* @return void
*/
function testSerializeOnMultiDimensionalArray() {
$data = array(
'Statuses' => array(
array('Status' => array('id' => 1)),
array('Status' => array('id' => 2))
)
);
$result =& new Xml($data, array('format' => 'tags'));
$expected = '1 2 ';
$this->assertIdentical($result->toString(), $expected);
}
/**
* test serialization of boolean and null values. false = 0, true = 1, null = ''
*
* @return void
**/
function testSerializationOfBooleanAndBooleanishValues() {
$xml =& new Xml(array('data' => array('example' => false)));
$result = $xml->toString(false);
$expected = ' ';
$this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s');
$xml =& new Xml(array('data' => array('example' => true)));
$result = $xml->toString(false);
$expected = ' ';
$this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s');
$xml =& new Xml(array('data' => array('example' => null)));
$result = $xml->toString(false);
$expected = ' ';
$this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s');
$xml =& new Xml(array('data' => array('example' => 0)));
$result = $xml->toString(false);
$expected = ' ';
$this->assertEqual($result, $expected, 'Boolean-ish values incorrectly handled. %s');
$xml =& new Xml(array('data' => array('example' => 1)));
$result = $xml->toString(false);
$expected = ' ';
$this->assertEqual($result, $expected, 'Boolean-ish values incorrectly handled. %s');
}
/**
* testSimpleArray method
*
* @access public
* @return void
*/
function testSimpleArray() {
$xml = new Xml(array('hello' => 'world'), array('format' => 'tags'));
$result = $xml->toString(false);
$expected = ' ';
$this->assertEqual($expected, $result);
}
/**
* testSimpleObject method
*
* @access public
* @return void
*/
function testSimpleObject() {
$input = new StdClass();
$input->hello = 'world';
$xml = new Xml($input, array('format' => 'tags'));
$result = $xml->toString(false);
$expected = ' ';
$this->assertEqual($expected, $result);
}
/**
* testHeader method
*
* @access public
* @return void
*/
function testHeader() {
$input = new stdClass();
$input->hello = 'world';
$xml = new Xml($input, array('format' => 'tags'));
$result = $xml->toString(array('header' => true));
$expected = '<'.'?xml version="1.0" encoding="UTF-8" ?'.'>'."\n".' ';
$this->assertEqual($expected, $result);
}
/**
* testOwnerAssignment method
*
* @access public
* @return void
*/
function testOwnerAssignment() {
$xml = new Xml();
$node =& $xml->createElement('hello', 'world');
$owner =& $node->document();
$this->assertTrue($xml === $owner);
$children =& $node->children;
$childOwner =& $children[0]->document();
$this->assertTrue($xml === $childOwner);
}
/**
* testArraySingleSerialization method
*
* @access public
* @return void
*/
function testArraySingleSerialization() {
$input = array(
'Post' => array(
'id' => '1', 'author_id' => '1', 'title' => 'First Post',
'body' => 'First Post Body', 'published' => 'Y',
'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
),
'Author' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'
)
);
$expected = '1 1 ';
$expected .= ' ';
$expected .= ' ';
$expected .= ' 1 ';
$expected .= ' ';
$expected .= '';
$expected .= ' ';
$xml = new Xml($input, array('format' => 'tags'));
$result = $xml->toString(false);
$this->assertEqual($expected, $result);
}
/**
* testArraySerialization method
*
* @access public
* @return void
*/
function testArraySerialization() {
$input = array(
array(
'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial')
),
array(
'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
'Industry' => array('id' => 2, 'name' => 'Education'),
)
);
$expected = '1 1 1 0 1 1 1 Touch Screen Kiosk 1 Financial 2 2 1 0 2 2 2007-11-26 14:48:36 2 Awareness Campaign 2 Education ';
$xml = new Xml($input, array('format' => 'tags'));
$result = $xml->toString(array('header' => false, 'cdata' => false));
$this->assertEqual($expected, $result);
}
/**
* testNestedArraySerialization method
*
* @access public
* @return void
*/
function testNestedArraySerialization() {
$input = array(
array(
'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial'),
'BusinessSolution' => array(array('id' => 6, 'name' => 'Convert Sales')),
'MediaType' => array(
array('id' => 15, 'name' => 'Print'),
array('id' => 7, 'name' => 'Web Demo'),
array('id' => 6, 'name' => 'CD-ROM')
)
),
array(
'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
'Industry' => array('id' => 2, 'name' => 'Education'),
'BusinessSolution' => array(
array('id' => 4, 'name' => 'Build Relationship'),
array('id' => 6, 'name' => 'Convert Sales')
),
'MediaType' => array(
array('id' => 17, 'name' => 'Web'),
array('id' => 6, 'name' => 'CD-ROM')
)
)
);
$expected = '1 1 1 0 1 1 1 Touch Screen Kiosk 1 Financial 6 Convert Sales 15 Print 7 Web Demo 6 CD-ROM 2 2 1 0 2 2 2007-11-26 14:48:36 2 Awareness Campaign 2 Education 4 Build Relationship 6 Convert Sales 17 Web 6 CD-ROM ';
$xml = new Xml($input, array('format' => 'tags'));
$result = $xml->toString(array('header' => false, 'cdata' => false));
$this->assertEqual($expected, $result);
}
/**
* Prove that serialization with a given root node works
* as expected.
*
* @access public
* @return void
* @link https://trac.cakephp.org/ticket/6294
*/
function testArraySerializationWithRoot() {
$input = array(
array('Shirt' => array('id' => 1, 'color' => 'green')),
array('Shirt' => array('id' => 2, 'color' => 'blue')),
);
$expected = ' ';
$expected .= ' ';
$Xml = new Xml($input, array('root' => 'collection'));
$result = $Xml->toString(array('header' => false));
$this->assertEqual($expected, $result);
}
/**
* testCloneNode
*
* @access public
* @return void
*/
function testCloneNode() {
$node =& new XmlNode('element', 'myValue');
$twin =& $node->cloneNode();
$this->assertEqual($node, $twin);
}
/**
* testNextSibling
*
* @access public
* @return void
*/
function testNextSibling() {
$input = array(
array(
'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '1.89', 'industry_id' => '1.56', 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial')
),
array(
'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '2.2', 'industry_id' => 2.2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
'Industry' => array('id' => 2, 'name' => 'Education'),
)
);
$xml =& new Xml($input, array('format' => 'tags'));
$node =& $xml->children[0]->children[0];
$nextSibling =& $node->nextSibling();
$this->assertEqual($nextSibling, $xml->children[0]->children[1]);
$nextSibling2 =& $nextSibling->nextSibling();
$this->assertEqual($nextSibling2, $xml->children[0]->children[2]);
$noFriends =& $xml->children[0]->children[12];
$this->assertNull($noFriends->nextSibling());
}
/**
* testPreviousSibling
*
* @access public
* @return void
*/
function testPreviousSibling() {
$input = array(
array(
'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '1.89', 'industry_id' => '1.56', 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial')
),
array(
'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '2.2', 'industry_id' => 2.2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
'Industry' => array('id' => 2, 'name' => 'Education'),
)
);
$xml =& new Xml($input, array('format' => 'tags'));
$node =& $xml->children[0]->children[1];
$prevSibling =& $node->previousSibling();
$this->assertEqual($prevSibling, $xml->children[0]->children[0]);
$this->assertNull($prevSibling->previousSibling());
}
/**
* testAddAndRemoveAttributes
*
* @access public
* @return void
*/
function testAddAndRemoveAttributes() {
$node =& new XmlElement('myElement', 'superValue');
$this->assertTrue(empty($node->attributes));
$attrs = array(
'id' => 'test',
'show' => 1,
'is_spotlight' => 1,
);
$node->addAttribute($attrs);
$this->assertEqual($node->attributes, $attrs);
$node =& new XmlElement('myElement', 'superValue');
$node->addAttribute('test', 'value');
$this->assertTrue(isset($node->attributes['test']));
$node =& new XmlElement('myElement', 'superValue');
$obj =& new StdClass();
$obj->class = 'info';
$obj->id = 'primaryInfoBox';
$node->addAttribute($obj);
$expected = array(
'class' => 'info',
'id' => 'primaryInfoBox',
);
$this->assertEqual($node->attributes, $expected);
$result = $node->removeAttribute('class');
$this->assertTrue($result);
$this->assertFalse(isset($node->attributes['class']));
$result = $node->removeAttribute('missing');
$this->assertFalse($result);
}
/**
* Tests that XML documents with non-standard spacing (i.e. leading whitespace, whole document
* on one line) still parse properly.
*
* @return void
*/
function testParsingWithNonStandardWhitespace() {
$raw = '1.0 ';
$array = array('Prices' => array('price' => 1.0));
$xml = new Xml($raw);
$this->assertEqual($xml->toArray(), $array);
$this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"');
$xml = new Xml(' ' . $raw);
$this->assertEqual($xml->toArray(), $array);
$this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"');
$xml = new Xml("\n" . $raw);
$this->assertEqual($xml->toArray(), $array);
$this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"');
}
/* Not implemented yet */
/* function testChildFilter() {
$input = array(
array(
'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial'),
'BusinessSolution' => array(array('id' => 6, 'name' => 'Convert Sales')),
'MediaType' => array(
array('id' => 15, 'name' => 'Print'),
array('id' => 7, 'name' => 'Web Demo'),
array('id' => 6, 'name' => 'CD-ROM')
)
),
array(
'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
'Industry' => array('id' => 2, 'name' => 'Education'),
'BusinessSolution' => array(
array('id' => 4, 'name' => 'Build Relationship'),
array('id' => 6, 'name' => 'Convert Sales')
),
'MediaType' => array(
array('id' => 17, 'name' => 'Web'),
array('id' => 6, 'name' => 'CD-ROM')
)
)
);
$xml = new Xml($input, array('format' => 'tags', 'tags' => array(
'MediaType' => array('value' => 'id', 'children' => false),
'JobType' => array('children' => array()),
'Industry' => array('children' => array('name')),
'show' => false
)));
$result = $xml->toString(array('header' => false, 'cdata' => false));
$expected = '1 1 0 1 1 1 Touch Screen Kiosk Financial 6 Convert Sales 15 7 6 2 2 0 2 2 2007-11-26 14:48:36 2 Awareness Campaign Education 4 Build Relationship 6 Convert Sales 17 6 ';
$this->assertEqual($expected, $result);
} */
/* Broken due to a Set class issue */
/* function testMixedArray() {
$input = array('OptionGroup' => array(
array('name' => 'OptA', 'id' => 12, 'OptA 1', 'OptA 2', 'OptA 3', 'OptA 4', 'OptA 5', 'OptA 6'),
array('name' => 'OptB', 'id' => 12, 'OptB 1', 'OptB 2', 'OptB 3', 'OptB 4', 'OptB 5', 'OptB 6')
));
$expected = 'OptA 12 OptA 1 OptA 2 OptA 3 OptA 4 OptA 5 OptA 6 OptB 12 OptB 1 OptB 2 OptB 3 OptB 4 OptB 5 OptB 6 ';
$xml = new Xml($input, array('format' => 'tags'));
$result = $xml->toString(array('header' => false, 'cdata' => false));
$this->assertEqual($expected, $result);
} */
/* function testMixedNestedArray() {
$input = array(
'OptionA' => array(
'name' => 'OptA',
'id' => 12,
'opt' => array('OptA 1', 'OptA 2', 'OptA 3', 'OptA 4', 'OptA 5', 'OptA 6')
),
'OptionB' => array(
'name' => 'OptB',
'id' => 12,
'opt' => array('OptB 1', 'OptB 2', 'OptB 3', 'OptB 4', 'OptB 5', 'OptB 6')
)
);
$expected = 'OptA 12 OptA 1 OptA 2 OptA 3 OptA 4 OptA 5 OptA 6 OptB 12 OptB 1 OptB 2 OptB 3 OptB 4 OptB 5 OptB 6 ';
$xml = new Xml($input, array('format' => 'tags'));
$result = $xml->toString(array('header' => false, 'cdata' => false));
$this->assertEqual($expected, $result);
} */
/* function testMixedArrayAttributes() {
$input = array('OptionGroup' => array(
array(
'name' => 'OptA',
'id' => 12,
array('opt' => 'OptA 1'),
array('opt' => 'OptA 2'),
array('opt' => 'OptA 3'),
array('opt' => 'OptA 4'),
array('opt' => 'OptA 5'),
array('opt' => 'OptA 6')
),
array(
'name' => 'OptB',
'id' => 12,
array('opt' => 'OptB 1'),
array('opt' => 'OptB 2'),
array('opt' => 'OptB 3'),
array('opt' => 'OptB 4'),
array('opt' => 'OptB 5'),
array('opt' => 'OptB 6')
)
));
$expected = 'OptA 1 OptA 2 OptA 3 OptA 4 OptA 5 OptA 6 OptB 1 OptB 2 OptB 3 OptB 4 OptB 5 OptB 6 ';
$options = array('tags' => array('option_group' => array('attributes' => array('id', 'name'))));
$xml = new Xml($input, $options);
$result = $xml->toString(false);
$this->assertEqual($expected, $result);
} */
/* Not implemented yet */
/* function testTagMap() {
$input = array(
array(
'Project' => array('id' => 1, 'title' => null, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial')
),
array(
'Project' => array('id' => 2, 'title' => null, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
'Industry' => array('id' => 2, 'name' => 'Education'),
)
);
$expected = '1 0 1 1 Touch Screen Kiosk Financial 1 0 2 2 2007-11-26 14:48:36 Awareness Campaign Education ';
$xml = new Xml($input, array('tags' => array(
'Project' => array('attributes' => array('id')),
'style' => array('attributes' => array('id')),
'JobType' => array('name' => 'jobtype', 'attributes' => array('id'), 'value' => 'name'),
'Industry' => array('attributes' => array('id'))
)));
$result = $xml->toString(array('header' => false, 'cdata' => false));
$this->assertEqual($expected, $result);
} */
/**
* testAllCData method
*
* @access public
* @return void
*/
function testAllCData() {
$input = array(
array(
'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '1.89', 'industry_id' => '1.56', 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial')
),
array(
'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => '1', 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => '2.2', 'industry_id' => 2.2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
'Industry' => array('id' => 2, 'name' => 'Education'),
)
);
$expected = '1 1 1 0 1.89 1.56 1 1 2 2 1 0 2.2 2.2 2 2 ';
$xml = new Xml($input, array('format' => 'tags'));
$result = $xml->toString(array('header' => false, 'cdata' => true));
$this->assertEqual($expected, $result);
}
/* PHP-native Unicode support pending */
/* function testConvertEntities() {
$input = array('project' => 'écît');
$xml = new Xml($input);
$result = $xml->toString(array('header' => false, 'cdata' => false, 'convertEntities' => true));
$expected = 'écît ';
$this->assertEqual($result, $expected);
} */
/**
* testWhitespace method
*
* @access public
* @return void
*/
function testWhitespace() {
$input = array(
array(
'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial')
),
array(
'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
'Industry' => array('id' => 2, 'name' => 'Education'),
)
);
$expected = "\n\t\n\t\t\n\t\t\t1\n\t\t \n\t\t \n\t\t\n\t\t\t1\n\t\t \n\t\t\n\t\t\t1\n\t\t \n\t\t \n\t\t\n\t\t\t0\n\t\t \n\t\t\n\t\t\t1\n\t\t \n\t\t\n\t\t\t1\n\t\t \n\t\t \n\t\t \n\t\t\n\t\t\n\t\t\t\n\t\t\t\t1\n\t\t\t \n\t\t\t\n\t\t\t\tTouch Screen Kiosk\n\t\t\t \n\t\t \n\t\t\n\t\t\t\n\t\t\t\t1\n\t\t\t \n\t\t\t\n\t\t\t\tFinancial\n\t\t\t \n\t\t \n\t \n\t\n\t\t\n\t\t\t2\n\t\t \n\t\t \n\t\t\n\t\t\t2\n\t\t \n\t\t\n\t\t\t1\n\t\t \n\t\t \n\t\t\n\t\t\t0\n\t\t \n\t\t\n\t\t\t2\n\t\t \n\t\t\n\t\t\t2\n\t\t \n\t\t\n\t\t\t2007-11-26 14:48:36\n\t\t \n\t\t \n\t\t\n\t\t\n\t\t\t\n\t\t\t\t2\n\t\t\t \n\t\t\t\n\t\t\t\tAwareness Campaign\n\t\t\t \n\t\t \n\t\t\n\t\t\t\n\t\t\t\t2\n\t\t\t \n\t\t\t\n\t\t\t\tEducation\n\t\t\t \n\t\t \n\t \n";
$xml = new Xml($input, array('format' => 'tags'));
$result = $xml->toString(array('header' => false, 'cdata' => false, 'whitespace' => true));
$this->assertEqual($expected, $result);
}
/**
* testSetSerialization method
*
* @access public
* @return void
*/
function testSetSerialization() {
$input = array(
array(
'Project' => array('id' => 1, 'title' => null, 'client_id' => 1, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 1, 'industry_id' => 1, 'modified' => null, 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 1, 'name' => 'Touch Screen Kiosk'),
'Industry' => array('id' => 1, 'name' => 'Financial')
),
array(
'Project' => array('id' => 2, 'title' => null, 'client_id' => 2, 'show' => 1, 'is_spotlight' => null, 'style_id' => 0, 'job_type_id' => 2, 'industry_id' => 2, 'modified' => '2007-11-26 14:48:36', 'created' => null),
'Style' => array('id' => null, 'name' => null),
'JobType' => array('id' => 2, 'name' => 'Awareness Campaign'),
'Industry' => array('id' => 2, 'name' => 'Education'),
)
);
$expected = '1 1 1 0 1 1 1 Touch Screen Kiosk 1 Financial 2 2 1 0 2 2 2007-11-26 14:48:36 2 Awareness Campaign 2 Education ';
$xml = new Xml(Set::map($input), array('format' => 'tags'));
$result = $xml->toString(array('header' => false, 'cdata' => false));
$this->assertEqual($expected, $result);
}
/**
* testSimpleParsing method
*
* @access public
* @return void
*/
function testSimpleParsing() {
$source = ' ';
$xml = new Xml($source);
$result = $xml->toString();
$this->assertEqual($source, $result);
}
/**
* test that elements with empty tag values do not collapse and corrupt data structures
*
* @access public
* @return void
**/
function testElementCollapsing() {
$xmlDataThatFails = '
';
$Xml = new Xml();
$Xml->load('' . $xmlDataThatFails);
$result = $Xml->toArray(false);
$this->assertTrue(is_array($result));
$expected = array(
'resultpackage' => array(
'result' => array(
0 => array(
'value' => '46b1c46ed3af9',
'qid' => '46b1c46ed6208'),
1 => array(
'qid' => '46b1c46ed332a'),
2 => array(
'value' => '46b1c46ed69d8',
'qid' => '46b1c46ed90e6'),
3 => array(
'value' => '46b1c46ed5a38',
'qid' => '46b1c46ed71a7'),
4 => array(
'value' => '46b1c46ed98b6',
'qid' => '46b1c46ed8146'),
5 => array(
'qid' => '46b1c46ed7978'),
6 => array(
'qid' => '46b1c46ed4a98'),
7 => array(
'qid' => '46b1c46ed42c8'),
8 => array(
'value' => '46b1c46ed8917',
'qid' => '46b1c46ed5268'),
)
));
$this->assertEqual(
count($result['resultpackage']['result']), count($expected['resultpackage']['result']),
'Incorrect array length %s');
$this->assertFalse(
isset($result['resultpackage']['result'][0][0]['qid']), 'Nested array exists, data is corrupt. %s');
$this->assertEqual($result, $expected);
}
/**
* testMixedParsing method
*
* @access public
* @return void
*/
function testMixedParsing() {
$source = ' ';
$xml = new Xml($source);
$result = $xml->toString();
$this->assertEqual($source, $result);
}
/**
* testComplexParsing method
*
* @access public
* @return void
*/
function testComplexParsing() {
$source = '1 1 1 0 1 1 1 Touch Screen Kiosk 1 Financial 2 2 1 0 2 2 2007-11-26 14:48:36 2 Awareness Campaign 2 Education ';
$xml = new Xml($source);
$result = $xml->toString(array('cdata' => false));
$this->assertEqual($source, $result);
}
/**
* testNamespaceParsing method
*
* @access public
* @return void
*/
function testNamespaceParsing() {
$source = 'value value ';
$xml = new Xml($source);
$result = $xml->toString(array('cdata' => false));
$this->assertEqual($source, $result);
$children = $xml->children('container');
$this->assertEqual($children[0]->namespace, 'a');
$children = $children[0]->children('rule');
$this->assertEqual($children[0]->namespace, 'b');
}
/**
* testNamespaces method
*
* @access public
* @return void
*/
function testNamespaces() {
$source = 'value value ';
$xml = new Xml($source);
$expects = 'value value ';
$_xml = XmlManager::getInstance();
$xml->addNamespace('f', 'http://example.com/f');
$result = $xml->toString(array('cdata' => false));
$this->assertEqual($expects, $result);
}
/**
* testEscapeCharSerialization method
*
* @access public
* @return void
*/
function testEscapeCharSerialization() {
$xml = new Xml(array('text' => 'JavaScript & DHTML'), array('attributes' => false, 'format' => 'attributes'));
$result = $xml->toString(false);
$expected = ' ';
$this->assertEqual($expected, $result);
}
/**
* testCompleteEscapeCharSerialization method
*
* @access public
* @return void
*/
function testCompleteEscapeCharSerialization() {
$xml = new Xml(array('text' => '<>&"\''), array('attributes' => false, 'format' => 'attributes'));
$result = $xml->toString(false);
$expected = ' ';
$this->assertEqual($expected, $result);
}
/**
* testToArray method
*
* @access public
* @return void
*/
function testToArray() {
App::import('Set');
$string = '
Cake PHP Google Group
http://groups.google.com/group/cake-php
Search this group before posting anything. There are over 20,000 posts and it's very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.
en
-
constructng result array when using findall
http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f
i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay->(hasMany)ServiceTi me->(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] => Array(
http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f
bmil...@gmail.com(bpscrugs)
Fri, 28 Dec 2007 00:44:14 UT
-
Re: share views between actions?
http://groups.google.com/group/cake-php/msg/8b350d898707dad8
Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple "RTFM" would suffice. I'll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other
http://groups.google.com/group/cake-php/msg/8b350d898707dad8
subtropolis.z...@gmail.com(subtropolis zijn)
Fri, 28 Dec 2007 00:45:01 UT
';
$xml = new Xml($string);
$result = $xml->toArray();
$expected = array('Rss' => array(
'version' => '2.0',
'Channel' => array(
'title' => 'Cake PHP Google Group',
'link' => 'http://groups.google.com/group/cake-php',
'description' => 'Search this group before posting anything. There are over 20,000 posts and it's very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
'language' => 'en',
'Item' => array(
array(
'title' => 'constructng result array when using findall',
'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
'description' => "i'm using cakephp to construct a logical data model array that will be passed to a flex app. I have the following model association: ServiceDay->(hasMany)ServiceTi me->(hasMany)ServiceTimePrice. So what the current output from my findall is something like this example:
Array( [0] => Array(",
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
'author' => 'bmil...@gmail.com(bpscrugs)',
'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
),
array(
'title' => 'Re: share views between actions?',
'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
'description' => 'Then perhaps you might do us all a favour and refrain from replying to things you do not understand. That goes especially for asinine comments. Indeed. To sum up: No comment. In my day, a simple "RTFM" would suffice. I\'ll keep in mind to ignore any further responses from you. You (and I) were referring to the *online documentation*, not other',
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
)
)
)
));
$this->assertEqual($result, $expected);
$string =' ';
$xml = new Xml($string);
$result = $xml->toArray();
$expected = array('Data' => array('Post' => array('title' => 'Title of this post', 'description' => 'cool')));
$this->assertEqual($result, $expected);
$xml = new Xml('An example of a correctly reversed XMLNode ');
$result = Set::reverse($xml);
$expected = array(
'Example' => array(
'Item' => array(
'title' => 'An example of a correctly reversed XMLNode',
'Desc' => array(),
)
)
);
$this->assertIdentical($result, $expected);
$xml = new Xml('title1 title2 ');
$result = $xml->toArray();
$expected = array(
'Example' => array(
'Item' => array(
'attr' => '123',
'Titles' => array(
'Title' => array('title1', 'title2')
)
)
)
);
$this->assertIdentical($result, $expected);
$xml = new Xml('list textforitems ');
$result = $xml->toArray();
$expected = array(
'Example' => array(
'attr' => 'ex_attr',
'Item' => array(
'attr' => '123',
'titles' => 'list',
'value' => 'textforitems'
)
)
);
$this->assertIdentical($result, $expected);
$xml = new Xml('list textforitems ');
$example = $xml->child('example');
$item = $example->child('item');
$result = $item->toArray();
$expected = array(
'attr' => '123',
'titles' => 'list',
'value' => 'textforitems'
);
$this->assertIdentical($result, $expected);
$string = '
Cake PHP Google Group
http://groups.google.com/group/cake-php
Search this group before posting anything. There are over 20,000 posts and it's very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.
en
-
constructng result array when using findall
http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f
i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay->(hasMany)ServiceTi me->(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] => Array(
cakephp
http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f
bmil...@gmail.com(bpscrugs)
Fri, 28 Dec 2007 00:44:14 UT
-
Re: share views between actions?
http://groups.google.com/group/cake-php/msg/8b350d898707dad8
Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple "RTFM" would suffice. I'll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other
cakephp
http://groups.google.com/group/cake-php/msg/8b350d898707dad8
subtropolis.z...@gmail.com(subtropolis zijn)
Fri, 28 Dec 2007 00:45:01 UT
';
$xml = new Xml($string);
$result = $xml->toArray();
$expected = array('Rss' => array(
'version' => '2.0',
'Channel' => array(
'title' => 'Cake PHP Google Group',
'link' => 'http://groups.google.com/group/cake-php',
'description' => 'Search this group before posting anything. There are over 20,000 posts and it's very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
'language' => 'en',
'Item' => array(
array(
'title' => 'constructng result array when using findall',
'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
'description' => "i'm using cakephp to construct a logical data model array that will be passed to a flex app. I have the following model association: ServiceDay->(hasMany)ServiceTi me->(hasMany)ServiceTimePrice. So what the current output from my findall is something like this example:
Array( [0] => Array(",
'creator' => 'cakephp',
'Category' => array('cakephp', 'model'),
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
'author' => 'bmil...@gmail.com(bpscrugs)',
'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
),
array(
'title' => 'Re: share views between actions?',
'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
'description' => 'Then perhaps you might do us all a favour and refrain from replying to things you do not understand. That goes especially for asinine comments. Indeed. To sum up: No comment. In my day, a simple "RTFM" would suffice. I\'ll keep in mind to ignore any further responses from you. You (and I) were referring to the *online documentation*, not other',
'creator' => 'cakephp',
'Category' => array('cakephp', 'model'),
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
)
)
)
));
$this->assertEqual($result, $expected);
$text = "
1
2
3
4
";
$xml = new Xml($text);
$result = $xml->toArray();
$expected = array('Course' => array(
'Comps' => array(
'Comp' => array(
1, 2, 3, 4
)
)
));
$this->assertEqual($result, $expected);
$text = '
xri://$xrds*simple
2008-04-13T07:34:58Z
http://oauth.net/core/1.0/endpoint/authorize
http://oauth.net/core/1.0/parameters/auth-header
http://oauth.net/core/1.0/parameters/uri-query
https://ma.gnolia.com/oauth/authorize
http://ma.gnolia.com/oauth/authorize
xri://$xrds*simple
http://oauth.net/discovery/1.0
#oauth
';
$xml = new Xml($text);
$result = $xml->toArray();
$expected = array('XRDS' => array(
'xmlns' => 'xri://$xrds',
'XRD' => array(
array(
'xml:id' => 'oauth',
'xmlns' => 'xri://$XRD*($v*2.0)',
'version' => '2.0',
'Type' => 'xri://$xrds*simple',
'Expires' => '2008-04-13T07:34:58Z',
'Service' => array(
'Type' => array(
'http://oauth.net/core/1.0/endpoint/authorize',
'http://oauth.net/core/1.0/parameters/auth-header',
'http://oauth.net/core/1.0/parameters/uri-query'
),
'URI' => array(
array(
'value' => 'https://ma.gnolia.com/oauth/authorize',
'priority' => '10',
),
array(
'value' => 'http://ma.gnolia.com/oauth/authorize',
'priority' => '20'
)
)
)
),
array(
'xmlns' => 'xri://$XRD*($v*2.0)',
'version' => '2.0',
'Type' => 'xri://$xrds*simple',
'Service' => array(
'priority' => '10',
'Type' => 'http://oauth.net/discovery/1.0',
'URI' => '#oauth'
)
)
)
));
$this->assertEqual($result, $expected);
}
/**
* testAppend method
*
* @access public
* @return void
*/
function testAppend() {
$parentNode = new XmlNode('ourParentNode');
$parentNode->append( new XmlNode('ourChildNode'));
$first =& $parentNode->first();
$this->assertEqual($first->name, 'ourChildNode');
$string = 'ourChildNode';
$parentNode = new XmlNode('ourParentNode');
$parentNode->append($string);
$last =& $parentNode->last();
$this->assertEqual($last->name, 'ourChildNode');
$this->expectError();
$parentNode->append($parentNode);
}
/**
* testNamespacing method
*
* @access public
* @return void
*/
function testNamespacing() {
$node = new Xml(' ');
$node->addNamespace('cake', 'http://cakephp.org');
$this->assertEqual($node->toString(), ' ');
$this->assertTrue($node->removeNamespace('cake'));
$this->assertEqual($node->toString(), ' ');
$node = new Xml(' ');
$this->assertTrue($node->removeNamespace('cake'));
$this->assertEqual($node->toString(), ' ');
$node->addNamespace('cake', 'http://cakephp.org');
$this->assertEqual($node->toString(), ' ');
}
/**
* testCamelize method
*
* @access public
* @return void
*/
function testCamelize() {
$xmlString = 'examples.getStateName ' .
'41 ';
$Xml = new Xml($xmlString);
$expected = array(
'methodCall' => array(
'methodName' => 'examples.getStateName',
'params' => array(
'param' => array('value' => array('i4' => 41)))));
$this->assertEqual($expected, $Xml->toArray(false));
$Xml = new Xml($xmlString);
$expected = array(
'MethodCall' => array(
'methodName' => 'examples.getStateName',
'Params' => array(
'Param' => array('Value' => array('i4' => 41)))));
$this->assertEqual($expected, $Xml->toArray());
}
/**
* testNumericDataHandling method
*
* @access public
* @return void
*/
function testNumericDataHandling() {
$data = '012345 ';
$node = new Xml();
$node->load($data);
$node->parse();
$result = $node->first();
$result = $result->children("data");
$result = $result[0]->first();
$this->assertEqual($result->value, '012345');
}
}
?>