Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test demonstrating attaching a listener to queries #14716

Merged
merged 1 commit into from
Jul 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering if this check could be a bit brittle if the exact text of MySQL's error message changes, or MariaDB diverges from MySQL, or someone runs the test with MySQL in a different locale. Could look for substrings like "nativecode=1064" and "'/* Forgot trailing comment markerSELECT * FROM civicrm_domain'" instead?

$e->getCause()->getUserInfo()
);
Civi::dispatcher()->removeListener('civi.db.query', $listener);
return;
}
Civi::dispatcher()->removeListener('civi.db.query', $listener);
$this->fail('String not altered');
}

}