Skip to content

Commit

Permalink
Merge branch '2.1' into 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeram committed Jun 1, 2012
2 parents 55c4ad3 + 048dc8d commit 4c64535
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 20 deletions.
6 changes: 6 additions & 0 deletions lib/Cake/Error/ExceptionRenderer.php
Expand Up @@ -267,6 +267,12 @@ protected function _outputMessage($template) {
$this->controller->render($template);
$this->controller->afterFilter();
$this->controller->response->send();
} catch (MissingViewException $e) {
try {
$this->_outputMessage('error500');
} catch (Exception $e) {
$this->_outputMessageSafe('error500');
}
} catch (Exception $e) {
$this->controller->set(array(
'error' => $e,
Expand Down
14 changes: 7 additions & 7 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -53,9 +53,8 @@ class DboSource extends DataSource {

/**
* Caches result from query parsing operations. Cached results for both DboSource::name() and
* DboSource::conditions() will be stored here. Method caching uses `crc32()` which is
* fast but can collisions more easily than other hashing algorithms. If you have problems
* with collisions, set DboSource::$cacheMethods to false.
* DboSource::conditions() will be stored here. Method caching uses `md5()`. If you have
* problems with collisions, set DboSource::$cacheMethods to false.
*
* @var array
*/
Expand Down Expand Up @@ -791,7 +790,7 @@ public function cacheMethod($method, $key, $value = null) {
* Strips fields out of SQL functions before quoting.
*
* Results of this method are stored in a memory cache. This improves performance, but
* because the method uses a simple hashing algorithm it can infrequently have collisions.
* because the method uses a hashing algorithm it can have collisions.
* Setting DboSource::$cacheMethods to false will disable the memory cache.
*
* @param mixed $data Either a string with a column to quote. An array of columns to quote or an
Expand All @@ -811,7 +810,7 @@ public function name($data) {
}
return $data;
}
$cacheKey = crc32($this->startQuote . $data . $this->endQuote);
$cacheKey = md5($this->startQuote . $data . $this->endQuote);
if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
return $return;
}
Expand Down Expand Up @@ -2284,7 +2283,8 @@ public function fields(Model $model, $alias = null, $fields = array(), $quote =
$model->alias,
$virtualFields,
$fields,
$quote
$quote,
ConnectionManager::getSourceName($this)
);
$cacheKey = md5(serialize($cacheKey));
if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
Expand Down Expand Up @@ -2384,7 +2384,7 @@ public function fields(Model $model, $alias = null, $fields = array(), $quote =
* is given it will be integer cast as condition. Null will return 1 = 1.
*
* Results of this method are stored in a memory cache. This improves performance, but
* because the method uses a simple hashing algorithm it can infrequently have collisions.
* because the method uses a hashing algorithm it can have collisions.
* Setting DboSource::$cacheMethods to false will disable the memory cache.
*
* @param mixed $conditions Array or string of conditions, or any value.
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Model/Model.php
Expand Up @@ -1175,7 +1175,7 @@ public function set($one, $two = null) {
*/
protected function _setAliasData($data) {
$models = array_keys($this->getAssociated());
$schema = array_keys($this->schema());
$schema = array_keys((array)$this->schema());
foreach ($data as $field => $value) {
if (in_array($field, $schema) || !in_array($field, $models)) {
$data[$this->alias][$field] = $value;
Expand Down
23 changes: 12 additions & 11 deletions lib/Cake/Test/Case/BasicsTest.php
Expand Up @@ -690,7 +690,8 @@ public function testDebug() {
'this-is-a-test'
###########################
EXPECTED;
$expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT)), __LINE__ - 8);
$expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 8);

$this->assertEquals($expected, $result);

ob_start();
Expand All @@ -704,7 +705,7 @@ public function testDebug() {
</pre>
</div>
EXPECTED;
$expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT)), __LINE__ - 10);
$expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
$this->assertEquals($expected, $result);

ob_start();
Expand All @@ -718,7 +719,7 @@ public function testDebug() {
</pre>
</div>
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT)), __LINE__ - 10);
$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
$this->assertEquals($expected, $result);

ob_start();
Expand All @@ -732,7 +733,7 @@ public function testDebug() {
</pre>
</div>
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT)), __LINE__ - 10);
$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
$this->assertEquals($expected, $result);

