~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/libs/model/behaviors/translate.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * TranslateBehaviorTest file
 
4
 *
 
5
 * PHP versions 4 and 5
 
6
 *
 
7
 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
 
8
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
9
 *
 
10
 *  Licensed under The Open Group Test Suite License
 
11
 *  Redistributions of files must retain the above copyright notice.
 
12
 *
 
13
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
14
 * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
 
15
 * @package       cake
 
16
 * @subpackage    cake.tests.cases.libs.model.behaviors
 
17
 * @since         CakePHP(tm) v 1.2.0.5669
 
18
 * @license       http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
 
19
 */
 
20
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
 
21
        define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
 
22
}
 
23
 
 
24
App::import('Core', array('AppModel', 'Model'));
 
25
require_once(dirname(dirname(__FILE__)) . DS . 'models.php');
 
26
 
 
27
/**
 
28
 * TranslateBehaviorTest class
 
29
 *
 
30
 * @package       cake
 
31
 * @subpackage    cake.tests.cases.libs.model.behaviors
 
32
 */
 
33
class TranslateBehaviorTest extends CakeTestCase {
 
34
 
 
35
/**
 
36
 * autoFixtures property
 
37
 *
 
38
 * @var bool false
 
39
 * @access public
 
40
 */
 
41
        var $autoFixtures = false;
 
42
 
 
43
/**
 
44
 * fixtures property
 
45
 *
 
46
 * @var array
 
47
 * @access public
 
48
 */
 
49
        var $fixtures = array(
 
50
                'core.translated_item', 'core.translate', 'core.translate_table',
 
51
                'core.translated_article', 'core.translate_article', 'core.user', 'core.comment', 'core.tag', 'core.articles_tag',
 
52
                'core.translate_with_prefix'
 
53
        );
 
54
 
 
55
/**
 
56
 * endTest method
 
57
 *
 
58
 * @access public
 
59
 * @return void
 
60
 */
 
61
        function endTest() {
 
62
                ClassRegistry::flush();
 
63
        }
 
64
 
 
65
/**
 
66
 * testTranslateModel method
 
67
 *
 
68
 * @access public
 
69
 * @return void
 
70
 */
 
71
        function testTranslateModel() {
 
72
                $TestModel =& new Tag();
 
73
                $TestModel->translateTable = 'another_i18n';
 
74
                $TestModel->Behaviors->attach('Translate', array('title'));
 
75
                $translateModel =& $TestModel->Behaviors->Translate->translateModel($TestModel);
 
76
                $this->assertEqual($translateModel->name, 'I18nModel');
 
77
                $this->assertEqual($translateModel->useTable, 'another_i18n');
 
78
 
 
79
                $TestModel =& new User();
 
80
                $TestModel->Behaviors->attach('Translate', array('title'));
 
81
                $translateModel =& $TestModel->Behaviors->Translate->translateModel($TestModel);
 
82
                $this->assertEqual($translateModel->name, 'I18nModel');
 
83
                $this->assertEqual($translateModel->useTable, 'i18n');
 
84
 
 
85
                $TestModel =& new TranslatedArticle();
 
86
                $translateModel =& $TestModel->Behaviors->Translate->translateModel($TestModel);
 
87
                $this->assertEqual($translateModel->name, 'TranslateArticleModel');
 
88
                $this->assertEqual($translateModel->useTable, 'article_i18n');
 
89
 
 
90
                $TestModel =& new TranslatedItem();
 
91
                $translateModel =& $TestModel->Behaviors->Translate->translateModel($TestModel);
 
92
                $this->assertEqual($translateModel->name, 'TranslateTestModel');
 
93
                $this->assertEqual($translateModel->useTable, 'i18n');
 
94
        }
 
95
 
 
96
/**
 
97
 * testLocaleFalsePlain method
 
98
 *
 
99
 * @access public
 
100
 * @return void
 
101
 */
 
102
        function testLocaleFalsePlain() {
 
103
                $this->loadFixtures('Translate', 'TranslatedItem');
 
104
 
 
105
                $TestModel =& new TranslatedItem();
 
106
                $TestModel->locale = false;
 
107
 
 
108
                $result = $TestModel->read(null, 1);
 
109
                $expected = array('TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'));
 
110
                $this->assertEqual($result, $expected);
 
111
 
 
112
                $result = $TestModel->find('all', array('fields' => array('slug')));
 
113
                $expected = array(
 
114
                        array('TranslatedItem' => array('slug' => 'first_translated')),
 
115
                        array('TranslatedItem' => array('slug' => 'second_translated')),
 
116
                        array('TranslatedItem' => array('slug' => 'third_translated'))
 
117
                );
 
118
                $this->assertEqual($result, $expected);
 
119
        }
 
