propel /test/testsuite/generator/behavior/i18n/I18nBehaviorObjectBuilderModifierTest.php

Language PHP Lines 289
MD5 Hash 7a9a6081ce23616efc1bcbfc314e2cf2
Repository https://github.com/mattleff/propel.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php

/*
 *	$Id: VersionableBehaviorTest.php 1460 2010-01-17 22:36:48Z francois $
 * This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license    MIT License
 */

require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__) . '/../../../../../generator/lib/util/PropelQuickBuilder.php';
require_once dirname(__FILE__) . '/../../../../../generator/lib/behavior/i18n/I18nBehavior.php';
require_once dirname(__FILE__) . '/../../../../../runtime/lib/Propel.php';

/**
 * Tests for I18nBehavior class object modifier
 *
 * @author     Franรงois Zaninotto
 * @version    $Revision: 2251 $
 * @package    generator.behavior.i18n
 */
class I18nBehaviorObjectBuilderModifierTest extends PHPUnit_Framework_TestCase
{
	public function setUp()
	{
		if (!class_exists('I18nBehaviorTest1')) {
			$schema = <<<EOF
<database name="i18n_behavior_test_1">
	<table name="i18n_behavior_test_1">
		<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
		<column name="foo" type="INTEGER" />
		<column name="bar" type="VARCHAR" size="100" />
		<behavior name="i18n">
			<parameter name="i18n_columns" value="bar" />
		</behavior>
	</table>
	<table name="i18n_behavior_test_2">
		<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
		<column name="foo" type="INTEGER" />
		<column name="bar1" type="VARCHAR" size="100" />
		<column name="bar2" type="LONGVARCHAR" lazyLoad="true" />
		<column name="bar3" type="TIMESTAMP" />
		<column name="bar4" type="LONGVARCHAR" description="This is the Bar4 column" />
		<behavior name="i18n">
			<parameter name="i18n_columns" value="bar1,bar2,bar3,bar4" />
			<parameter name="default_locale" value="fr_FR" />
			<parameter name="locale_alias" value="culture" />
		</behavior>
	</table>
</database>
EOF;
			PropelQuickBuilder::buildSchema($schema);
		}
	}

	public function testPostDeleteEmulatesOnDeleteCascade()
	{
		I18nBehaviorTest1Query::create()->deleteAll();
		I18nBehaviorTest1I18nQuery::create()->deleteAll();
		$o = new I18nBehaviorTest1();
		$o->setFoo(123);
		$o->setLocale('en_EN');
		$o->setBar('hello');
		$o->setLocale('fr_FR');
		$o->setBar('bonjour');
		$o->save();
		$this->assertEquals(2, I18nBehaviorTest1I18nQuery::create()->count());
		$o->clearI18nBehaviorTest1I18ns();
		$o->delete();
		$this->assertEquals(0, I18nBehaviorTest1I18nQuery::create()->count());
	}

	public function testGetTranslationReturnsTranslationObject()
	{
		$o = new I18nBehaviorTest1();
		$translation = $o->getTranslation();
		$this->assertTrue($translation instanceof I18nBehaviorTest1I18n);
	}

	public function testGetTranslationOnNewObjectReturnsNewTranslation()
	{
		$o = new I18nBehaviorTest1();
		$translation = $o->getTranslation();
		$this->assertTrue($translation->isNew());
	}

	public function testGetTranslationOnPersistedObjectReturnsNewTranslation()
	{
		$o = new I18nBehaviorTest1();
		$o->save();
		$translation = $o->getTranslation();
		$this->assertTrue($translation->isNew());
	}

	public function testGetTranslationOnPersistedObjectWithTranslationReturnsExistingTranslation()
	{
		$o = new I18nBehaviorTest1();
		$translation = new I18nBehaviorTest1I18n();
		$o->addI18nBehaviorTest1I18n($translation);
		$o->save();
		$translation = $o->getTranslation();
		$this->assertFalse($translation->isNew());
	}

	public function testGetTranslationAcceptsALocaleParameter()
	{
		$o = new I18nBehaviorTest1();
		$translation1 = new I18nBehaviorTest1I18n();
		$translation1->setLocale('en_EN');
		$o->addI18nBehaviorTest1I18n($translation1);
		$translation2 = new I18nBehaviorTest1I18n();
		$translation2->setLocale('fr_FR');
		$o->addI18nBehaviorTest1I18n($translation2);
		$o->save();
		$this->assertEquals($translation1, $o->getTranslation('en_EN'));
		$this->assertEquals($translation2, $o->getTranslation('fr_FR'));
	}
	
	public function testGetTranslationSetsTheLocaleOnTheTranslation()
	{
		$o = new I18nBehaviorTest1();
		$o->save();
		$translation = $o->getTranslation();
		$this->assertEquals('en_EN', $translation->getLocale());
		$o = new I18nBehaviorTest2();
		$o->save();
		$translation = $o->getTranslation();
		$this->assertEquals('fr_FR', $translation->getLocale());
	}

	public function testGetTranslationUsesInternalCollectionIfAvailable()
	{
		$o = new I18nBehaviorTest1();
		$translation1 = new I18nBehaviorTest1I18n();
		$translation1->setLocale('en_EN');
		$o->addI18nBehaviorTest1I18n($translation1);
		$translation2 = new I18nBehaviorTest1I18n();
		$translation2->setLocale('fr_FR');
		$o->addI18nBehaviorTest1I18n($translation2);
		$translation = $o->getTranslation('en_EN');
		$this->assertEquals($translation1, $translation);
	}
	
