Browse Source

Changing key to conditions + adding test

Patrick Conroy 11 years ago
parent
commit
b8fbe9ba42

+ 7 - 11
src/ORM/Behavior/TranslateBehavior.php

@@ -78,7 +78,7 @@ class TranslateBehavior extends Behavior
         'model' => '',
         'onlyTranslated' => false,
         'strategy' => 'subquery',
-        'fieldConditions' => ['model' => '']
+        'conditions' => ['model' => '']
     ];
 
     /**
@@ -111,12 +111,12 @@ class TranslateBehavior extends Behavior
             $this->_translationTable = TableRegistry::get($translationAlias);
         }
 
-        $this->config('fieldConditions.model', $this->config('model') ?: $this->config('fieldConditions.model') ?: $this->_table->alias());
+        $this->config('conditions.model', $this->config('model') ?: $this->config('conditions.model') ?: $this->_table->alias());
 
         $this->setupFieldAssociations(
             $this->_config['fields'],
             $this->_config['translationTable'],
-            $this->_config['fieldConditions'],
+            $this->_config['conditions'],
             $this->_config['strategy']
         );
     }
@@ -130,7 +130,7 @@ class TranslateBehavior extends Behavior
      *
      * @param array $fields list of fields to create associations for
      * @param string $table the table name to use for storing each field translation
-     * @param array $fieldConditions conditions for finding fields
+     * @param array $conditions conditions for finding fields
      * @param string $strategy the strategy used in the _i18n association
      *
      * @return void
@@ -148,11 +148,7 @@ class TranslateBehavior extends Behavior
                 $name . '.field' => $field,
             ];
             foreach ($fieldConditions as $fieldName => $fieldValue) {
-                if (is_numeric($fieldName)) {
-                    $conditions[] = $name . '.' . $fieldValue;
-                } else {
-                    $conditions[$name . '.' . $fieldName] = $fieldValue;
-                }
+                $conditions[$name . '.' . $fieldName] = $fieldValue;
             }
             if (!TableRegistry::exists($name)) {
                 $fieldTable = TableRegistry::get($name, [
@@ -273,7 +269,7 @@ class TranslateBehavior extends Behavior
         $fields = array_keys($values);
         $primaryKey = (array)$this->_table->primaryKey();
         $key = $entity->get(current($primaryKey));
-        $model = $this->config('fieldConditions.model');
+        $model = $this->config('conditions.model');
 
         $preexistent = $this->_translationTable->find()
             ->select(['id', 'field'])
@@ -479,7 +475,7 @@ class TranslateBehavior extends Behavior
         }
 
         $results = $this->_findExistingTranslations($find);
-        $model = $this->config('fieldConditions.model');
+        $model = $this->config('conditions.model');
 
         foreach ($find as $i => $translation) {
             if (!empty($results[$i])) {

+ 3 - 0
tests/Fixture/TranslatesFixture.php

@@ -53,6 +53,9 @@ class TranslatesFixture extends TestFixture
     public $records = [
         ['locale' => 'eng', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'],
         ['locale' => 'eng', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'body', 'content' => 'Content #1'],
+        ['locale' => 'eng', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'description', 'content' => 'Description #1'],
+        ['locale' => 'spa', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'body', 'content' => 'Contenido #1'],
+        ['locale' => 'spa', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'description', 'content' => ''],
         ['locale' => 'deu', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'],
         ['locale' => 'deu', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'body', 'content' => 'Inhalt #1'],
         ['locale' => 'cze', 'model' => 'Articles', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1'],

+ 22 - 3
tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php

@@ -209,7 +209,7 @@ class TranslateBehaviorTest extends TestCase
             [
                 'id' => 1,
                 'title' => 'First Article',
-                'body' => 'First Article Body',
+                'body' => 'Contenido #1',
                 'comments' => [
                     ['article_id' => 1, 'comment' => 'First Comment for First Article', '_locale' => 'spa'],
                     ['article_id' => 1, 'comment' => 'Second Comment for First Article', '_locale' => 'spa'],
@@ -328,9 +328,10 @@ class TranslateBehaviorTest extends TestCase
         $results = $table->find('translations');
         $expected = [
             [
-                'eng' => ['title' => 'Title #1', 'body' => 'Content #1', 'locale' => 'eng'],
+                'eng' => ['title' => 'Title #1', 'body' => 'Content #1', 'description' => 'Description #1', 'locale' => 'eng'],
                 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
-                'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
+                'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze'],
+                'spa' => ['body' => 'Contenido #1', 'locale' => 'spa', 'description' => '']
             ],
             [
                 'eng' => ['title' => 'Title #2', 'body' => 'Content #2', 'locale' => 'eng'],
@@ -968,4 +969,22 @@ class TranslateBehaviorTest extends TestCase
         $results = $table->find('translations')->all();
         $this->assertCount(1, $results);
     }
+
+    /**
+     * Tests that conditions set when defining the behavior are applied correctly
+     *
+     * @return void
+     */
+    public function testConditions()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->addBehavior('Translate', [
+            'fields' => ['title', 'body', 'description'],
+            'conditions' => ['content <>' => '']
+        ]);
+        $table->locale('spa');
+        $result = $table->find()->first();
+        $this->assertNull($result->description);
+    }
+
 }