Implementing validation domain extraction in the ExtractionTask

This commit is contained in:
Jose Lorenzo Rodriguez 2011-06-20 00:36:31 -04:30
parent 93ac47fa60
commit 33c74b6062
4 changed files with 109 additions and 29 deletions

View file

@ -104,6 +104,13 @@ class ExtractTask extends Shell {
*/ */
protected $_extractValidation = true; protected $_extractValidation = true;
/**
* Holds the validation string domain to use for validation messages when extracting
*
* @var boolean
*/
protected $_validationDomain = 'default';
/** /**
* Execution method always used for tasks * Execution method always used for tasks
* *
@ -147,6 +154,9 @@ class ExtractTask extends Shell {
if (!empty($this->params['ignore-model-validation']) || !$this->_isExtractingApp()) { if (!empty($this->params['ignore-model-validation']) || !$this->_isExtractingApp()) {
$this->_extractValidation = false; $this->_extractValidation = false;
} }
if (!empty($this->params['validation-domain'])) {
$this->_validationDomain = $this->params['validation-domain'];
}
if (isset($this->params['output'])) { if (isset($this->params['output'])) {
$this->_output = $this->params['output']; $this->_output = $this->params['output'];
@ -228,12 +238,12 @@ class ExtractTask extends Shell {
->addOption('exclude-plugins', array( ->addOption('exclude-plugins', array(
'boolean' => true, 'boolean' => true,
'default' => true, 'default' => true,
'help' => __d('cake_console', 'Ignores all files in plugins.') 'help' => __d('cake_console', 'Ignores all files in plugins if this command is run inside from the same app directory')
)) ))
->addOption('ignore-model-validation', array( ->addOption('ignore-model-validation', array(
'boolean' => true, 'boolean' => true,
'default' => false, 'default' => false,
'help' => __d('cake_console', 'Ignores validation messages in the $validate property. Needs to be run in from the same app directory') 'help' => __d('cake_console', 'Ignores validation messages in the $validate property. If this flag is not set and the command is run from the same app directory, all messages in model validation rules will be extracted as tokens')
)) ))
->addOption('validation-domain', array( ->addOption('validation-domain', array(
'help' => __d('cake_console', 'If set to a value, the localization domain to be used for model validation messages') 'help' => __d('cake_console', 'If set to a value, the localization domain to be used for model validation messages')
@ -333,7 +343,7 @@ class ExtractTask extends Shell {
if (!$this->_extractValidation) { if (!$this->_extractValidation) {
return; return;
} }
$models = App::objects('Model'); $models = App::objects('Model', null, false);
App::uses('AppModel', 'Model'); App::uses('AppModel', 'Model');
foreach ($models as $model) { foreach ($models as $model) {
App::uses($model, 'Model'); App::uses($model, 'Model');
@ -343,8 +353,12 @@ class ExtractTask extends Shell {
if (empty($validate)) { if (empty($validate)) {
continue; continue;
} }
$file = $reflection->getFileName(); $file = $reflection->getFileName();
$domain = 'default'; $domain = $this->_validationDomain;
if (!empty($properties['validationDomain'])) {
$domain = $properties['validationDomain'];
}
foreach ($validate as $field => $rules) { foreach ($validate as $field => $rules) {
$this->_processValidationRules($field, $rules, $file, $domain); $this->_processValidationRules($field, $rules, $file, $domain);
} }

View file

@ -134,6 +134,15 @@ class Model extends Object {
*/ */
public $validationErrors = array(); public $validationErrors = array();
/**
* Nane of the validation string domain to use when translating validation errors.
*
* @var string
* @access public
*/
public $validationDomain = null;
/** /**
* Database table prefix for tables in model. * Database table prefix for tables in model.
* *
@ -168,14 +177,6 @@ class Model extends Object {
*/ */
public $tableToModel = array(); public $tableToModel = array();
/**
* Whether or not to log transactions for this model.
*
* @var boolean
* @access public
*/
public $logTransactions = false;
/** /**
* Whether or not to cache queries for this model. This enables in-memory * Whether or not to cache queries for this model. This enables in-memory
* caching only, the results are not stored beyond the current request. * caching only, the results are not stored beyond the current request.
@ -345,22 +346,6 @@ class Model extends Object {
*/ */
private $__insertID = null; private $__insertID = null;
/**
* The number of records returned by the last query.
*
* @var integer
* @access private
*/
private $__numRows = null;
/**
* The number of records affected by the last query.
*
* @var integer
* @access private
*/
private $__affectedRows = null;
/** /**
* Has the datasource been configured. * Has the datasource been configured.
* *

View file

@ -60,6 +60,7 @@ class ExtractTaskTest extends CakeTestCase {
$Folder = new Folder($this->path); $Folder = new Folder($this->path);
$Folder->delete(); $Folder->delete();
App::build();
} }
/** /**
@ -236,7 +237,8 @@ class ExtractTaskTest extends CakeTestCase {
*/ */
public function testExtractModelValidation() { public function testExtractModelValidation() {
App::build(array( App::build(array(
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS) 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
)); ));
$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false); $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
$this->in = $this->getMock('ConsoleInput', array(), array(), '', false); $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
@ -269,4 +271,47 @@ class ExtractTaskTest extends CakeTestCase {
$pattern = '#msgid "Post body is super required"#'; $pattern = '#msgid "Post body is super required"#';
$this->assertPattern($pattern, $result); $this->assertPattern($pattern, $result);
} }
/**
* Tests that the task will inspect application models and extract the validation messages from them
* while using a custom validation domain for the messages set on the model itself
*
* @return void
*/
public function testExtractModelValidationWithDomainInModel() {
App::build(array(
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Model' . DS)
));
$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
$this->Task = $this->getMock('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'] = CAKE . 'Test' . DS . 'test_app' . DS;
$this->Task->params['output'] = $this->path . DS;
$this->Task->params['exclude-plugins'] = true;
$this->Task->params['ignore-model-validation'] = false;
$this->Task->execute();
$result = file_get_contents($this->path . DS . 'test_plugin.pot');
$pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field title#';
$this->assertPattern($pattern, $result);
$pattern = '#Plugin/TestPlugin/Model/TestPluginPost.php:validation for field body#';
$this->assertPattern($pattern, $result);
$pattern = '#msgid "Post title is required"#';
$this->assertPattern($pattern, $result);
$pattern = '#msgid "Post body is required"#';
$this->assertPattern($pattern, $result);
$pattern = '#msgid "Post body is super required"#';
$this->assertPattern($pattern, $result);
}
} }

View file

@ -33,4 +33,40 @@ class TestPluginPost extends TestPluginAppModel {
* @var string * @var string
*/ */
public $useTable = 'posts'; public $useTable = 'posts';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'title' => array(
'rule' => array('custom', '.*'),
'allowEmpty' => true,
'required' => false,
'message' => 'Post title is required'
),
'body' => array(
'first_rule' => array(
'rule' => array('custom', '.*'),
'allowEmpty' => true,
'required' => false,
'message' => 'Post body is required'
),
'second_rule' => array(
'rule' => array('custom', '.*'),
'allowEmpty' => true,
'required' => false,
'message' => 'Post body is super required'
)
),
);
/**
* Translation domain to use for validation messages
*
* @var string
*/
public $validationDomain = 'test_plugin';
} }