Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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
		$pattern = '/\#: (\\\\|\/)extract\.ctp:36\nmsgid "letter"/';
166
		$this->assertRegExp($pattern, $result, 'Strings with context should not overwrite strings without context');
167
 
168
		$pattern = '/\#: (\\\\|\/)extract\.ctp:37;39\nmsgctxt "A"\nmsgid "letter"/';
169
		$this->assertRegExp($pattern, $result, 'Should contain string with context "A"');
170
 
171
		$pattern = '/\#: (\\\\|\/)extract\.ctp:38\nmsgctxt "B"\nmsgid "letter"/';
172
		$this->assertRegExp($pattern, $result, 'Should contain string with context "B"');
173
 
174
		$pattern = '/\#: (\\\\|\/)extract\.ctp:40\nmsgid "%d letter"\nmsgid_plural "%d letters"/';
175
		$this->assertRegExp($pattern, $result, 'Plural strings with context should not overwrite strings without context');
176
 
177
		$pattern = '/\#: (\\\\|\/)extract\.ctp:41\nmsgctxt "A"\nmsgid "%d letter"\nmsgid_plural "%d letters"/';
178
		$this->assertRegExp($pattern, $result, 'Should contain plural string with context "A"');
179
 
180
		// extract.ctp - reading the domain.pot
181
		$result = file_get_contents($this->path . DS . 'domain.pot');
182
 
183
		$pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
184
		$this->assertNotRegExp($pattern, $result);
185
		$pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
186
		$this->assertNotRegExp($pattern, $result);
187
 
188
		$pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
189
		$this->assertRegExp($pattern, $result);
190
		$pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
191
		$this->assertRegExp($pattern, $result);
192
	}
193
 
194
/**
195
 * testExtractCategory method
196
 *
197
 * @return void
198
 */
199
	public function testExtractCategory() {
200
		$this->Task->interactive = false;
201
 
202
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages';
203
		$this->Task->params['output'] = $this->path . DS;
204
		$this->Task->params['extract-core'] = 'no';
205
		$this->Task->params['merge'] = 'no';
206
		$this->Task->expects($this->never())->method('err');
207
		$this->Task->expects($this->any())->method('in')
208
			->will($this->returnValue('y'));
209
		$this->Task->expects($this->never())->method('_stop');
210
 
211
		$this->Task->execute();
212
		$this->assertTrue(file_exists($this->path . DS . 'LC_NUMERIC' . DS . 'default.pot'));
213
		$this->assertFalse(file_exists($this->path . DS . 'LC_TIME' . DS . 'default.pot'));
214
 
215
		$result = file_get_contents($this->path . DS . 'default.pot');
216
 
217
		$pattern = '/\#: .*extract\.ctp:31\n/';
218
		$this->assertNotRegExp($pattern, $result);
219
 
220
		$pattern = '/\#: .*extract\.ctp:33\n/';
221
		$this->assertNotRegExp($pattern, $result);
222
	}
223
 
224
/**
225
 * test exclusions
226
 *
227
 * @return void
228
 */
229
	public function testExtractWithExclude() {
230
		$this->Task->interactive = false;
231
 
232
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View';
233
		$this->Task->params['output'] = $this->path . DS;
234
		$this->Task->params['exclude'] = 'Pages,Layouts';
235
		$this->Task->params['extract-core'] = 'no';
236
 
237
		$this->Task->expects($this->any())->method('in')
238
			->will($this->returnValue('y'));
239
 
240
		$this->Task->execute();
241
		$this->assertTrue(file_exists($this->path . DS . 'default.pot'));
242
		$result = file_get_contents($this->path . DS . 'default.pot');
243
 
244
		$pattern = '/\#: .*extract\.ctp:6\n/';
245
		$this->assertNotRegExp($pattern, $result);
246
 
247
		$pattern = '/\#: .*default\.ctp:26\n/';
248
		$this->assertNotRegExp($pattern, $result);
249
	}
250
 
251
/**
252
 * test extract can read more than one path.
253
 *
254
 * @return void
255
 */
