From e635f9d4345a805759eee569af9bc0900f764c9b Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sat, 20 Jul 2013 12:36:11 -0700 Subject: [PATCH] CRM-12556 - Add unit tests for fetching contact by username ---------------------------------------- * CRM-12556: Assign api_key for a user via command-line http://issues.civicrm.org/jira/browse/CRM-12556 --- tests/phpunit/api/v3/ContactTest.php | 59 +++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index a4e40ca6a2..d064c21c80 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -86,7 +86,8 @@ class api_v3_ContactTest extends CiviUnitTestCase { 'civicrm_contribution', 'civicrm_line_item', 'civicrm_website', - 'civicrm_relationship' + 'civicrm_relationship', + 'civicrm_uf_match', ); $this->quickCleanup($tablesToTruncate); @@ -1376,6 +1377,62 @@ class api_v3_ContactTest extends CiviUnitTestCase { civicrm_api('contact', 'delete', $contact); } + /** + * Test for Contact.get id=@user:username + */ + function testContactGetByUsername() { + // setup - create contact with a uf-match + $cid = $this->individualCreate(array( + 'contact_type' => 'Individual', + 'first_name' => 'testGetByUsername', + 'last_name' => 'testGetByUsername', + )); + + $ufMatchParams = array( + 'domain_id' => CRM_Core_Config::domainID(), + 'uf_id' => 99, + 'uf_name' => 'the-email-matching-key-is-not-really-the-username', + 'contact_id' => $cid, + ); + $ufMatch = CRM_Core_BAO_UFMatch::create($ufMatchParams); + $this->assertTrue(is_numeric($ufMatch->id)); + + // setup - mock the calls to CRM_Utils_System_*::getUfId + $userSystem = $this->getMock('CRM_Utils_System_UnitTests', array('getUfId')); + $userSystem->expects($this->once()) + ->method('getUfId') + ->with($this->equalTo('exampleUser')) + ->will($this->returnValue(99)); + CRM_Core_Config::singleton()->userSystem = $userSystem; + + // perform a lookup + $result = $this->callAPISuccess('Contact', 'get', array( + 'version' => 3, + 'id' => '@user:exampleUser', + )); + $this->assertEquals('testGetByUsername', $result['values'][$cid]['first_name']); + } + + /** + * Test for Contact.get id=@user:username (with an invalid username) + */ + function testContactGetByUnknownUsername() { + // setup - mock the calls to CRM_Utils_System_*::getUfId + $userSystem = $this->getMock('CRM_Utils_System_UnitTests', array('getUfId')); + $userSystem->expects($this->once()) + ->method('getUfId') + ->with($this->equalTo('exampleUser')) + ->will($this->returnValue(NULL)); + CRM_Core_Config::singleton()->userSystem = $userSystem; + + // perform a lookup + $result = $this->callAPIFailure('Contact', 'get', array( + 'version' => 3, + 'id' => '@user:exampleUser', + )); + $this->assertRegExp('/cannot be resolved to a contact ID/', $result['error_message']); + } + /** * Verify attempt to create individual with chained arrays */ -- 2.25.1