Skip to content

Commit

Permalink
Changing key to conditions + adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Conroy committed Feb 3, 2015
1 parent 39abf18 commit b8fbe9b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
18 changes: 7 additions & 11 deletions src/ORM/Behavior/TranslateBehavior.php
Expand Up @@ -78,7 +78,7 @@ class TranslateBehavior extends Behavior
'model' => '',
'onlyTranslated' => false,
'strategy' => 'subquery',
'fieldConditions' => ['model' => '']
'conditions' => ['model' => '']
];

/**
Expand Down Expand Up @@ -111,12 +111,12 @@ public function initialize(array $config)
$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']
);
}
Expand All @@ -130,7 +130,7 @@ public function initialize(array $config)
*
* @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
Expand All @@ -148,11 +148,7 @@ public function setupFieldAssociations($fields, $table, $fieldConditions, $strat
$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, [
Expand Down Expand Up @@ -273,7 +269,7 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
$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'])
Expand Down Expand Up @@ -479,7 +475,7 @@ protected function _bundleTranslatedFields($entity)
}

$results = $this->_findExistingTranslations($find);
$model = $this->config('fieldConditions.model');
$model = $this->config('conditions.model');

foreach ($find as $i => $translation) {
if (!empty($results[$i])) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixture/TranslatesFixture.php
Expand Up @@ -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'],
Expand Down
25 changes: 22 additions & 3 deletions tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
Expand Up @@ -209,7 +209,7 @@ public function testFindSingleLocaleAssociatedEnv()
[
'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'],
Expand Down Expand Up @@ -328,9 +328,10 @@ public function testFindTranslations()
$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'],
Expand Down Expand Up @@ -968,4 +969,22 @@ public function testFilterUntranslatedWithFinder()
$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);
}

}

0 comments on commit b8fbe9b

Please sign in to comment.