* Copyright 2005-2008, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* 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.
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake.tests
* @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');
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs
*/
class XmlTest extends UnitTestCase {
/**
* 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);
}
/**
* 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 = '111';
$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 = '1110111Touch Screen Kiosk1Financial2210222007-11-26 14:48:362Awareness Campaign2Education';
$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 = '1110111Touch Screen Kiosk1Financial6Convert Sales15Print7Web Demo6CD-ROM2210222007-11-26 14:48:362Awareness Campaign2Education4Build Relationship6Convert Sales17Web6CD-ROM';
$xml = new Xml($input, array('format' => 'tags'));
$result = $xml->toString(array('header' => false, 'cdata' => false));
$this->assertEqual($expected, $result);
}
/*
* 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 = '110111Touch Screen KioskFinancial6Convert Sales1576220222007-11-26 14:48:362Awareness CampaignEducation4Build Relationship6Convert Sales176';
// $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 = 'OptA12OptA 1OptA 2OptA 3OptA 4OptA 5OptA 6OptB12OptB 1OptB 2OptB 3OptB 4OptB 5OptB 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 = 'OptA12OptA 1OptA 2OptA 3OptA 4OptA 5OptA 6OptB12OptB 1OptB 2OptB 3OptB 4OptB 5OptB 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 1OptA 2OptA 3OptA 4OptA 5OptA 6OptB 1OptB 2OptB 3OptB 4OptB 5OptB 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 = '1011Touch Screen KioskFinancial10222007-11-26 14:48:36Awareness CampaignEducation';
//
// $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 = '11101.891.561122102.22.222';
$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 = '1110111Touch Screen Kiosk1Financial2210222007-11-26 14:48:362Awareness Campaign2Education';
$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);
}
/**
* 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 = '1110111Touch Screen Kiosk1Financial2210222007-11-26 14:48:362Awareness Campaign2Education';
$xml = new Xml($source);
$result = $xml->toString(array('cdata' => false));
$this->assertEqual($source, $result);
}
/**
* testNamespaceParsing method
*
* @access public
* @return void
*/
function testNamespaceParsing() {
$source = 'valuevalue';
$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 = 'valuevalue';
$xml = new Xml($source);
$expects = 'valuevalue';
$_xml = XmlManager::getInstance();
$xml->addNamespace('f', 'http://example.com/f');
$result = $xml->toString(array('cdata' => false));
$this->assertEqual($expects, $result);
}
/*
* @todo Add test for default namespaces
*/
}
?>