120
 
 
121
/**
 
122
 * testLocaleFalseAssociations method
 
123
 *
 
124
 * @access public
 
125
 * @return void
 
126
 */
 
127
        function testLocaleFalseAssociations() {
 
128
                $this->loadFixtures('Translate', 'TranslatedItem');
 
129
 
 
130
                $TestModel =& new TranslatedItem();
 
131
                $TestModel->locale = false;
 
132
                $TestModel->unbindTranslation();
 
133
                $translations = array('title' => 'Title', 'content' => 'Content');
 
134
                $TestModel->bindTranslation($translations, false);
 
135
 
 
136
                $result = $TestModel->read(null, 1);
 
137
                $expected = array(
 
138
                        'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'),
 
139
                        'Title' => array(
 
140
                                array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'),
 
141
                                array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
 
142
                                array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1')
 
143
                        ),
 
144
                        'Content' => array(
 
145
                                array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
 
146
                                array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
 
147
                                array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1')
 
148
                        )
 
149
                );
 
150
                $this->assertEqual($result, $expected);
 
151
 
 
152
                $TestModel->hasMany['Title']['fields'] = $TestModel->hasMany['Content']['fields'] = array('content');
 
153
                $TestModel->hasMany['Title']['conditions']['locale'] = $TestModel->hasMany['Content']['conditions']['locale'] = 'eng';
 
154
 
 
155
                $result = $TestModel->find('all', array('fields' => array('TranslatedItem.slug')));
 
156
                $expected = array(
 
157
                        array(
 
158
                                'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'),
 
159
                                'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')),
 
160
                                'Content' => array(array('foreign_key' => 1, 'content' => 'Content #1'))
 
161
                        ),
 
162
                        array(
 
163
                                'TranslatedItem' => array('id' => 2, 'slug' => 'second_translated'),
 
164
                                'Title' => array(array('foreign_key' => 2, 'content' => 'Title #2')),
 
165
                                'Content' => array(array('foreign_key' => 2, 'content' => 'Content #2'))
 
166
                        ),
 
167
                        array(
 
168
                                'TranslatedItem' => array('id' => 3, 'slug' => 'third_translated'),
 
169
                                'Title' => array(array('foreign_key' => 3, 'content' => 'Title #3')),
 
170
                                'Content' => array(array('foreign_key' => 3, 'content' => 'Content #3'))
 
171
                        )
 
172
                );
 
173
                $this->assertEqual($result, $expected);
 
174
        }
 
175
 
 
176
/**
 
177
 * testLocaleSingle method
 
178
 *
 
179
 * @access public
 
180
 * @return void
 
181
 */
 
182
        function testLocaleSingle() {
 
183
                $this->loadFixtures('Translate', 'TranslatedItem');
 
184
 
 
185
                $TestModel =& new TranslatedItem();
 
186
                $TestModel->locale = 'eng';
 
187
                $result = $TestModel->read(null, 1);
 
188
                $expected = array(
 
189
                        'TranslatedItem' => array(
 
190
                                'id' => 1,
 
191
                                'slug' => 'first_translated',
 
192
                                'locale' => 'eng',
 
193
                                'title' => 'Title #1',
 
194
                                'content' => 'Content #1'
 
195
                        )
 
196
                );
 
197
                $this->assertEqual($result, $expected);
 
198
 
 
199
                $result = $TestModel->find('all');
 
200
                $expected = array(
 
201
                        array(
 
202
                                'TranslatedItem' => array(
 
203
                                        'id' => 1,
 
204
                                        'slug' => 'first_translated',
 
205
                                        'locale' => 'eng',
 
206
                                        'title' => 'Title #1',
 
207
                                        'content' => 'Content #1'
 
208
                                )
 
209
                        ),
 
210
                        array(
 
211
                                'TranslatedItem' => array(
 
212
                                        'id' => 2,
 
213
                                        'slug' => 'second_translated',
 
214
                                        'locale' => 'eng',
 
215
                                        'title' => 'Title #2',
 
216
                                        'content' => 'Content #2'
 
217
                                )
 
218
                        ),
 
219
                        array(
 
220
                                'TranslatedItem' => array(
 
221
                                        'id' => 3,
 
222
                                        'slug' => 'third_translated',
 
223
                                        'locale' => 'eng',
 
224
                                        'title' => 'Title #3',
 
225
                                        'content' => 'Content #3'
 
226
                                )
 
227
                        )
 
228
                );
 
229
                $this->assertEqual($result, $expected);
 
230
        }
 
231
 
 
232
/**
 
233
 * testLocaleSingleWithConditions method
 
234
 *
 
235
 * @access public
 
236
 * @return void
 
237
 */
 
