Browse Source

Merge pull request #4012 from cakephp/issue-3997

Fix a bunch of small issues.
Mark Story 11 years ago
parent
commit
cbd98332fa

+ 0 - 1
src/Console/Command/Task/ModelTask.php

@@ -631,7 +631,6 @@ class ModelTask extends BakeTask {
 		$filename = $path . 'Table' . DS . $name . 'Table.php';
 		$this->out("\n" . __d('cake_console', 'Baking table class for %s...', $name), 1, Shell::QUIET);
 		$this->createFile($filename, $out);
-		TableRegistry::clear();
 		return $out;
 	}
 

+ 0 - 1
src/Console/Command/Task/TestTask.php

@@ -236,7 +236,6 @@ class TestTask extends BakeTask {
  * @return object And instance of the class that is going to be tested.
  */
 	public function buildTestSubject($type, $class) {
-		TableRegistry::clear();
 		if (strtolower($type) === 'table') {
 			list($namespace, $name) = namespaceSplit($class);
 			$name = str_replace('Table', '', $name);

+ 21 - 5
src/Console/Command/Task/ViewTask.php

@@ -56,11 +56,11 @@ class ViewTask extends BakeTask {
 	public $controllerClass = null;
 
 /**
- * Name of the table views are being baked against.
+ * Name with plugin of the model being used
  *
  * @var string
  */
-	public $tableName = null;
+	public $modelName = null;
 
 /**
  * The template file to use
@@ -119,6 +119,7 @@ class ViewTask extends BakeTask {
 			$controller = $this->params['controller'];
 		}
 		$this->controller($name, $controller);
+		$this->model($name);
 
 		if (isset($template)) {
 			$this->template = $template;
@@ -142,6 +143,21 @@ class ViewTask extends BakeTask {
 	}
 
 /**
+ * Set the model class for the table.
+ *
+ * @param string $table The table/model that is being baked.
+ * @return void
+ */
+	public function model($table) {
+		$tableName = $this->_controllerName($table);
+		$plugin = null;
+		if (!empty($this->params['plugin'])) {
+			$plugin = $this->params['plugin'] . '.';
+		}
+		$this->modelName = $plugin . $tableName;
+	}
+
+/**
  * Set the controller related properties.
  *
  * @param string $table The table/model that is being baked.
@@ -149,9 +165,9 @@ class ViewTask extends BakeTask {
  * @return void
  */
 	public function controller($table, $controller = null) {
-		$this->tableName = $this->_controllerName($table);
+		$tableName = $this->_controllerName($table);
 		if (empty($controller)) {
-			$controller = $this->tableName;
+			$controller = $tableName;
 		}
 		$this->controllerName = $controller;
 
@@ -237,7 +253,7 @@ class ViewTask extends BakeTask {
  * @return array Returns an variables to be made available to a view template
  */
 	protected function _loadController() {
-		$modelObj = TableRegistry::get($this->tableName);
+		$modelObj = TableRegistry::get($this->modelName);
 
 		$primaryKey = (array)$modelObj->primaryKey();
 		$displayField = $modelObj->displayField();

+ 22 - 19
src/Network/Request.php

@@ -402,10 +402,11 @@ class Request implements \ArrayAccess {
 	protected function _processFiles($post, $files) {
 		if (is_array($files)) {
 			foreach ($files as $key => $data) {
-				if (!is_numeric($key)) {
-					$this->_processFileData($post, '', $data, $key);
-				} else {
+				if (isset($data['tmp_name']) && is_string($data['tmp_name'])) {
 					$post[$key] = $data;
+				} else {
+					$keyData = isset($post[$key]) ? $post[$key] : [];
+					$post[$key] = $this->_processFileData($keyData, $data);
 				}
 			}
 		}
@@ -414,31 +415,33 @@ class Request implements \ArrayAccess {
 
 /**
  * Recursively walks the FILES array restructuring the data
- * into something sane and useable.
+ * into something sane and usable.
  *
- * @param array &$post The post data having files inserted into
+ * @param array $data The data being built
+ * @param array $post The post data being traversed
  * @param string $path The dot separated path to insert $data into.
- * @param array $data The data to traverse/insert.
- * @param string $field The terminal field name, which is the top level key in $_FILES.
+ * @param string $field The terminal field in the path. This is one of the
+ *   $_FILES properties e.g. name, tmp_name, size, error
  * @return void
  */
-	protected function _processFileData(&$post, $path, $data, $field) {
-		foreach ($data as $key => $fields) {
-			$newPath = $key;
-			if (!empty($path)) {
-				$newPath = $path . '.' . $key;
+	protected function _processFileData($data, $post, $path = '', $field = '') {
+		foreach ($post as $key => $fields) {
+			$newField = $field;
+			$newPath = $path;
+			if ($path === '' && $newField === '') {
+				$newField = $key;
+			}
+			if ($field === $newField) {
+				$newPath .= '.' . $key;
 			}
 			if (is_array($fields)) {
-				$this->_processFileData($post, $newPath, $fields, $field);
+				$data = $this->_processFileData($data, $fields, $newPath, $newField);
 			} else {
-				if (strpos($newPath, '.') === false) {
-					$newPath = $field . '.' . $key;
-				} else {
-					$newPath .= '.' . $field;
-				}
-				$post = Hash::insert($post, $newPath, $fields);
+				$newPath = trim($newPath . '.' . $field, '.');
+				$data = Hash::insert($data, $newPath, $fields);
 			}
 		}
+		return $data;
 	}
 
 /**

+ 0 - 12
tests/TestCase/Console/Command/Task/TestTaskTest.php

@@ -233,18 +233,6 @@ class TestTaskTest extends TestCase {
 	}
 
 /**
- * creating test subjects should clear the registry so the registry is always fresh
- *
- * @return void
- */
-	public function testRegistryClearWhenBuildingTestObjects() {
-		$articles = TableRegistry::get('Articles');
-		$this->Task->buildTestSubject('Table', 'Posts');
-
-		$this->assertFalse(TableRegistry::exists('Articles'));
-	}
-
-/**
  * Dataprovider for class name generation.
  *
  * @return array

+ 58 - 12
tests/TestCase/Console/Command/Task/ViewTaskTest.php

@@ -87,7 +87,9 @@ class ViewTaskTest extends TestCase {
  *
  * @var array
  */
-	public $fixtures = array('core.article', 'core.post', 'core.comment', 'core.articles_tag', 'core.tag');
+	public $fixtures = array(
+		'core.article', 'core.post', 'core.comment',
+		'core.articles_tag', 'core.tag', 'core.test_plugin_comment');
 
 /**
  * setUp method
@@ -160,7 +162,6 @@ class ViewTaskTest extends TestCase {
 	public function testControllerVariations($name) {
 		$this->Task->controller($name);
 		$this->assertEquals('ViewTaskComments', $this->Task->controllerName);
-		$this->assertEquals('ViewTaskComments', $this->Task->tableName);
 	}
 
 /**
@@ -172,7 +173,6 @@ class ViewTaskTest extends TestCase {
 		$this->Task->params['plugin'] = 'TestPlugin';
 		$this->Task->controller('Tests');
 		$this->assertEquals('Tests', $this->Task->controllerName);
-		$this->assertEquals('Tests', $this->Task->tableName);
 		$this->assertEquals(
 			'TestPlugin\Controller\TestsController',
 			$this->Task->controllerClass
@@ -188,7 +188,6 @@ class ViewTaskTest extends TestCase {
 		$this->Task->params['prefix'] = 'Admin';
 		$this->Task->controller('Posts');
 		$this->assertEquals('Posts', $this->Task->controllerName);
-		$this->assertEquals('Posts', $this->Task->tableName);
 		$this->assertEquals(
 			'TestApp\Controller\Admin\PostsController',
 			$this->Task->controllerClass
@@ -197,7 +196,6 @@ class ViewTaskTest extends TestCase {
 		$this->Task->params['plugin'] = 'TestPlugin';
 		$this->Task->controller('Comments');
 		$this->assertEquals('Comments', $this->Task->controllerName);
-		$this->assertEquals('Comments', $this->Task->tableName);
 		$this->assertEquals(
 			'TestPlugin\Controller\Admin\CommentsController',
 			$this->Task->controllerClass
@@ -212,7 +210,6 @@ class ViewTaskTest extends TestCase {
 	public function testControllerWithOverride() {
 		$this->Task->controller('Comments', 'Posts');
 		$this->assertEquals('Posts', $this->Task->controllerName);
-		$this->assertEquals('Comments', $this->Task->tableName);
 		$this->assertEquals(
 			'TestApp\Controller\PostsController',
 			$this->Task->controllerClass
@@ -220,6 +217,33 @@ class ViewTaskTest extends TestCase {
 	}
 
 /**
+ * Test the model() method.
+ *
+ * @return void
+ */
+	public function testModel() {
+		$this->Task->model('Articles');
+		$this->assertEquals('Articles', $this->Task->modelName);
+
+		$this->Task->model('NotThere');
+		$this->assertEquals('NotTheres', $this->Task->modelName);
+	}
+
+/**
+ * Test model() method with plugins.
+ *
+ * @return void
+ */
+	public function testModelPlugin() {
+		$this->Task->params['plugin'] = 'TestPlugin';
+		$this->Task->model('TestPluginComments');
+		$this->assertEquals(
+			'TestPlugin.TestPluginComments',
+			$this->Task->modelName
+		);
+	}
+
+/**
  * Test getPath()
  *
  * @return void
@@ -332,7 +356,7 @@ class ViewTaskTest extends TestCase {
  */
 	public function testBakeView() {
 		$this->Task->controllerName = 'ViewTaskComments';
-		$this->Task->tableName = 'ViewTaskComments';
+		$this->Task->modelName = 'ViewTaskComments';
 		$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
 
 		$this->Task->expects($this->at(0))
@@ -352,7 +376,7 @@ class ViewTaskTest extends TestCase {
  */
 	public function testBakeEdit() {
 		$this->Task->controllerName = 'ViewTaskComments';
-		$this->Task->tableName = 'ViewTaskComments';
+		$this->Task->modelName = 'ViewTaskComments';
 		$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
 
 		$this->Task->expects($this->at(0))->method('createFile')
@@ -373,7 +397,7 @@ class ViewTaskTest extends TestCase {
  */
 	public function testBakeIndex() {
 		$this->Task->controllerName = 'ViewTaskComments';
-		$this->Task->tableName = 'ViewTaskComments';
+		$this->Task->modelName = 'ViewTaskComments';
 		$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
 
 		$this->Task->expects($this->at(0))->method('createFile')
@@ -385,13 +409,35 @@ class ViewTaskTest extends TestCase {
 	}
 
 /**
+ * test Bake with plugins
+ *
+ * @return void
+ */
+	public function testBakeIndexPlugin() {
+		$this->Task->controllerName = 'ViewTaskComments';
+		$this->Task->modelName = 'TestPlugin.TestPluginComments';
+		$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
+		$table = TableRegistry::get('TestPlugin.TestPluginComments');
+		$table->belongsTo('Articles');
+
+		$this->Task->expects($this->at(0))
+			->method('createFile')
+			->with(
+				$this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
+				$this->stringContains('$viewTaskComment->article->id')
+			);
+
+		$this->Task->bake('index', true);
+	}
+
+/**
  * test that baking a view with no template doesn't make a file.
  *
  * @return void
  */
 	public function testBakeWithNoTemplate() {
 		$this->Task->controllerName = 'ViewTaskComments';
-		$this->Task->tableName = 'ViewTaskComments';
+		$this->Task->modelName = 'ViewTaskComments';
 		$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
 
 		$this->Task->expects($this->never())->method('createFile');
@@ -405,7 +451,7 @@ class ViewTaskTest extends TestCase {
  */
 	public function testBakeActions() {
 		$this->Task->controllerName = 'ViewTaskComments';
-		$this->Task->tableName = 'ViewTaskComments';
+		$this->Task->modelName = 'ViewTaskComments';
 		$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
 
 		$this->Task->expects($this->at(0))
@@ -435,7 +481,7 @@ class ViewTaskTest extends TestCase {
  */
 	public function testCustomAction() {
 		$this->Task->controllerName = 'ViewTaskComments';
-		$this->Task->tableName = 'ViewTaskComments';
+		$this->Task->modelName = 'ViewTaskComments';
 		$this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
 
 		$this->Task->expects($this->any())->method('in')

+ 92 - 225
tests/TestCase/Network/RequestTest.php

@@ -259,235 +259,102 @@ class RequestTest extends TestCase {
 	}
 
 /**
- * Test parsing of FILES array
+ * Test processing files with `file` field names.
  *
  * @return void
  */
-	public function testFilesParsing() {
-		$files = array(
-			'name' => array(
-				'File' => array(
-						array('data' => 'cake_sqlserver_patch.patch'),
-						array('data' => 'controller.diff'),
-						array('data' => ''),
-						array('data' => ''),
-					),
-					'Post' => array('attachment' => 'jquery-1.2.1.js'),
-				),
-			'type' => array(
-				'File' => array(
-					array('data' => ''),
-					array('data' => ''),
-					array('data' => ''),
-					array('data' => ''),
-				),
-				'Post' => array('attachment' => 'application/x-javascript'),
-			),
-			'tmp_name' => array(
-				'File' => array(
-					array('data' => '/private/var/tmp/phpy05Ywj'),
-					array('data' => '/private/var/tmp/php7MBztY'),
-					array('data' => ''),
-					array('data' => ''),
-				),
-				'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
-			),
-			'error' => array(
-				'File' => array(
-					array('data' => 0),
-					array('data' => 0),
-					array('data' => 4),
-					array('data' => 4)
-				),
-				'Post' => array('attachment' => 0)
-			),
-			'size' => array(
-				'File' => array(
-					array('data' => 6271),
-					array('data' => 350),
-					array('data' => 0),
-					array('data' => 0),
-				),
-				'Post' => array('attachment' => 80469)
-			),
-		);
-
-		$request = new Request(compact('files'));
-		$expected = array(
-			'File' => array(
-				array(
-					'data' => array(
-						'name' => 'cake_sqlserver_patch.patch',
-						'type' => '',
-						'tmp_name' => '/private/var/tmp/phpy05Ywj',
-						'error' => 0,
-						'size' => 6271,
-					)
-				),
-				array(
-					'data' => array(
-						'name' => 'controller.diff',
-						'type' => '',
-						'tmp_name' => '/private/var/tmp/php7MBztY',
-						'error' => 0,
-						'size' => 350,
-					)
-				),
-				array(
-					'data' => array(
-						'name' => '',
-						'type' => '',
-						'tmp_name' => '',
-						'error' => 4,
-						'size' => 0,
-					)
-				),
-				array(
-					'data' => array(
-						'name' => '',
-						'type' => '',
-						'tmp_name' => '',
-						'error' => 4,
-						'size' => 0,
-					)
-				),
-			),
-			'Post' => array(
-				'attachment' => array(
-					'name' => 'jquery-1.2.1.js',
-					'type' => 'application/x-javascript',
-					'tmp_name' => '/private/var/tmp/phpEwlrIo',
+	public function testProcessFilesNested() {
+		$files = [
+			'image_main' => [
+				'name' => ['file' => 'born on.txt'],
+				'type' => ['file' => 'text/plain'],
+				'tmp_name' => ['file' => '/private/var/tmp/php'],
+				'error' => ['file' => 0],
+				'size' => ['file' => 17178]
+			],
+			0 => [
+				'name' => ['image' => 'scratch.text'],
+				'type' => ['image' => 'text/plain'],
+				'tmp_name' => ['image' => '/private/var/tmp/phpChIZPb'],
+				'error' => ['image' => 0],
+				'size' => ['image' => 1490]
+			],
+			'pictures' => [
+				'name' => [
+					0 => ['file' => 'a-file.png'],
+					1 => ['file' => 'a-moose.png']
+				],
+				'type' => [
+					0 => ['file' => 'image/png'],
+					1 => ['file' => 'image/jpg']
+				],
+				'tmp_name' => [
+					0 => ['file' => '/tmp/file123'],
+					1 => ['file' => '/tmp/file234']
+				],
+				'error' => [
+					0 => ['file' => '0'],
+					1 => ['file' => '0']
+				],
+				'size' => [
+					0 => ['file' => 17188],
+					1 => ['file' => 2010]
+				],
+			]
+		];
+		$post = [
+			'pictures' => [
+				0 => ['name' => 'A cat'],
+				1 => ['name' => 'A moose']
+			],
+			0 => [
+				'name' => 'A dog'
+			]
+		];
+		$request = new Request(compact('files', 'post'));
+		$expected = [
+			'image_main' => [
+				'file' => [
+					'name' => 'born on.txt',
+					'type' => 'text/plain',
+					'tmp_name' => '/private/var/tmp/php',
 					'error' => 0,
-					'size' => 80469,
-				)
-			)
-		);
-		$this->assertEquals($expected, $request->data);
-
-		$files = array(
-			'name' => array(
-				'Document' => array(
-					1 => array(
-						'birth_cert' => 'born on.txt',
-						'passport' => 'passport.txt',
-						'drivers_license' => 'ugly pic.jpg'
-					),
-					2 => array(
-						'birth_cert' => 'aunt betty.txt',
-						'passport' => 'betty-passport.txt',
-						'drivers_license' => 'betty-photo.jpg'
-					),
-				),
-			),
-			'type' => array(
-				'Document' => array(
-					1 => array(
-						'birth_cert' => 'application/octet-stream',
-						'passport' => 'application/octet-stream',
-						'drivers_license' => 'application/octet-stream',
-					),
-					2 => array(
-						'birth_cert' => 'application/octet-stream',
-						'passport' => 'application/octet-stream',
-						'drivers_license' => 'application/octet-stream',
-					)
-				)
-			),
-			'tmp_name' => array(
-				'Document' => array(
-					1 => array(
-						'birth_cert' => '/private/var/tmp/phpbsUWfH',
-						'passport' => '/private/var/tmp/php7f5zLt',
-						'drivers_license' => '/private/var/tmp/phpMXpZgT',
-					),
-					2 => array(
-						'birth_cert' => '/private/var/tmp/php5kHZt0',
-						'passport' => '/private/var/tmp/phpnYkOuM',
-						'drivers_license' => '/private/var/tmp/php9Rq0P3',
-					)
-				)
-			),
-			'error' => array(
-				'Document' => array(
-					1 => array(
-						'birth_cert' => 0,
-						'passport' => 0,
-						'drivers_license' => 0,
-					),
-					2 => array(
-						'birth_cert' => 0,
-						'passport' => 0,
-						'drivers_license' => 0,
-					)
-				)
-			),
-			'size' => array(
-				'Document' => array(
-					1 => array(
-						'birth_cert' => 123,
-						'passport' => 458,
-						'drivers_license' => 875,
-					),
-					2 => array(
-						'birth_cert' => 876,
-						'passport' => 976,
-						'drivers_license' => 9783,
-					)
-				)
-			)
-		);
-
-		$request = new Request(compact('files'));
-		$expected = array(
-			'Document' => array(
-				1 => array(
-					'birth_cert' => array(
-						'name' => 'born on.txt',
-						'tmp_name' => '/private/var/tmp/phpbsUWfH',
-						'error' => 0,
-						'size' => 123,
-						'type' => 'application/octet-stream',
-					),
-					'passport' => array(
-						'name' => 'passport.txt',
-						'tmp_name' => '/private/var/tmp/php7f5zLt',
-						'error' => 0,
-						'size' => 458,
-						'type' => 'application/octet-stream',
-					),
-					'drivers_license' => array(
-						'name' => 'ugly pic.jpg',
-						'tmp_name' => '/private/var/tmp/phpMXpZgT',
-						'error' => 0,
-						'size' => 875,
-						'type' => 'application/octet-stream',
-					),
-				),
-				2 => array(
-					'birth_cert' => array(
-						'name' => 'aunt betty.txt',
-						'tmp_name' => '/private/var/tmp/php5kHZt0',
-						'error' => 0,
-						'size' => 876,
-						'type' => 'application/octet-stream',
-					),
-					'passport' => array(
-						'name' => 'betty-passport.txt',
-						'tmp_name' => '/private/var/tmp/phpnYkOuM',
-						'error' => 0,
-						'size' => 976,
-						'type' => 'application/octet-stream',
-					),
-					'drivers_license' => array(
-						'name' => 'betty-photo.jpg',
-						'tmp_name' => '/private/var/tmp/php9Rq0P3',
-						'error' => 0,
-						'size' => 9783,
-						'type' => 'application/octet-stream',
-					),
-				),
-			)
-		);
+					'size' => 17178,
+				]
+			],
+			'pictures' => [
+				0 => [
+					'name' => 'A cat',
+					'file' => [
+						'name' => 'a-file.png',
+						'type' => 'image/png',
+						'tmp_name' => '/tmp/file123',
+						'error' => '0',
+						'size' => 17188,
+					]
+				],
+				1 => [
+					'name' => 'A moose',
+					'file' => [
+						'name' => 'a-moose.png',
+						'type' => 'image/jpg',
+						'tmp_name' => '/tmp/file234',
+						'error' => '0',
+						'size' => 2010,
+					]
+				]
+			],
+			0 => [
+				'name' => 'A dog',
+				'image' => [
+					'name' => 'scratch.text',
+					'type' => 'text/plain',
+					'tmp_name' => '/private/var/tmp/phpChIZPb',
+					'error' => 0,
+					'size' => 1490
+				]
+			]
+		];
 		$this->assertEquals($expected, $request->data);
 	}