Skip to content

Commit

Permalink
Make TranslateBehavior use the same locator as AssociationCollection.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Jun 15, 2018
1 parent 34e90fa commit 27fbaac
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ORM/Behavior/TranslateBehavior.php
Expand Up @@ -108,6 +108,8 @@ public function __construct(Table $table, array $config = [])

if (isset($config['tableLocator'])) {
$this->_tableLocator = $config['tableLocator'];
} else {
$this->_tableLocator = $table->associations()->getTableLocator();
}

parent::__construct($table, $config);
Expand Down
46 changes: 46 additions & 0 deletions tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
Expand Up @@ -18,6 +18,7 @@
use Cake\I18n\I18n;
use Cake\ORM\Behavior\Translate\TranslateTrait;
use Cake\ORM\Entity;
use Cake\ORM\Locator\TableLocator;
use Cake\TestSuite\TestCase;
use Cake\Validation\Validator;

Expand Down Expand Up @@ -1782,4 +1783,49 @@ public function testBuildMarshalMapUpdateEntitiesValidationErrors()
];
$this->assertEquals($expected, $entity->getError('es'));
}

/**
* Test that the behavior uses associations' locator.
*
* @return void
*/
public function testDefaultTableLocator()
{
$locator = new TableLocator();

$table = $locator->get('Articles');
$table->addBehavior('Translate', [
'fields' => ['title', 'body'],
'validator' => 'custom'
]);

$behaviorLocator = $table->behaviors()->get('Translate')->getTableLocator();

$this->assertSame($locator, $behaviorLocator);
$this->assertSame($table->associations()->getTableLocator(), $behaviorLocator);
$this->assertNotSame($this->getTableLocator(), $behaviorLocator);
}

/**
* Test that the behavior uses a custom locator.
*
* @return void
*/
public function testCustomTableLocator()
{
$locator = new TableLocator();

$table = $this->getTableLocator()->get('Articles');
$table->addBehavior('Translate', [
'fields' => ['title', 'body'],
'validator' => 'custom',
'tableLocator' => $locator,
]);

$behaviorLocator = $table->behaviors()->get('Translate')->getTableLocator();

$this->assertSame($locator, $behaviorLocator);
$this->assertNotSame($table->associations()->getTableLocator(), $behaviorLocator);
$this->assertNotSame($this->getTableLocator(), $behaviorLocator);
}
}

0 comments on commit 27fbaac

Please sign in to comment.