From f0bbcb3ffcd720a9202a5a9d04d838092e7dac29 Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Sat, 8 Jul 2017 16:51:32 +0200 Subject: [PATCH] fixed parsing of conditions with 'NOT' in TranslateBehavior --- lib/Cake/Model/Behavior/TranslateBehavior.php | 21 ++++++++-- .../Model/Behavior/TranslateBehaviorTest.php | 41 +++++++++---------- lib/Cake/Test/Case/Model/ModelReadTest.php | 37 ++++++++--------- 3 files changed, 56 insertions(+), 43 deletions(-) diff --git a/lib/Cake/Model/Behavior/TranslateBehavior.php b/lib/Cake/Model/Behavior/TranslateBehavior.php index 203bf9b55..07e2635f2 100644 --- a/lib/Cake/Model/Behavior/TranslateBehavior.php +++ b/lib/Cake/Model/Behavior/TranslateBehavior.php @@ -245,11 +245,26 @@ class TranslateBehavior extends ModelBehavior { * @return array The list of translated fields that are in the conditions. */ protected function _checkConditions(Model $Model, $query) { - $conditionFields = array(); if (empty($query['conditions']) || (!empty($query['conditions']) && !is_array($query['conditions']))) { - return $conditionFields; + return array(); } - foreach ($query['conditions'] as $col => $val) { + return $this->_getConditionFields($Model, $query['conditions']); + } + + /** + * Extracts condition field names recursively. + * + * @param Model $Model The model being read. + * @param array $conditions The conditions array. + * @return array The list of condition fields. + */ + protected function _getConditionFields(Model $Model, $conditions) { + $conditionFields = array(); + foreach ($conditions as $col => $val) { + if (is_array($val)) { + $subConditionFields = $this->_getConditionFields($Model, $val); + $conditionFields = array_merge($conditionFields, $subConditionFields); + } foreach ($this->settings[$Model->alias] as $field => $assoc) { if (is_numeric($field)) { $field = $assoc; diff --git a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php index 37c42465b..5f74583c2 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php @@ -1460,7 +1460,7 @@ class TranslateBehaviorTest extends CakeTestCase { 'table' => (object)array( 'tablePrefix' => '', 'table' => 'article_i18n', - 'schemaName' => 'test', + 'schemaName' => 'cakephp_test', ), 'conditions' => array( 'TranslatedArticle.id' => (object)array( @@ -1478,7 +1478,7 @@ class TranslateBehaviorTest extends CakeTestCase { 'table' => (object)array( 'tablePrefix' => '', 'table' => 'article_i18n', - 'schemaName' => 'test', + 'schemaName' => 'cakephp_test', ), 'conditions' => array( 'TranslatedArticle.id' => (object)array( @@ -1535,13 +1535,30 @@ class TranslateBehaviorTest extends CakeTestCase { ), 'fields' => 'COUNT(DISTINCT(`TranslatedArticle`.`id`)) AS count', 'joins' => array( + array( + 'type' => 'INNER', + 'alias' => 'TranslateArticleModel', + 'table' => (object)array( + 'tablePrefix' => '', + 'table' => 'article_i18n', + 'schemaName' => 'cakephp_test', + ), + 'conditions' => array( + '`TranslatedArticle`.`id`' => (object)array( + 'type' => 'identifier', + 'value' => '`TranslateArticleModel`.`foreign_key`', + ), + '`TranslateArticleModel`.`model`' => 'TranslatedArticle', + '`TranslateArticleModel`.`locale`' => 'eng', + ), + ), array( 'type' => 'INNER', 'alias' => 'I18n__title', 'table' => (object)array( 'tablePrefix' => '', 'table' => 'article_i18n', - 'schemaName' => 'test', + 'schemaName' => 'cakephp_test', ), 'conditions' => array( 'TranslatedArticle.id' => (object)array( @@ -1553,24 +1570,6 @@ class TranslateBehaviorTest extends CakeTestCase { 'I18n__title.locale' => 'eng', ), ), - array( - 'type' => 'INNER', - 'alias' => 'I18n__body', - 'table' => (object)array( - 'tablePrefix' => '', - 'table' => 'article_i18n', - 'schemaName' => 'test', - ), - 'conditions' => array( - 'TranslatedArticle.id' => (object)array( - 'type' => 'identifier', - 'value' => 'I18n__body.foreign_key', - ), - 'I18n__body.model' => 'TranslatedArticle', - 'I18n__body.field' => 'body', - 'I18n__body.locale' => 'eng', - ), - ), ), 'limit' => 2, 'offset' => null, diff --git a/lib/Cake/Test/Case/Model/ModelReadTest.php b/lib/Cake/Test/Case/Model/ModelReadTest.php index 8c7148d89..f2c1713b9 100644 --- a/lib/Cake/Test/Case/Model/ModelReadTest.php +++ b/lib/Cake/Test/Case/Model/ModelReadTest.php @@ -6505,6 +6505,23 @@ class ModelReadTest extends BaseModelTest { ), 'fields' => 'COUNT(DISTINCT(`TranslatedArticle`.`id`)) AS count', 'joins' => array( + array( + 'type' => 'INNER', + 'alias' => 'TranslateArticleModel', + 'table' => (object)array( + 'tablePrefix' => '', + 'table' => 'article_i18n', + 'schemaName' => 'cakephp_test', + ), + 'conditions' => array( + '`TranslatedArticle`.`id`' => (object)array( + 'type' => 'identifier', + 'value' => '`TranslateArticleModel`.`foreign_key`', + ), + '`TranslateArticleModel`.`model`' => 'TranslatedArticle', + '`TranslateArticleModel`.`locale`' => 'eng', + ), + ), array( 'type' => 'INNER', 'alias' => 'I18n__title', @@ -6523,24 +6540,6 @@ class ModelReadTest extends BaseModelTest { 'I18n__title.locale' => 'eng', ), ), - array( - 'type' => 'INNER', - 'alias' => 'I18n__body', - 'table' => (object)array( - 'tablePrefix' => '', - 'table' => 'article_i18n', - 'schemaName' => 'test', - ), - 'conditions' => array( - 'TranslatedArticle.id' => (object)array( - 'type' => 'identifier', - 'value' => 'I18n__body.foreign_key', - ), - 'I18n__body.model' => 'TranslatedArticle', - 'I18n__body.field' => 'body', - 'I18n__body.locale' => 'eng', - ), - ), ), 'limit' => 2, 'offset' => null, @@ -7325,7 +7324,7 @@ class ModelReadTest extends BaseModelTest { 'limit' => 2, ); $result = $TestModel->find('count', $options); - $this->assertEquals(2, $result); + $this->assertEquals(3, $result); } /**