diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php index da2f2a721b1..3fbddc4a455 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php @@ -3019,4 +3019,89 @@ function testConditionsWithComplexVirtualFields() { $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result); $this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result); } + +/** + * test calculate to generate claculate statements on virtual fields + * + * @return void + */ + function testVirtualFieldsInCalculate() { + $Article = ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'this_moment' => 'NOW()', + 'two' => '1 + 1', + 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->Dbo->fullTableName('comments') . + ' WHERE Article.id = ' . $this->Dbo->fullTableName('comments'). '.article_id' + ); + + $result = $this->Dbo->calculate($Article, 'count', array('this_moment')); + $expected = 'COUNT(NOW()) AS `count`'; + $this->assertEqual($expected, $result); + + $result = $this->Dbo->calculate($Article, 'max', array('comment_count')); + $expected = 'MAX(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `comment_count`'; + $this->assertEqual($expected, $result); + } + +/** + * test reading virtual fields containing newlines when recursive > 0 + * + * @return void + */ + function testReadVirtualFieldsWithNewLines() { + $Article = new Article(); + $Article->recursive = 1; + $Article->virtualFields = array( + 'test' => ' + User.id + User.id + ' + ); + $result = $this->Dbo->fields($Article, null, array()); + $result = $this->Dbo->fields($Article, $Article->alias, $result); + $this->assertPattern('/[`\"]User[`\"]\.[`\"]id[`\"] \+ [`\"]User[`\"]\.[`\"]id[`\"]/', $result[7]); + } + +/** + * test group to generate GROUP BY statements on virtual fields + * + * @return void + */ + function testVirtualFieldsInGroup() { + $Article = ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'this_year' => 'YEAR(Article.created)' + ); + + $result = $this->Dbo->group('this_year', $Article); + + $expected = " GROUP BY (YEAR(`Article`.`created`))"; + $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->Dbo->fields($Article, null, $fields); + $qs = $this->Dbo->startQuote; + $qe = $this->Dbo->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]); + } } \ No newline at end of file diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index b2a3bee7b2b..a934aee5d64 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -999,29 +999,6 @@ function testVirtualFieldsInOrder() { $this->assertEqual($expected, $result); } -/** - * test calculate to generate claculate statements on virtual fields - * - * @return void - */ - function testVirtualFieldsInCalculate() { - $Article = ClassRegistry::init('Article'); - $Article->virtualFields = array( - 'this_moment' => 'NOW()', - 'two' => '1 + 1', - 'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') . - ' WHERE Article.id = ' . $this->db->fullTableName('comments'). '.article_id' - ); - - $result = $this->db->calculate($Article, 'count', array('this_moment')); - $expected = 'COUNT(NOW()) AS `count`'; - $this->assertEqual($expected, $result); - - $result = $this->db->calculate($Article, 'max', array('comment_count')); - $expected = 'MAX(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `comment_count`'; - $this->assertEqual($expected, $result); - } - /** * test a full example of using virtual fields * @@ -1079,68 +1056,6 @@ function testVirtualFieldsComplexRead() { $this->assertTrue($result[0]['DataTest']['complicated'] > 0); } -/** - * 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 reading virtual fields containing newlines when recursive > 0 - * - * @return void - */ - function testReadVirtualFieldsWithNewLines() { - $Article = new Article(); - $Article->recursive = 1; - $Article->virtualFields = array( - 'test' => ' - User.id + User.id - ' - ); - $result = $this->db->fields($Article, null, array()); - $result = $this->db->fields($Article, $Article->alias, $result); - $this->assertPattern('/[`\"]User[`\"]\.[`\"]id[`\"] \+ [`\"]User[`\"]\.[`\"]id[`\"]/', $result[7]); - } - -/** - * test group to generate GROUP BY statements on virtual fields - * - * @return void - */ - function testVirtualFieldsInGroup() { - $Article = ClassRegistry::init('Article'); - $Article->virtualFields = array( - 'this_year' => 'YEAR(Article.created)' - ); - - $result = $this->db->group('this_year', $Article); - - $expected = " GROUP BY (YEAR(`Article`.`created`))"; - $this->assertEqual($expected, $result); - } - /** * test the permutations of fullTableName() * @@ -1185,7 +1100,7 @@ function testFieldsUsingMethodCache() { $this->testDb->cacheMethods = false; $this->assertTrue(empty($this->testDb->methodCache['fields']), 'Cache not empty'); - $Article =& ClassRegistry::init('Article'); + $Article = ClassRegistry::init('Article'); $this->testDb->fields($Article, null, array('title', 'body', 'published')); $this->assertTrue(empty($this->testDb->methodCache['fields']), 'Cache not empty'); }