Skip to content

Commit

Permalink
Completing support for extracting model validation messages from appl…
Browse files Browse the repository at this point in the history
…ication plugins
  • Loading branch information
lorenzo committed Jul 7, 2011
1 parent d2519ae commit eaa80bb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/Cake/Console/Command/Task/ExtractTask.php
Expand Up @@ -132,6 +132,7 @@ public function execute() {
CakePlugin::load($plugin);
}
$this->_paths = array(CakePlugin::path($plugin));
$this->params['plugin'] = $plugin;
} else {
$defaultPath = APP;
$message = __d('cake_console', "What is the path you would like to extract?\n[Q]uit [D]one");
Expand All @@ -157,7 +158,7 @@ public function execute() {
$this->_exclude = array_merge($this->_exclude, App::path('plugins'));
}

if (!empty($this->params['ignore-model-validation']) || !$this->_isExtractingApp()) {
if (!empty($this->params['ignore-model-validation']) || (!$this->_isExtractingApp() && empty($plugin))) {
$this->_extractValidation = false;
}
if (!empty($this->params['validation-domain'])) {
Expand Down Expand Up @@ -354,10 +355,16 @@ protected function _extractValidationMessages() {
if (!$this->_extractValidation) {
return;
}
$models = App::objects('Model', null, false);
App::uses('AppModel', 'Model');
$plugin = null;
if (!empty($this->params['plugin'])) {
App::uses($this->params['plugin'] . 'AppModel', $this->params['plugin'] . '.Model');
$plugin = $this->params['plugin'] . '.';
}
$models = App::objects($plugin . 'Model', null, false);

foreach ($models as $model) {
App::uses($model, 'Model');
App::uses($model, $plugin . 'Model');
$reflection = new ReflectionClass($model);
$properties = $reflection->getDefaultProperties();
$validate = $properties['validate'];
Expand Down
44 changes: 43 additions & 1 deletion lib/Cake/Test/Case/Console/Command/Task/ExtractTaskTest.php
Expand Up @@ -61,6 +61,7 @@ public function tearDown() {
$Folder = new Folder($this->path);
$Folder->delete();
App::build();
CakePlugin::unload();
}

/**
Expand Down Expand Up @@ -255,7 +256,6 @@ public function testExtractPlugin() {
$this->assertNoPattern('#Pages#', $result);
$this->assertContains('translate.ctp:1', $result);
$this->assertContains('This is a translatable string', $result);
CakePlugin::unload();
}

/**
Expand Down Expand Up @@ -342,4 +342,46 @@ public function testExtractModelValidationWithDomainInModel() {
$this->assertPattern($pattern, $result);
}


/**
* Test that the extract shell can obtain validation messages from models inside a specific plugin
*
* @return void
*/
public function testExtractModelValidationInPlugin() {
App::build(array(
'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);
$this->Task = $this->getMock('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 = '#Model/TestPluginPost.php:validation for field title#';
$this->assertPattern($pattern, $result);

$pattern = '#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);

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

0 comments on commit eaa80bb

Please sign in to comment.