	public function testRemoveTranslation()
	{
		$o = new I18nBehaviorTest1();
		$translation1 = new I18nBehaviorTest1I18n();
		$translation1->setLocale('en_EN');
		$o->addI18nBehaviorTest1I18n($translation1);
		$translation2 = new I18nBehaviorTest1I18n();
		$translation2->setLocale('fr_FR');
		$translation2->setBar('bonjour');
		$o->addI18nBehaviorTest1I18n($translation2);
		$o->save();
		$this->assertEquals(2, $o->countI18nBehaviorTest1I18ns());
		$o->removeTranslation('fr_FR');
		$this->assertEquals(1, $o->countI18nBehaviorTest1I18ns());
		$translation = $o->getTranslation('fr_FR');
		$this->assertNotEquals($translation->getBar(), $translation2->getBar());
	}

	public function testLocaleSetterAndGetterExist()
	{
		$this->assertTrue(method_exists('I18nBehaviorTest1', 'setLocale'));
		$this->assertTrue(method_exists('I18nBehaviorTest1', 'getLocale'));
	}

	public function testGetLocaleReturnsDefaultLocale()
	{
		$o = new I18nBehaviorTest1();
		$this->assertEquals('en_EN', $o->getLocale());
		$o = new I18nBehaviorTest2();
		$this->assertEquals('fr_FR', $o->getLocale());
	}

	public function testSetLocale()
	{
		$o = new I18nBehaviorTest1();
		$o->setLocale('fr_FR');
		$this->assertEquals('fr_FR', $o->getLocale());
	}

	public function testSetLocaleUsesDefaultLocale()
	{
		$o = new I18nBehaviorTest1();
		$o->setLocale('fr_FR');
		$o->setLocale();
		$this->assertEquals('en_EN', $o->getLocale());
	}

	public function testLocaleSetterAndGetterAliasesExist()
	{
		$this->assertTrue(method_exists('I18nBehaviorTest2', 'setCulture'));
		$this->assertTrue(method_exists('I18nBehaviorTest2', 'getCulture'));
	}

	public function testGetLocaleAliasReturnsDefaultLocale()
	{
		$o = new I18nBehaviorTest2();
		$this->assertEquals('fr_FR', $o->getCulture());
	}

	public function testSetLocaleAlias()
	{
		$o = new I18nBehaviorTest2();
		$o->setCulture('en_EN');
		$this->assertEquals('en_EN', $o->getCulture());
	}

	public function testGetCurrentTranslationUsesDefaultLocale()
	{
		$o = new I18nBehaviorTest1();
		$t = $o->getCurrentTranslation();
		$this->assertEquals('en_EN', $t->getLocale());
		$o = new I18nBehaviorTest2();
		$t = $o->getCurrentTranslation();
		$this->assertEquals('fr_FR', $t->getLocale());
	}

	public function testGetCurrentTranslationUsesCurrentLocale()
	{
		$o = new I18nBehaviorTest1();
		$o->setLocale('fr_FR');
		$this->assertEquals('fr_FR', $o->getCurrentTranslation()->getLocale());
		$o->setLocale('pt_PT');
		$this->assertEquals('pt_PT', $o->getCurrentTranslation()->getLocale());
	}
	
	public function testI18nColumnGetterUsesCurrentTranslation()
	{
		$o = new I18nBehaviorTest1();
		$t1 = $o->getCurrentTranslation();
		$t1->setBar('hello');
		$o->setLocale('fr_FR');
		$t2 = $o->getCurrentTranslation();
		$t2->setBar('bonjour');
		//$o->save();
		$o->setLocale('en_EN');
		$this->assertEquals('hello', $o->getBar());
		$o->setLocale('fr_FR');
		$this->assertEquals('bonjour', $o->getBar());
	}

	public function testI18nColumnSetterUsesCurrentTranslation()
	{
		$o = new I18nBehaviorTest1();
		$o->setBar('hello');
		$o->setLocale('fr_FR');
		$o->setBar('bonjour');
		$o->setLocale('en_EN');
		$this->assertEquals('hello', $o->getBar());
		$o->setLocale('fr_FR');
		$this->assertEquals('bonjour', $o->getBar());
	}
	
	public function testTranslationsArePersisted()
	{
		$o = new I18nBehaviorTest1();
		$o->save();
		$count = I18nBehaviorTest1I18nQuery::create()
			->filterById($o->getId())
			->count();
		$this->assertEquals(0, $count);
		$o->setBar('hello');
		$o->setLocale('fr_FR');
		$o->setBar('bonjour');
		$o->save();
		$count = I18nBehaviorTest1I18nQuery::create()
			->filterById($o->getId())
			->count();
		$this->assertEquals(2, $count);
	}
	
	public function testClearRemovesExistingTranlsations()
	{
		$o = new I18nBehaviorTest1();
		$translation1 = new I18nBehaviorTest1I18n();
		$translation1->setBar('baz');
		$translation1->setLocale('fr_FR');
		$o->addI18nBehaviorTest1I18n($translation1);
		$o->clear();
		$this->assertEquals('en_EN', $o->getLocale());
		$t1 = $o->getTranslation('fr_FR');
		$this->assertEquals('', $t1->getBar());
	}

}
Back to Top