Fix unbindTranslation not unbinding.

There were documented use cases that have never worked.  Fix that.
Also rename a method so it better describes what it does.

Fixes #2913
This commit is contained in:
mark_story 2012-05-27 21:25:55 -04:00
parent b27a3aab7b
commit 0bfcd49249
2 changed files with 31 additions and 6 deletions

View file

@ -484,7 +484,8 @@ class TranslateBehavior extends ModelBehavior {
* *
* @param Model $model instance of model * @param Model $model instance of model
* @param string|array $fields string with field or array(field1, field2=>AssocName, field3) * @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 * @return boolean
* @throws CakeException when attempting to bind a translating called name. This is not allowed * @throws CakeException when attempting to bind a translating called name. This is not allowed
* as it shadows Model::$name. * 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 (is_null($association)) {
if ($reset) { if ($reset) {
@ -553,17 +554,17 @@ class TranslateBehavior extends ModelBehavior {
* *
* @param string $field The field to update. * @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])) { if (array_key_exists($field, $this->settings[$model->alias])) {
unset($this->settings[$model->alias][$field]); unset($this->settings[$model->alias][$field]);
} elseif (in_array($field, $this->settings[$model->alias])) { } 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'])) { if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
unset($this->runtime[$model->alias]['fields'][$field]); unset($this->runtime[$model->alias]['fields'][$field]);
} elseif (in_array($field, $this->runtime[$model->alias]['fields'])) { } 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; $association = $value;
} }
$this->_updateSettings($model, $field); $this->_removeField($model, $field);
if (!is_null($association) && (isset($model->hasMany[$association]) || isset($model->__backAssociation['hasMany'][$association]))) { if (!is_null($association) && (isset($model->hasMany[$association]) || isset($model->__backAssociation['hasMany'][$association]))) {
$associations[] = $association; $associations[] = $association;

View file

@ -1015,4 +1015,28 @@ class TranslateBehaviorTest extends CakeTestCase {
$TestModel = new TranslatedItem(); $TestModel = new TranslatedItem();
$TestModel->bindTranslation(array('name' => 'name')); $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);
}
} }