Merge branch '2.0' into 2.0-merge

Conflicts:
	lib/Cake/Test/Case/Controller/ScaffoldTest.php
	lib/Cake/Test/Case/Model/ConnectionManagerTest.php
	lib/Cake/Test/Case/TestSuite/CakeTestFixtureTest.php
	lib/Cake/Test/Case/Utility/SanitizeTest.php
This commit is contained in:
Jose Lorenzo Rodriguez 2011-06-17 18:04:03 -04:30
commit 2a39e6869c
8 changed files with 43 additions and 157 deletions

View file

@ -20,7 +20,6 @@
*/
App::uses('DataSource', 'Model/Datasource');
config('database');
/**
* Manages loaded instances of DataSource objects
@ -54,7 +53,7 @@ class ConnectionManager {
protected static $_connectionsEnum = array();
/**
* Indicates if the init code for this class has alredy been executed
* Indicates if the init code for this class has already been executed
*
* @var boolean
*/
@ -107,6 +106,8 @@ class ConnectionManager {
/**
* Gets the list of available DataSource connections
* This will only return the datasources instantiated by this manager
* It differs from enumConnectionObjects, since the latter will return all configured connections
*
* @return array List of available connections
*/
@ -133,7 +134,7 @@ class ConnectionManager {
return $name;
}
}
return '';
return null;
}
/**

View file

@ -213,6 +213,7 @@ class SchemaShellTest extends CakeTestCase {
public function testDumpWithFileWriting() {
$this->Shell->params = array(
'name' => 'i18n',
'connection' => 'test',
'write' => TMP . 'tests' . DS . 'i18n.sql'
);
$this->Shell->expects($this->once())->method('_stop');

View file

@ -366,7 +366,7 @@ class ScaffoldViewTest extends CakeTestCase {
'action' => 'admin_add',
'admin' => true
));
$Controller->plugin = 'test_plugin';
$Controller->plugin = 'TestPlugin';
$ScaffoldView = new TestScaffoldView($Controller);
$result = $ScaffoldView->testGetFilename('admin_add');
@ -825,7 +825,8 @@ class ScaffoldTest extends CakeTestCase {
*
* @return void
*/
public function testHabtmFieldAdditionWithScaffoldForm() {
function testHabtmFieldAdditionWithScaffoldForm() {
CakePlugin::unload();
$params = array(
'plugin' => null,
'pass' => array(1),

View file

@ -5,7 +5,7 @@
*
* PHP 5
*
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
@ -55,11 +55,22 @@ class ConnectionManagerTest extends CakeTestCase {
* @return void
*/
public function testGetDataSource() {
App::build(array(
'Model/Datasource' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS
)
));
$name = 'test_get_datasource';
$config = array('datasource' => 'Test2Source');
$connection = ConnectionManager::create($name, $config);
$connections = ConnectionManager::enumConnectionObjects();
$this->assertTrue((bool)(count(array_keys($connections) >= 1)));
$source = ConnectionManager::getDataSource(key($connections));
$source = ConnectionManager::getDataSource('test_get_datasource');
$this->assertTrue(is_object($source));
ConnectionManager::drop('test_get_datasource');
}
/**
@ -91,7 +102,7 @@ class ConnectionManagerTest extends CakeTestCase {
$this->assertEqual($connection->configKeyName, $name);
$this->assertEqual($connection->config, $config);
App::build();
ConnectionManager::drop($name);
}
/**
@ -115,7 +126,7 @@ class ConnectionManagerTest extends CakeTestCase {
$this->assertEqual($connection->configKeyName, $name);
$this->assertEqual($connection->config, $config);
App::build();
ConnectionManager::drop($name);
}
/**
@ -138,7 +149,7 @@ class ConnectionManagerTest extends CakeTestCase {
$this->assertTrue(class_exists('DboDummy'));
$this->assertEqual($connection->configKeyName, $name);
App::build();
ConnectionManager::drop($name);
}
/**
@ -164,7 +175,7 @@ class ConnectionManagerTest extends CakeTestCase {
$this->assertTrue(class_exists('TestLocalDriver'));
$this->assertEqual($connection->configKeyName, $name);
$this->assertEqual($connection->config, $config);
App::build();
ConnectionManager::drop($name);
}
/**
@ -174,11 +185,10 @@ class ConnectionManagerTest extends CakeTestCase {
* @return void
*/
public function testSourceList() {
ConnectionManager::getDataSource('test');
$sources = ConnectionManager::sourceList();
$this->assertTrue(count($sources) >= 1);
$connections = array('default', 'test', 'test');
$this->assertTrue(count(array_intersect($sources, $connections)) >= 1);
$this->assertTrue(in_array('test', array_keys($sources)));
}
/**
@ -189,15 +199,14 @@ class ConnectionManagerTest extends CakeTestCase {
*/
public function testGetSourceName() {
$connections = ConnectionManager::enumConnectionObjects();
$name = key($connections);
$source = ConnectionManager::getDataSource($name);
$source = ConnectionManager::getDataSource('test');
$result = ConnectionManager::getSourceName($source);
$this->assertEqual($result, $name);
$this->assertEqual('test', $result);
$source = new StdClass();
$result = ConnectionManager::getSourceName($source);
$this->assertEqual($result, null);
$this->assertNull($result);
}
/**
@ -243,7 +252,7 @@ class ConnectionManagerTest extends CakeTestCase {
$connections = ConnectionManager::enumConnectionObjects();
$this->assertTrue((bool)(count(array_keys($connections) >= 1)));
$source = ConnectionManager::getDataSource(key($connections));
$source = ConnectionManager::getDataSource('test');
$this->assertTrue(is_object($source));
$config = $source->config;
@ -285,39 +294,45 @@ class ConnectionManagerTest extends CakeTestCase {
ConnectionManager::create('connection1', array('datasource' => 'Test2Source'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection1']);
ConnectionManager::drop('connection1');
ConnectionManager::create('connection2', array('datasource' => 'Test2Source'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection2']);
ConnectionManager::drop('connection2');
ConnectionManager::create('connection3', array('datasource' => 'TestPlugin.TestSource'));
$connections = ConnectionManager::enumConnectionObjects();
$expected['datasource'] = 'TestPlugin.TestSource';
$this->assertEqual($expected, $connections['connection3']);
ConnectionManager::drop('connection3');
ConnectionManager::create('connection4', array('datasource' => 'TestPlugin.TestSource'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection4']);
ConnectionManager::drop('connection4');
ConnectionManager::create('connection5', array('datasource' => 'Test2OtherSource'));
$connections = ConnectionManager::enumConnectionObjects();
$expected['datasource'] = 'Test2OtherSource';
$this->assertEqual($expected, $connections['connection5']);
ConnectionManager::drop('connection5');
ConnectionManager::create('connection6', array('datasource' => 'Test2OtherSource'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection6']);
ConnectionManager::drop('connection6');
ConnectionManager::create('connection7', array('datasource' => 'TestPlugin.TestOtherSource'));
$connections = ConnectionManager::enumConnectionObjects();
$expected['datasource'] = 'TestPlugin.TestOtherSource';
$this->assertEqual($expected, $connections['connection7']);
ConnectionManager::drop('connection7');
ConnectionManager::create('connection8', array('datasource' => 'TestPlugin.TestOtherSource'));
$connections = ConnectionManager::enumConnectionObjects();
$this->assertEqual($expected, $connections['connection8']);
ConnectionManager::drop('connection8');
}
/**

View file

@ -320,9 +320,7 @@ class CakeTestFixtureTest extends CakeTestCase {
* @return void
*/
public function testImport() {
$defaultDb = ConnectionManager::getDataSource('default');
$testSuiteDb = ConnectionManager::getDataSource('test');
$defaultConfig = $defaultDb->config;
$testSuiteConfig = $testSuiteDb->config;
ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
$newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
@ -331,16 +329,12 @@ class CakeTestFixtureTest extends CakeTestCase {
$Source->create($newTestSuiteDb);
$Source->insert($newTestSuiteDb);
$defaultDb->config = $newTestSuiteDb->config;
$Fixture = new CakeTestFixtureDefaultImportFixture();
$Fixture->fields = $Fixture->records = null;
$Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite');
$Fixture->init();
$this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created'));
$defaultDb->config = $defaultConfig;
$keys = array_flip(ClassRegistry::keys());
$this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
@ -354,10 +348,7 @@ class CakeTestFixtureTest extends CakeTestCase {
* @return void
*/
public function testImportWithRecords() {
$defaultDb = ConnectionManager::getDataSource('default');
$testSuiteDb = ConnectionManager::getDataSource('test');
$defaultConfig = $defaultDb->config;
$testSuiteConfig = $testSuiteDb->config;
ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
$newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
@ -366,7 +357,6 @@ class CakeTestFixtureTest extends CakeTestCase {
$Source->create($newTestSuiteDb);
$Source->insert($newTestSuiteDb);
$defaultDb->config = $newTestSuiteDb->config;
$Fixture = new CakeTestFixtureDefaultImportFixture();
$Fixture->fields = $Fixture->records = null;
@ -378,8 +368,6 @@ class CakeTestFixtureTest extends CakeTestCase {
$this->assertFalse(empty($Fixture->records[0]), 'No records loaded on importing fixture.');
$this->assertTrue(isset($Fixture->records[0]['name']), 'No name loaded for first record');
$defaultDb->config = $defaultConfig;
$Source->drop($newTestSuiteDb);
}

View file

@ -170,12 +170,12 @@ class SanitizeTest extends CakeTestCase {
$array = array(array('\\$', array('key' => 'test & "quote" \'other\' ;.$ \\$ symbol.' . "\r" . 'another line')));
$expected = array(array('$', array('key' => 'test & "quote" \'other\' ;.$ $ symbol.another line')));
$result = Sanitize::clean($array, array('encode' => false, 'escape' => false));
$result = Sanitize::clean($array, array('encode' => false, 'escape' => false, 'connection' => 'test'));
$this->assertEqual($expected, $result);
$string = '';
$expected = '';
$result = Sanitize::clean($string);
$result = Sanitize::clean($string, array('connection' => 'test'));
$this->assertEqual($string, $expected);
$data = array(
@ -198,7 +198,7 @@ class SanitizeTest extends CakeTestCase {
)
)
);
$result = Sanitize::clean($data);
$result = Sanitize::clean($data, array('connection' => 'test'));
$this->assertEqual($result, $data);
}
@ -465,55 +465,4 @@ HTML;
$result = Sanitize::stripTags($string, 'h2', 'a', 'img');
$this->assertEqual($expected, $result);
}
/**
* testFormatColumns method
*
* @access public
* @return void
*/
public function testFormatColumns() {
$this->autoFixtures = true;
$this->fixtureManager->load($this);
$this->loadFixtures('DataTest', 'Article');
$this->DataTest = new SanitizeDataTest(array('alias' => 'DataTest'));
$data = array('DataTest' => array(
'id' => 'z',
'count' => '12a',
'float' => '2.31456',
'updated' => '2008-01-01'
)
);
$this->DataTest->set($data);
$expected = array('DataTest' => array(
'id' => '0',
'count' => '12',
'float' => 2.31456,
'updated' => '2008-01-01 00:00:00',
));
Sanitize::formatColumns($this->DataTest);
$result = $this->DataTest->data;
$this->assertEqual($expected, $result);
$this->Article = new SanitizeArticle(array('alias' => 'Article'));
$data = array('Article' => array(
'id' => 'ZB',
'user_id' => '12',
'title' => 'title of article',
'body' => 'body text',
'published' => 'QQQQQQQ',
));
$this->Article->set($data);
$expected = array('Article' => array(
'id' => '0',
'user_id' => '12',
'title' => 'title of article',
'body' => 'body text',
'published' => 'QQQQQQQ',
));
Sanitize::formatColumns($this->Article);
$result = $this->Article->data;
$this->assertEqual($expected, $result);
}
}

View file

@ -1,5 +1,5 @@
<?php
App::uses('DboSource', 'Model/Datasource');
class DboDummy extends DboSource {
public function connect() {
return true;

View file

@ -257,73 +257,4 @@ class Sanitize {
return $data;
}
}
/**
* Formats column data from definition in DBO's $columns array
*
* @param Model $model The model containing the data to be formatted
*/
public static function formatColumns($model) {
foreach ($model->data as $name => $values) {
if ($name == $model->alias) {
$curModel = $model;
} elseif (isset($model->{$name}) && is_object($model->{$name}) && is_subclass_of($model->{$name}, 'Model')) {
$curModel = $model->{$name};
} else {
$curModel = null;
}
if ($curModel != null) {
foreach ($values as $column => $data) {
$colType = $curModel->getColumnType($column);
if ($colType != null) {
$db = ConnectionManager::getDataSource($curModel->useDbConfig);
$colData = $db->columns[$colType];
if (isset($colData['limit']) && strlen(strval($data)) > $colData['limit']) {
$data = substr(strval($data), 0, $colData['limit']);
}
if (isset($colData['formatter']) || isset($colData['format'])) {
switch (strtolower($colData['formatter'])) {
case 'date':
$data = date($colData['format'], strtotime($data));
break;
case 'sprintf':
$data = sprintf($colData['format'], $data);
break;
case 'intval':
$data = intval($data);
break;
case 'floatval':
$data = floatval($data);
break;
}
}
$model->data[$name][$column]=$data;
/*
switch ($colType) {
case 'integer':
case 'int':
return $data;
break;
case 'string':
case 'text':
case 'binary':
case 'date':
case 'time':
case 'datetime':
case 'timestamp':
case 'date':
return "'" . $data . "'";
break;
}
*/
}
}
}
}
}
}