Browse Source

Implementing validation domain extraction in the ExtractionTask

Jose Lorenzo Rodriguez 14 years ago
parent
commit
33c74b6062

+ 18 - 4
lib/Cake/Console/Command/Task/ExtractTask.php

@@ -105,6 +105,13 @@ class ExtractTask extends Shell {
 	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
  *
  * @return void
@@ -147,6 +154,9 @@ class ExtractTask extends Shell {
 		if (!empty($this->params['ignore-model-validation']) || !$this->_isExtractingApp()) {
 			$this->_extractValidation = false;
 		}
+		if (!empty($this->params['validation-domain'])) {
+			$this->_validationDomain = $this->params['validation-domain'];
+		}
 
 		if (isset($this->params['output'])) {
 			$this->_output = $this->params['output'];
@@ -228,12 +238,12 @@ class ExtractTask extends Shell {
 			->addOption('exclude-plugins', array(
 				'boolean' => 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(
 				'boolean' => true,
 				'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(
 				'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) {
 			return;
 		}
-		$models = App::objects('Model');
+		$models = App::objects('Model', null, false);
 		App::uses('AppModel', 'Model');
 		foreach ($models as $model) {
 			App::uses($model, 'Model');
@@ -343,8 +353,12 @@ class ExtractTask extends Shell {
 			if (empty($validate)) {
 				continue;
 			}
+
 			$file = $reflection->getFileName();
-			$domain = 'default';
+			$domain = $this->_validationDomain;
+			if (!empty($properties['validationDomain'])) {
+				$domain = $properties['validationDomain'];
+			}
 			foreach ($validate as $field => $rules) {
 				$this->_processValidationRules($field, $rules, $file, $domain);
 			}

+ 9 - 24
lib/Cake/Model/Model.php

@@ -134,6 +134,15 @@ class Model extends Object {
  */
 	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.
  *
@@ -169,14 +178,6 @@ class Model extends Object {
 	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
  * caching only, the results are not stored beyond the current request.
  *
@@ -346,22 +347,6 @@ class Model extends Object {
 	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.
  *
  * @var boolean

+ 46 - 1
lib/Cake/Test/Case/Console/Command/Task/ExtractTaskTest.php

@@ -60,6 +60,7 @@ class ExtractTaskTest extends CakeTestCase {
 
 		$Folder = new Folder($this->path);
 		$Folder->delete();
+		App::build();
 	}
 
 /**
@@ -236,7 +237,8 @@ class ExtractTaskTest extends CakeTestCase {
  */
 	public function testExtractModelValidation() {
 		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->in = $this->getMock('ConsoleInput', array(), array(), '', false);
@@ -269,4 +271,47 @@ class ExtractTaskTest extends CakeTestCase {
 		$pattern = '#msgid "Post body is super required"#';
 		$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);
+	}
+
 }

+ 36 - 0
lib/Cake/Test/test_app/Plugin/TestPlugin/Model/TestPluginPost.php

@@ -33,4 +33,40 @@ class TestPluginPost extends TestPluginAppModel {
  * @var string
  */
 	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';
+
 }