From 1a7f0799cab0cbd533d49afeda59a20bdf9ecbee Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Mon, 5 Mar 2018 11:06:48 +0000 Subject: [PATCH] Add unit tests for testSendSmsNoPhoneNumber,testSendSmsFixedPhoneNumber,testSendSmsMobilePhoneNumber, create new CiviTestSMSProvider --- CRM/SMS/Provider.php | 6 +- .../phpunit/CRM/Activity/BAO/ActivityTest.php | 157 ++++++++++++++++++ tests/phpunit/CRM/SMS/ProviderTest.php | 19 +-- .../phpunit/CiviTest/CiviTestSMSProvider.php | 64 +++++++ 4 files changed, 229 insertions(+), 17 deletions(-) create mode 100644 tests/phpunit/CiviTest/CiviTestSMSProvider.php diff --git a/CRM/SMS/Provider.php b/CRM/SMS/Provider.php index cbbe3f1d4f..77b92087b9 100644 --- a/CRM/SMS/Provider.php +++ b/CRM/SMS/Provider.php @@ -76,11 +76,11 @@ abstract class CRM_SMS_Provider { require_once "{$providerClass}.php"; } else { - // If we are running unit tests we simulate an SMS provider with the name "testSmsProvider" - if ($providerName !== 'testSmsProvider') { + // If we are running unit tests we simulate an SMS provider with the name "CiviTestSMSProvider" + if ($providerName !== 'CiviTestSMSProvider') { CRM_Core_Error::fatal("Could not locate extension for {$providerName}."); } - $providerClass = 'testSmsProvider'; + $providerClass = 'CiviTestSMSProvider'; } self::$_singleton[$cacheKey] = $providerClass::singleton($providerParams, $force); diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index 98a5433bec..cb74742666 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -9,6 +9,7 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase { parent::setUp(); $this->prepareForACLs(); CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts', 'access CiviCRM'); + $this->setupForSmsTests(); } /** @@ -24,9 +25,43 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase { ); $this->quickCleanup($tablesToTruncate); $this->cleanUpAfterACLs(); + $this->setupForSmsTests(TRUE); parent::tearDown(); } + /** + * Setup or clean up SMS tests + * @param bool $teardown + * + * @throws \CiviCRM_API3_Exception + */ + public function setupForSmsTests($teardown = FALSE) { + require_once 'CiviTest/CiviTestSMSProvider.php'; + + // Option value params for CiviTestSMSProvider + $groupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'sms_provider_name', 'id', 'name'); + $params = array( + 'option_group_id' => $groupID, + 'label' => 'unittestSMS', + 'value' => 'unit.test.sms', + 'name' => 'CiviTestSMSProvider', + 'is_default' => 1, + 'is_active' => 1, + 'version' => 3, + ); + + if ($teardown) { + // Test completed, delete provider + $providerOptionValueResult = civicrm_api3('option_value', 'get', $params); + civicrm_api3('option_value', 'delete', array('id' => $providerOptionValueResult['id'])); + return; + } + + // Create an SMS provider "CiviTestSMSProvider". Civi handles "CiviTestSMSProvider" as a special case and allows it to be instantiated + // in CRM/Sms/Provider.php even though it is not an extension. + civicrm_api3('option_value', 'create', $params); + } + /** * Test case for create() method. */ @@ -1154,4 +1189,126 @@ $text ); } + public function testSendSmsNoPhoneNumber() { + list($sent, $activityId, $success) = $this->createSendSmsTest(0); + $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activityId, 'version' => $this->_apiversion)); + + $outBoundSmsActivityId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'); + $activityStatusCompleted = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); + $details = 'createSendSmsTest text'; + $this->assertEquals($activity['activity_type_id'], $outBoundSmsActivityId, 'Wrong activity type is set.'); + $this->assertEquals($activity['status_id'], $activityStatusCompleted, 'Expected activity status Completed.'); + $this->assertEquals($activity['subject'], 'createSendSmsTest subject', 'Activity subject does not match.'); + $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); + $this->assertEquals("Recipient phone number is invalid or recipient does not want to receive SMS", $sent[0]->message, "Expected error doesn't match"); + $this->assertEquals(0, $success, "Expected success to be 0"); + } + + public function testSendSmsFixedPhoneNumber() { + list($sent, $activityId, $success) = $this->createSendSmsTest(1); + $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activityId, 'version' => $this->_apiversion)); + + $outBoundSmsActivityId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'); + $activityStatusCompleted = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); + $details = 'createSendSmsTest text'; + $this->assertEquals($activity['activity_type_id'], $outBoundSmsActivityId, 'Wrong activity type is set.'); + $this->assertEquals($activity['status_id'], $activityStatusCompleted, 'Expected activity status Completed.'); + $this->assertEquals($activity['subject'], 'createSendSmsTest subject', 'Activity subject does not match.'); + $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); + $this->assertEquals("Recipient phone number is invalid or recipient does not want to receive SMS", $sent[0]->message, "Expected error doesn't match"); + $this->assertEquals(0, $success, "Expected success to be 0"); + } + + public function testSendSmsMobilePhoneNumber() { + list($sent, $activityId, $success) = $this->createSendSmsTest(2); + $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activityId, 'version' => $this->_apiversion)); + + $outBoundSmsActivityId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'); + $activityStatusCompleted = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); + $details = 'createSendSmsTest text'; + $this->assertEquals($activity['activity_type_id'], $outBoundSmsActivityId, 'Wrong activity type is set.'); + $this->assertEquals($activity['status_id'], $activityStatusCompleted, 'Expected activity status Completed.'); + $this->assertEquals($activity['subject'], 'createSendSmsTest subject', 'Activity subject does not match.'); + $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); + $this->assertEquals(TRUE, $sent, "Expected sent should be true"); + $this->assertEquals(1, $success, "Expected success to be 1"); + } + + /** + * @param int $phoneType (0=no phone, phone_type option group (1=fixed, 2=mobile) + */ + public function createSendSmsTest($phoneType = 0) { + $provider = civicrm_api3('SmsProvider', 'create', array( + 'name' => "CiviTestSMSProvider", + 'api_type' => "1", + "username" => "1", + "password" => "1", + "api_type" => "1", + "api_url" => "1", + "api_params" => "a=1", + "is_default" => "1", + "is_active" => "1", + "domain_id" => "1", + )); + $smsProviderParams['provider_id'] = $provider['id']; + + // Create a contact + $contactId = $this->individualCreate(); + $contactsResult = $this->civicrm_api('contact', 'get', array('id' => $contactId, 'version' => $this->_apiversion)); + $contactDetails = $contactsResult['values']; + + // Get contactIds from contact details + foreach ($contactDetails as $contact) { + $contactIds[] = $contact['contact_id']; + } + + $activityParams['sms_text_message'] = __FUNCTION__ . ' text'; + $activityParams['activity_subject'] = __FUNCTION__ . ' subject'; + + // ActivityParams is overwritten by sendSms but we need it for results + $activityParamsCopy = $activityParams; + + // Get a "logged in" user to set as source of Sms. + $session = CRM_Core_Session::singleton(); + $sourceContactId = $session->get('userID'); + + // Create a user, then a phone number + $this->_testSmsContactId = $this->createLoggedInUser(); + // Create phone number + switch ($phoneType) { + case 0: + // No phone number + break; + + case 2: + // Create a mobile phone number + $phone = civicrm_api3('Phone', 'create', array( + 'contact_id' => $contactId, + 'phone' => 123456, + 'phone_type_id' => "Mobile", + )); + break; + + case 1: + // Create a fixed phone number + $phone = civicrm_api3('Phone', 'create', array( + 'contact_id' => $contactId, + 'phone' => 654321, + 'phone_type_id' => "Phone", + )); + break; + } + + // Now run the actual test + list($sent, $activityId, $success) = CRM_Activity_BAO_Activity::sendSms( + $contactDetails, + $activityParams, + $smsProviderParams, + $contactIds, + $sourceContactId + ); + + return array($sent, $activityId, $success); + } + } diff --git a/tests/phpunit/CRM/SMS/ProviderTest.php b/tests/phpunit/CRM/SMS/ProviderTest.php index fade1624f8..4bf7cd57a3 100644 --- a/tests/phpunit/CRM/SMS/ProviderTest.php +++ b/tests/phpunit/CRM/SMS/ProviderTest.php @@ -32,6 +32,8 @@ * @subpackage API_Contribution * @group headless */ +require_once 'CiviTest/CiviTestSMSProvider.php'; + class CRM_SMS_ProviderTest extends CiviUnitTestCase { /** @@ -57,7 +59,7 @@ class CRM_SMS_ProviderTest extends CiviUnitTestCase { */ public function testProcessInbound() { $testSourceContact = $this->individualCreate(array('phone' => array(1 => array('phone_type_id' => 'Phone', 'location_type_id' => 'Home', 'phone' => '+61487654321')))); - $provider = new testSMSProvider(); + $provider = new CiviTestSMSProvider('CiviTestSMSProvider'); $result = $provider->processInbound('+61412345678', 'This is a test message', '+61487654321'); $this->assertEquals('This is a test message', $result->details); $this->assertEquals('+61412345678', $result->phone_number); @@ -67,7 +69,7 @@ class CRM_SMS_ProviderTest extends CiviUnitTestCase { * CRM-20238 Add test of processInbound function where no To is passed into the function */ public function testProcessInboundNoTo() { - $provider = new testSMSProvider(); + $provider = new CiviTestSMSProvider('CiviTestSMSProvider'); $result = $provider->processInbound('+61412345678', 'This is a test message', NULL, '12345'); $this->assertEquals('This is a test message', $result->details); $this->assertEquals('+61412345678', $result->phone_number); @@ -83,7 +85,7 @@ class CRM_SMS_ProviderTest extends CiviUnitTestCase { * CRM-20238 Add test of ProcessInbound function where no To number is passed into the function but the toContactId gets set in a hook */ public function testProcessInboundSetToContactIDUsingHook() { - $provider = new testSMSProvider(); + $provider = new CiviTestSMSProvider('CiviTestSMSProvider'); $this->hookClass->setHook('civicrm_inboundSMS', array($this, 'smsHookTest')); $result = $provider->processInbound('+61412345678', 'This is a test message', NULL, '12345'); $this->assertEquals('This is a test message', $result->details); @@ -101,14 +103,3 @@ class CRM_SMS_ProviderTest extends CiviUnitTestCase { } } - -/** - * Test SMS provider to allow for testing - */ -class testSMSProvider extends CRM_SMS_Provider { - - public function send($recipients, $header, $message, $dncID = NULL) { - parent::send($recipients, $header, $message, $dncID); - } - -} diff --git a/tests/phpunit/CiviTest/CiviTestSMSProvider.php b/tests/phpunit/CiviTest/CiviTestSMSProvider.php new file mode 100644 index 0000000000..4675e35d61 --- /dev/null +++ b/tests/phpunit/CiviTest/CiviTestSMSProvider.php @@ -0,0 +1,64 @@ +provider = $provider; + } + + public static function &singleton($providerParams = array(), $force = FALSE) { + if (isset($providerParams['provider'])) { + $providers = CRM_SMS_BAO_Provider::getProviders(NULL, array('name' => $providerParams['provider'])); + $provider = current($providers); + $providerID = $provider['id']; + } + else { + $providerID = CRM_Utils_Array::value('provider_id', $providerParams); + } + $skipAuth = $providerID ? FALSE : TRUE; + $cacheKey = (int) $providerID; + + if (!isset(self::$_singleton[$cacheKey]) || $force) { + $provider = array(); + if ($providerID) { + $provider = CRM_SMS_BAO_Provider::getProviderInfo($providerID); + } + self::$_singleton[$cacheKey] = new CiviTestSMSProvider($provider, $skipAuth); + } + return self::$_singleton[$cacheKey]; + } + + public function send($recipients, $header, $message, $dncID = NULL) { + } + +} -- 2.25.1