Skip to content

Commit

Permalink
Switching FixtureTask to use var_export() instead of custom
Browse files Browse the repository at this point in the history
escaping code.
Fixes issues with quotes in text.
Fixes #1922
  • Loading branch information
markstory committed Aug 21, 2011
1 parent f9ff4e1 commit 9daa969
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
22 changes: 9 additions & 13 deletions cake/console/libs/tasks/fixture.php
Expand Up @@ -296,35 +296,30 @@ function _generateRecords($tableInfo, $recordCount = 1) {
$insert = substr($insert, 0, (int)$fieldInfo['length'] - 2); $insert = substr($insert, 0, (int)$fieldInfo['length'] - 2);
} }
} }
$insert = "'$insert'";
break; break;
case 'timestamp': case 'timestamp':
$ts = time(); $insert = time();
$insert = "'$ts'";
break; break;
case 'datetime': case 'datetime':
$ts = date('Y-m-d H:i:s'); $insert = date('Y-m-d H:i:s');
$insert = "'$ts'";
break; break;
case 'date': case 'date':
$ts = date('Y-m-d'); $insert = date('Y-m-d');
$insert = "'$ts'";
break; break;
case 'time': case 'time':
$ts = date('H:i:s'); $insert = date('H:i:s');
$insert = "'$ts'";
break; break;
case 'boolean': case 'boolean':
$insert = 1; $insert = 1;
break; break;
case 'text': case 'text':
$insert = "'Lorem ipsum dolor sit amet, aliquet feugiat."; $insert = "Lorem ipsum dolor sit amet, aliquet feugiat.";
$insert .= " Convallis morbi fringilla gravida,"; $insert .= " Convallis morbi fringilla gravida,";
$insert .= " phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin"; $insert .= " phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin";
$insert .= " venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla"; $insert .= " venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla";
$insert .= " vestibulum massa neque ut et, id hendrerit sit,"; $insert .= " vestibulum massa neque ut et, id hendrerit sit,";
$insert .= " feugiat in taciti enim proin nibh, tempor dignissim, rhoncus"; $insert .= " feugiat in taciti enim proin nibh, tempor dignissim, rhoncus";
$insert .= " duis vestibulum nunc mattis convallis.'"; $insert .= " duis vestibulum nunc mattis convallis.";
break; break;
} }
$record[$field] = $insert; $record[$field] = $insert;
Expand All @@ -346,7 +341,8 @@ function _makeRecordString($records) {
foreach ($records as $record) { foreach ($records as $record) {
$values = array(); $values = array();
foreach ($record as $field => $value) { foreach ($record as $field => $value) {
$values[] = "\t\t\t'$field' => $value"; $val = var_export($value, true);
$values[] = "\t\t\t'$field' => $val";
} }
$out .= "\t\tarray(\n"; $out .= "\t\tarray(\n";
$out .= implode(",\n", $values); $out .= implode(",\n", $values);
Expand Down Expand Up @@ -387,7 +383,7 @@ function _getRecordsFromTable($modelName, $useTable = null) {
foreach ($records as $record) { foreach ($records as $record) {
$row = array(); $row = array();
foreach ($record[$modelObject->alias] as $field => $value) { foreach ($record[$modelObject->alias] as $field => $value) {
$row[$field] = $db->value($value, $schema[$field]['type']); $row[$field] = $value;
} }
$out[] = $row; $out[] = $row;
} }
Expand Down
22 changes: 22 additions & 0 deletions cake/tests/cases/console/libs/tasks/fixture.test.php
Expand Up @@ -167,6 +167,28 @@ function testImportRecordsFromDatabaseWithConditions() {
$this->assertPattern('/Third Article/', $result, 'Missing import data %s'); $this->assertPattern('/Third Article/', $result, 'Missing import data %s');
} }


/**
* Ensure that fixture data doesn't get overly escaped.
*
* @return void
*/
function testImportRecordsNoEscaping() {
$Article = ClassRegistry::init('Article');
$Article->updateAll(array('body' => "'Body \"value\"'"));

$this->Task->interactive = true;
$this->Task->setReturnValueAt(0, 'in', 'WHERE 1=1 LIMIT 10');
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$result = $this->Task->bake('Article', false, array(
'fromTable' => true,
'schema' => 'Article',
'records' => false
));

$this->assertPattern("/'body' => 'Body \"value\"'/", $result, 'Data has escaping %s');
}

/** /**
* test that execute passes runs bake depending with named model. * test that execute passes runs bake depending with named model.
* *
Expand Down

0 comments on commit 9daa969

Please sign in to comment.