256
	public function testExtractMultiplePaths() {
257
		$this->Task->interactive = false;
258
 
259
		$this->Task->params['paths'] =
260
			CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages,' .
261
			CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts';
262
 
263
		$this->Task->params['output'] = $this->path . DS;
264
		$this->Task->params['extract-core'] = 'no';
265
		$this->Task->expects($this->never())->method('err');
266
		$this->Task->expects($this->never())->method('_stop');
267
		$this->Task->execute();
268
 
269
		$result = file_get_contents($this->path . DS . 'default.pot');
270
 
271
		$pattern = '/msgid "Add User"/';
272
		$this->assertRegExp($pattern, $result);
273
	}
274
 
275
/**
276
 * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
277
 *
278
 * @return void
279
 */
280
	public function testExtractExcludePlugins() {
281
		App::build(array(
282
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
283
		));
284
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
285
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
286
		$this->Task = $this->getMock('ExtractTask',
287
			array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
288
			array($this->out, $this->out, $this->in)
289
		);
290
		$this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
291
 
292
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
293
		$this->Task->params['output'] = $this->path . DS;
294
		$this->Task->params['exclude-plugins'] = true;
295
 
296
		$this->Task->execute();
297
		$result = file_get_contents($this->path . DS . 'default.pot');
298
		$this->assertNotRegExp('#TestPlugin#', $result);
299
	}
300
 
301
/**
302
 * Test that is possible to extract messages form a single plugin
303
 *
304
 * @return void
305
 */
306
	public function testExtractPlugin() {
307
		App::build(array(
308
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
309
		));
310
 
311
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
312
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
313
		$this->Task = $this->getMock('ExtractTask',
314
			array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
315
			array($this->out, $this->out, $this->in)
316
		);
317
 
318
		$this->Task->params['output'] = $this->path . DS;
319
		$this->Task->params['plugin'] = 'TestPlugin';
320
 
321
		$this->Task->execute();
322
		$result = file_get_contents($this->path . DS . 'default.pot');
323
		$this->assertNotRegExp('#Pages#', $result);
324
		$this->assertContains('translate.ctp:1', $result);
325
		$this->assertContains('This is a translatable string', $result);
326
		$this->assertContains('I can haz plugin model validation message', $result);
327
	}
328
 
329
/**
330
 * Tests that the task will inspect application models and extract the validation messages from them
331
 *
332
 * @return void
333
 */
334
	public function testExtractModelValidation() {
335
		App::build(array(
336
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
337
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
338
		), App::RESET);
339
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
340
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
341
		$this->Task = $this->getMock('ExtractTask',
342
			array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
343
			array($this->out, $this->out, $this->in)
344
		);
345
		$this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
346
 
347
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
348
		$this->Task->params['output'] = $this->path . DS;
349
		$this->Task->params['extract-core'] = 'no';
350
		$this->Task->params['exclude-plugins'] = true;
351
		$this->Task->params['ignore-model-validation'] = false;
352
 
353
		$this->Task->execute();
354
		$result = file_get_contents($this->path . DS . 'default.pot');
355
 
356
		$pattern = preg_quote('#Model/PersisterOne.php:validation for field title#', '\\');
357
		$this->assertRegExp($pattern, $result);
358
 
359
		$pattern = preg_quote('#Model/PersisterOne.php:validation for field body#', '\\');
360
		$this->assertRegExp($pattern, $result);
361
 
362
		$pattern = '#msgid "Post title is required"#';
363
		$this->assertRegExp($pattern, $result);
364
 
365
		$pattern = '#msgid "You may enter up to %s chars \(minimum is %s chars\)"#';
366
		$this->assertRegExp($pattern, $result);
367
 
368
		$pattern = '#msgid "Post body is required"#';
369
		$this->assertRegExp($pattern, $result);
370
 
371
		$pattern = '#msgid "Post body is super required"#';
372
		$this->assertRegExp($pattern, $result);
373
 
374
		$this->assertContains('msgid "double \\"quoted\\" validation"', $result, 'Strings with quotes not handled correctly');
375
		$this->assertContains("msgid \"single 'quoted' validation\"", $result, 'Strings with quotes not handled correctly');
376
	}
377
 
378
/**
379
 *  Tests that the task will inspect application models and extract the validation messages from them
380
 *	while using a custom validation domain for the messages set on the model itself
381
 *
382
 * @return void
383
 */