238
        function testLocaleSingleWithConditions() {
 
239
                $this->loadFixtures('Translate', 'TranslatedItem');
 
240
 
 
241
                $TestModel =& new TranslatedItem();
 
242
                $TestModel->locale = 'eng';
 
243
                $result = $TestModel->find('all', array('conditions' => array('slug' => 'first_translated')));
 
244
                $expected = array(
 
245
                        array(
 
246
                                'TranslatedItem' => array(
 
247
                                        'id' => 1,
 
248
                                        'slug' => 'first_translated',
 
249
                                        'locale' => 'eng',
 
250
                                        'title' => 'Title #1',
 
251
                                        'content' => 'Content #1'
 
252
                                )
 
253
                        )
 
254
                );
 
255
                $this->assertEqual($result, $expected);
 
256
 
 
257
                $result = $TestModel->find('all', array('conditions' => "TranslatedItem.slug = 'first_translated'"));
 
258
                $expected = array(
 
259
                        array(
 
260
                                'TranslatedItem' => array(
 
261
                                        'id' => 1,
 
262
                                        'slug' => 'first_translated',
 
263
                                        'locale' => 'eng',
 
264
                                        'title' => 'Title #1',
 
265
                                        'content' => 'Content #1'
 
266
                                )
 
267
                        )
 
268
                );
 
269
                $this->assertEqual($result, $expected);
 
270
        }
 
271
 
 
272
/**
 
273
 * testLocaleSingleAssociations method
 
274
 *
 
275
 * @access public
 
276
 * @return void
 
277
 */
 
278
        function testLocaleSingleAssociations() {
 
279
                $this->loadFixtures('Translate', 'TranslatedItem');
 
280
 
 
281
                $TestModel =& new TranslatedItem();
 
282
                $TestModel->locale = 'eng';
 
283
                $TestModel->unbindTranslation();
 
284
                $translations = array('title' => 'Title', 'content' => 'Content');
 
285
                $TestModel->bindTranslation($translations, false);
 
286
 
 
287
                $result = $TestModel->read(null, 1);
 
288
                $expected = array(
 
289
                        'TranslatedItem' => array(
 
290
                                'id' => 1,
 
291
                                'slug' => 'first_translated',
 
292
                                'locale' => 'eng',
 
293
                                'title' => 'Title #1',
 
294
                                'content' => 'Content #1'
 
295
                        ),
 
296
                        'Title' => array(
 
297
                                array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'),
 
298
                                array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
 
299
                                array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1')
 
300
                        ),
 
301
                        'Content' => array(
 
302
                                array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
 
303
                                array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
 
304
                                array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1')
 
305
                        )
 
306
                );
 
307
                $this->assertEqual($result, $expected);
 
308
 
 
309
                $TestModel->hasMany['Title']['fields'] = $TestModel->hasMany['Content']['fields'] = array('content');
 
310
                $TestModel->hasMany['Title']['conditions']['locale'] = $TestModel->hasMany['Content']['conditions']['locale'] = 'eng';
 
311
 
 
312
                $result = $TestModel->find('all', array('fields' => array('TranslatedItem.title')));
 
313
                $expected = array(
 
314
                        array(
 
315
                                'TranslatedItem' => array('id' => 1, 'locale' => 'eng', 'title' => 'Title #1'),
 
316
                                'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')),
 
317
                                'Content' => array(array('foreign_key' => 1, 'content' => 'Content #1'))
 
318
                        ),
 
319
                        array(
 
320
                                'TranslatedItem' => array('id' => 2, 'locale' => 'eng', 'title' => 'Title #2'),
 
321
                                'Title' => array(array('foreign_key' => 2, 'content' => 'Title #2')),
 
322
                                'Content' => array(array('foreign_key' => 2, 'content' => 'Content #2'))
 
323
                        ),
 
324
                        array(
 
325
                                'TranslatedItem' => array('id' => 3, 'locale' => 'eng', 'title' => 'Title #3'),
 
326
                                'Title' => array(array('foreign_key' => 3, 'content' => 'Title #3')),
 
327
                                'Content' => array(array('foreign_key' => 3, 'content' => 'Content #3'))
 
328
                        )
 
329
                );
 
330
                $this->assertEqual($result, $expected);
 
331
        }
 
332
 
 
333
/**
 
334
 * testLocaleMultiple method
 
335
 *
 
336
 * @access public
 
337
 * @return void
 
338
 */
 
