Merge remote branch 'origin/2.0' into feature/2.0/pdo

This commit is contained in:
José Lorenzo Rodríguez 2010-10-24 19:55:02 -04:30
commit 34813ab35c
13 changed files with 145 additions and 99 deletions

View file

@ -151,8 +151,8 @@ if (!function_exists('sortByKey')) {
function h($text, $double = true, $charset = null) {
if (is_array($text)) {
$texts = array();
foreach ($text as $t) {
$texts[] = h($t, $double, $charset);
foreach ($text as $k => $t) {
$texts[$k] = h($t, $double, $charset);
}
return $texts;
}

View file

@ -48,7 +48,7 @@
}
}
if ($isKey !== true) {
echo "\t\t<td><?php echo \${$singularVar}['{$modelClass}']['{$field}']; ?>&nbsp;</td>\n";
echo "\t\t<td><?php echo h(\${$singularVar}['{$modelClass}']['{$field}']); ?>&nbsp;</td>\n";
}
}

View file

@ -35,7 +35,7 @@ foreach ($fields as $field) {
}
if ($isKey !== true) {
echo "\t\t<dt<?php if (\$i % 2 == 0) echo \$class;?>><?php echo __('" . Inflector::humanize($field) . "'); ?></dt>\n";
echo "\t\t<dd<?php if (\$i++ % 2 == 0) echo \$class;?>>\n\t\t\t<?php echo \${$singularVar}['{$modelClass}']['{$field}']; ?>\n\t\t\t&nbsp;\n\t\t</dd>\n";
echo "\t\t<dd<?php if (\$i++ % 2 == 0) echo \$class;?>>\n\t\t\t<?php echo h(\${$singularVar}['{$modelClass}']['{$field}']); ?>\n\t\t\t&nbsp;\n\t\t</dd>\n";
}
}
?>

View file

