Browse Source

Start updating ViewTask.

Fix template name generation for prefixed actions. Since actions no
longer have prefixes in the names we can't use that as as way to
find the prefixed templates. ViewTask still supports using `admin_add`
as a way to generate `Foos/Admin/add.ctp` though. Add/Edit actions will
fallback to a `form` template if it exists.
mark_story 12 years ago
parent
commit
1d2a648717

+ 42 - 34
src/Console/Command/Task/ViewTask.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * The View Tasks handles creating and updating view files.
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -32,7 +30,7 @@ class ViewTask extends BakeTask {
  *
  * @var array
  */
-	public $tasks = ['Project', 'Controller', 'DbConfig', 'Template'];
+	public $tasks = ['Project', 'Controller', 'DbConfig', 'Model', 'Template'];
 
 /**
  * path to View directory
@@ -86,15 +84,19 @@ class ViewTask extends BakeTask {
  */
 	public function execute() {
 		parent::execute();
-		if (empty($this->args)) {
-			$this->_interactive();
-		}
-		if (empty($this->args[0])) {
-			return;
-		}
+
 		if (!isset($this->connection)) {
 			$this->connection = 'default';
 		}
+
+		if (empty($this->args)) {
+			$this->out(__d('cake_console', 'Possible tables to bake views for based on your current database:'));
+			foreach ($this->Model->listAll() as $table) {
+				$this->out('- ' . $this->_controllerName($table));
+			}
+			return true;
+		}
+
 		$action = null;
 		$this->controllerName = $this->_controllerName($this->args[0]);
 
@@ -268,6 +270,7 @@ class ViewTask extends BakeTask {
 			$this->err(__d('cake_console', "The file '%s' could not be found.\nIn order to bake a view, you'll need to first create the controller.", $file));
 			return $this->_stop();
 		}
+
 		$controllerObj = new $controllerClassName();
 		$controllerObj->plugin = $this->plugin;
 		$controllerObj->constructClasses();
@@ -279,8 +282,8 @@ class ViewTask extends BakeTask {
 			$displayField = $modelObj->displayField;
 			$singularVar = Inflector::variable($modelClass);
 			$singularHumanName = $this->_singularHumanName($this->controllerName);
-			$schema = $modelObj->schema(true);
-			$fields = array_keys($schema);
+			$schema = $modelObj->schema();
+			$fields = $schema->fields();
 			$associations = $this->_associations($modelObj);
 		} else {
 			$primaryKey = $displayField = null;
@@ -394,22 +397,24 @@ class ViewTask extends BakeTask {
 			return $this->template;
 		}
 		$themePath = $this->Template->getThemePath();
+
+		if (!empty($this->params['prefix'])) {
+			$prefixed = Inflector::underscore($this->params['prefix']) . '_' . $action;
+			if (file_exists($themePath . 'views/' . $prefixed . '.ctp')) {
+				return $prefixed;
+			}
+			$generic = preg_replace('/(.*)(_add|_edit)$/', '\1_form', $prefixed);
+			if (file_exists($themePath . 'views/' . $generic . '.ctp')) {
+				return $generic;
+			}
+		}
 		if (file_exists($themePath . 'views/' . $action . '.ctp')) {
 			return $action;
 		}
-		$template = $action;
-		$prefixes = Configure::read('Routing.prefixes');
-		foreach ((array)$prefixes as $prefix) {
-			if (strpos($template, $prefix) !== false) {
-				$template = str_replace($prefix . '_', '', $template);
-			}
-		}
-		if (in_array($template, ['add', 'edit'])) {
-			$template = 'form';
-		} elseif (preg_match('@(_add|_edit)$@', $template)) {
-			$template = str_replace(['_add', '_edit'], '_form', $template);
+		if (in_array($action, ['add', 'edit'])) {
+			return 'form';
 		}
-		return $template;
+		return $action;
 	}
 
 /**
@@ -431,9 +436,6 @@ class ViewTask extends BakeTask {
 		])->addOption('plugin', [
 			'short' => 'p',
 			'help' => __d('cake_console', 'Plugin to bake the view into.')
-		])->addOption('admin', [
-			'help' => __d('cake_console', 'Set to only bake views for a prefix in Routing.prefixes'),
-			'boolean' => true
 		])->addOption('theme', [
 			'short' => 't',
 			'help' => __d('cake_console', 'Theme to use when baking code.')
@@ -443,6 +445,8 @@ class ViewTask extends BakeTask {
 		])->addOption('force', [
 			'short' => 'f',
 			'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
+		])->addOption('prefix', [
+			'help' => __d('cake_console', 'The routing prefix to generate views for.'),
 		])->addSubcommand('all', [
 			'help' => __d('cake_console', 'Bake all CRUD action views for all controllers. Requires models and controllers to exist.')
 		])->epilog(
@@ -459,17 +463,21 @@ class ViewTask extends BakeTask {
  * @return array $associations
  */
 	protected function _associations(Model $model) {
-		$keys = ['belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'];
+		$keys = ['BelongsTo', 'HasOne', 'HasMany', 'BelongsToMany'];
 		$associations = [];
 
 		foreach ($keys as $type) {
-			foreach ($model->{$type} as $assocKey => $assocData) {
-				list(, $modelClass) = pluginSplit($assocData['className']);
-				$associations[$type][$assocKey]['primaryKey'] = $model->{$assocKey}->primaryKey;
-				$associations[$type][$assocKey]['displayField'] = $model->{$assocKey}->displayField;
-				$associations[$type][$assocKey]['foreignKey'] = $assocData['foreignKey'];
-				$associations[$type][$assocKey]['controller'] = Inflector::pluralize(Inflector::underscore($modelClass));
-				$associations[$type][$assocKey]['fields'] = array_keys($model->{$assocKey}->schema(true));
+			foreach ($model->associations()->type($type) as $assoc) {
+				$target = $assoc->target();
+				$assocName = $assoc->name();
+				$alias = $target->alias();
+				$assoiations[$type][$assocName] = [
+					'primaryKey' => $target->primaryKey(),
+					'displayField' => $target->displayField(),
+					'foreignKey' => $assoc->foreignKey(),
+					'controller' => Inflector::underscore($alias),
+					'fields' => $target->schema()->columns(),
+				];
 			}
 		}
 		return $associations;

+ 73 - 129
tests/TestCase/Console/Command/Task/ViewTaskTest.php

@@ -1,9 +1,5 @@
 <?php
 /**
- * ViewTask Test file
- *
- * Test Case for view generation shell task
- *
  * CakePHP : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -32,24 +28,13 @@ use Cake\TestSuite\TestCase;
  */
 class ViewTaskCommentsTable extends Table {
 
-/**
- * Table name
- *
- * @var string
- */
-	protected $_table = 'comments';
-
-/**
- * Belongs To Associations
- *
- * @var array
- */
-	public $belongsTo = array(
-		'Article' => array(
-			'className' => 'TestTest.ViewTaskArticle',
+	public function intialize(array $config) {
+		$this->table('comments');
+		$this->belongsTo('Articles', [
+			'className' => 'TestTest.ViewTaskArticles',
 			'foreignKey' => 'article_id'
-		)
-	);
+		]);
+	}
 }
 
 /**
@@ -58,12 +43,9 @@ class ViewTaskCommentsTable extends Table {
  */
 class ViewTaskArticlesTable extends Table {
 
-/**
- * Table name
- *
- * @var string
- */
-	protected $_table = 'articles';
+	public function intialize(array $config) {
+		$this->table('articles');
+	}
 }
 
 /**
@@ -72,7 +54,7 @@ class ViewTaskArticlesTable extends Table {
  */
 class ViewTaskCommentsController extends Controller {
 
-	public $modelClass = 'Cake\Model\Repository\ViewTaskCommentsTable';
+	public $modelClass = 'Cake\Test\TestCase\Console\Command\ViewTaskCommentsTable';
 
 /**
  * Testing public controller action
@@ -94,80 +76,7 @@ class ViewTaskCommentsController extends Controller {
 
 
 /**
- * Test View Task Articles Controller
- *
- */
-class ViewTaskArticlesController extends Controller {
-
-	public $modelClass = 'Cake\Model\ViewTaskArticle';
-
-/**
- * Test public controller action
- *
- * @return void
- */
-	public function index() {
-	}
-
-/**
- * Test public controller action
- *
- * @return void
- */
-	public function add() {
-	}
-
-/**
- * Test admin prefixed controller action
- *
- * @return void
- */
-	public function admin_index() {
-	}
-
-/**
- * Test admin prefixed controller action
- *
- * @return void
- */
-	public function admin_add() {
-	}
-
-/**
- * Test admin prefixed controller action
- *
- * @return void
- */
-	public function admin_view() {
-	}
-
-/**
- * Test admin prefixed controller action
- *
- * @return void
- */
-	public function admin_edit() {
-	}
-
-/**
- * Test admin prefixed controller action
- *
- * @return void
- */
-	public function admin_delete() {
-	}
-
-}
-
-// Alias classes
-class_alias(__NAMESPACE__ . '\ViewTaskArticlesController', 'Cake\Controller\ViewTaskArticlesController');
-class_alias(__NAMESPACE__ . '\ViewTaskCommentsController', 'Cake\Controller\ViewTaskCommentsController');
-class_alias(__NAMESPACE__ . '\ViewTaskCommentsTable', 'Cake\Model\Repository\ViewTaskCommentsTable');
-class_alias(__NAMESPACE__ . '\ViewTaskArticlesTable', 'Cake\Model\Repostiory\ViewTaskArticlesTable');
-
-/**
  * ViewTaskTest class
- *
  */
 class ViewTaskTest extends TestCase {
 
@@ -187,23 +96,23 @@ class ViewTaskTest extends TestCase {
  */
 	public function setUp() {
 		parent::setUp();
-		$this->markTestIncomplete('Model baking will not work as models do not work.');
 
-		$out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
-		$in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
+		$out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
+		$in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
 
 		$this->Task = $this->getMock('Cake\Console\Command\Task\ViewTask',
-			array('in', 'err', 'createFile', '_stop'),
-			array($out, $out, $in)
+			['in', 'err', 'createFile', '_stop'],
+			[$out, $out, $in]
 		);
 		$this->Task->Template = new TemplateTask($out, $out, $in);
-		$this->Task->Controller = $this->getMock('Cake\Console\Command\Task\ControllerTask', array(), array($out, $out, $in));
-		$this->Task->Project = $this->getMock('Cake\Console\Command\Task\ProjectTask', array(), array($out, $out, $in));
-		$this->Task->DbConfig = $this->getMock('Cake\Console\Command\Task\DbConfigTask', array(), array($out, $out, $in));
+		$this->Task->Controller = $this->getMock('Cake\Console\Command\Task\ControllerTask', [], [$out, $out, $in]);
+		$this->Task->Model = $this->getMock('Cake\Console\Command\Task\ModelTask', [], [$out, $out, $in]);
+		$this->Task->Project = $this->getMock('Cake\Console\Command\Task\ProjectTask', [], [$out, $out, $in]);
+		$this->Task->DbConfig = $this->getMock('Cake\Console\Command\Task\DbConfigTask', [], [$out, $out, $in]);
 
 		$this->Task->path = TMP;
 		$this->Task->Template->params['theme'] = 'default';
-		$this->Task->Template->templatePaths = array('default' => CAKE . 'Console/Templates/default/');
+		$this->Task->Template->templatePaths = ['default' => CAKE . 'Console/Templates/default/'];
 	}
 
 /**
@@ -213,7 +122,7 @@ class ViewTaskTest extends TestCase {
  */
 	public function tearDown() {
 		parent::tearDown();
-		unset($this->Task, $this->Dispatch);
+		unset($this->Task);
 	}
 
 /**
@@ -224,15 +133,15 @@ class ViewTaskTest extends TestCase {
 	public function testGetContent() {
 		$vars = array(
 			'modelClass' => 'TestViewModel',
-			'schema' => array(),
+			'schema' => [],
 			'primaryKey' => 'id',
 			'displayField' => 'name',
 			'singularVar' => 'testViewModel',
 			'pluralVar' => 'testViewModels',
 			'singularHumanName' => 'Test View Model',
 			'pluralHumanName' => 'Test View Models',
-			'fields' => array('id', 'name', 'body'),
-			'associations' => array()
+			'fields' => ['id', 'name', 'body'],
+			'associations' => []
 		);
 		$result = $this->Task->getContent('view', $vars);
 
@@ -247,26 +156,25 @@ class ViewTaskTest extends TestCase {
 	}
 
 /**
- * test getContent() using an admin_prefixed action.
+ * test getContent() using a routing prefix action.
  *
  * @return void
  */
-	public function testGetContentWithAdminAction() {
-		$_back = Configure::read('Routing');
-		Configure::write('Routing.prefixes', array('admin'));
+	public function testGetContentWithRoutingPrefix() {
 		$vars = array(
 			'modelClass' => 'TestViewModel',
-			'schema' => array(),
+			'schema' => [],
 			'primaryKey' => 'id',
 			'displayField' => 'name',
 			'singularVar' => 'testViewModel',
 			'pluralVar' => 'testViewModels',
 			'singularHumanName' => 'Test View Model',
 			'pluralHumanName' => 'Test View Models',
-			'fields' => array('id', 'name', 'body'),
-			'associations' => array()
+			'fields' => ['id', 'name', 'body'],
+			'associations' => []
 		);
-		$result = $this->Task->getContent('admin_view', $vars);
+		$this->Task->params['prefix'] = 'Admin';
+		$result = $this->Task->getContent('view', $vars);
 
 		$this->assertRegExp('/Delete Test View Model/', $result);
 		$this->assertRegExp('/Edit Test View Model/', $result);
@@ -277,12 +185,10 @@ class ViewTaskTest extends TestCase {
 		$this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
 		$this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
 
-		$result = $this->Task->getContent('admin_add', $vars);
+		$result = $this->Task->getContent('add', $vars);
 		$this->assertRegExp("/input\('name'\)/", $result);
 		$this->assertRegExp("/input\('body'\)/", $result);
 		$this->assertRegExp('/List Test View Models/', $result);
-
-		Configure::write('Routing', $_back);
 	}
 
 /**
@@ -291,6 +197,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeView() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->controllerName = 'ViewTaskComments';
 
 		$this->Task->expects($this->at(0))->method('createFile')
@@ -308,6 +215,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeEdit() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->controllerName = 'ViewTaskComments';
 
 		$this->Task->expects($this->at(0))->method('createFile')
@@ -324,6 +232,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeIndex() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->controllerName = 'ViewTaskComments';
 
 		$this->Task->expects($this->at(0))->method('createFile')
@@ -340,6 +249,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeWithNoTemplate() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->controllerName = 'ViewTaskComments';
 
 		$this->Task->expects($this->never())->method('createFile');
@@ -352,6 +262,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeWithPlugin() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->markTestIncomplete('Still fails because of issues with modelClass');
 
 		$this->Task->controllerName = 'ViewTaskComments';
@@ -379,6 +290,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testBakeActions() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->controllerName = 'ViewTaskComments';
 
 		$this->Task->expects($this->at(0))->method('createFile')
@@ -406,6 +318,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testCustomAction() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->controllerName = 'ViewTaskComments';
 
 		$this->Task->expects($this->any())->method('in')
@@ -426,6 +339,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteIntoAll() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->args[0] = 'all';
 
 		$this->Task->Controller->expects($this->once())->method('listAll')
@@ -452,6 +366,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteIntoAllWithActionName() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->args = array('all', 'index');
 
 		$this->Task->Controller->expects($this->once())->method('listAll')
@@ -472,6 +387,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithActionParam() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->args[0] = 'ViewTaskComments';
 		$this->Task->args[1] = 'view';
 
@@ -490,6 +406,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithController() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->args[0] = 'ViewTaskComments';
 
 		$this->Task->expects($this->at(0))->method('createFile')
@@ -523,6 +440,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithControllerVariations($name) {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->args = array($name);
 
 		$this->Task->expects($this->at(0))->method('createFile')
@@ -545,6 +463,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithControllerAndAdminFlag() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$_back = Configure::read('Routing');
 		Configure::write('Routing.prefixes', array('admin'));
 		$this->Task->args[0] = 'ViewTaskArticles';
@@ -572,6 +491,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteInteractive() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->connection = 'test';
 		$this->Task->args = array();
 		$this->Task->params = array();
@@ -616,6 +536,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteWithAlternateTemplates() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		$this->Task->connection = 'test';
 		$this->Task->args = array('ViewTaskComments', 'index', 'list');
 		$this->Task->params = array();
@@ -634,6 +555,7 @@ class ViewTaskTest extends TestCase {
  * @return void
  */
 	public function testExecuteInteractiveWithAdmin() {
+		$this->markTestIncomplete('Model baking will not work as models do not work.');
 		Configure::write('Routing.prefixes', array('admin'));
 		$this->Task->connection = 'test';
 		$this->Task->args = array();
@@ -687,18 +609,40 @@ class ViewTaskTest extends TestCase {
 		$result = $this->Task->getTemplate('add');
 		$this->assertEquals('form', $result);
 
-		Configure::write('Routing.prefixes', array('admin'));
+		$result = $this->Task->getTemplate('edit');
+		$this->assertEquals('form', $result);
+
+		$result = $this->Task->getTemplate('view');
+		$this->assertEquals('view', $result);
+
+		$result = $this->Task->getTemplate('index');
+		$this->assertEquals('index', $result);
+	}
 
-		$result = $this->Task->getTemplate('admin_add');
+/**
+ * Test getting prefixed views.
+ *
+ * @return void
+ */
+	public function testGetTemplatePrefixed() {
+		$this->Task->params['prefix'] = 'Admin';
+
+		$result = $this->Task->getTemplate('add');
 		$this->assertEquals('form', $result);
 
 		$this->Task->Template->templatePaths = array(
-			'test' => CAKE . 'Test/' . 'TestApp/Console/Templates/test/'
+			'test' => CORE_TESTS . '/test_app/TestApp/Console/Templates/test/'
 		);
 		$this->Task->Template->params['theme'] = 'test';
 
-		$result = $this->Task->getTemplate('admin_edit');
+		$result = $this->Task->getTemplate('edit');
 		$this->assertEquals('admin_edit', $result);
+
+		$result = $this->Task->getTemplate('add');
+		$this->assertEquals('admin_form', $result);
+
+		$result = $this->Task->getTemplate('view');
+		$this->assertEquals('view', $result);
 	}
 
 }

+ 0 - 0
tests/test_app/TestApp/Console/Templates/test/views/admin_form.ctp