339
        function testLocaleMultiple() {
 
340
                $this->loadFixtures('Translate', 'TranslatedItem');
 
341
 
 
342
                $TestModel =& new TranslatedItem();
 
343
                $TestModel->locale = array('deu', 'eng', 'cze');
 
344
                $delete = array(
 
345
                        array('locale' => 'deu'),
 
346
                        array('foreign_key' => 1, 'field' => 'title', 'locale' => 'eng'),
 
347
                        array('foreign_key' => 1, 'field' => 'content', 'locale' => 'cze'),
 
348
                        array('foreign_key' => 2, 'field' => 'title', 'locale' => 'cze'),
 
349
                        array('foreign_key' => 2, 'field' => 'content', 'locale' => 'eng'),
 
350
                        array('foreign_key' => 3, 'field' => 'title')
 
351
                );
 
352
                $I18nModel =& ClassRegistry::getObject('TranslateTestModel');
 
353
                $I18nModel->deleteAll(array('or' => $delete));
 
354
 
 
355
                $result = $TestModel->read(null, 1);
 
356
                $expected = array(
 
357
                        'TranslatedItem' => array(
 
358
                                'id' => 1,
 
359
                                'slug' => 'first_translated',
 
360
                                'locale' => 'deu',
 
361
                                'title' => 'Titulek #1',
 
362
                                'content' => 'Content #1'
 
363
                        )
 
364
                );
 
365
                $this->assertEqual($result, $expected);
 
366
 
 
367
                $result = $TestModel->find('all', array('fields' => array('slug', 'title', 'content')));
 
368
                $expected = array(
 
369
                        array(
 
370
                                'TranslatedItem' => array(
 
371
                                        'slug' => 'first_translated',
 
372
                                        'locale' => 'deu',
 
373
                                        'title' => 'Titulek #1',
 
374
                                        'content' => 'Content #1'
 
375
                                )
 
376
                        ),
 
377
                        array(
 
378
                                'TranslatedItem' => array(
 
379
                                        'slug' => 'second_translated',
 
380
                                        'locale' => 'deu',
 
381
                                        'title' => 'Title #2',
 
382
                                        'content' => 'Obsah #2'
 
383
                                )
 
384
                        ),
 
385
                        array(
 
386
                                'TranslatedItem' => array(
 
387
                                        'slug' => 'third_translated',
 
388
                                        'locale' => 'deu',
 
389
                                        'title' => '',
 
390
                                        'content' => 'Content #3'
 
391
                                )
 
392
                        )
 
393
                );
 
394
                $this->assertEqual($result, $expected);
 
395
        }
 
396
 
 
397
/**
 
398
 * testMissingTranslation method
 
399
 *
 
400
 * @access public
 
401
 * @return void
 
402
 */
 
403
        function testMissingTranslation() {
 
404
                $this->loadFixtures('Translate', 'TranslatedItem');
 
405
 
 
406
                $TestModel =& new TranslatedItem();
 
407
                $TestModel->locale = 'rus';
 
408
                $result = $TestModel->read(null, 1);
 
409
                $this->assertFalse($result);
 
410
 
 
411
                $TestModel->locale = array('rus');
 
412
                $result = $TestModel->read(null, 1);
 
413
                $expected = array(
 
414
                        'TranslatedItem' => array(
 
415
                                'id' => 1,
 
416
                                'slug' => 'first_translated',
 
417
                                'locale' => 'rus',
 
418
                                'title' => '',
 
419
                                'content' => ''
 
420
                        )
 
421
                );
 
422
                $this->assertEqual($result, $expected);
 
423
        }
 
424
 
 
425
/**
 
426
 * testTranslatedFindList method
 
427
 *
 
428
 * @access public
 
429
 * @return void
 
430
 */
 
431
        function testTranslatedFindList() {
 
432
                $this->loadFixtures('Translate', 'TranslatedItem');
 
433
 
 
434
                $TestModel =& new TranslatedItem();
 
435
                $TestModel->locale = 'deu';
 
436
                $TestModel->displayField = 'title';
 
437
                $result = $TestModel->find('list', array('recursive' => 1));
 
438
                $expected = array(1 => 'Titel #1', 2 => 'Titel #2', 3 => 'Titel #3');
 
439
                $this->assertEqual($result, $expected);
 
440
 
 
441
                // MSSQL trigger an error and stops the page even if the debug = 0
 
442
                if ($this->db->config['driver'] != 'mssql') {
 
443
                        $debug = Configure::read('debug');
 
444
                        Configure::write('debug', 0);
 
445
 
 
446
                        $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => false));
 
447
                        $this->assertEqual($result, array());
 
448
 
 
449
                        $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'after'));
 
450
                        $this->assertEqual($result, array());
 
451
                        Configure::write('debug', $debug);
 
452
                }
 
453
 
 
454
                $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'before'));
 
455
                $expected = array(1 => null, 2 => null, 3 => null);
 
456
                $this->assertEqual($result, $expected);
 
457
        }
 
458
 
 
459
/**
 
460
 * testReadSelectedFields method
 
461
 *
 
462
 * @access public
 
463
 * @return void
 
464
 */
 
