Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * ExtractTaskTest file
4
 *
5
 * Test Case for i18n extraction shell task
6
 *
7
 * CakePHP :  Rapid Development Framework (http://cakephp.org)
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://cakephp.org CakePHP Project
16
 * @package       Cake.Test.Case.Console.Command.Task
17
 * @since         CakePHP v 1.2.0.7726
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20
 
21
App::uses('Folder', 'Utility');
22
App::uses('ConsoleOutput', 'Console');
23
App::uses('ConsoleInput', 'Console');
24
App::uses('ShellDispatcher', 'Console');
25
App::uses('Shell', 'Console');
26
App::uses('ExtractTask', 'Console/Command/Task');
27
 
28
/**
29
 * ExtractTaskTest class
30
 *
31
 * @package       Cake.Test.Case.Console.Command.Task
32
 */
33
class ExtractTaskTest extends CakeTestCase {
34
 
35
/**
36
 * setUp method
37
 *
38
 * @return void
39
 */
40
	public function setUp() {
41
		parent::setUp();
42
		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
43
		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
44
 
45
		$this->Task = $this->getMock(
46
			'ExtractTask',
47
			array('in', 'out', 'err', '_stop'),
48
			array($out, $out, $in)
49
		);
50
		$this->path = TMP . 'tests' . DS . 'extract_task_test';
51
		new Folder($this->path . DS . 'locale', true);
52
	}
53
 
54
/**
55
 * tearDown method
56
 *
57
 * @return void
58
 */
59
	public function tearDown() {
60
		parent::tearDown();
61
		unset($this->Task);
62
 
63
		$Folder = new Folder($this->path);
64
		$Folder->delete();
65
		CakePlugin::unload();
66
	}
67
 
68
/**
69
 * testExecute method
70
 *
71
 * @return void
72
 */
73
	public function testExecute() {
74
		$this->Task->interactive = false;
75
 
76
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages';
77
		$this->Task->params['output'] = $this->path . DS;
78
		$this->Task->params['extract-core'] = 'no';
79
		$this->Task->expects($this->never())->method('err');
80
		$this->Task->expects($this->any())->method('in')
81
			->will($this->returnValue('y'));
82
		$this->Task->expects($this->never())->method('_stop');
83
 
84
		$this->Task->execute();
85
		$this->assertTrue(file_exists($this->path . DS . 'default.pot'));
86
		$result = file_get_contents($this->path . DS . 'default.pot');
87
 
88
		$this->assertFalse(file_exists($this->path . DS . 'cake.pot'));
89
 
90
		$pattern = '/"Content-Type\: text\/plain; charset\=utf-8/';
91
		$this->assertRegExp($pattern, $result);
92
		$pattern = '/"Content-Transfer-Encoding\: 8bit/';
93
		$this->assertRegExp($pattern, $result);
94
		$pattern = '/"Plural-Forms\: nplurals\=INTEGER; plural\=EXPRESSION;/';
95
		$this->assertRegExp($pattern, $result);
96
 
97
		// home.ctp
98
		$pattern = '/msgid "Your tmp directory is writable."\nmsgstr ""\n/';
99
		$this->assertRegExp($pattern, $result);
100
 
101
		$pattern = '/msgid "Your tmp directory is NOT writable."\nmsgstr ""\n/';
102
		$this->assertRegExp($pattern, $result);
103
 
104
		$pattern = '/msgid "The %s is being used for caching. To change the config edit ';
105
		$pattern .= 'APP\/config\/core.php "\nmsgstr ""\n/';
106
		$this->assertRegExp($pattern, $result);
107
 
108
		$pattern = '/msgid "Your cache is NOT working. Please check ';
109
		$pattern .= 'the settings in APP\/config\/core.php"\nmsgstr ""\n/';
110
		$this->assertRegExp($pattern, $result);
111
 
112
		$pattern = '/msgid "Your database configuration file is present."\nmsgstr ""\n/';
113
		$this->assertRegExp($pattern, $result);
114
 
115
		$pattern = '/msgid "Your database configuration file is NOT present."\nmsgstr ""\n/';
116
		$this->assertRegExp($pattern, $result);
117
 
118
		$pattern = '/msgid "Rename config\/database.php.default to ';
119
		$pattern .= 'config\/database.php"\nmsgstr ""\n/';
120
		$this->assertRegExp($pattern, $result);
121
 
122
		$pattern = '/msgid "Cake is able to connect to the database."\nmsgstr ""\n/';
123
		$this->assertRegExp($pattern, $result);
124
 
125
		$pattern = '/msgid "Cake is NOT able to connect to the database."\nmsgstr ""\n/';
126
		$this->assertRegExp($pattern, $result);
127
 
128
		$pattern = '/msgid "Editing this Page"\nmsgstr ""\n/';
129
		$this->assertRegExp($pattern, $result);
130
 
131
		$pattern = '/msgid "To change the content of this page, create: APP\/views\/pages\/home\.ctp/';
132
		$this->assertRegExp($pattern, $result);
133
 
134
		$pattern = '/To change its layout, create: APP\/views\/layouts\/default\.ctp\./s';
135
		$this->assertRegExp($pattern, $result);
136
 
137
		// extract.ctp
138
		$pattern = '/\#: (\\\\|\/)extract\.ctp:15;6\n';
139
		$pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
140
		$this->assertRegExp($pattern, $result);
141
 
142
		$pattern = '/msgid "You have %d new message."\nmsgstr ""/';
143
		$this->assertNotRegExp($pattern, $result, 'No duplicate msgid');
144
 
145
		$pattern = '/\#: (\\\\|\/)extract\.ctp:7\n';
146
		$pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
147
		$this->assertRegExp($pattern, $result);
148
 
149
		$pattern = '/\#: (\\\\|\/)extract\.ctp:14\n';
150
		$pattern .= '\#: (\\\\|\/)home\.ctp:68\n';
151
		$pattern .= 'msgid "Editing this Page"\nmsgstr ""/';
152
		$this->assertRegExp($pattern, $result);
153
 
154
		$pattern = '/\#: (\\\\|\/)extract\.ctp:22\nmsgid "';
155
		$pattern .= 'Hot features!';
156
		$pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
157
		$pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
158
		$pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
159
		$pattern .= '"\nmsgstr ""/';
160
		$this->assertRegExp($pattern, $result);
161
 
162
		$this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
163
		$this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
164
 
165
		// extract.ctp - reading the domain.pot
166
		$result = file_get_contents($this->path . DS . 'domain.pot');
167
 
168
		$pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
169
		$this->assertNotRegExp($pattern, $result);
170
		$pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
171
		$this->assertNotRegExp($pattern, $result);
172
 
173
		$pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
174
		$this->assertRegExp($pattern, $result);
175
		$pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
176
		$this->assertRegExp($pattern, $result);
177
	}
178
 
179
/**
180
 * testExtractCategory method
181
 *
182
 * @return void
183
 */
184
	public function testExtractCategory() {
185
		$this->Task->interactive = false;
186
 
187
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages';
188
		$this->Task->params['output'] = $this->path . DS;
189
		$this->Task->params['extract-core'] = 'no';
190
		$this->Task->params['merge'] = 'no';
191
		$this->Task->expects($this->never())->method('err');
192
		$this->Task->expects($this->any())->method('in')
193
			->will($this->returnValue('y'));
194
		$this->Task->expects($this->never())->method('_stop');
195
 
196
		$this->Task->execute();
197
		$this->assertTrue(file_exists($this->path . DS . 'LC_TIME' . DS . 'default.pot'));
198
 
199
		$result = file_get_contents($this->path . DS . 'default.pot');
200
 
201
		$pattern = '/\#: .*extract\.ctp:31\n/';
202
		$this->assertNotRegExp($pattern, $result);
203
	}
204
 
205
/**
206
 * test exclusions
207
 *
208
 * @return void
209
 */
210
	public function testExtractWithExclude() {
211
		$this->Task->interactive = false;
212
 
213
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View';
214
		$this->Task->params['output'] = $this->path . DS;
215
		$this->Task->params['exclude'] = 'Pages,Layouts';
216
		$this->Task->params['extract-core'] = 'no';
217
 
218
		$this->Task->expects($this->any())->method('in')
219
			->will($this->returnValue('y'));
220
 
221
		$this->Task->execute();
222
		$this->assertTrue(file_exists($this->path . DS . 'default.pot'));
223
		$result = file_get_contents($this->path . DS . 'default.pot');
224
 
225
		$pattern = '/\#: .*extract\.ctp:6\n/';
226
		$this->assertNotRegExp($pattern, $result);
227
 
228
		$pattern = '/\#: .*default\.ctp:26\n/';
229
		$this->assertNotRegExp($pattern, $result);
230
	}
231
 
232
/**
233
 * test extract can read more than one path.
234
 *
235
 * @return void
236
 */
237
	public function testExtractMultiplePaths() {
238
		$this->Task->interactive = false;
239
 
240
		$this->Task->params['paths'] =
241
			CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages,' .
242
			CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts';
243
 
244
		$this->Task->params['output'] = $this->path . DS;
245
		$this->Task->params['extract-core'] = 'no';
246
		$this->Task->expects($this->never())->method('err');
247
		$this->Task->expects($this->never())->method('_stop');
248
		$this->Task->execute();
249
 
250
		$result = file_get_contents($this->path . DS . 'default.pot');
251
 
252
		$pattern = '/msgid "Add User"/';
253
		$this->assertRegExp($pattern, $result);
254
	}
255
 
256
/**
257
 * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
258
 *
259
 * @return void
260
 */
261
	public function testExtractExcludePlugins() {
262
		App::build(array(
263
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
264
		));
265
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
266
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
267
		$this->Task = $this->getMock('ExtractTask',
268
			array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
269
			array($this->out, $this->out, $this->in)
270
		);
271
		$this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
272
 
273
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
274
		$this->Task->params['output'] = $this->path . DS;
275
		$this->Task->params['exclude-plugins'] = true;
276
 
277
		$this->Task->execute();
278
		$result = file_get_contents($this->path . DS . 'default.pot');
279
		$this->assertNotRegExp('#TestPlugin#', $result);
280
	}
281
 
282
/**
283
 * Test that is possible to extract messages form a single plugin
284
 *
285
 * @return void
286
 */
287
	public function testExtractPlugin() {
288
		App::build(array(
289
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
290
		));
291
 
292
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
293
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
294
		$this->Task = $this->getMock('ExtractTask',
295
			array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
296
			array($this->out, $this->out, $this->in)
297
		);
298
 
299
		$this->Task->params['output'] = $this->path . DS;
300
		$this->Task->params['plugin'] = 'TestPlugin';
301
 
302
		$this->Task->execute();
303
		$result = file_get_contents($this->path . DS . 'default.pot');
304
		$this->assertNotRegExp('#Pages#', $result);
305
		$this->assertContains('translate.ctp:1', $result);
306
		$this->assertContains('This is a translatable string', $result);
307
		$this->assertContains('I can haz plugin model validation message', $result);
308
	}
309
 
310
/**
311
 * Tests that the task will inspect application models and extract the validation messages from them
312
 *
313
 * @return void
314
 */
315
	public function testExtractModelValidation() {
316
		App::build(array(
317
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
318
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
319
		), App::RESET);
320
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
321
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
322
		$this->Task = $this->getMock('ExtractTask',
323
			array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
324
			array($this->out, $this->out, $this->in)
325
		);
326
		$this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
327
 
328
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
329
		$this->Task->params['output'] = $this->path . DS;
330
		$this->Task->params['extract-core'] = 'no';
331
		$this->Task->params['exclude-plugins'] = true;
332
		$this->Task->params['ignore-model-validation'] = false;
333
 
334
		$this->Task->execute();
335
		$result = file_get_contents($this->path . DS . 'default.pot');
336
 
337
		$pattern = preg_quote('#Model/PersisterOne.php:validation for field title#', '\\');
338
		$this->assertRegExp($pattern, $result);
339
 
340
		$pattern = preg_quote('#Model/PersisterOne.php:validation for field body#', '\\');
341
		$this->assertRegExp($pattern, $result);
342
 
343
		$pattern = '#msgid "Post title is required"#';
344
		$this->assertRegExp($pattern, $result);
345
 
346
		$pattern = '#msgid "You may enter up to %s chars \(minimum is %s chars\)"#';
347
		$this->assertRegExp($pattern, $result);
348
 
349
		$pattern = '#msgid "Post body is required"#';
350
		$this->assertRegExp($pattern, $result);
351
 
352
		$pattern = '#msgid "Post body is super required"#';
353
		$this->assertRegExp($pattern, $result);
354
 
355
		$this->assertContains('msgid "double \\"quoted\\" validation"', $result, 'Strings with quotes not handled correctly');
356
		$this->assertContains("msgid \"single 'quoted' validation\"", $result, 'Strings with quotes not handled correctly');
357
	}
358
 
359
/**
360
 *  Tests that the task will inspect application models and extract the validation messages from them
361
 *	while using a custom validation domain for the messages set on the model itself
362
 *
363
 * @return void
364
 */
365
	public function testExtractModelValidationWithDomainInModel() {
366
		App::build(array(
367
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Model' . DS)
368
		));
369
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
370
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
371
		$this->Task = $this->getMock('ExtractTask',
372
			array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
373
			array($this->out, $this->out, $this->in)
374
		);
375
		$this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
376
 
377
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
378
		$this->Task->params['output'] = $this->path . DS;
379
		$this->Task->params['extract-core'] = 'no';
380
		$this->Task->params['exclude-plugins'] = true;
381
		$this->Task->params['ignore-model-validation'] = false;
382
 
383
		$this->Task->execute();
384
		$result = file_get_contents($this->path . DS . 'test_plugin.pot');
385
 
386
		$pattern = preg_quote('#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#', '\\');
387
		$this->assertRegExp($pattern, $result);
388
 
389
		$pattern = preg_quote('#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field body#', '\\');
390
		$this->assertRegExp($pattern, $result);
391
 
392
		$pattern = '#msgid "Post title is required"#';
393
		$this->assertRegExp($pattern, $result);
394
 
395
		$pattern = '#msgid "Post body is required"#';
396
		$this->assertRegExp($pattern, $result);
397
 
398
		$pattern = '#msgid "Post body is super required"#';
399
		$this->assertRegExp($pattern, $result);
400
	}
401
 
402
/**
403
 *  Test that the extract shell can obtain validation messages from models inside a specific plugin
404
 *
405
 * @return void
406
 */
407
	public function testExtractModelValidationInPlugin() {
408
		App::build(array(
409
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
410
		));
411
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
412
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
413
		$this->Task = $this->getMock('ExtractTask',
414
			array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
415
			array($this->out, $this->out, $this->in)
416
		);
417
 
418
		$this->Task->params['output'] = $this->path . DS;
419
		$this->Task->params['ignore-model-validation'] = false;
420
		$this->Task->params['plugin'] = 'TestPlugin';
421
 
422
		$this->Task->execute();
423
		$result = file_get_contents($this->path . DS . 'test_plugin.pot');
424
 
425
		$pattern = preg_quote('#Model/TestPluginPost.php:validation for field title#', '\\');
426
		$this->assertRegExp($pattern, $result);
427
 
428
		$pattern = preg_quote('#Model/TestPluginPost.php:validation for field body#', '\\');
429
		$this->assertRegExp($pattern, $result);
430
 
431
		$pattern = '#msgid "Post title is required"#';
432
		$this->assertRegExp($pattern, $result);
433
 
434
		$pattern = '#msgid "Post body is required"#';
435
		$this->assertRegExp($pattern, $result);
436
 
437
		$pattern = '#msgid "Post body is super required"#';
438
		$this->assertRegExp($pattern, $result);
439
 
440
		$pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#';
441
		$this->assertNotRegExp($pattern, $result);
442
	}
443
 
444
/**
445
 *  Test that the extract shell overwrites existing files with the overwrite parameter
446
 *
447
 * @return void
448
 */
449
	public function testExtractOverwrite() {
450
		$this->Task->interactive = false;
451
 
452
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
453
		$this->Task->params['output'] = $this->path . DS;
454
		$this->Task->params['extract-core'] = 'no';
455
		$this->Task->params['overwrite'] = true;
456
 
457
		file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
458
		$this->assertTrue(file_exists($this->path . DS . 'default.pot'));
459
		$original = file_get_contents($this->path . DS . 'default.pot');
460
 
461
		$this->Task->execute();
462
		$result = file_get_contents($this->path . DS . 'default.pot');
463
		$this->assertNotEquals($original, $result);
464
	}
465
 
466
/**
467
 *  Test that the extract shell scans the core libs
468
 *
469
 * @return void
470
 */
471
	public function testExtractCore() {
472
		$this->Task->interactive = false;
473
 
474
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
475
		$this->Task->params['output'] = $this->path . DS;
476
		$this->Task->params['extract-core'] = 'yes';
477
 
478
		$this->Task->execute();
479
		$this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
480
		$result = file_get_contents($this->path . DS . 'cake.pot');
481
 
482
		$pattern = '/msgid "Yesterday, %s"\nmsgstr ""\n/';
483
		$this->assertRegExp($pattern, $result);
484
 
485
		$this->assertTrue(file_exists($this->path . DS . 'cake_dev.pot'));
486
		$result = file_get_contents($this->path . DS . 'cake_dev.pot');
487
 
488
		$pattern = '/#: Console\/Templates\//';
489
		$this->assertNotRegExp($pattern, $result);
490
 
491
		$pattern = '/#: Test\//';
492
		$this->assertNotRegExp($pattern, $result);
493
	}
494
}