Skip to content

Commit

Permalink
Merge branch 'master' into 2.3
Browse files Browse the repository at this point in the history
Conflicts:
	lib/Cake/Console/Command/Task/ModelTask.php
	lib/Cake/Model/Model.php
  • Loading branch information
markstory committed Jan 27, 2013
2 parents 1fc9641 + ce7f85a commit 4af6039
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 54 deletions.
10 changes: 10 additions & 0 deletions lib/Cake/Console/Command/Task/ControllerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,22 @@ public function all() {
$this->listAll($this->connection, false);
ClassRegistry::config('Model', array('ds' => $this->connection));
$unitTestExists = $this->_checkUnitTest();

$admin = false;
if (!empty($this->params['admin'])) {
$admin = $this->Project->getPrefix();
}

foreach ($this->__tables as $table) {
$model = $this->_modelName($table);
$controller = $this->_controllerName($model);
App::uses($model, 'Model');
if (class_exists($model)) {
$actions = $this->bakeActions($controller);
if ($admin) {
$this->out(__d('cake_console', 'Adding %s methods', $admin));
$actions .= "\n" . $this->bakeActions($controller, $admin);
}
if ($this->bake($controller, $actions) && $unitTestExists) {
$this->bakeTest($controller);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Console/Command/Task/DbConfigTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected function _verify($config) {
$this->out(__d('cake_console', 'The following database configuration will be created:'));
$this->hr();
$this->out(__d('cake_console', "Name: %s", $name));
$this->out(__d('cake_console', "Datasource: %s", $datasource));
$this->out(__d('cake_console', "Datasource: %s", $datasource));
$this->out(__d('cake_console', "Persistent: %s", $persistent));
$this->out(__d('cake_console', "Host: %s", $host));

Expand Down
20 changes: 8 additions & 12 deletions lib/Cake/Console/Command/Task/ModelTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,21 +619,17 @@ public function findHasOneAndMany(Model $model, $associations) {
public function findHasAndBelongsToMany(Model $model, $associations) {
$foreignKey = $this->_modelKey($model->name);
foreach ($this->_tables as $otherTable) {
$tableName = null;
$offset = strpos($otherTable, $model->table . '_');
$otherOffset = strpos($otherTable, '_' . $model->table);

if ($offset !== false) {
$offset = strlen($model->table . '_');
$habtmName = $this->_modelName(substr($otherTable, $offset));
$associations['hasAndBelongsToMany'][] = array(
'alias' => $habtmName,
'className' => $habtmName,
'foreignKey' => $foreignKey,
'associationForeignKey' => $this->_modelKey($habtmName),
'joinTable' => $otherTable
);
} elseif ($otherOffset !== false) {
$habtmName = $this->_modelName(substr($otherTable, 0, $otherOffset));
if ($offset === 0) {
$tableName = substr($otherTable, strlen($model->table . '_'));
} elseif ($otherOffset === 0) {
$tableName = substr($otherTable, 0, $otherOffset);
}
if ($tableName && in_array($tableName, $this->_tables)) {
$habtmName = $this->_modelName($tableName);
$associations['hasAndBelongsToMany'][] = array(
'alias' => $habtmName,
'className' => $habtmName,
Expand Down
6 changes: 4 additions & 2 deletions lib/Cake/Console/Command/Task/PluginTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,17 @@ public function bake($plugin) {
$controllerFileName = $plugin . 'AppController.php';

$out = "<?php\n\n";
$out .= "App::uses('AppController', 'Controller');\n\n";
$out .= "class {$plugin}AppController extends AppController {\n\n";
$out .= "}\n\n";
$out .= "}\n";
$this->createFile($this->path . $plugin . DS . 'Controller' . DS . $controllerFileName, $out);

$modelFileName = $plugin . 'AppModel.php';

$out = "<?php\n\n";
$out .= "App::uses('AppModel', 'Model');\n\n";
$out .= "class {$plugin}AppModel extends AppModel {\n\n";
$out .= "}\n\n";
$out .= "}\n";
$this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);

$this->_modifyBootstrap($plugin);
Expand Down
3 changes: 2 additions & 1 deletion lib/Cake/Controller/Component/CookieComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ public function startup(Controller $controller) {
* @param string|array $key Key for the value
* @param mixed $value Value
* @param boolean $encrypt Set to true to encrypt value, false otherwise
* @param integer|string $expires Can be either Unix timestamp, or date string
* @param integer|string $expires Can be either the number of seconds until a cookie
* expires, or a strtotime compatible time offset.
* @return void
* @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::write
*/
Expand Down
5 changes: 3 additions & 2 deletions lib/Cake/Model/Datasource/CakeSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,10 @@ public static function write($name, $value = null) {
* @return void
*/
public static function destroy() {
if (self::started()) {
session_destroy();
if (!self::started()) {
self::start();
}
session_destroy();
self::clear();
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Cake/Model/Datasource/DboSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -1153,10 +1153,12 @@ protected function _filterResults(&$results, Model $model, $filtered = array())
}
$linkedModel = $model->{$className};
$filtering[] = $className;
foreach ($results as &$result) {
foreach ($results as $key => &$result) {
$data = $linkedModel->afterFind(array(array($className => $result[$className])), false);
if (isset($data[0][$className])) {
$result[$className] = $data[0][$className];
} else {
unset($results[$key]);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ protected function _createLinks() {
$plugin = null;

if (is_numeric($assoc)) {
unset ($this->{$type}[$assoc]);
unset($this->{$type}[$assoc]);
$assoc = $value;
$value = array();

Expand Down Expand Up @@ -1164,7 +1164,7 @@ public function set($one, $two = null) {

foreach ($fieldSet as $fieldName => $fieldValue) {
if (isset($this->validationErrors[$fieldName])) {
unset ($this->validationErrors[$fieldName]);
unset($this->validationErrors[$fieldName]);
}

if ($modelName === $this->alias) {
Expand Down Expand Up @@ -2525,7 +2525,7 @@ public function deleteAll($conditions, $cascade = true, $callbacks = false) {
'fields' => "{$this->alias}.{$this->primaryKey}",
'recursive' => 0), compact('conditions'))
);
if ($ids === false) {
if ($ids === false || $ids === null) {
return false;
}

Expand Down
7 changes: 6 additions & 1 deletion lib/Cake/Network/CakeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,12 @@ protected function _url() {
} elseif (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '://') === false) {
$uri = $_SERVER['REQUEST_URI'];
} elseif (isset($_SERVER['REQUEST_URI'])) {
$uri = substr($_SERVER['REQUEST_URI'], strlen(FULL_BASE_URL));
$qPosition = strpos($_SERVER['REQUEST_URI'], '?');
if ($qPosition !== false && strpos($_SERVER['REQUEST_URI'], '://') > $qPosition) {
$uri = $_SERVER['REQUEST_URI'];
} else {
$uri = substr($_SERVER['REQUEST_URI'], strlen(FULL_BASE_URL));
}
} elseif (isset($_SERVER['PHP_SELF']) && isset($_SERVER['SCRIPT_NAME'])) {
$uri = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['PHP_SELF']);
} elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
Expand Down
58 changes: 38 additions & 20 deletions lib/Cake/Test/Case/Console/Command/Task/ControllerTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public function setUp() {
array($out, $out, $in)
);
$this->Task->Test = $this->getMock('TestTask', array(), array($out, $out, $in));

if (!defined('ARTICLE_MODEL_CREATED')) {
$this->markTestSkipped('Could not run as an Article, Tag or Comment model was already loaded.');
}
}

/**
Expand Down Expand Up @@ -340,7 +344,6 @@ public function testBakeWithPlugin() {
* @return void
*/
public function testBakeActionsUsingSessions() {
$this->skipIf(!defined('ARTICLE_MODEL_CREATED'), 'Testing bakeActions requires Article, Comment & Tag Model to be undefined.');

$result = $this->Task->bakeActions('BakeArticles', null, true);

Expand Down Expand Up @@ -380,7 +383,6 @@ public function testBakeActionsUsingSessions() {
* @return void
*/
public function testBakeActionsWithNoSessions() {
$this->skipIf(!defined('ARTICLE_MODEL_CREATED'), 'Testing bakeActions requires Article, Tag, Comment Models to be undefined.');

$result = $this->Task->bakeActions('BakeArticles', null, false);

Expand Down Expand Up @@ -513,9 +515,7 @@ public function testExecuteIntoAll() {
if ($count != count($this->fixtures)) {
$this->markTestSkipped('Additional tables detected.');
}
if (!defined('ARTICLE_MODEL_CREATED')) {
$this->markTestSkipped('Execute into all could not be run as an Article, Tag or Comment model was already loaded.');
}

$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$this->Task->args = array('all');
Expand All @@ -532,15 +532,45 @@ public function testExecuteIntoAll() {
$this->Task->execute();
}

/**
* Test execute() with all and --admin
*
* @return void
*/
public function testExecuteIntoAllAdmin() {
$count = count($this->Task->listAll('test'));
if ($count != count($this->fixtures)) {
$this->markTestSkipped('Additional tables detected.');
}

$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$this->Task->args = array('all');
$this->Task->params['admin'] = true;

$this->Task->Project->expects($this->any())
->method('getPrefix')
->will($this->returnValue('admin_'));
$this->Task->expects($this->any())
->method('_checkUnitTest')
->will($this->returnValue(true));
$this->Task->Test->expects($this->once())->method('bake');

$filename = '/my/path/BakeArticlesController.php';
$this->Task->expects($this->once())->method('createFile')->with(
$filename,
$this->stringContains('function admin_index')
)->will($this->returnValue(true));

$this->Task->execute();
}

/**
* test that `cake bake controller foos` works.
*
* @return void
*/
public function testExecuteWithController() {
if (!defined('ARTICLE_MODEL_CREATED')) {
$this->markTestSkipped('Execute with scaffold param requires no Article, Tag or Comment model to be defined');
}
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$this->Task->args = array('BakeArticles');
Expand Down Expand Up @@ -572,9 +602,6 @@ public static function nameVariations() {
* @return void
*/
public function testExecuteWithControllerNameVariations($name) {
if (!defined('ARTICLE_MODEL_CREATED')) {
$this->markTestSkipped('Execute with scaffold param requires no Article, Tag or Comment model to be defined.');
}
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$this->Task->args = array($name);
Expand All @@ -592,9 +619,6 @@ public function testExecuteWithControllerNameVariations($name) {
* @return void
*/
public function testExecuteWithPublicParam() {
if (!defined('ARTICLE_MODEL_CREATED')) {
$this->markTestSkipped('Execute with public param requires no Article, Tag or Comment model to be defined.');
}
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
$this->Task->args = array('BakeArticles');
Expand All @@ -614,9 +638,6 @@ public function testExecuteWithPublicParam() {
* @return void
*/
public function testExecuteWithControllerAndBoth() {
if (!defined('ARTICLE_MODEL_CREATED')) {
$this->markTestSkipped('Execute with controller and both requires no Article, Tag or Comment model to be defined.');
}
$this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
Expand All @@ -636,9 +657,6 @@ public function testExecuteWithControllerAndBoth() {
* @return void
*/
public function testExecuteWithControllerAndAdmin() {
if (!defined('ARTICLE_MODEL_CREATED')) {
$this->markTestSkipped('Execute with controller and admin requires no Article, Tag or Comment model to be defined.');
}
$this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
$this->Task->connection = 'test';
$this->Task->path = '/my/path/';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function testSessionReadWrite() {
$this->assertEquals($Session->read('Test'), $array);
$Session->delete('Test');

$this->assertFalse($Session->write(array('Test'), 'some value'));
$this->assertTrue($Session->write(array('Test'), 'some value'));
$this->assertTrue($Session->write(array('Test' => 'some value')));
$this->assertEquals('some value', $Session->read('Test'));
$Session->delete('Test');
Expand Down
20 changes: 20 additions & 0 deletions lib/Cake/Test/Case/Model/ModelDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,26 @@ public function testDeleteAllUnknownColumn() {
$this->assertFalse($result, 'deleteAll returned true when find query generated sql error. %s');
}

/**
* testDeleteAllFailedFind method
*
* Eg: Behavior callback stops the event, find returns null
*
* @return void
*/
public function testDeleteAllFailedFind() {
$this->loadFixtures('Article');
$this->getMock('Article', array('find'), array(), 'ArticleDeleteAll');

$TestModel = new ArticleDeleteAll();
$TestModel->expects($this->once())
->method('find')
->will($this->returnValue(null));

$result = $TestModel->deleteAll(array('Article.user_id' => 999));
$this->assertFalse($result);
}

/**
* testRecursiveDel method
*
Expand Down
24 changes: 24 additions & 0 deletions lib/Cake/Test/Case/Model/ModelReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3005,6 +3005,30 @@ public function testSelfAssociationAfterFind() {
$this->assertEquals($afterFindData, $noAfterFindData);
}

/**
* Test that afterFind can completely unset data.
*
* @return void
*/
public function testAfterFindUnset() {
$this->loadFixtures('Article', 'Comment', 'User');
$model = new CustomArticle();
$model->bindModel(array(
'hasMany' => array(
'ModifiedComment' => array(
'className' => 'ModifiedComment',
'foreignKey' => 'article_id',
)
)
));
$model->ModifiedComment->remove = true;
$result = $model->find('all');
$this->assertTrue(
empty($result[0]['ModifiedComment']),
'Zeroith row should be removed by afterFind'
);
}

/**
* testFindThreadedNoParent method
*
Expand Down
10 changes: 10 additions & 0 deletions lib/Cake/Test/Case/Model/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,13 @@ class ModifiedComment extends CakeTestModel {
*/
public $useTable = 'comments';

/**
* Property used to toggle filtering of results
*
* @var boolean
*/
public $remove = false;

/**
* belongsTo property
*
Expand All @@ -567,6 +574,9 @@ public function afterFind($results, $primary = false) {
if (isset($results[0])) {
$results[0]['Comment']['callback'] = 'Fire';
}
if ($this->remove) {
return array();
}
return $results;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/Cake/Test/Case/Network/CakeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ public function testQueryStringAndNamedParams() {
$_SERVER['REQUEST_URI'] = '/tasks/index/page:1/?ts=123456';
$request = new CakeRequest();
$this->assertEquals('tasks/index/page:1/', $request->url);

$_SERVER['REQUEST_URI'] = '/some/path?url=http://cakephp.org';
$request = new CakeRequest();
$this->assertEquals('some/path', $request->url);

$_SERVER['REQUEST_URI'] = FULL_BASE_URL . '/other/path?url=http://cakephp.org';
$request = new CakeRequest();
$this->assertEquals('other/path', $request->url);
}

/**
Expand Down
Loading

0 comments on commit 4af6039

Please sign in to comment.