465
        function testReadSelectedFields() {
 
466
                $this->loadFixtures('Translate', 'TranslatedItem');
 
467
 
 
468
                $TestModel =& new TranslatedItem();
 
469
                $TestModel->locale = 'eng';
 
470
                $result = $TestModel->find('all', array('fields' => array('slug', 'TranslatedItem.content')));
 
471
                $expected = array(
 
472
                        array('TranslatedItem' => array('slug' => 'first_translated', 'locale' => 'eng', 'content' => 'Content #1')),
 
473
                        array('TranslatedItem' => array('slug' => 'second_translated', 'locale' => 'eng', 'content' => 'Content #2')),
 
474
                        array('TranslatedItem' => array('slug' => 'third_translated', 'locale' => 'eng', 'content' => 'Content #3'))
 
475
                );
 
476
                $this->assertEqual($result, $expected);
 
477
 
 
478
                $result = $TestModel->find('all', array('fields' => array('TranslatedItem.slug', 'content')));
 
479
                $this->assertEqual($result, $expected);
 
480
 
 
481
                $TestModel->locale = array('eng', 'deu', 'cze');
 
482
                $delete = array(array('locale' => 'deu'), array('field' => 'content', 'locale' => 'eng'));
 
483
                $I18nModel =& ClassRegistry::getObject('TranslateTestModel');
 
484
                $I18nModel->deleteAll(array('or' => $delete));
 
485
 
 
486
                $result = $TestModel->find('all', array('fields' => array('title', 'content')));
 
487
                $expected = array(
 
488
                        array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #1', 'content' => 'Obsah #1')),
 
489
                        array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #2', 'content' => 'Obsah #2')),
 
490
                        array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #3', 'content' => 'Obsah #3'))
 
491
                );
 
492
                $this->assertEqual($result, $expected);
 
493
        }
 
494
 
 
495
/**
 
496
 * testSaveCreate method
 
497
 *
 
498
 * @access public
 
499
 * @return void
 
500
 */
 
501
        function testSaveCreate() {
 
502
                $this->loadFixtures('Translate', 'TranslatedItem');
 
503
 
 
504
                $TestModel =& new TranslatedItem();
 
505
                $TestModel->locale = 'spa';
 
506
                $data = array('slug' => 'fourth_translated', 'title' => 'Leyenda #4', 'content' => 'Contenido #4');
 
507
                $TestModel->create($data);
 
508
                $TestModel->save();
 
509
                $result = $TestModel->read();
 
510
                $expected = array('TranslatedItem' => array_merge($data, array('id' => $TestModel->id, 'locale' => 'spa')));
 
511
                $this->assertEqual($result, $expected);
 
512
        }
 
513
 
 
514
/**
 
515
 * testSaveUpdate method
 
516
 *
 
517
 * @access public
 
518
 * @return void
 
519
 */
 
520
        function testSaveUpdate() {
 
521
                $this->loadFixtures('Translate', 'TranslatedItem');
 
522
 
 
523
                $TestModel =& new TranslatedItem();
 
524
                $TestModel->locale = 'spa';
 
525
                $oldData = array('slug' => 'fourth_translated', 'title' => 'Leyenda #4');
 
526
                $TestModel->create($oldData);
 
527
                $TestModel->save();
 
528
                $id = $TestModel->id;
 
529
                $newData = array('id' => $id, 'content' => 'Contenido #4');
 
530
                $TestModel->create($newData);
 
531
                $TestModel->save();
 
532
                $result = $TestModel->read(null, $id);
 
533
                $expected = array('TranslatedItem' => array_merge($oldData, $newData, array('locale' => 'spa')));
 
534
                $this->assertEqual($result, $expected);
 
535
        }
 
536
 
 
537
/**
 
538
 * testMultipleCreate method
 
539
 *
 
540
 * @access public
 
541
 * @return void
 
542
 */
 
543
        function testMultipleCreate() {
 
544
                $this->loadFixtures('Translate', 'TranslatedItem');
 
545
 
 
546
                $TestModel =& new TranslatedItem();
 
547
                $TestModel->locale = 'deu';
 
548
                $data = array(
 
549
                        'slug' => 'new_translated',
 
550
                        'title' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
 
551
                        'content' => array('eng' => 'New content', 'spa' => 'Nuevo contenido')
 
552
                );
 
553
                $TestModel->create($data);
 
554
                $TestModel->save();
 
555
 
 
556
                $TestModel->unbindTranslation();
 
557
                $translations = array('title' => 'Title', 'content' => 'Content');
 
558
                $TestModel->bindTranslation($translations, false);
 
559
                $TestModel->locale = array('eng', 'spa');
 
560
 
 
561
                $result = $TestModel->read();
 
562
                $expected = array(
 
563
                        'TranslatedItem' => array('id' => 4, 'slug' => 'new_translated', 'locale' => 'eng', 'title' => 'New title', 'content' => 'New content'),
 
564
                        'Title' => array(
 
565
                                array('id' => 21, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'title', 'content' => 'New title'),
 
566
                                array('id' => 22, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'title', 'content' => 'Nuevo leyenda')
 
567
                        ),
 
568
                        'Content' => array(
 
569
                                array('id' => 19, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'content', 'content' => 'New content'),
 
570
                                array('id' => 20, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'content', 'content' => 'Nuevo contenido')
 
571
                        )
 
572
                );
 
573
                $this->assertEqual($result, $expected);
 
574
        }
 