ob_start();
Expand All @@ -753,9 +754,9 @@ public function testDebug() {
###########################
EXPECTED;
if (php_sapi_name() == 'cli') {
$expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT)), __LINE__ - 17);
$expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 17);
} else {
$expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT)), __LINE__ - 19);
$expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
}
$this->assertEquals($expected, $result);

Expand All @@ -777,9 +778,9 @@ public function testDebug() {
###########################
EXPECTED;
if (php_sapi_name() == 'cli') {
$expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT)), __LINE__ - 17);
$expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 17);
} else {
$expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT)), __LINE__ - 19);
$expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
}
$this->assertEquals($expected, $result);

Expand All @@ -792,7 +793,7 @@ public function testDebug() {
'<div>this-is-a-test</div>'
###########################
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT)), __LINE__ - 8);
$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 8);
$this->assertEquals($expected, $result);

ob_start();
Expand All @@ -804,7 +805,7 @@ public function testDebug() {
'<div>this-is-a-test</div>'
###########################
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT)), __LINE__ - 8);
$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 8);
$this->assertEquals($expected, $result);

ob_start();
Expand All @@ -816,7 +817,7 @@ public function testDebug() {
'<div>this-is-a-test</div>'
###########################
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT)), __LINE__ - 8);
$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 8);
$this->assertEquals($expected, $result);
}

Expand Down
43 changes: 43 additions & 0 deletions lib/Cake/Test/Case/Error/ExceptionRendererTest.php
Expand Up @@ -277,6 +277,49 @@ public function testErrorMethodCoercion() {
$this->assertEquals($exception, $ExceptionRenderer->error);
}

/**
* test that helpers in custom CakeErrorController are not lost
*/
public function testCakeErrorHelpersNotLost() {
$testApp = CAKE . 'Test' . DS . 'test_app' . DS;
App::build(array(
'Controller' => array(
$testApp . 'Controller' . DS
),
'View/Helper' => array(
$testApp . 'View' . DS . 'Helper' . DS
),
'View/Layouts' => array(
$testApp . 'View' . DS . 'Layouts' . DS
),
'Error' => array(
$testApp . 'Error' . DS
),
), App::RESET);
Configure::write('Error', array(
'handler' => 'TestAppsErrorHandler::handleError',
'level' => E_ALL & ~E_DEPRECATED,
'trace' => true
));

Configure::write('Exception', array(
'handler' => 'TestAppsErrorHandler::handleException',
'renderer' => 'TestAppsExceptionRenderer',
'log' => true
));

App::uses('TestAppsErrorController', 'Controller');
App::uses('TestAppsExceptionRenderer', 'Error');

$exception = new SocketException('socket exception');
$renderer = new TestAppsExceptionRenderer($exception);

ob_start();
$renderer->render();
$result = ob_get_clean();
$this->assertContains('<b>peeled</b>', $result);
}

/**
* test that unknown exception types with valid status codes are treated correctly.
*
Expand Down
70 changes: 70 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -59,6 +59,30 @@ public function nestedTransactionSupported() {

}

class DboSecondTestSource extends DboSource {

public $startQuote = '_';

public $endQuote = '_';

public function connect($config = array()) {
$this->connected = true;
}

public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) {
return parent::_mergeAssociation($data, $merge, $association, $type, $selfJoin);
}

public function setConfig($config = array()) {
$this->config = $config;
}

public function setConnection($conn) {
$this->_connection = $conn;
}

}

/**
* DboSourceTest class
*
Expand Down Expand Up @@ -605,6 +629,21 @@ public function testCacheMethod() {
$this->assertNull($result);
}


/**
* Test that rare collisions do not happen with method caching
*
* @return void
*/
public function testNameMethodCacheCollisions() {
$this->testDb->cacheMethods = true;
$this->testDb->flushMethodCache();
$this->testDb->name('Model.fieldlbqndkezcoapfgirmjsh');
$result = $this->testDb->name('Model.fieldkhdfjmelarbqnzsogcpi');
$expected = '`Model`.`fieldkhdfjmelarbqnzsogcpi`';
$this->assertEquals($expected, $result);
}

