TranslateTraitTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM\Behavior\Translate;
  16. use Cake\ORM\Behavior\Translate\TranslateTrait;
  17. use Cake\ORM\Entity;
  18. use Cake\TestSuite\TestCase;
  19. class TestEntity extends Entity
  20. {
  21. use TranslateTrait;
  22. }
  23. /**
  24. * Translate behavior test case
  25. */
  26. class TranslateTraitTest extends TestCase
  27. {
  28. /**
  29. * Tests that missing translation entries are created automatically
  30. *
  31. * @return void
  32. */
  33. public function testTranslationCreate()
  34. {
  35. $entity = new TestEntity();
  36. $entity->translation('eng')->set('title', 'My Title');
  37. $this->assertEquals('My Title', $entity->translation('eng')->get('title'));
  38. $this->assertTrue($entity->isDirty('_translations'));
  39. $entity->translation('spa')->set('body', 'Contenido');
  40. $this->assertEquals('My Title', $entity->translation('eng')->get('title'));
  41. $this->assertEquals('Contenido', $entity->translation('spa')->get('body'));
  42. }
  43. /**
  44. * Tests that modifying existing translation entries work
  45. *
  46. * @return void
  47. */
  48. public function testTranslationModify()
  49. {
  50. $entity = new TestEntity();
  51. $entity->set('_translations', [
  52. 'eng' => new Entity(['title' => 'My Title']),
  53. 'spa' => new Entity(['title' => 'Titulo']),
  54. ]);
  55. $this->assertEquals('My Title', $entity->translation('eng')->get('title'));
  56. $this->assertEquals('Titulo', $entity->translation('spa')->get('title'));
  57. }
  58. /**
  59. * Tests empty translations.
  60. *
  61. * @return void
  62. */
  63. public function testTranslationEmpty()
  64. {
  65. $entity = new TestEntity();
  66. $entity->set('_translations', [
  67. 'eng' => new Entity(['title' => 'My Title']),
  68. 'spa' => new Entity(['title' => 'Titulo']),
  69. ]);
  70. $this->assertTrue($entity->translation('pol')->isNew());
  71. $this->assertInstanceOf('Cake\Test\TestCase\ORM\Behavior\Translate\TestEntity', $entity->translation('pol'));
  72. }
  73. /**
  74. * Tests that just accessing the translation will mark the property as dirty, this
  75. * is to facilitate the saving process by not having to remember to mark the property
  76. * manually
  77. *
  78. * @return void
  79. */
  80. public function testTranslationDirty()
  81. {
  82. $entity = new TestEntity();
  83. $entity->set('_translations', [
  84. 'eng' => new Entity(['title' => 'My Title']),
  85. 'spa' => new Entity(['title' => 'Titulo']),
  86. ]);
  87. $entity->clean();
  88. $this->assertEquals('My Title', $entity->translation('eng')->get('title'));
  89. $this->assertTrue($entity->isDirty('_translations'));
  90. }
  91. }