From feb7e5d4eba32a4e24f7b6306576ea1e66d624dd Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 3 Jul 2019 14:44:05 +1200 Subject: [PATCH] Add unit test demonstrating attaching a listener to queries Unit test for https://github.com/civicrm/civicrm-packages/pull/180 --- tests/phpunit/CRM/Core/DAOTest.php | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/phpunit/CRM/Core/DAOTest.php b/tests/phpunit/CRM/Core/DAOTest.php index e2bae8e16d..dee82c0b13 100644 --- a/tests/phpunit/CRM/Core/DAOTest.php +++ b/tests/phpunit/CRM/Core/DAOTest.php @@ -475,4 +475,46 @@ class CRM_Core_DAOTest extends CiviUnitTestCase { $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'); + } + } -- 2.25.1