Skip to content

Commit

Permalink
Remove code in ExtractTask that parsed old style validation messages.
Browse files Browse the repository at this point in the history
Validation messages are now just normal i18n content and don't need
special handling at all.
  • Loading branch information
markstory committed Apr 8, 2014
1 parent bc15d4c commit 5f06f4f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 210 deletions.
113 changes: 0 additions & 113 deletions src/Console/Command/Task/ExtractTask.php
Expand Up @@ -93,13 +93,6 @@ class ExtractTask extends Shell {
*/
protected $_exclude = [];

/**
* Holds whether this call should extract model validation messages
*
* @var boolean
*/
protected $_extractValidation = true;

/**
* Holds the validation string domain to use for validation messages when extracting
*
Expand Down Expand Up @@ -183,9 +176,6 @@ public function execute() {
$this->_exclude = array_merge($this->_exclude, App::path('Plugin'));
}

if (!empty($this->params['ignore-model-validation']) || (!$this->_isExtractingApp() && empty($plugin))) {
$this->_extractValidation = false;
}
if (!empty($this->params['validation-domain'])) {
$this->_validationDomain = $this->params['validation-domain'];
}
Expand Down Expand Up @@ -288,12 +278,10 @@ protected function _extract() {
$this->out(__d('cake_console', 'Output Directory: ') . $this->_output);
$this->hr();
$this->_extractTokens();
$this->_extractValidationMessages();
$this->_buildFiles();
$this->_writeFiles();
$this->_paths = $this->_files = $this->_storage = [];
$this->_translations = $this->_tokens = [];
$this->_extractValidation = true;
$this->out();
$this->out(__d('cake_console', 'Done.'));
}
Expand Down Expand Up @@ -436,107 +424,6 @@ protected function _parse($functionName, $map) {
}
}

/**
* Looks for models in the application and extracts the validation messages
* to be added to the translation map
*
* @return void
*/
protected function _extractValidationMessages() {
if (!$this->_extractValidation) {
return;
}

$plugins = [null];
if (empty($this->params['exclude-plugins'])) {
$plugins = array_merge($plugins, App::objects('Plugin', null, false));
}
foreach ($plugins as $plugin) {
$this->_extractPluginValidationMessages($plugin);
}
}

/**
* Extract validation messages from application or plugin models
*
* @param string $plugin Plugin name or `null` to process application models
* @return void
*/
protected function _extractPluginValidationMessages($plugin = null) {
if (!empty($plugin)) {
if (!Plugin::loaded($plugin)) {
return;
}
$plugin = $plugin . '.';
}
$models = App::objects($plugin . 'Model', null, false);

foreach ($models as $model) {
$modelClass = App::classname($plugin . $model, 'Model');
$reflection = new \ReflectionClass($modelClass);
if (!$reflection->isSubClassOf('Cake\Model\Model')) {
continue;
}
$properties = $reflection->getDefaultProperties();
$validate = $properties['validate'];
if (empty($validate)) {
continue;
}

$file = $reflection->getFileName();
$domain = $this->_validationDomain;
if (!empty($properties['validationDomain'])) {
$domain = $properties['validationDomain'];
}
foreach ($validate as $field => $rules) {
$this->_processValidationRules($field, $rules, $file, $domain);
}
}
}

/**
* Process a validation rule for a field and looks for a message to be added
* to the translation map
*
* @param string $field the name of the field that is being processed
* @param array $rules the set of validation rules for the field
* @param string $file the file name where this validation rule was found
* @param string $domain default domain to bind the validations to
* @param string $category the translation category
* @return void
*/
protected function _processValidationRules($field, $rules, $file, $domain, $category = 'LC_MESSAGES') {
if (!is_array($rules)) {
return;
}

$dims = Hash::dimensions($rules);
if ($dims === 1 || ($dims === 2 && isset($rules['message']))) {
$rules = [$rules];
}

foreach ($rules as $rule => $validateProp) {
$msgid = null;
if (isset($validateProp['message'])) {
if (is_array($validateProp['message'])) {
$msgid = $validateProp['message'][0];
} else {
$msgid = $validateProp['message'];
}
} elseif (is_string($rule)) {
$msgid = $rule;
}
if ($msgid) {
$msgid = $this->_formatString(sprintf("'%s'", $msgid));
$details = [
'file' => $file,
'line' => 'validation for field ' . $field
];
$this->_addTranslation($category, $domain, $msgid, $details);
}
}
}

/**
* Build the translate template file contents out of obtained strings
*
Expand Down
99 changes: 2 additions & 97 deletions tests/TestCase/Console/Command/Task/ExtractTaskTest.php
Expand Up @@ -209,10 +209,10 @@ public function testExtractExcludePlugins() {
$this->out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
$this->in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
$this->Task = $this->getMock('Cake\Console\Command\Task\ExtractTask',
array('_isExtractingApp', '_extractValidationMessages', 'in', 'out', 'err', 'clear', '_stop'),
array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
array($this->out, $this->out, $this->in)
);
$this->Task->expects($this->exactly(2))
$this->Task->expects($this->exactly(1))
->method('_isExtractingApp')
->will($this->returnValue(true));

Expand Down Expand Up @@ -243,106 +243,11 @@ public function testExtractPlugin() {
$this->Task->params['output'] = $this->path . DS;
$this->Task->params['plugin'] = 'TestPlugin';

$this->markTestIncomplete('Extracting validation messages from plugin models is not working.');
$this->Task->execute();
$result = file_get_contents($this->path . DS . 'default.pot');
$this->assertNotRegExp('#Pages#', $result);
$this->assertRegExp('/translate\.ctp:\d+/', $result);
$this->assertContains('This is a translatable string', $result);
$this->assertContains('I can haz plugin model validation message', $result);
}

/**
* Tests that the task will inspect application models and extract the validation messages from them
*
* @return void
*/
public function testExtractModelValidation() {
$this->markTestIncomplete('Extracting validation messages is not working right now.');
Configure::write('App.namespace', 'TestApp');
Plugin::load('TestPlugin');

$this->out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
$this->in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
$this->Task = $this->getMock('Cake\Console\Command\Task\ExtractTask',
array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
array($this->out, $this->out, $this->in)
);
$this->Task->expects($this->exactly(2))
->method('_isExtractingApp')
->will($this->returnValue(true));

$this->Task->params['paths'] = TEST_APP . 'TestApp/';
$this->Task->params['output'] = $this->path . DS;
$this->Task->params['extract-core'] = 'no';
$this->Task->params['exclude-plugins'] = true;
$this->Task->params['ignore-model-validation'] = false;

$this->Task->execute();
$result = file_get_contents($this->path . DS . 'default.pot');

$pattern = preg_quote('#Model/PersisterOne.php:validation for field title#', '\\');
$this->assertRegExp($pattern, $result);

$pattern = preg_quote('#Model/PersisterOne.php:validation for field body#', '\\');
$this->assertRegExp($pattern, $result);

$pattern = '#msgid "Post title is required"#';
$this->assertRegExp($pattern, $result);

$pattern = '#msgid "You may enter up to %s chars \(minimum is %s chars\)"#';
$this->assertRegExp($pattern, $result);

$pattern = '#msgid "Post body is required"#';
$this->assertRegExp($pattern, $result);

$pattern = '#msgid "Post body is super required"#';
$this->assertRegExp($pattern, $result);

$this->assertContains('msgid "double \\"quoted\\" validation"', $result, 'Strings with quotes not handled correctly');
$this->assertContains("msgid \"single 'quoted' validation\"", $result, 'Strings with quotes not handled correctly');
}

/**
* Test that the extract shell can obtain validation messages from models inside a specific plugin
*
* @return void
*/
public function testExtractModelValidationInPlugin() {
$this->markTestIncomplete('Extracting validation messages is not working right now.');
Configure::write('App.namespace', 'TestApp');
Plugin::load('TestPlugin');
$this->out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
$this->in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
$this->Task = $this->getMock('Cake\Console\Command\Task\ExtractTask',
array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
array($this->out, $this->out, $this->in)
);

$this->Task->params['output'] = $this->path . DS;
$this->Task->params['ignore-model-validation'] = false;
$this->Task->params['plugin'] = 'TestPlugin';

$this->Task->execute();
$result = file_get_contents($this->path . DS . 'test_plugin.pot');

$pattern = preg_quote('#Model/TestPluginPost.php:validation for field title#', '\\');
$this->assertRegExp($pattern, $result);

$pattern = preg_quote('#Model/TestPluginPost.php:validation for field body#', '\\');
$this->assertRegExp($pattern, $result);

$pattern = '#msgid "Post title is required"#';
$this->assertRegExp($pattern, $result);

$pattern = '#msgid "Post body is required"#';
$this->assertRegExp($pattern, $result);

$pattern = '#msgid "Post body is super required"#';
$this->assertRegExp($pattern, $result);

$pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#';
$this->assertNotRegExp($pattern, $result);
}

/**
Expand Down

0 comments on commit 5f06f4f

Please sign in to comment.