TranslateBehaviorTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 CakePHP(tm) v 3.0.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Model\Behavior;
  16. use Cake\Event\Event;
  17. use Cake\Model\Behavior\TranslateBehavior;
  18. use Cake\ORM\Entity;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Translate behavior test case
  23. */
  24. class TranslateBehaviorTest extends TestCase {
  25. /**
  26. * fixtures
  27. *
  28. * @var array
  29. */
  30. public $fixtures = [
  31. 'core.translate',
  32. 'core.article'
  33. ];
  34. /**
  35. * Tests that fields from a translated model are overriden
  36. *
  37. * @return void
  38. */
  39. public function testFindSingleLocale() {
  40. $table = TableRegistry::get('Articles');
  41. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  42. $table->locale('eng');
  43. $results = $table->find()->combine('title', 'body', 'id')->toArray();
  44. $expected = [
  45. 1 => ['Title #1' => 'Content #1'],
  46. 2 => ['Title #2' => 'Content #2'],
  47. 3 => ['Title #3' => 'Content #3'],
  48. ];
  49. $this->assertSame($expected, $results);
  50. }
  51. /**
  52. * Tests that overriding fields with the translate behavior works when
  53. * using conditions and that all other columns are preserved
  54. *
  55. * @return void
  56. */
  57. public function testFindSingleLocaleWithConditions() {
  58. $table = TableRegistry::get('Articles');
  59. $table->addBehavior('Translate');
  60. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  61. $table->locale('eng');
  62. $results = $table->find()
  63. ->where(['Articles.id' => 2])
  64. ->all();
  65. $this->assertCount(1, $results);
  66. $row = $results->first();
  67. $expected = [
  68. 'id' => 2,
  69. 'title' => 'Title #2',
  70. 'body' => 'Content #2',
  71. 'author_id' => 3,
  72. 'published' => 'Y',
  73. '_locale' => 'eng'
  74. ];
  75. $this->assertEquals($expected, $row->toArray());
  76. }
  77. }