Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 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
 
356
/**
357
 *  Tests that the task will inspect application models and extract the validation messages from them
358
 *	while using a custom validation domain for the messages set on the model itself
359
 *
360
 * @return void
361
 */
362
	public function testExtractModelValidationWithDomainInModel() {
363
		App::build(array(
364
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Model' . DS)
365
		));
366
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
367
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
368
		$this->Task = $this->getMock('ExtractTask',
369
			array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
370
			array($this->out, $this->out, $this->in)
371
		);
372
		$this->Task->expects($this->exactly(2))->method('_isExtractingApp')->will($this->returnValue(true));
373
 
374
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
375
		$this->Task->params['output'] = $this->path . DS;
376
		$this->Task->params['extract-core'] = 'no';
377
		$this->Task->params['exclude-plugins'] = true;
378
		$this->Task->params['ignore-model-validation'] = false;
379
 
380
		$this->Task->execute();
381
		$result = file_get_contents($this->path . DS . 'test_plugin.pot');
382
 
383
		$pattern = preg_quote('#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#', '\\');
384
		$this->assertRegExp($pattern, $result);
385
 
386
		$pattern = preg_quote('#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field body#', '\\');
387
		$this->assertRegExp($pattern, $result);
388
 
389
		$pattern = '#msgid "Post title is required"#';
390
		$this->assertRegExp($pattern, $result);
391
 
392
		$pattern = '#msgid "Post body is required"#';
393
		$this->assertRegExp($pattern, $result);
394
 
395
		$pattern = '#msgid "Post body is super required"#';
396
		$this->assertRegExp($pattern, $result);
397
	}
398
 
399
/**
400
 *  Test that the extract shell can obtain validation messages from models inside a specific plugin
401
 *
402
 * @return void
403
 */
404
	public function testExtractModelValidationInPlugin() {
405
		App::build(array(
406
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
407
		));
408
		$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
409
		$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
410
		$this->Task = $this->getMock('ExtractTask',
411
			array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
412
			array($this->out, $this->out, $this->in)
413
		);
414
 
415
		$this->Task->params['output'] = $this->path . DS;
416
		$this->Task->params['ignore-model-validation'] = false;
417
		$this->Task->params['plugin'] = 'TestPlugin';
418
 
419
		$this->Task->execute();
420
		$result = file_get_contents($this->path . DS . 'test_plugin.pot');
421
 
422
		$pattern = preg_quote('#Model/TestPluginPost.php:validation for field title#', '\\');
423
		$this->assertRegExp($pattern, $result);
424
 
425
		$pattern = preg_quote('#Model/TestPluginPost.php:validation for field body#', '\\');
426
		$this->assertRegExp($pattern, $result);
427
 
428
		$pattern = '#msgid "Post title is required"#';
429
		$this->assertRegExp($pattern, $result);
430
 
431
		$pattern = '#msgid "Post body is required"#';
432
		$this->assertRegExp($pattern, $result);
433
 
434
		$pattern = '#msgid "Post body is super required"#';
435
		$this->assertRegExp($pattern, $result);
436
 
437
		$pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#';
438
		$this->assertNotRegExp($pattern, $result);
439
	}
440
 
441
/**
442
 *  Test that the extract shell overwrites existing files with the overwrite parameter
443
 *
444
 * @return void
445
 */
446
	public function testExtractOverwrite() {
447
		$this->Task->interactive = false;
448
 
449
		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
450
		$this->Task->params['output'] = $this->path . DS;
451
		$this->Task->params['extract-core'] = 'no';
452
		$this->Task->params['overwrite'] = true;
453
 
454
		file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
455
		$this->assertTrue(file_exists($this->path . DS . 'default.pot'));
456
		$original = file_get_contents($this->path . DS . 'default.pot');
457
 
458
		$this->Task->execute();
459
		$result = file_get_contents($this->path . DS . 'default.pot');
460
		$this->assertNotEquals($original, $result);
461
	}
462
 
463
/**
464
 *  Test that the extract shell scans the core libs
465
 *
466
 * @return void
467
 */
468
	public function testExtractCore() {
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'] = 'yes';
474
 
475
		$this->Task->execute();
476
		$this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
477
		$result = file_get_contents($this->path . DS . 'cake.pot');
478
 
479
		$pattern = '/msgid "Yesterday, %s"\nmsgstr ""\n/';
480
		$this->assertRegExp($pattern, $result);
481
 
482
		$this->assertTrue(file_exists($this->path . DS . 'cake_dev.pot'));
483
		$result = file_get_contents($this->path . DS . 'cake_dev.pot');
484
 
485
		$pattern = '/#: Console\/Templates\//';
486
		$this->assertNotRegExp($pattern, $result);
487
 
488
		$pattern = '/#: Test\//';
489
		$this->assertNotRegExp($pattern, $result);
490
	}
491
}