575
 
 
576
/**
 
577
 * testMultipleUpdate method
 
578
 *
 
579
 * @access public
 
580
 * @return void
 
581
 */
 
582
        function testMultipleUpdate() {
 
583
                $this->loadFixtures('Translate', 'TranslatedItem');
 
584
 
 
585
                $TestModel =& new TranslatedItem();
 
586
                $TestModel->locale = 'eng';
 
587
                $TestModel->validate['title'] = 'notEmpty';
 
588
                $data = array('TranslatedItem' => array(
 
589
                        'id' => 1,
 
590
                        'title' => array('eng' => 'New Title #1', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
 
591
                        'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
 
592
                ));
 
593
                $TestModel->create();
 
594
                $TestModel->save($data);
 
595
 
 
596
                $TestModel->unbindTranslation();
 
597
                $translations = array('title' => 'Title', 'content' => 'Content');
 
598
                $TestModel->bindTranslation($translations, false);
 
599
                $result = $TestModel->read(null, 1);
 
600
                $expected = array(
 
601
                        'TranslatedItem' => array('id' => '1', 'slug' => 'first_translated', 'locale' => 'eng', 'title' => 'New Title #1', 'content' => 'New Content #1'),
 
602
                        'Title' => array(
 
603
                                array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'New Title #1'),
 
604
                                array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Neue Titel #1'),
 
605
                                array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Novy Titulek #1')
 
606
                        ),
 
607
                        'Content' => array(
 
608
                                array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'New Content #1'),
 
609
                                array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Neue Inhalt #1'),
 
610
                                array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Novy Obsah #1')
 
611
                        )
 
612
                );
 
613
                $this->assertEqual($result, $expected);
 
614
 
 
615
                $TestModel->unbindTranslation($translations);
 
616
                $TestModel->bindTranslation(array('title', 'content'), false);
 
617
        }
 
618
 
 
619
/**
 
620
 * testMixedCreateUpdateWithArrayLocale method
 
621
 *
 
622
 * @access public
 
623
 * @return void
 
624
 */
 
625
        function testMixedCreateUpdateWithArrayLocale() {
 
626
                $this->loadFixtures('Translate', 'TranslatedItem');
 
627
 
 
628
                $TestModel =& new TranslatedItem();
 
629
                $TestModel->locale = array('cze', 'deu');
 
630
                $data = array('TranslatedItem' => array(
 
631
                        'id' => 1,
 
632
                        'title' => array('eng' => 'Updated Title #1', 'spa' => 'Nuevo leyenda #1'),
 
633
                        'content' => 'Upraveny obsah #1'
 
634
                ));
 
635
                $TestModel->create();
 
636
                $TestModel->save($data);
 
637
 
 
638
                $TestModel->unbindTranslation();
 
639
                $translations = array('title' => 'Title', 'content' => 'Content');
 
640
                $TestModel->bindTranslation($translations, false);
 
641
                $result = $TestModel->read(null, 1);
 
642
                $expected = array(
 
643
                        'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated', 'locale' => 'cze', 'title' => 'Titulek #1', 'content' => 'Upraveny obsah #1'),
 
644
                        'Title' => array(
 
645
                                array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Updated Title #1'),
 
646
                                array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
 
647
                                array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1'),
 
648
                                array('id' => 19, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Nuevo leyenda #1')
 
649
                        ),
 
650
                        'Content' => array(
 
651
                                array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
 
652
                                array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
 
653
                                array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Upraveny obsah #1')
 
654
                        )
 
655
                );
 
656
                $this->assertEqual($result, $expected);
 
657
        }
 
658
 
 
659
/**
 
660
 * testValidation method
 
661
 *
 
662
 * @access public
 
663
 * @return void
 
664
 */
 
