Browse Source

Move and update tests for TranslateBehavior marshalling.

Add tests for TranslateBehavior::buildMarshalMap()
Mark Story 9 years ago
parent
commit
2c8352aaa0
2 changed files with 293 additions and 255 deletions
  1. 217 0
      tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
  2. 76 255
      tests/TestCase/ORM/MarshallerTest.php

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

@@ -20,6 +20,7 @@ use Cake\ORM\Behavior\Translate\TranslateTrait;
 use Cake\ORM\Entity;
 use Cake\ORM\TableRegistry;
 use Cake\TestSuite\TestCase;
+use Cake\Validation\Validator;
 
 /**
  * Stub entity class
@@ -1159,4 +1160,220 @@ class TranslateBehaviorTest extends TestCase
         $this->assertEquals('First Article1', $results['eng']['title']);
         $this->assertEquals('Description #1', $results['eng']['description']);
     }
+
+    /**
+     * Test that no properties are enabled when the translations
+     * option is off.
+     *
+     * @return void
+     */
+    public function testBuildMarshalMapTranslationsOff()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
+
+        $marshaller = $table->marshaller();
+        $translate = $table->behaviors()->get('Translate');
+        $result = $translate->buildMarhshalMap($marshaller, [], ['translations' => false]);
+        $this->assertSame([], $result);
+    }
+
+    /**
+     * Test building a marshal map with translations on.
+     *
+     * @return void
+     */
+    public function testBuildMarshalMapTranslationsOn()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
+        $marshaller = $table->marshaller();
+        $translate = $table->behaviors()->get('Translate');
+
+        $result = $translate->buildMarhshalMap($marshaller, [], ['translations' => true]);
+        $this->assertArrayHasKey('_translations', $result);
+        $this->assertInstanceOf('Closure', $result['_translations']);
+
+        $result = $translate->buildMarhshalMap($marshaller, [], []);
+        $this->assertArrayHasKey('_translations', $result);
+        $this->assertInstanceOf('Closure', $result['_translations']);
+    }
+
+    /**
+     * Test marshalling non-array data
+     *
+     * @return void
+     */
+    public function testBuildMarshalMapNonArrayData()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
+        $translate = $table->behaviors()->get('Translate');
+
+        $map = $translate->buildMarhshalMap($table->marshaller(), [], []);
+        $entity = $table->newEntity();
+        $result = $map['_translations']('garbage', $entity);
+        $this->assertNull($result, 'Non-array should not error out.');
+        $this->assertEmpty($entity->errors());
+        $this->assertEmpty($entity->get('_translations'));
+    }
+
+    /**
+     * Test buildMarshalMap() builds new entities.
+     *
+     * @return void
+     */
+    public function testBuildMarshalMapBuildEntities()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
+        $translate = $table->behaviors()->get('Translate');
+
+        $map = $translate->buildMarhshalMap($table->marshaller(), [], []);
+        $entity = $table->newEntity();
+        $data = [
+            'en' => [
+                'title' => 'English Title',
+                'body' => 'English Content'
+            ],
+            'es' => [
+                'title' => 'Titulo Español',
+                'body' => 'Contenido Español'
+            ]
+        ];
+        $result = $map['_translations']($data, $entity);
+        $this->assertEmpty($entity->errors(), 'No validation errors.');
+        $this->assertCount(2, $result);
+        $this->assertArrayHasKey('en', $result);
+        $this->assertArrayHasKey('es', $result);
+        $this->assertEquals('English Title', $result['en']->title);
+        $this->assertEquals('Titulo Español', $result['es']->title);
+    }
+
+    /**
+     * Test that validation errors are added to the original entity.
+     *
+     * @return void
+     */
+    public function testBuildMarshalMapBuildEntitiesValidationErrors()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->addBehavior('Translate', [
+            'fields' => ['title', 'body'],
+            'validator' => 'custom'
+        ]);
+        $validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
+        $table->validator('custom', $validator);
+        $translate = $table->behaviors()->get('Translate');
+
+        $entity = $table->newEntity();
+        $map = $translate->buildMarhshalMap($table->marshaller(), [], []);
+        $data = [
+            'en' => [
+                'title' => 'English Title',
+                'body' => 'English Content'
+            ],
+            'es' => [
+                'title' => '',
+                'body' => 'Contenido Español'
+            ]
+        ];
+        $result = $map['_translations']($data, $entity);
+        $this->assertNotEmpty($entity->errors(), 'Needs validation errors.');
+        $expected = [
+            'title' => [
+                '_empty' => 'This field cannot be left empty'
+            ]
+        ];
+        $this->assertEquals($expected, $entity->errors('es'));
+
+        $this->assertEquals('English Title', $result['en']->title);
+        $this->assertEquals('', $result['es']->title);
+    }
+
+    /**
+     * Test that marshalling updates existing translation entities.
+     *
+     * @return void
+     */
+    public function testBuildMarshalMapUpdateExistingEntities()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->addBehavior('Translate', [
+            'fields' => ['title', 'body'],
+        ]);
+        $translate = $table->behaviors()->get('Translate');
+
+        $entity = $table->newEntity();
+        $es = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
+        $en = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
+        $entity->set('_translations', [
+            'es' => $es,
+            'en' => $en,
+        ]);
+        $map = $translate->buildMarhshalMap($table->marshaller(), [], []);
+        $data = [
+            'en' => [
+                'title' => 'English Title',
+            ],
+            'es' => [
+                'title' => 'Spanish Title',
+            ]
+        ];
+        $result = $map['_translations']($data, $entity);
+        $this->assertEmpty($entity->errors(), 'No validation errors.');
+        $this->assertSame($en, $result['en']);
+        $this->assertSame($es, $result['es']);
+        $this->assertSame($en, $entity->get('_translations')['en']);
+        $this->assertSame($es, $entity->get('_translations')['es']);
+
+        $this->assertEquals('English Title', $result['en']->title);
+        $this->assertEquals('Spanish Title', $result['es']->title);
+        $this->assertEquals('Old body', $result['en']->body);
+        $this->assertEquals('Old body', $result['es']->body);
+    }
+
+    /**
+     * Test that updating translation records works with validations.
+     *
+     * @return void
+     */
+    public function testBuildMarshalMapUpdateEntitiesValidationErrors()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->addBehavior('Translate', [
+            'fields' => ['title', 'body'],
+            'validator' => 'custom'
+        ]);
+        $validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
+        $table->validator('custom', $validator);
+        $translate = $table->behaviors()->get('Translate');
+
+        $entity = $table->newEntity();
+        $es = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
+        $en = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
+        $entity->set('_translations', [
+            'es' => $es,
+            'en' => $en,
+        ]);
+        $map = $translate->buildMarhshalMap($table->marshaller(), [], []);
+        $data = [
+            'en' => [
+                'title' => 'English Title',
+                'body' => 'English Content'
+            ],
+            'es' => [
+                'title' => '',
+                'body' => 'Contenido Español'
+            ]
+        ];
+        $result = $map['_translations']($data, $entity);
+        $this->assertNotEmpty($entity->errors(), 'Needs validation errors.');
+        $expected = [
+            'title' => [
+                '_empty' => 'This field cannot be left empty'
+            ]
+        ];
+        $this->assertEquals($expected, $entity->errors('es'));
+    }
 }