@ -831,8 +831,13 @@ class Model extends Object {
return;
}
if (is_object($one)) {
if ($one instanceof SimpleXMLElement || $one instanceof DOMNode) {
App::import('Core', 'Xml');
$one = $this->_normalizeXmlData(Xml::toArray($one));
} else {
$one = Set::reverse($one);
}
}
if (is_array($one)) {
$data = $one;
@ -868,6 +873,26 @@ class Model extends Object {
return $data;
}
/**
* Normalize Xml::toArray() to use in Model::save()
*
* @param array $xml XML as array
* @return array
*/
protected function _normalizeXmlData(array $xml) {
$return = array();
foreach ($xml as $key => $value) {
if (is_array($value)) {
$return[Inflector::camelize($key)] = $this->_normalizeXmlData($value);
} elseif ($key[0] === '@') {
$return[substr($key, 1)] = $value;
} else {
$return[$key] = $value;
}
}
return $return;
}
/**
* Deconstructs a complex data type (array or object) into a single field value.
*

View file

@ -315,8 +315,9 @@ class RssHelper extends AppHelper {
if (!empty($namespace)) {
$xml .= ' xmlns:"' . $namespace . '"';
}
$bareName = $name;
if (strpos($name, ':') !== false) {
list($prefix, ) = explode(':', $name, 2);
list($prefix, $bareName) = explode(':', $name, 2);
switch ($prefix) {
case 'atom':
$xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
@ -327,15 +328,17 @@ class RssHelper extends AppHelper {
$content = '<![CDATA[' . $content . ']]>';
}
$xml .= '>' . $content . '</' . $name. '>';
$elem = Xml::build($xml);
$elem = Xml::build($xml, array('return' => 'domdocument'));
$nodes = $elem->getElementsByTagName($bareName);
foreach ($attrib as $key => $value) {
$elem->addAttribute($key, $value);
$nodes->item(0)->setAttribute($key, $value);
}
foreach ($children as $child) {
$elem->addChild($child);
foreach ($children as $k => $child) {
$child = $elem->createElement($name, $child);
$nodes->item(0)->appendChild($child);
}
$xml = $elem->asXML();
$xml = $elem->saveXml();
$xml = trim(substr($xml, strpos($xml, '?>') + 2));
return $xml;
}

View file

@ -47,7 +47,7 @@ echo "\n";
}
}
if ($isKey !== true) {
echo "\t\t<td>\n\t\t\t" . ${$singularVar}[$modelClass][$_field] . " \n\t\t</td>\n";
echo "\t\t<td>" . h(${$singularVar}[$modelClass][$_field]) . "</td>\n";
}
}

View file

@ -40,7 +40,7 @@ foreach ($scaffoldFields as $_field) {
}
if ($isKey !== true) {
echo "\t\t<dt{$class}>" . Inflector::humanize($_field) . "</dt>\n";
echo "\t\t<dd{$class}>\n\t\t\t{${$singularVar}[$modelClass][$_field]}\n&nbsp;\t\t</dd>\n";
echo "\t\t<dd{$class}>" . h(${$singularVar}[$modelClass][$_field]) . "&nbsp;</dd>\n";
}
}
?>

View file

@ -88,14 +88,14 @@ class Xml {
return self::fromArray((array)$input, $options);
} elseif (strpos($input, '<') !== false) {
if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
return new SimpleXMLElement($input);
return new SimpleXMLElement($input, LIBXML_NOCDATA);
}
$dom = new DOMDocument();
$dom->loadXML($input);
return $dom;
} elseif (file_exists($input) || strpos($input, 'http://') === 0 || strpos($input, 'https://') === 0) {
if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
return new SimpleXMLElement($input, null, true);
return new SimpleXMLElement($input, LIBXML_NOCDATA, true);
}
$dom = new DOMDocument();
$dom->load($input);

View file

@ -228,6 +228,14 @@ class BasicsTest extends CakeTestCase {
'&nbsp;'
);
$this->assertEqual($expected, $result);
$arr = array('f' => '<foo>', 'n' => '&nbsp;');
$result = h($arr, false);
$expected = array(
'f' => '&lt;foo&gt;',
'n' => '&nbsp;'
);
$this->assertEqual($expected, $result);
}
/**

View file

@ -1020,12 +1020,20 @@ class ModelWriteTest extends BaseModelTest {
App::import('Core', 'Xml');
$Article = new Article();
$Article->save(new Xml('<article title="test xml" user_id="5" />'));
$result = $Article->save(new Xml('<article title="test xml" user_id="5" />'));
$result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
$this->assertFalse(empty($result));
$results = $Article->find(array('Article.title' => 'test xml'));
$this->assertFalse(empty($results));
$result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
$this->assertFalse(empty($result));
$results = $Article->find(array('Article.title' => 'testing'));
$this->assertFalse(empty($results));
$result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
$this->assertFalse(empty($result));
$results = $Article->find(array('Article.title' => 'testing with DOMDocument'));
$this->assertFalse(empty($results));
}
/**

View file

@ -2731,7 +2731,7 @@ class SetTest extends CakeTestCase {
$xml = Xml::build($string);
$result = Set::reverse($xml);
$expected = array('rss' => array(
'version' => '2.0',
'@version' => '2.0',
'channel' => array(
'title' => 'Cake PHP Google Group',
'link' => 'http://groups.google.com/group/cake-php',
@ -2742,7 +2742,7 @@ class SetTest extends CakeTestCase {
'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 <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] =&gt; Array(",
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
'author' => 'bmil...@gmail.com(bpscrugs)',
'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
),
@ -2750,7 +2750,7 @@ class SetTest extends CakeTestCase {
'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 <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 &quot;RTFM&quot; 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',
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
'guid' => array('@isPermaLink' => 'true', '@' => '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'
)
@ -2762,7 +2762,7 @@ class SetTest extends CakeTestCase {
$xml = Xml::build($string);
$result = Set::reverse($xml);
$expected = array('data' => array('post' => array('title' => 'Title of this post', 'description' => 'cool')));
$expected = array('data' => array('post' => array('@title' => 'Title of this post', '@description' => 'cool')));
$this->assertEqual($result, $expected);
$xml = Xml::build('<example><item><title>An example of a correctly reversed SimpleXMLElement</title><desc/></item></example>');
@ -2782,7 +2782,7 @@ class SetTest extends CakeTestCase {
$expected =
array('example' => array(
'item' => array(
'attr' => '123',
'@attr' => '123',
'titles' => array(
'title' => array('title1', 'title2')
)
@ -2795,11 +2795,11 @@ class SetTest extends CakeTestCase {
$result = Set::reverse($xml);
$expected =
array('example' => array(
'attr' => 'ex_attr',
'@attr' => 'ex_attr',
'item' => array(
'attr' => '123',
'@attr' => '123',
'titles' => 'list',
'value' => 'textforitems'
'@' => 'textforitems'
)
)
);
@ -2841,7 +2841,7 @@ class SetTest extends CakeTestCase {
$result = Set::reverse($xml);
$expected = array('rss' => array(
'version' => '2.0',
'@version' => '2.0',
'channel' => array(
'title' => 'Cake PHP Google Group',
'link' => 'http://groups.google.com/group/cake-php',
@ -2852,9 +2852,9 @@ class SetTest extends CakeTestCase {
'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 <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] =&gt; Array(",
'creator' => 'cakephp',
'dc:creator' => 'cakephp',
'category' => array('cakephp', 'model'),
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
'author' => 'bmil...@gmail.com(bpscrugs)',
'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
),
@ -2862,9 +2862,9 @@ class SetTest extends CakeTestCase {
'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 <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 &quot;RTFM&quot; 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',
'creator' => 'cakephp',
'dc:creator' => 'cakephp',
'category' => array('cakephp', 'model'),
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
'guid' => array('@isPermaLink' => 'true', '@' => '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'
)
@ -2901,8 +2901,8 @@ class SetTest extends CakeTestCase {
$expected = array('XRDS' => array(
'XRD' => array(
array(
'id' => 'oauth',
'version' => '2.0',
'@xml:id' => 'oauth',
'@version' => '2.0',
'Type' => 'xri://$xrds*simple',
'Expires' => '2008-04-13T07:34:58Z',
'Service' => array(
@ -2913,21 +2913,21 @@ class SetTest extends CakeTestCase {
),
'URI' => array(
array(
'value' => 'https://ma.gnolia.com/oauth/authorize',
'priority' => '10',
'@' => 'https://ma.gnolia.com/oauth/authorize',
'@priority' => '10',
),
array(
'value' => 'http://ma.gnolia.com/oauth/authorize',
'priority' => '20'
'@' => 'http://ma.gnolia.com/oauth/authorize',
'@priority' => '20'
)
)
)
),
array(
'version' => '2.0',
'@version' => '2.0',
'Type' => 'xri://$xrds*simple',
'Service' => array(
'priority' => '10',
'@priority' => '10',
'Type' => 'http://oauth.net/discovery/1.0',
'URI' => '#oauth'
)

View file

@ -35,7 +35,7 @@ class RssHelperTest extends CakeTestCase {
* @return void
*/
function setUp() {
$controller = null;
parent::setUp();
$this->View = new View($controller);
$this->Rss = new RssHelper($this->View);
}
@ -47,6 +47,7 @@ class RssHelperTest extends CakeTestCase {
* @return void
*/
function tearDown() {
parent::tearDown();
unset($this->Rss);
}
@ -65,15 +66,6 @@ class RssHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
$result = $this->Rss->document(array('contrived' => 'parameter'));
$expected = array(
'rss' => array(
'version' => '2.0'
),
'<parameter'
);
$this->assertTags($result, $expected);
$result = $this->Rss->document(null, 'content');
$expected = array(
'rss' => array(
@ -173,6 +165,7 @@ class RssHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
}
function testChannelElementAttributes() {
$attrib = array();
$elements = array(
@ -288,15 +281,9 @@ class RssHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
$item = array(
'title' => array(
'value' => 'My Title',
'cdata' => true,
),
'title' => 'My Title',
'link' => 'http://www.example.com/1',
'description' => array(
'value' => 'descriptive words',
'cdata' => true,
),
'description' => 'descriptive words',
'pubDate' => '2008-05-31 12:00:00',
'guid' => 'http://www.example.com/1'
);
@ -305,13 +292,13 @@ class RssHelperTest extends CakeTestCase {
$expected = array(
'<item',
'<title',
'<![CDATA[My Title]]',
'My Title',
'/title',
'<link',
'http://www.example.com/1',
'/link',
'<description',
'<![CDATA[descriptive words]]',
'descriptive words',
'/description',
'<pubDate',
date('r', strtotime('2008-05-31 12:00:00')),
@ -324,21 +311,57 @@ class RssHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
$item = array(
'title' => array(
'value' => 'My Title & more',
'cdata' => true
)
'title' => 'My Title & more'
);
$result = $this->Rss->item(null, $item);
$expected = array(
'<item',
'<title', 'My Title &amp; more', '/title',
'/item'
);
$this->assertTags($result, $expected);
$item = array(
'title' => 'Foo bar',
'link' => array(
'url' => 'http://example.com/foo?a=1&b=2',
'convertEntities' => false
),
'description' => array(
'value' => 'descriptive words',
'cdata' => true,
),
'pubDate' => '2008-05-31 12:00:00'
);
$result = $this->Rss->item(null, $item);
$expected = array(
'<item',
'<title',
'<![CDATA[My Title &amp; more]]',
'Foo bar',
'/title',
'<link',
'http://example.com/foo?a=1&amp;b=2',
'/link',
'<description',
'<![CDATA[descriptive words]]',
'/description',
'<pubDate',
date('r', strtotime('2008-05-31 12:00:00')),
'/pubDate',
'<guid',
'http://example.com/foo?a=1&amp;b=2',
'/guid',
'/item'
);
$this->assertTags($result, $expected);
}
/**
* test item() with cdata blocks.
*
* @return void
*/
function testItemCdata() {
$item = array(
'title' => array(
'value' => 'My Title & more',
@ -360,7 +383,7 @@ class RssHelperTest extends CakeTestCase {
'category' => array(
'value' => 'CakePHP',
'cdata' => true,
'domain' => 'http://www.cakephp.org'
'domain' => 'http://www.cakephp.org',
)
);
$result = $this->Rss->item(null, $item);
@ -454,40 +477,6 @@ class RssHelperTest extends CakeTestCase {
'/item'
);
$this->assertTags($result, $expected);
$item = array(
'title' => 'Foo bar',
'link' => array(
'url' => 'http://example.com/foo?a=1&b=2',
'convertEntities' => false
),
'description' => array(
'value' => 'descriptive words',
'cdata' => true,
),
'pubDate' => '2008-05-31 12:00:00'
);
$result = $this->Rss->item(null, $item);
$expected = array(
'<item',
'<title',
'Foo bar',
'/title',
'<link',
'http://example.com/foo?a=1&amp;b=2',
'/link',
'<description',
'<![CDATA[descriptive words]]',
'/description',
'<pubDate',
date('r', strtotime('2008-05-31 12:00:00')),
'/pubDate',
'<guid',
'http://example.com/foo?a=1&amp;b=2',
'/guid',
'/item'
);
$this->assertTags($result, $expected);
}
/**

View file

@ -797,6 +797,19 @@ class XmlTest extends CakeTestCase {
$this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
}
/**
* test that CDATA blocks don't get screwed up by SimpleXml
*
* @return void
*/
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
*