384
	public function testExtractModelValidationWithDomainInModel() {
385
		App::build(array(
386
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Model' . DS)
387
		));
388
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
389
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
390
		$this->Task = $this->getMock('ExtractTask',
391
			array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
392
			array($this->out, $this->out, $this->in)
393
		);
394
		$this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
395
 
396
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
397
		$this->Task->params['output'] = $this->path . DS;
398
		$this->Task->params['extract-core'] = 'no';
399
		$this->Task->params['exclude-plugins'] = true;
400
		$this->Task->params['ignore-model-validation'] = false;
401
 
402
		$this->Task->execute();
403
		$result = file_get_contents($this->path . DS . 'test_plugin.pot');
404
 
405
		$pattern = preg_quote('#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#', '\\');
406
		$this->assertRegExp($pattern, $result);
407
 
408
		$pattern = preg_quote('#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field body#', '\\');
409
		$this->assertRegExp($pattern, $result);
410
 
411
		$pattern = '#msgid "Post title is required"#';
412
		$this->assertRegExp($pattern, $result);
413
 
414
		$pattern = '#msgid "Post body is required"#';
415
		$this->assertRegExp($pattern, $result);
416
 
417
		$pattern = '#msgid "Post body is super required"#';
418
		$this->assertRegExp($pattern, $result);
419
	}
420
 
421
/**
422
 *  Test that the extract shell can obtain validation messages from models inside a specific plugin
423
 *
424
 * @return void
425
 */
426
	public function testExtractModelValidationInPlugin() {
427
		App::build(array(
428
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
429
		));
430
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
431
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
432
		$this->Task = $this->getMock('ExtractTask',
433
			array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
434
			array($this->out, $this->out, $this->in)
435
		);
436
 
437
		$this->Task->params['output'] = $this->path . DS;
438
		$this->Task->params['ignore-model-validation'] = false;
439
		$this->Task->params['plugin'] = 'TestPlugin';
440
 
441
		$this->Task->execute();
442
		$result = file_get_contents($this->path . DS . 'test_plugin.pot');
443
 
444
		$pattern = preg_quote('#Model/TestPluginPost.php:validation for field title#', '\\');
445
		$this->assertRegExp($pattern, $result);
446
 
447
		$pattern = preg_quote('#Model/TestPluginPost.php:validation for field body#', '\\');
448
		$this->assertRegExp($pattern, $result);
449
 
450
		$pattern = '#msgid "Post title is required"#';
451
		$this->assertRegExp($pattern, $result);
452
 
453
		$pattern = '#msgid "Post body is required"#';
454
		$this->assertRegExp($pattern, $result);
455
 
456
		$pattern = '#msgid "Post body is super required"#';
457
		$this->assertRegExp($pattern, $result);
458
 
459
		$pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#';
460
		$this->assertNotRegExp($pattern, $result);
461
	}
462
 
463
/**
464
 *  Test that the extract shell overwrites existing files with the overwrite parameter
465
 *
466
 * @return void
467
 */
468
	public function testExtractOverwrite() {
469
		$this->Task->interactive = false;
470
 
471
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
472
		$this->Task->params['output'] = $this->path . DS;
473
		$this->Task->params['extract-core'] = 'no';
474
		$this->Task->params['overwrite'] = true;
475
 
476
		file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
477
		$this->assertTrue(file_exists($this->path . DS . 'default.pot'));
478
		$original = file_get_contents($this->path . DS . 'default.pot');
479
 
480
		$this->Task->execute();
481
		$result = file_get_contents($this->path . DS . 'default.pot');
482
		$this->assertNotEquals($original, $result);
483
	}
484
 
485
/**
486
 *  Test that the extract shell scans the core libs
487
 *
488
 * @return void
489
 */
490
	public function testExtractCore() {
491
		$this->Task->interactive = false;
492
 
493
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
494
		$this->Task->params['output'] = $this->path . DS;
495
		$this->Task->params['extract-core'] = 'yes';
496
 
497
		$this->Task->execute();
498
		$this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
499
		$result = file_get_contents($this->path . DS . 'cake.pot');
500
 
501
		$pattern = '/msgid "Yesterday, %s"\nmsgstr ""\n/';
502
		$this->assertRegExp($pattern, $result);
503
 
504
		$this->assertTrue(file_exists($this->path . DS . 'cake_dev.pot'));
505
		$result = file_get_contents($this->path . DS . 'cake_dev.pot');
506
 
507
		$pattern = '/#: Console\/Templates\//';
508
		$this->assertNotRegExp($pattern, $result);
509
 
510
		$pattern = '/#: Test\//';
511
		$this->assertNotRegExp($pattern, $result);
512
	}
513
}