3 require_once 'CiviTest/CiviUnitTestCase.php';
4 class CRM_Core_DAOTest
extends CiviUnitTestCase
{
5 function composeQueryExamples() {
7 // $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');
11 'SELECT * FROM whatever',
13 'SELECT * FROM whatever',
16 // CASE: Integer param
18 'SELECT * FROM whatever WHERE id = %1',
20 1 => array(10, 'Integer'),
22 'SELECT * FROM whatever WHERE id = 10',
27 'SELECT * FROM whatever WHERE name = %1',
29 1 => array('Alice', 'String'),
31 'SELECT * FROM whatever WHERE name = \'Alice\'',
36 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
38 1 => array('Alice', 'String'),
39 2 => array('Bob', 'String'),
41 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\'',
44 // CASE: Two params with special character (%1)
46 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
48 1 => array('Alice %2', 'String'),
49 2 => array('Bob', 'String'),
51 'SELECT * FROM whatever WHERE name = \'Alice %2\' AND title = \'Bob\'',
54 // CASE: Two params with special character ($1)
56 'SELECT * FROM whatever WHERE name = %1 AND title = %2',
58 1 => array('Alice $1', 'String'),
59 2 => array('Bob', 'String'),
61 'SELECT * FROM whatever WHERE name = \'Alice $1\' AND title = \'Bob\'',
68 * @dataProvider composeQueryExamples
70 function testComposeQuery($inputSql, $inputParams, $expectSql) {
71 $actualSql = CRM_Core_DAO
::composeQuery($inputSql, $inputParams);
72 $this->assertEquals($expectSql, $actualSql);
75 // CASE: Two params where the %2 is already present in the query
76 // NOTE: This case should rightly FAIL, as using strstr in the replace mechanism will turn
77 // the query into: SELECT * FROM whatever WHERE name = 'Alice' AND title = 'Bob' AND year LIKE ''Bob'012'
78 // So, to avoid such ERROR, the query should be framed like:
79 // 'SELECT * FROM whatever WHERE name = %1 AND title = %3 AND year LIKE '%2012'
80 // $params[3] = array('Bob', 'String');
81 // i.e. the place holder should be unique and should not contain in any other operational use in query
82 function testComposeQueryFailure() {
84 'SELECT * FROM whatever WHERE name = %1 AND title = %2 AND year LIKE \'%2012\' ',
86 1 => array('Alice', 'String'),
87 2 => array('Bob', 'String'),
89 'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\' AND year LIKE \'%2012\' ',
91 list($inputSql, $inputParams, $expectSql) = $cases[0];
92 $actualSql = CRM_Core_DAO
::composeQuery($inputSql, $inputParams);
93 $this->assertFalse(($expectSql == $actualSql));