Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markstory committed May 28, 2012
1 parent b27a3aa commit 0bfcd49
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/Cake/Model/Behavior/TranslateBehavior.php
Expand Up @@ -484,7 +484,8 @@ public function translateModel(Model $model) {
*
* @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.
Expand All @@ -511,7 +512,7 @@ public function bindTranslation(Model $model, $fields, $reset = true) {
);
}

$this->_updateSettings($model, $field);
$this->_removeField($model, $field);

if (is_null($association)) {
if ($reset) {
Expand Down Expand Up @@ -553,17 +554,17 @@ public function bindTranslation(Model $model, $fields, $reset = true) {
*
* @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)));
}
}

Expand Down Expand Up @@ -599,7 +600,7 @@ public function unbindTranslation(Model $model, $fields = null) {
$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;
Expand Down
24 changes: 24 additions & 0 deletions lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -1015,4 +1015,28 @@ public function testExceptionOnNameTranslation() {
$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);
}

}

0 comments on commit 0bfcd49

Please sign in to comment.