Skip to content

Commit

Permalink
Add docs to translation marshalling tests.
Browse files Browse the repository at this point in the history
Most of these tests should move, leaving only an integration test or two
behind.
  • Loading branch information
markstory committed Aug 8, 2016
1 parent ea9c8e5 commit c8bf49a
Showing 1 changed file with 57 additions and 19 deletions.
76 changes: 57 additions & 19 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -3006,6 +3006,12 @@ public function testAssociationNoChanges()
$this->assertTrue($entity->user->isNew());
}

/**
* Test that primary key meta data is being read from the table
* and not the schema reflection when handling belongsToMany associations.
*
* @return void
*/
public function testEnsurePrimaryKeyBeingReadFromTableForHandlingEmptyStringPrimaryKey()
{
$data = [
Expand All @@ -3023,6 +3029,12 @@ public function testEnsurePrimaryKeyBeingReadFromTableForHandlingEmptyStringPrim
$this->assertNull($result->id);
}

/**
* Test that primary key meta data is being read from the table
* and not the schema reflection when handling belongsToMany associations.
*
* @return void
*/
public function testEnsurePrimaryKeyBeingReadFromTableWhenLoadingBelongsToManyRecordsByPrimaryKey()
{
$data = [
Expand Down Expand Up @@ -3060,6 +3072,13 @@ public function testEnsurePrimaryKeyBeingReadFromTableWhenLoadingBelongsToManyRe
$this->assertEquals($expected, $result->toArray());
}


/**
* Test one() propagates validation errors up.
*
* @return void
* @todo Move to TranslateBehavior test.
*/
public function testMergeTranslationsWithOneMethod()
{
$this->articles->behaviors()->load('Translate', [
Expand Down Expand Up @@ -3092,6 +3111,12 @@ public function testMergeTranslationsWithOneMethod()
$this->assertEquals($data['_translations']['en'], $translations['en']->toArray());
}

/**
* Test one() propagates validation errors up.
*
* @return void
* @todo Move to TranslateBehavior test.
*/
public function testMergeTranslationsWithOneMethodAndReturnErrors()
{
$this->articles->behaviors()->load('Translate', [
Expand Down Expand Up @@ -3132,7 +3157,13 @@ public function testMergeTranslationsWithOneMethodAndReturnErrors()
$this->assertEquals($expected, $errors['_translations']);
}

public function testMergeTransaltionsWithMergeMethod()
/**
* Test merge() creates new translations.
*
* @return void
* @todo Move to TranslateBehavior test.
*/
public function testMergeTranslationsWithMergeMethodNewTranslations()
{
$this->articles->behaviors()->load('Translate', [
'fields' => ['title', 'body']
Expand Down Expand Up @@ -3167,16 +3198,19 @@ public function testMergeTransaltionsWithMergeMethod()
$this->assertEquals($data['_translations']['en'], $translations['en']->toArray());
}

/**
* Test that adding _translations to fieldList allows
* all translated entities in.
*
* @return void
* @todo Move to TranslateBehavior test.
*/
public function testMergeTranslationsWithOneMethodWithFieldList()
{
$this->articles->behaviors()->load('Translate', [
'fields' => ['title', 'body'],
'validator' => 'custom'
]);

$validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
$this->articles->validator('custom', $validator);

$data = [
'author_id' => 1,
'_translations' => [
Expand All @@ -3194,22 +3228,20 @@ public function testMergeTranslationsWithOneMethodWithFieldList()
$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, ['fieldList' => ['author_id', 'title', '_translations']]);

$expected = [
'en' => [
'title' => 'English Title'
],
'es' => [
'title' => 'Titulo Español'
]
];
$translations = $result->get('_translations');

$this->assertTrue($result->has('author_id'));
$this->assertEmpty($result->errors());
$this->assertEquals($expected['en'], $translations['en']->toArray());
$this->assertEquals($expected['es'], $translations['es']->toArray());

$translations = $result->get('_translations');
$this->assertEquals(['title' => 'English Title'], $translations['en']->toArray());
$this->assertEquals(['title' => 'Titulo Español'], $translations['es']->toArray());
}

/**
* Test merge() with translations and failing validation rules.
*
* @return void
* @todo Move to TranslateBehavior test.
*/
public function testMergeTranslationsWithMergeMethodAndReturnErrors()
{
$this->articles->behaviors()->load('Translate', [
Expand Down Expand Up @@ -3246,10 +3278,16 @@ public function testMergeTranslationsWithMergeMethodAndReturnErrors()
]
];
$errors = $result->errors();
$this->assertArrayHasKey('_translations', $errors);
$this->assertEquals($expected, $errors['_translations']);
$this->assertArrayHasKey('_translations', $errors, 'Validation errors should propagate up');
$this->assertEquals($expected, $errors['_translations'], 'Translation validation errors should match');
}

/**
* test merge with translations and passing validation rules applied
*
* @return void
* @todo Move to TranslateBehavior test.
*/
public function testMergeTranslationsWithMergeMethodUpdateFields()
{
$this->articles->behaviors()->load('Translate', [
Expand Down

0 comments on commit c8bf49a

Please sign in to comment.