TranslateTraitTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Model\Behavior\Translate;
  16. use Cake\Model\Behavior\Translate\TranslateTrait;
  17. use Cake\ORM\Entity;
  18. use Cake\TestSuite\TestCase;
  19. class TestEntity extends Entity {
  20. use TranslateTrait;
  21. }
  22. /**
  23. * Translate behavior test case
  24. */
  25. class TranslateTraitTest extends TestCase {
  26. /**
  27. * Tests that missing translation entries are created automatically
  28. *
  29. * @return void
  30. */
  31. public function testTranslationCreate() {
  32. $entity = new TestEntity;
  33. $entity->translation('eng')->set('title', 'My Title');
  34. $this->assertEquals('My Title', $entity->translation('eng')->get('title'));
  35. $this->assertTrue($entity->dirty('_translations'));
  36. $entity->translation('spa')->set('body', 'Contenido');
  37. $this->assertEquals('My Title', $entity->translation('eng')->get('title'));
  38. $this->assertEquals('Contenido', $entity->translation('spa')->get('body'));
  39. }
  40. /**
  41. * Tests that modifying existing translation entries work
  42. *
  43. * @return void
  44. */
  45. public function testTranslationModify() {
  46. $entity = new TestEntity;
  47. $entity->set('_translations', [
  48. 'eng' => new Entity(['title' => 'My Title']),
  49. 'spa' => new Entity(['title' => 'Titulo'])
  50. ]);
  51. $this->assertEquals('My Title', $entity->translation('eng')->get('title'));
  52. $this->assertEquals('Titulo', $entity->translation('spa')->get('title'));
  53. }
  54. /**
  55. * Tests that just accessing the translation will mark the property as dirty, this
  56. * is to facilitate the saving process by not having to remember to mark the property
  57. * manually
  58. *
  59. * @return void
  60. */
  61. public function testTranslationDirty() {
  62. $entity = new TestEntity;
  63. $entity->set('_translations', [
  64. 'eng' => new Entity(['title' => 'My Title']),
  65. 'spa' => new Entity(['title' => 'Titulo'])
  66. ]);
  67. $entity->clean();
  68. $this->assertEquals('My Title', $entity->translation('eng')->get('title'));
  69. $this->assertTrue($entity->dirty('_translations'));
  70. }
  71. }