Browse Source

Adding tests for custom i18n tables

Patrick Conroy 11 years ago
parent
commit
1faac3e2a9

+ 2 - 0
src/ORM/Behavior/TranslateBehavior.php

@@ -111,6 +111,8 @@ 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 string $model the model field value
+     * @param string $strategy the strategy used in the _i18n association
+     * 
      * @return void
      */
     public function setupFieldAssociations($fields, $table, $model, $strategy)

+ 41 - 0
tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php

@@ -77,6 +77,46 @@ class TranslateBehaviorTest extends TestCase
     }
 
     /**
+     * Tests that custom translation tables are respected
+     * 
+     * @return void
+     */
+    public function testCustomTranslationTable() {
+        $table = TableRegistry::get('Articles');
+
+        $table->addBehavior('Translate', [
+            'translationTable' => '\TestApp\Model\Table\I18nTable',
+            'fields' => ['title', 'body']
+        ]);
+
+        $items = $table->associations();
+        $i18n = $items->getByProperty('_i18n');
+
+        $this->assertEquals('TestApp-Model-Table-I18nTable', $i18n->name());
+        $this->assertEquals('custom_i18n_table', $i18n->target()->table());
+        $this->assertEquals('test_custom_i18n_datasource', $i18n->target()->connection()->configName());
+    }
+
+    /**
+     * Tests that the strategy can be changed for i18n
+     * 
+     * @return void
+     */
+    public function testStrategy() {
+        $table = TableRegistry::get('Articles');
+
+        $table->addBehavior('Translate', [
+            'strategy' => 'select',
+            'fields' => ['title', 'body']
+        ]);
+
+        $items = $table->associations();
+        $i18n = $items->getByProperty('_i18n');
+
+        $this->assertEquals('select', $i18n->strategy());
+    }
+
+    /**
      * Tests that fields from a translated model are overriden
      *
      * @return void
@@ -926,4 +966,5 @@ class TranslateBehaviorTest extends TestCase
         $results = $table->find('translations')->all();
         $this->assertCount(1, $results);
     }
+
 }

+ 1 - 0
tests/bootstrap.php

@@ -94,6 +94,7 @@ if (!getenv('db_dsn')) {
 }
 
 ConnectionManager::config('test', ['url' => getenv('db_dsn')]);
+ConnectionManager::config('test_custom_i18n_datasource', ['url' => getenv('db_dsn')]);
 
 Configure::write('Session', [
     'defaults' => 'php'

+ 33 - 0
tests/test_app/TestApp/Model/Table/I18nTable.php

@@ -0,0 +1,33 @@
+<?php
+/**
+ * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
+ * Copyright 2005-2013, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\Model\Table;
+
+use Cake\ORM\Table;
+
+/**
+ * I18n table class
+ *
+ */
+class I18nTable extends Table
+{
+
+    public function initialize(array $config)
+    {
+        $this->table('custom_i18n_table');
+    }
+
+    public static function defaultConnectionName()
+    {
+        return 'custom_i18n_datasource';
+    }
+
+}