public function testGetReferenceColumns() {
// choose CRM_Core_DAO_Email as an arbitrary example
$emailRefs = CRM_Core_DAO_Email::getReferenceColumns();
- $refsByTarget = array();
+ $refsByTarget = [];
foreach ($emailRefs as $refSpec) {
$refsByTarget[$refSpec->getTargetTable()] = $refSpec;
}
public function testGetReferencesToTable() {
$refs = CRM_Core_DAO::getReferencesToTable(CRM_Financial_DAO_FinancialType::getTableName());
- $refsBySource = array();
+ $refsBySource = [];
foreach ($refs as $refSpec) {
$refsBySource[$refSpec->getReferenceTable()] = $refSpec;
}
}
public function testFindReferences() {
- $params = array(
+ $params = [
'first_name' => 'Testy',
'last_name' => 'McScallion',
'contact_type' => 'Individual',
- );
+ ];
$contact = CRM_Contact_BAO_Contact::add($params);
$this->assertNotNull($contact->id);
- $params = array(
+ $params = [
'email' => 'spam@dev.null',
'contact_id' => $contact->id,
'is_primary' => 0,
'location_type_id' => 1,
- );
+ ];
$email = CRM_Core_BAO_Email::add($params);
$refs = $contact->findReferences();
- $refsByTable = array();
+ $refsByTable = [];
foreach ($refs as $refObj) {
$refsByTable[$refObj->__table] = $refObj;
}
* @return array
*/
public function composeQueryExamples() {
- $cases = array();
+ $cases = [];
// $cases[] = array('Input-SQL', 'Input-Params', 'Expected-SQL');
- $cases[0] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('', 'String')), 'UPDATE civicrm_foo SET bar = \'\'');
- $cases[1] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('the text', 'String')), 'UPDATE civicrm_foo SET bar = \'the text\'');
- $cases[2] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array(NULL, 'String')), self::ABORTED_SQL);
- $cases[3] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('null', 'String')), 'UPDATE civicrm_foo SET bar = NULL');
+ $cases[0] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['', 'String']], 'UPDATE civicrm_foo SET bar = \'\''];
+ $cases[1] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['the text', 'String']], 'UPDATE civicrm_foo SET bar = \'the text\''];
+ $cases[2] = ['UPDATE civicrm_foo SET bar = %1', [1 => [NULL, 'String']], self::ABORTED_SQL];
+ $cases[3] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['null', 'String']], 'UPDATE civicrm_foo SET bar = NULL'];
- $cases[3] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('', 'Float')), self::ABORTED_SQL);
- $cases[4] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('1.23', 'Float')), 'UPDATE civicrm_foo SET bar = 1.23');
- $cases[5] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array(NULL, 'Float')), self::ABORTED_SQL);
- $cases[6] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('null', 'Float')), self::ABORTED_SQL);
+ $cases[3] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['', 'Float']], self::ABORTED_SQL];
+ $cases[4] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['1.23', 'Float']], 'UPDATE civicrm_foo SET bar = 1.23'];
+ $cases[5] = ['UPDATE civicrm_foo SET bar = %1', [1 => [NULL, 'Float']], self::ABORTED_SQL];
+ $cases[6] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['null', 'Float']], self::ABORTED_SQL];
- $cases[11] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('', 'Money')), self::ABORTED_SQL);
- $cases[12] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('1.23', 'Money')), 'UPDATE civicrm_foo SET bar = 1.23');
- $cases[13] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array(NULL, 'Money')), self::ABORTED_SQL);
- $cases[14] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('null', 'Money')), self::ABORTED_SQL);
+ $cases[11] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['', 'Money']], self::ABORTED_SQL];
+ $cases[12] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['1.23', 'Money']], 'UPDATE civicrm_foo SET bar = 1.23'];
+ $cases[13] = ['UPDATE civicrm_foo SET bar = %1', [1 => [NULL, 'Money']], self::ABORTED_SQL];
+ $cases[14] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['null', 'Money']], self::ABORTED_SQL];
- $cases[15] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('', 'Int')), self::ABORTED_SQL);
- $cases[16] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('123', 'Int')), 'UPDATE civicrm_foo SET bar = 123');
- $cases[17] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array(NULL, 'Int')), self::ABORTED_SQL);
- $cases[18] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('null', 'Int')), self::ABORTED_SQL);
+ $cases[15] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['', 'Int']], self::ABORTED_SQL];
+ $cases[16] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['123', 'Int']], 'UPDATE civicrm_foo SET bar = 123'];
+ $cases[17] = ['UPDATE civicrm_foo SET bar = %1', [1 => [NULL, 'Int']], self::ABORTED_SQL];
+ $cases[18] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['null', 'Int']], self::ABORTED_SQL];
- $cases[19] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('', 'Timestamp')), 'UPDATE civicrm_foo SET bar = null');
- $cases[20] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('20150102030405', 'Timestamp')), 'UPDATE civicrm_foo SET bar = 20150102030405');
- $cases[21] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array(NULL, 'Timestamp')), 'UPDATE civicrm_foo SET bar = null');
- $cases[22] = array('UPDATE civicrm_foo SET bar = %1', array(1 => array('null', 'Timestamp')), self::ABORTED_SQL);
+ $cases[19] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['', 'Timestamp']], 'UPDATE civicrm_foo SET bar = null'];
+ $cases[20] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['20150102030405', 'Timestamp']], 'UPDATE civicrm_foo SET bar = 20150102030405'];
+ $cases[21] = ['UPDATE civicrm_foo SET bar = %1', [1 => [NULL, 'Timestamp']], 'UPDATE civicrm_foo SET bar = null'];
+ $cases[22] = ['UPDATE civicrm_foo SET bar = %1', [1 => ['null', 'Timestamp']], self::ABORTED_SQL];
// CASE: No params
- $cases[1000] = array(
+ $cases[1000] = [
'SELECT * FROM whatever',
- array(),
+ [],
'SELECT * FROM whatever',
- );
+ ];
// CASE: Integer param
- $cases[1001] = array(
+ $cases[1001] = [
'SELECT * FROM whatever WHERE id = %1',
- array(
- 1 => array(10, 'Integer'),
- ),
+ [
+ 1 => [10, 'Integer'],
+ ],
'SELECT * FROM whatever WHERE id = 10',
- );
+ ];
// CASE: String param
- $cases[1002] = array(
+ $cases[1002] = [
'SELECT * FROM whatever WHERE name = %1',
- array(
- 1 => array('Alice', 'String'),
- ),
+ [
+ 1 => ['Alice', 'String'],
+ ],
'SELECT * FROM whatever WHERE name = \'Alice\'',
- );
+ ];
// CASE: Two params
- $cases[1003] = array(
+ $cases[1003] = [
'SELECT * FROM whatever WHERE name = %1 AND title = %2',
- array(
- 1 => array('Alice', 'String'),
- 2 => array('Bob', 'String'),
- ),
+ [
+ 1 => ['Alice', 'String'],
+ 2 => ['Bob', 'String'],
+ ],
'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\'',
- );
+ ];
// CASE: Two params with special character (%1)
- $cases[1004] = array(
+ $cases[1004] = [
'SELECT * FROM whatever WHERE name = %1 AND title = %2',
- array(
- 1 => array('Alice %2', 'String'),
- 2 => array('Bob', 'String'),
- ),
+ [
+ 1 => ['Alice %2', 'String'],
+ 2 => ['Bob', 'String'],
+ ],
'SELECT * FROM whatever WHERE name = \'Alice %2\' AND title = \'Bob\'',
- );
+ ];
// CASE: Two params with special character ($1)
- $cases[1005] = array(
+ $cases[1005] = [
'SELECT * FROM whatever WHERE name = %1 AND title = %2',
- array(
- 1 => array('Alice $1', 'String'),
- 2 => array('Bob', 'String'),
- ),
+ [
+ 1 => ['Alice $1', 'String'],
+ 2 => ['Bob', 'String'],
+ ],
'SELECT * FROM whatever WHERE name = \'Alice $1\' AND title = \'Bob\'',
- );
+ ];
return $cases;
}
* i.e. the place holder should be unique and should not contain in any other operational use in query
*/
public function testComposeQueryFailure() {
- $cases[] = array(
+ $cases[] = [
'SELECT * FROM whatever WHERE name = %1 AND title = %2 AND year LIKE \'%2012\' ',
- array(
- 1 => array('Alice', 'String'),
- 2 => array('Bob', 'String'),
- ),
+ [
+ 1 => ['Alice', 'String'],
+ 2 => ['Bob', 'String'],
+ ],
'SELECT * FROM whatever WHERE name = \'Alice\' AND title = \'Bob\' AND year LIKE \'%2012\' ',
- );
+ ];
list($inputSql, $inputParams, $expectSql) = $cases[0];
$actualSql = CRM_Core_DAO::composeQuery($inputSql, $inputParams);
$this->assertFalse(($expectSql == $actualSql));
* @return array
*/
public function sqlNameDataProvider() {
- return array(
- array('this is a long string', 30, FALSE, 'this is a long string'),
- array(
+ return [
+ ['this is a long string', 30, FALSE, 'this is a long string'],
+ [
'this is an even longer string which is exactly 60 character',
60,
FALSE,
'this is an even longer string which is exactly 60 character',
- ),
- array(
+ ],
+ [
'this is an even longer string which is exactly 60 character',
60,
TRUE,
'this is an even longer string which is exactly 60 character',
- ),
- array(
+ ],
+ [
'this is an even longer string which is a bit more than 60 character',
60,
FALSE,
'this is an even longer string which is a bit more than 60 ch',
- ),
- array(
+ ],
+ [
'this is an even longer string which is a bit more than 60 character',
60,
TRUE,
'this is an even longer string which is a bit more th_c1cbd519',
- ),
- );
+ ],
+ ];
}
/**
* requireSafeDBName() method (to check valid database name)
*/
public function testRequireSafeDBName() {
- $databases = array(
+ $databases = [
'testdb' => TRUE,
'test_db' => TRUE,
'TEST_db' => TRUE,
'testdb;Delete test' => FALSE,
'123456' => FALSE,
'test#$%^&*' => FALSE,
- );
- $testDetails = array();
+ ];
+ $testDetails = [];
foreach ($databases as $database => $val) {
$this->assertEquals(CRM_Core_DAO::requireSafeDBName($database), $val);
}
*/
public function testDAOtoArray() {
$format = 'user[%s]';
- $params = array(
+ $params = [
'first_name' => 'Testy',
'last_name' => 'McScallion',
'contact_type' => 'Individual',
- );
+ ];
$dao = CRM_Contact_BAO_Contact::add($params);
$query = "SELECT contact_type, display_name FROM civicrm_contact WHERE id={$dao->id}";
- $toArray = array(
+ $toArray = [
'contact_type' => 'Individual',
'display_name' => 'Testy McScallion',
- );
- $modifiedKeyArray = array();
+ ];
+ $modifiedKeyArray = [];
foreach ($toArray as $k => $v) {
$modifiedKeyArray[sprintf($format, $k)] = $v;
}
* CRM-17748: Test internal DAO options
*/
public function testDBOptions() {
- $contactIDs = array();
+ $contactIDs = [];
for ($i = 0; $i < 10; $i++) {
- $contactIDs[] = $this->individualCreate(array(
+ $contactIDs[] = $this->individualCreate([
'first_name' => 'Alan' . substr(sha1(rand()), 0, 7),
'last_name' => 'Smith' . substr(sha1(rand()), 0, 4),
- ));
+ ]);
}
// Test option 'result_buffering'
// cleanup
foreach ($contactIDs as $contactID) {
- $this->callAPISuccess('Contact', 'delete', array('id' => $contactID));
+ $this->callAPISuccess('Contact', 'delete', ['id' => $contactID]);
}
}
$dao = CRM_Core_DAO::executeQuery($sql);
$contactsFetchedFromBufferedQuery = $dao->fetchAll();
- $dao->free();
$dao = CRM_Core_DAO::executeUnbufferedQuery($sql);
$contactsFetchedFromUnbufferedQuery = $dao->fetchAll();
- $dao->free();
$this->checkArrayEquals($contactsFetchedFromBufferedQuery, $contactsFetchedFromUnbufferedQuery);
}
* @return array
*/
public function serializationMethods() {
- $constants = array();
- $simpleData = array(
+ $constants = [];
+ $simpleData = [
NULL,
- array('Foo', 'Bar', '3', '4', '5'),
- array(),
- array('0'),
- );
- $complexData = array(
- array(
+ ['Foo', 'Bar', '3', '4', '5'],
+ [],
+ ['0'],
+ ];
+ $complexData = [
+ [
'foo' => 'bar',
- 'baz' => array('1', '2', '3', array('one', 'two')),
+ 'baz' => ['1', '2', '3', ['one', 'two']],
'3' => '0',
- ),
- );
+ ],
+ ];
$daoInfo = new ReflectionClass('CRM_Core_DAO');
foreach ($daoInfo->getConstants() as $constant => $val) {
if ($constant == 'SERIALIZE_JSON' || $constant == 'SERIALIZE_PHP') {
- $constants[] = array($val, array_merge($simpleData, $complexData));
+ $constants[] = [$val, array_merge($simpleData, $complexData)];
}
elseif (strpos($constant, 'SERIALIZE_') === 0) {
- $constants[] = array($val, $simpleData);
+ $constants[] = [$val, $simpleData];
}
}
return $constants;
$this->assertEquals(2, $i);
}
+ /**
+ * Test modifying a query in a hook.
+ *
+ * Test that adding a sensible string does not cause failure.
+ *
+ * @throws \Exception
+ */
+ public function testModifyQuery() {
+ /**
+ * @param \Civi\Core\Event\QueryEvent $e
+ */
+ $listener = function($e) {
+ $e->query = '/* User : hooked */' . $e->query;
+ };
+ Civi::dispatcher()->addListener('civi.db.query', $listener);
+ CRM_Core_DAO::executeQuery('SELECT * FROM civicrm_domain');
+
+ Civi::dispatcher()->removeListener('civi.db.query', $listener);
+ }
+
+ /**
+ * Test modifying a query in a hook.
+ *
+ * Demonstrate it is modified showing the query now breaks.
+ */
+ public function testModifyAndBreakQuery() {
+ /**
+ * @param \Civi\Core\Event\QueryEvent $e
+ */
+ $listener = function($e) {
+ $e->query = '/* Forgot trailing comment marker' . $e->query;
+ };
+ Civi::dispatcher()->addListener('civi.db.query', $listener);
+ try {
+ CRM_Core_DAO::executeQuery('SELECT * FROM civicrm_domain');
+ }
+ catch (PEAR_Exception $e) {
+ $this->assertEquals(
+ "SELECT * FROM civicrm_domain [nativecode=1064 ** You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/* Forgot trailing comment markerSELECT * FROM civicrm_domain' at line 1]",
+ $e->getCause()->getUserInfo()
+ );
+ Civi::dispatcher()->removeListener('civi.db.query', $listener);
+ return;
+ }
+ Civi::dispatcher()->removeListener('civi.db.query', $listener);
+ $this->fail('String not altered');
+ }
+
}