+ 76 - 255
tests/TestCase/ORM/MarshallerTest.php

@@ -2370,6 +2370,42 @@ class MarshallerTest extends TestCase
     }
 
     /**
+     * Test one() with translations
+     *
+     * @return void
+     */
+    public function testOneWithTranslations()
+    {
+        $this->articles->addBehavior('Translate', [
+            'fields' => ['title', 'body']
+        ]);
+
+        $data = [
+            'author_id' => 1,
+            '_translations' => [
+                'en' => [
+                    'title' => 'English Title',
+                    'body' => 'English Content'
+                ],
+                'es' => [
+                    'title' => 'Titulo Español',
+                    'body' => 'Contenido Español'
+                ]
+            ]
+        ];
+
+        $marshall = new Marshaller($this->articles);
+        $result = $marshall->one($data, []);
+        $this->assertEmpty($result->errors());
+
+        $translations = $result->get('_translations');
+        $this->assertCount(2, $translations);
+        $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['en']);
+        $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['es']);
+        $this->assertEquals($data['_translations']['en'], $translations['en']->toArray());
+    }
+
+    /**
      * Tests that it is possible to pass a fieldList option to the merge method
      *
      * @return void
@@ -2884,6 +2920,46 @@ class MarshallerTest extends TestCase
     }
 
     /**
+     * Test merge() with translate behavior integration
+     *
+     * @return void
+     */
+    public function testMergeWithTranslations()
+    {
+        $this->articles->addBehavior('Translate', [
+            'fields' => ['title', 'body']
+        ]);
+
+        $data = [
+            'author_id' => 1,
+            '_translations' => [
+                'en' => [
+                    'title' => 'English Title',
+                    'body' => 'English Content'
+                ],
+                'es' => [
+                    'title' => 'Titulo Español',
+                    'body' => 'Contenido Español'
+                ]
+            ]
+        ];
+
+        $marshall = new Marshaller($this->articles);
+        $entity = $this->articles->newEntity();
+        $result = $marshall->merge($entity, $data, []);
+
+        $this->assertSame($entity, $result);
+        $this->assertEmpty($result->errors());
+        $this->assertTrue($result->dirty('_translations'));
+
+        $translations = $result->get('_translations');
+        $this->assertCount(2, $translations);
+        $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['en']);
+        $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['es']);
+        $this->assertEquals($data['_translations']['en'], $translations['en']->toArray());
+    }
+
+    /**
      * Test Model.beforeMarshal event.
      *
      * @return void
@@ -3072,259 +3148,4 @@ class MarshallerTest extends TestCase
         $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', [
-            'fields' => ['title', 'body']
-        ]);
-
-        $data = [
-            'author_id' => 1,
-            '_translations' => [
-                'en' => [
-                    'title' => 'English Title',
-                    'body' => 'English Content'
-                ],
-                'es' => [
-                    'title' => 'Titulo Español',
-                    'body' => 'Contenido Español'
-                ]
-            ]
-        ];
-
-        $marshall = new Marshaller($this->articles);
-        $result = $marshall->one($data, []);
-
-        $translations = $result->get('_translations');
-
-        $this->assertEmpty($result->errors());
-        $this->assertCount(2, $translations);
-        $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['en']);
-        $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['es']);
-        $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', [
-            'fields' => ['title', 'body'],
-            'validator' => 'custom'
-        ]);
-
-        $validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
-        $this->articles->validator('custom', $validator);
-
-        $data = [
-            'author_id' => 1,
-            '_translations' => [
-                'en' => [
-                    'title' => 'English Title',
-                    'body' => 'English Content'
-                ],
-                'es' => [
-                    'title' => '',
-                    'body' => ''
-                ]
-            ]
-        ];
-
-        $marshall = new Marshaller($this->articles);
-        $result = $marshall->one($data, []);
-
-        $expected = [
-            'es' => [
-                'title' => [
-                    '_empty' => 'This field cannot be left empty'
-                ]
-            ]
-        ];
-        $errors = $result->errors();
-        $this->assertNotEmpty($errors);
-        $this->assertArrayHasKey('_translations', $errors);
-        $this->assertEquals($expected, $errors['_translations']);
-    }
-
-    /**
-     * Test merge() creates new translations.
-     *
-     * @return void
-     * @todo Move to TranslateBehavior test.
-     */
-    public function testMergeTranslationsWithMergeMethodNewTranslations()
-    {
-        $this->articles->behaviors()->load('Translate', [
-            'fields' => ['title', 'body']
-        ]);
-
-        $data = [
-            'author_id' => 1,
-            '_translations' => [
-                'en' => [
-                    'title' => 'English Title',
-                    'body' => 'English Content'
-                ],
-                'es' => [
-                    'title' => 'Titulo Español',
-                    'body' => 'Contenido Español'
-                ]
-            ]
-        ];
-
-        $marshall = new Marshaller($this->articles);
-        $entity = $this->articles->newEntity();
-        $result = $marshall->merge($entity, $data, []);
-
-        $translations = $result->get('_translations');
-
-        $this->assertSame($entity, $result);
-        $this->assertEmpty($result->errors());
-        $this->assertTrue($result->dirty('_translations'));
-        $this->assertCount(2, $translations);
-        $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['en']);
-        $this->assertInstanceOf(__NAMESPACE__ . '\OpenEntity', $translations['es']);
-        $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'],
-        ]);
-
-        $data = [
-            'author_id' => 1,
-            '_translations' => [
-                'en' => [
-                    'title' => 'English Title',
-                    'body' => 'English Content'
-                ],
-                'es' => [
-                    'title' => 'Titulo Español',
-                    'body' => ''
-                ]
-            ]
-        ];
-
-        $marshall = new Marshaller($this->articles);
-        $result = $marshall->one($data, ['fieldList' => ['author_id', 'title', '_translations']]);
-
-        $this->assertTrue($result->has('author_id'));
-        $this->assertEmpty($result->errors());
-
-        $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', [
-            'fields' => ['title', 'body'],
-            'validator' => 'custom'
-        ]);
-
-        $validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
-        $this->articles->validator('custom', $validator);
-
-        $data = [
-            'author_id' => 1,
-            '_translations' => [
-                'en' => [
-                    'title' => 'English Title',
-                    'body' => 'English Content'
-                ],
-                'es' => [
-                    'title' => '',
-                    'body' => ''
-                ]
-            ]
-        ];
-
-        $marshall = new Marshaller($this->articles);
-        $entity = $this->articles->newEntity();
-        $result = $marshall->merge($entity, $data, []);
-
-        $expected = [
-            'es' => [
-                'title' => [
-                    '_empty' => 'This field cannot be left empty'
-                ]
-            ]
-        ];
-        $errors = $result->errors();
-        $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', [
-            'fields' => ['title', 'body'],
-            'validator' => 'custom'
-        ]);
-
-        $validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
-        $this->articles->validator('custom', $validator);
-
-        $data = [
-            'author_id' => 1,
-            '_translations' => [
-                'en' => [
-                    'title' => 'English Title',
-                    'body' => 'English Content'
-                ],
-                'es' => [
-                    'title' => 'Titulo Español',
-                    'body' => ''
-                ]
-            ]
-        ];
-        $marshall = new Marshaller($this->articles);
-        $entity = $this->articles->newEntity($data);
-
-        $updateData = [
-            '_translations' => [
-                'es' => [
-                    'body' => 'Contenido Español'
-                ]
-            ]
-        ];
-        $result = $marshall->merge($entity, $updateData, []);
-
-        $entityData = $result->toArray();
-        $this->assertEquals('Contenido Español', $entityData['_translations']['es']['body']);
-        $this->assertEmpty($result->errors());
-    }
 }