/**
* testLog method
*
Expand Down Expand Up @@ -837,6 +876,37 @@ public function testFieldsUsingMethodCache() {
$this->assertTrue(empty(DboTestSource::$methodCache['fields']), 'Cache not empty');
}


/**
* test that fields() method cache detects datasource changes
*
* @return void
*/
public function testFieldsCacheKeyWithDatasourceChange() {
ConnectionManager::create('firstschema', array(
'datasource' => 'DboTestSource'
));
ConnectionManager::create('secondschema', array(
'datasource' => 'DboSecondTestSource'
));
Cache::delete('method_cache', '_cake_core_');
DboTestSource::$methodCache = array();
$Article = ClassRegistry::init('Article');

$Article->setDataSource('firstschema');
$ds = $Article->getDataSource();
$ds->cacheMethods = true;
$first = $ds->fields($Article, null, array('title', 'body', 'published'));

$Article->setDataSource('secondschema');
$ds = $Article->getDataSource();
$ds->cacheMethods = true;
$second = $ds->fields($Article, null, array('title', 'body', 'published'));

$this->assertNotEquals($first, $second);
$this->assertEquals(2, count(DboTestSource::$methodCache['fields']));
}

/**
* Test that group works without a model
*
Expand Down
14 changes: 14 additions & 0 deletions lib/Cake/Test/test_app/Controller/TestAppsErrorController.php
@@ -0,0 +1,14 @@
<?php

App::uses('CakeErrorController', 'Controller');

class TestAppsErrorController extends CakeErrorController {

public $helpers = array(
'Html',
'Session',
'Form',
'Banana',
);

}
21 changes: 21 additions & 0 deletions lib/Cake/Test/test_app/Error/TestAppsExceptionRenderer.php
@@ -0,0 +1,21 @@
<?php

class TestAppsExceptionRenderer extends ExceptionRenderer {

protected function _getController($exception) {
App::uses('TestAppsErrorController', 'Controller');
if (!$request = Router::getRequest(true)) {
$request = new CakeRequest();
}
$response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
try {
$controller = new TestAppsErrorController($request, $response);
$controller->layout = 'banana';
} catch (Exception $e) {
$controller = new Controller($request, $response);
$controller->viewPath = 'Errors';
}
return $controller;
}

}
5 changes: 5 additions & 0 deletions lib/Cake/Test/test_app/View/Helper/BananaHelper.php
Expand Up @@ -19,4 +19,9 @@
App::uses('Helper', 'View');

class BananaHelper extends Helper {

public function peel() {
return '<b>peeled</b>';
}

}
5 changes: 5 additions & 0 deletions lib/Cake/Test/test_app/View/Layouts/banana.ctp
@@ -0,0 +1,5 @@
<body>
<?php
echo $this->Banana->peel();
?>
</body>
2 changes: 1 addition & 1 deletion lib/Cake/basics.php
Expand Up @@ -77,7 +77,7 @@ function debug($var = false, $showHtml = null, $showFrom = true) {
$lineInfo = '';
if ($showFrom) {
$trace = Debugger::trace(array('start' => 1, 'depth' => 2, 'format' => 'array'));
$file = substr($trace[0]['file'], strlen(ROOT));
$file = str_replace(array(CAKE_CORE_INCLUDE_PATH, ROOT), '', $trace[0]['file']);
$line = $trace[0]['line'];
}
$html = <<<HTML
Expand Down

0 comments on commit 4c64535

Please sign in to comment.