From 0bfcd492498708aef074b832f56e8a89b5d48c4f Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 27 May 2012 21:25:55 -0400 Subject: [PATCH] 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 --- lib/Cake/Model/Behavior/TranslateBehavior.php | 13 +++++----- .../Model/Behavior/TranslateBehaviorTest.php | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/Cake/Model/Behavior/TranslateBehavior.php b/lib/Cake/Model/Behavior/TranslateBehavior.php index 83378a6c26f..f5c5c7b5cf8 100644 --- a/lib/Cake/Model/Behavior/TranslateBehavior.php +++ b/lib/Cake/Model/Behavior/TranslateBehavior.php @@ -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. @@ -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) { @@ -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))); } } @@ -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; diff --git a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php index 98fb2e29108..e9c0ba6a20c 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php @@ -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); + } + }