665
        function testValidation() {
 
666
                $this->loadFixtures('Translate', 'TranslatedItem');
 
667
 
 
668
                $TestModel =& new TranslatedItem();
 
669
                $TestModel->locale = 'eng';
 
670
                $TestModel->validate['title'] = '/Only this title/';
 
671
                $data = array('TranslatedItem' => array(
 
672
                        'id' => 1,
 
673
                        'title' => array('eng' => 'New Title #1', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
 
674
                        'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
 
675
                ));
 
676
                $TestModel->create();
 
677
                $this->assertFalse($TestModel->save($data));
 
678
                $this->assertEqual($TestModel->validationErrors['title'], 'This field cannot be left blank');
 
679
 
 
680
                $TestModel->locale = 'eng';
 
681
                $TestModel->validate['title'] = '/Only this title/';
 
682
                $data = array('TranslatedItem' => array(
 
683
                        'id' => 1,
 
684
                        'title' => array('eng' => 'Only this title', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
 
685
                        'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
 
686
                ));
 
687
                $TestModel->create();
 
688
                $this->assertTrue($TestModel->save($data));
 
689
        }
 
690
 
 
691
/**
 
692
 * testAttachDetach method
 
693
 *
 
694
 * @access public
 
695
 * @return void
 
696
 */
 
697
        function testAttachDetach() {
 
698
                $this->loadFixtures('Translate', 'TranslatedItem');
 
699
 
 
700
                $TestModel =& new TranslatedItem();
 
701
                $Behavior =& $this->Model->Behaviors->Translate;
 
702
 
 
703
                $TestModel->unbindTranslation();
 
704
                $translations = array('title' => 'Title', 'content' => 'Content');
 
705
                $TestModel->bindTranslation($translations, false);
 
706
 
 
707
                $result = array_keys($TestModel->hasMany);
 
708
                $expected = array('Title', 'Content');
 
709
                $this->assertEqual($result, $expected);
 
710
 
 
711
                $TestModel->Behaviors->detach('Translate');
 
712
                $result = array_keys($TestModel->hasMany);
 
713
                $expected = array();
 
714
                $this->assertEqual($result, $expected);
 
715
 
 
716
                $result = isset($TestModel->Behaviors->Translate);
 
717
                $this->assertFalse($result);
 
718
 
 
719
                $result = isset($Behavior->settings[$TestModel->alias]);
 
720
                $this->assertFalse($result);
 
721
 
 
722
                $result = isset($Behavior->runtime[$TestModel->alias]);
 
723
                $this->assertFalse($result);
 
724
 
 
725
                $TestModel->Behaviors->attach('Translate', array('title' => 'Title', 'content' => 'Content'));
 
726
                $result = array_keys($TestModel->hasMany);
 
727
                $expected = array('Title', 'Content');
 
728
                $this->assertEqual($result, $expected);
 
729
 
 
730
                $result = isset($TestModel->Behaviors->Translate);
 
731
                $this->assertTrue($result);
 
732
 
 
733
                $Behavior = $TestModel->Behaviors->Translate;
 
734
 
 
735
                $result = isset($Behavior->settings[$TestModel->alias]);
 
736
                $this->assertTrue($result);
 
737
 
 
738
                $result = isset($Behavior->runtime[$TestModel->alias]);
 
739
                $this->assertTrue($result);
 
740
        }
 
741
 
 
742
/**
 
743
 * testAnotherTranslateTable method
 
744
 *
 
745
 * @access public
 
746
 * @return void
 
747
 */
 
748
        function testAnotherTranslateTable() {
 
749
                $this->loadFixtures('Translate', 'TranslatedItem', 'TranslateTable');
 
750
 
 
751
                $TestModel =& new TranslatedItemWithTable();
 
752
                $TestModel->locale = 'eng';
 
753
                $result = $TestModel->read(null, 1);
 
754
                $expected = array(
 
755
                        'TranslatedItemWithTable' => array(
 
756
                                'id' => 1,
 
757
                                'slug' => 'first_translated',
 
758
                                'locale' => 'eng',
 
759
                                'title' => 'Another Title #1',
 
760
                                'content' => 'Another Content #1'
 
761
                        )
 
762
                );
 
763
                $this->assertEqual($result, $expected);
 
764
        }
 
765
 
 
766
/**
 
767
 * testTranslateWithAssociations method
 
768
 *
 
769
 * @access public
 
770
 * @return void
 
771
 */
 
772
        function testTranslateWithAssociations() {
 
773
                $this->loadFixtures('TranslateArticle', 'TranslatedArticle', 'User', 'Comment', 'ArticlesTag', 'Tag');
 
774
 
 
775
                $TestModel =& new TranslatedArticle();
 
776
                $TestModel->locale = 'eng';
 
777
                $recursive = $TestModel->recursive;
 
778
 
 
779
                $result = $TestModel->read(null, 1);
 
780
                $expected = array(
 
781
                        'TranslatedArticle' => array(
 
782
                                'id' => 1,
 
783
                                'user_id' => 1,
 
784
                                'published' => 'Y',
 
785
                                'created' => '2007-03-18 10:39:23',
 
786
                                'updated' => '2007-03-18 10:41:31',
 
787
                                'locale' => 'eng',
 
788
                                'title' => 'Title (eng) #1',
 
789
                                'body' => 'Body (eng) #1'
 
790
                        ),
 
791
                        'User' => array(
 
792
                                'id' => 1,
 
793
                                'user' => 'mariano',
 
794
                                'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
 
795
                                'created' => '2007-03-17 01:16:23',
 
796
                'updated' => '2007-03-17 01:18:31'
 
797
                        )
 
798
                );
 
799
                $this->assertEqual($result, $expected);
 
800
 
 
801
                $result = $TestModel->find('all', array('recursive' => -1));
 
802
                $expected = array(
 
803
                        array(
 
804
                                'TranslatedArticle' => array(
 
805
                                        'id' => 1,
 
806
                                        'user_id' => 1,
 
807
                                        'published' => 'Y',
 
808
                                        'created' => '2007-03-18 10:39:23',
 
809
                                        'updated' => '2007-03-18 10:41:31',
 
810
                                        'locale' => 'eng',
 
811
                                        'title' => 'Title (eng) #1',
 
812
                                        'body' => 'Body (eng) #1'
 
813
                                )
 
814
                        ),
 
815
                        array(
 
816
                                'TranslatedArticle' => array(
 
817
                                        'id' => 2,
 
818
                                        'user_id' => 3,
 
819
                                        'published' => 'Y',
 
820
                                        'created' => '2007-03-18 10:41:23',
 
821
                                        'updated' => '2007-03-18 10:43:31',
 
822
                                        'locale' => 'eng',
 
823
                                        'title' => 'Title (eng) #2',
 
824
                                        'body' => 'Body (eng) #2'
 
825
                                )
 
826
                        ),
 
827
                        array(
 
828
                                'TranslatedArticle' => array(
 
829
                                        'id' => 3,
 
830
                                        'user_id' => 1,
 
831
                                        'published' => 'Y',
 
832
                                        'created' => '2007-03-18 10:43:23',
 
833
                                        'updated' => '2007-03-18 10:45:31',
 
834
                                        'locale' => 'eng',
 
835
                                        'title' => 'Title (eng) #3',
 
836
                                        'body' => 'Body (eng) #3'
 
837
                                )
 
838
                        )
 
839
                );
 
840
                $this->assertEqual($result, $expected);
 
841
                $this->assertEqual($TestModel->recursive, $recursive);
 
842
 
 
843
                $TestModel->recursive = -1;
 
844
                $result = $TestModel->read(null, 1);
 
845
                $expected = array(
 
846
                        'TranslatedArticle' => array(
 
847
                                'id' => 1,
 
848
                                'user_id' => 1,
 
849
                                'published' => 'Y',
 
850
                                'created' => '2007-03-18 10:39:23',
 
851
                                'updated' => '2007-03-18 10:41:31',
 
852
                                'locale' => 'eng',
 
853
                                'title' => 'Title (eng) #1',
 
854
                                'body' => 'Body (eng) #1'
 
855
                        )
 
856
                );
 
857
                $this->assertEqual($result, $expected);
 
858
        }
 
859
/**
 
860
 * testTranslateTableWithPrefix method
 
861
 * Tests that is possible to have a translation model with a custom tablePrefix
 
862
 *
 
863
 * @access public
 
864
 * @return void
 
865
 */
 
866
        function testTranslateTableWithPrefix() {
 
867
                $this->loadFixtures('TranslateWithPrefix', 'TranslatedItem');
 
868
                $TestModel =& new TranslatedItem2;
 
869
                $TestModel->locale = 'eng';
 
870
                $result = $TestModel->read(null, 1);
 
871
                $expected = array('TranslatedItem' => array(
 
872
                        'id' => 1,
 
873
                        'slug' => 'first_translated',
 
874
                        'locale' => 'eng',
 
875
                        'content' => 'Content #1',
 
876
                        'title' => 'Title #1'
 
877
                ));
 
878
                $this->assertEqual($result, $expected);
 
879
        }
 
880
 
 
881
/**
 
882
 * Test infinite loops not occuring with unbindTranslation()
 
883
 *
 
884
 * @return void
 
885
 */
 
886
        function testUnbindTranslationInfinteLoop() {
 
887
                $this->loadFixtures('Translate', 'TranslatedItem');
 
888
 
 
889
                $TestModel =& new TranslatedItem();
 
890
                $TestModel->Behaviors->detach('Translate');
 
891
                $TestModel->actsAs = array();
 
892
                $TestModel->Behaviors->attach('Translate');
 
893
                $TestModel->bindTranslation(array('title', 'content'), true);
 
894
                $result = $TestModel->unbindTranslation();
 
895
 
 
896
                $this->assertFalse($result);
 
897
        }
 
898
}