From 7e6f3b47696299b6f0baab92994193fe76f7296f Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 17 Apr 2019 13:48:23 +1000 Subject: [PATCH] (NFC) Add in unit test checking that E2E test returns sensible contact_id when passed in a special token proving that session is working --- tests/phpunit/E2E/Extern/RestTest.php | 33 +++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/E2E/Extern/RestTest.php b/tests/phpunit/E2E/Extern/RestTest.php index a288acc5689e..1995fc0c2fc8 100644 --- a/tests/phpunit/E2E/Extern/RestTest.php +++ b/tests/phpunit/E2E/Extern/RestTest.php @@ -35,6 +35,7 @@ class E2E_Extern_RestTest extends CiviEndToEndTestCase { protected $session_id; protected $nocms_contact_id; protected $old_api_keys; + protected $adminContactId; /** * @param $apiResult @@ -272,6 +273,30 @@ public function testNotCMSUser_entityAction() { $this->assertAPIErrorCode($result, 1); } + /** + * Submit a request with an API key that exists but does not correspond to. + * a real user. Submit in "?entity=X&action=X" notation + */ + public function testGetCorrectUserBack() { + $client = CRM_Utils_HttpClient::singleton(); + + //Create contact with api_key + // The key associates with a real contact but not a real user + $params = array( + "entity" => "Contact", + "action" => "get", + "key" => $GLOBALS['_CV']['CIVI_SITE_KEY'], + "json" => "1", + "api_key" => self::getApiKey(), + "id" => "user_contact_id", + ); + list($status, $data) = $client->post($this->getRestUrl(), $params); + $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status); + $result = json_decode($data, TRUE); + $this->assertNotNull($result); + $this->assertEquals($result['id'], $this->adminContactId); + } + /** * Submit a request with an API key that exists but does not correspond to * a real user. Submit in "?q=civicrm/$entity/$action" notation @@ -305,13 +330,13 @@ public function testNotCMSUser_q() { protected function updateAdminApiKey() { /** @var int $adminContactId */ - $adminContactId = civicrm_api3('contact', 'getvalue', array( + $this->adminContactId = civicrm_api3('contact', 'getvalue', array( 'id' => '@user:' . $GLOBALS['_CV']['ADMIN_USER'], 'return' => 'id', )); - $this->old_api_keys[$adminContactId] = CRM_Core_DAO::singleValueQuery('SELECT api_key FROM civicrm_contact WHERE id = %1', [ - 1 => [$adminContactId, 'Positive'], + $this->old_api_keys[$this->adminContactId] = CRM_Core_DAO::singleValueQuery('SELECT api_key FROM civicrm_contact WHERE id = %1', [ + 1 => [$this->adminContactId, 'Positive'], ]); //$this->old_admin_api_key = civicrm_api3('Contact', 'get', array( @@ -320,7 +345,7 @@ protected function updateAdminApiKey() { //)); civicrm_api3('Contact', 'create', array( - 'id' => $adminContactId, + 'id' => $this->adminContactId, 'api_key' => self::getApiKey(), )); }