Adding tests for #1079 and fixing some formatting.

This commit is contained in:
Mark Story 2010-09-07 00:09:06 -04:00
parent c60edfae6d
commit e15069769d
2 changed files with 52 additions and 1 deletions

View file

@ -1923,7 +1923,7 @@ class DboSource extends DataSource {
foreach ($fields as $field) { foreach ($fields as $field) {
$virtualField = $this->name($alias . $this->virtualFieldSeparator . $field); $virtualField = $this->name($alias . $this->virtualFieldSeparator . $field);
$expression = $this->__quoteFields($model->getVirtualField($field)); $expression = $this->__quoteFields($model->getVirtualField($field));
$virtual[] = '(' .$expression . ") {$this->alias} {$virtualField}"; $virtual[] = '(' . $expression . ") {$this->alias} {$virtualField}";
} }
return $virtual; return $virtual;
} }

View file

@ -4359,6 +4359,30 @@ class DboSourceTest extends CakeTestCase {
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
} }
/**
* test that virtualFields with complex functions and aliases work.
*
* @return void
*/
function testConditionsWithComplexVirtualFields() {
$Article =& ClassRegistry::init('Article');
$Article->virtualFields = array(
'distance' => 'ACOS(SIN(20 * PI() / 180)
* SIN(Article.latitude * PI() / 180)
+ COS(20 * PI() / 180)
* COS(Article.latitude * PI() / 180)
* COS((50 - Article.longitude) * PI() / 180)
) * 180 / PI() * 60 * 1.1515 * 1.609344'
);
$conditions = array('distance >=' => 20);
$result = $this->db->conditions($conditions, true, true, $Article);
$this->assertPattern('/\) >= 20/', $result);
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result);
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result);
}
/** /**
* test order to generate query order clause for virtual fields * test order to generate query order clause for virtual fields
* *
@ -4432,6 +4456,33 @@ class DboSourceTest extends CakeTestCase {
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
} }
/**
* test that virtualFields with complex functions and aliases work.
*
* @return void
*/
function testFieldsWithComplexVirtualFields() {
$Article =& new Article();
$Article->virtualFields = array(
'distance' => 'ACOS(SIN(20 * PI() / 180)
* SIN(Article.latitude * PI() / 180)
+ COS(20 * PI() / 180)
* COS(Article.latitude * PI() / 180)
* COS((50 - Article.longitude) * PI() / 180)
) * 180 / PI() * 60 * 1.1515 * 1.609344'
);
$fields = array('id', 'distance');
$result = $this->db->fields($Article, null, $fields);
$qs = $this->db->startQuote;
$qe = $this->db->endQuote;
$this->assertEqual($result[0], "{$qs}Article{$qe}.{$qs}id{$qe}");
$this->assertPattern('/Article__distance/', $result[1]);
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result[1]);
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result[1]);
}
/** /**
* test group to generate GROUP BY statements on virtual fields * test group to generate GROUP BY statements on virtual fields
* *