mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge branch '2.1' into 2.2
Conflicts: lib/Cake/Test/Case/View/XmlViewTest.php
This commit is contained in:
commit
9a8ceaeba6
6 changed files with 91 additions and 15 deletions
|
@ -484,7 +484,8 @@ class TranslateBehavior extends ModelBehavior {
|
|||
*
|
||||
* @param Model $model instance of model
|
||||
* @param string|array $fields string with field or array(field1, field2=>AssocName, field3)
|
||||
* @param boolean $reset
|
||||
* @param boolean $reset Leave true to have the fields only modified for the next operation.
|
||||
* if false the field will be added for all future queries.
|
||||
* @return boolean
|
||||
* @throws CakeException when attempting to bind a translating called name. This is not allowed
|
||||
* as it shadows Model::$name.
|
||||
|
@ -511,7 +512,7 @@ class TranslateBehavior extends ModelBehavior {
|
|||
);
|
||||
}
|
||||
|
||||
$this->_updateSettings($model, $field);
|
||||
$this->_removeField($model, $field);
|
||||
|
||||
if (is_null($association)) {
|
||||
if ($reset) {
|
||||
|
@ -553,17 +554,17 @@ class TranslateBehavior extends ModelBehavior {
|
|||
*
|
||||
* @param string $field The field to update.
|
||||
*/
|
||||
protected function _updateSettings(Model $model, $field) {
|
||||
protected function _removeField(Model $model, $field) {
|
||||
if (array_key_exists($field, $this->settings[$model->alias])) {
|
||||
unset($this->settings[$model->alias][$field]);
|
||||
} elseif (in_array($field, $this->settings[$model->alias])) {
|
||||
$this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
|
||||
$this->settings[$model->alias] = array_merge(array_diff($this->settings[$model->alias], array($field)));
|
||||
}
|
||||
|
||||
if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
|
||||
unset($this->runtime[$model->alias]['fields'][$field]);
|
||||
} elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
|
||||
$this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
|
||||
$this->runtime[$model->alias]['fields'] = array_merge(array_diff($this->runtime[$model->alias]['fields'], array($field)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -599,7 +600,7 @@ class TranslateBehavior extends ModelBehavior {
|
|||
$association = $value;
|
||||
}
|
||||
|
||||
$this->_updateSettings($model, $field);
|
||||
$this->_removeField($model, $field);
|
||||
|
||||
if (!is_null($association) && (isset($model->hasMany[$association]) || isset($model->__backAssociation['hasMany'][$association]))) {
|
||||
$associations[] = $association;
|
||||
|
|
|
@ -1006,4 +1006,28 @@ class TranslateBehaviorTest extends CakeTestCase {
|
|||
$TestModel = new TranslatedItem();
|
||||
$TestModel->bindTranslation(array('name' => 'name'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that translations can be bound and unbound dynamically.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUnbindTranslation() {
|
||||
$this->loadFixtures('Translate', 'TranslatedItem');
|
||||
$Model = new TranslatedItem();
|
||||
$Model->unbindTranslation();
|
||||
$Model->bindTranslation(array('body', 'slug'), false);
|
||||
|
||||
$result = $Model->Behaviors->Translate->settings['TranslatedItem'];
|
||||
$this->assertEquals(array('body', 'slug'), $result);
|
||||
|
||||
$Model->unbindTranslation(array('body'));
|
||||
$result = $Model->Behaviors->Translate->settings['TranslatedItem'];
|
||||
$this->assertNotContains('body', $result);
|
||||
|
||||
$Model->unbindTranslation('slug');
|
||||
$result = $Model->Behaviors->Translate->settings['TranslatedItem'];
|
||||
$this->assertNotContains('slug', $result);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1344,6 +1344,33 @@ class SetTest extends CakeTestCase {
|
|||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that extract() + matching can hit null things.
|
||||
*/
|
||||
public function testExtractMatchesNull() {
|
||||
$data = array(
|
||||
'Country' => array(
|
||||
array('name' => 'Canada'),
|
||||
array('name' => 'Australia'),
|
||||
array('name' => null),
|
||||
)
|
||||
);
|
||||
$result = Set::extract('/Country[name=/Canada|^$/]', $data);
|
||||
$expected = array(
|
||||
array(
|
||||
'Country' => array(
|
||||
'name' => 'Canada',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'Country' => array(
|
||||
'name' => null,
|
||||
),
|
||||
),
|
||||
);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* testMatches method
|
||||
*
|
||||
|
|
|
@ -43,9 +43,27 @@ class XmlViewTest extends CakeTestCase {
|
|||
$View = new XmlView($Controller);
|
||||
$output = $View->render(false);
|
||||
|
||||
$expected = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<users><user>user1</user><user>user2</user></users>';
|
||||
$this->assertTextEquals($expected, trim($output));
|
||||
$this->assertSame(Xml::build($data)->asXML(), $output);
|
||||
$this->assertSame('application/xml', $Response->type());
|
||||
|
||||
$data = array(
|
||||
array(
|
||||
'User' => array(
|
||||
'username' => 'user1'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'User' => array(
|
||||
'username' => 'user2'
|
||||
)
|
||||
)
|
||||
);
|
||||
$Controller->set(array('users' => $data, '_serialize' => 'users'));
|
||||
$View = new XmlView($Controller);
|
||||
$output = $View->render(false);
|
||||
|
||||
$expected = Xml::build(array('response'=> array('users'=> $data)))->asXML();
|
||||
$this->assertSame($expected, $output);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,8 +118,11 @@ class XmlViewTest extends CakeTestCase {
|
|||
$View = new XmlView($Controller);
|
||||
$output = $View->render('index');
|
||||
|
||||
$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>';
|
||||
$this->assertSame($expected, str_replace(array("\r", "\n"), '', $output));
|
||||
$expected = array(
|
||||
'users'=> array('user'=> array('user1', 'user2'))
|
||||
);
|
||||
$expected = Xml::build($expected)->asXML();
|
||||
$this->assertSame($expected, $output);
|
||||
$this->assertSame('application/xml', $Response->type());
|
||||
$this->assertInstanceOf('HelperCollection', $View->Helpers);
|
||||
}
|
||||
|
|
|
@ -494,7 +494,7 @@ class Set {
|
|||
continue;
|
||||
}
|
||||
list(, $key, $op, $expected) = $match;
|
||||
if (!isset($data[$key])) {
|
||||
if (!(isset($data[$key]) || array_key_exists($key, $data))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ App::uses('Xml', 'Utility');
|
|||
*
|
||||
* `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
|
||||
*
|
||||
* When the view is rendered, the `$posts` view variable will be serialized
|
||||
* When the view is rendered, the `$posts` view variable will be serialized
|
||||
* into XML.
|
||||
*
|
||||
* **Note** The view variable you specify must be compatible with Xml::fromArray().
|
||||
|
@ -38,7 +38,7 @@ App::uses('Xml', 'Utility');
|
|||
* $this->set(compact('posts', 'users', 'stuff'));
|
||||
* $this->set('_serialize', array('posts', 'users'));
|
||||
* }}}
|
||||
*
|
||||
*
|
||||
* The above would generate a XML object that looks like:
|
||||
*
|
||||
* `<response><posts>...</posts><users>...</users></response>`
|
||||
|
@ -75,8 +75,8 @@ class XmlView extends View {
|
|||
* Render a XML view.
|
||||
*
|
||||
* Uses the special '_serialize' parameter to convert a set of
|
||||
* view variables into a XML response. Makes generating simple
|
||||
* XML responses very easy. You can omit the '_serialize' parameter,
|
||||
* view variables into a XML response. Makes generating simple
|
||||
* XML responses very easy. You can omit the '_serialize' parameter,
|
||||
* and use a normal view + layout as well.
|
||||
*
|
||||
* @param string $view The view being rendered.
|
||||
|
@ -93,6 +93,9 @@ class XmlView extends View {
|
|||
}
|
||||
} else {
|
||||
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
|
||||
if (is_array($data) && Set::numeric(array_keys($data))) {
|
||||
$data = array('response' => array($serialize => $data));
|
||||
}
|
||||
}
|
||||
$content = Xml::fromArray($data)->asXML();
|
||||
$this->Blocks->set('content', $content);
|
||||
|
|
Loading…
Reference in a new issue