Skip to content

Commit

Permalink
Merge pull request #14716 from eileenmcnaughton/db
Browse files Browse the repository at this point in the history
Add unit test demonstrating attaching a listener to queries
  • Loading branch information
colemanw authored Jul 22, 2019
2 parents ff49984 + feb7e5d commit cc07069
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/phpunit/CRM/Core/DAOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,46 @@ public function testCloneDAO() {
$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() {
$listener = function(\Civi\Core\Event\GenericHookEvent $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() {
$listener = function(\Civi\Core\Event\GenericHookEvent $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');
}

}

0 comments on commit cc07069

Please sign in to comment.