You are here

public function ALProfilesAPITest::testSaveEvent in Acquia Lift Connector 7

Same name and namespace in other branches
  1. 7.2 acquia_lift_profiles/tests/acquia_lift_profiles_unit.test \ALProfilesAPITest::testSaveEvent()

Test ALProfilesAPI->saveEvent()

File

acquia_lift_profiles/tests/acquia_lift_profiles_unit.test, line 161
Unit tests for Acquia Lift Profiles module.

Class

ALProfilesAPITest
@file Unit tests for Acquia Lift Profiles module.

Code

public function testSaveEvent() {
  $acquia_lift_profiles_api = $this
    ->getALProfilesAPI();
  $event_name = 'test_event';
  $event_label = 'My Test Event';
  $acquia_lift_profiles_api
    ->saveEvent($event_name, $event_label);

  // Define the requests we expect to have been made to our dummy http
  // client for this operation.
  $canonicalRequest = "PUT\naccept:application/json\n/dashboard/rest/{$acquia_lift_profiles_api->getAccountName()}/events/{$event_name}?label={$event_label}&type=OTHER";
  $hmac = base64_encode(hash_hmac('sha1', $canonicalRequest, $this->secretKey, TRUE));
  $auth_header = 'HMAC ' . $this->accessKey . ':' . $hmac;
  $encoded_label = rawurlencode($event_label);
  $requests = array(
    array(
      'type' => 'put',
      'uri' => "{$acquia_lift_profiles_api->getApiUrl()}/dashboard/rest/{$acquia_lift_profiles_api->getAccountName()}/events/{$event_name}?label={$encoded_label}&type=OTHER",
      'headers' => array(
        'Authorization' => $auth_header,
        'Accept' => 'application/json',
      ),
      'options' => array(),
      'body' => NULL,
    ),
  );

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::INFO,
      'message' => "The event {$event_name} has been saved to Acquia Lift Profiles",
    ),
  );
  $this
    ->assertLogs($logs);

  // Now try with a broken client.
  $acquia_lift_profiles_api = $this
    ->getALProfilesAPI(TRUE);
  try {
    $acquia_lift_profiles_api
      ->saveEvent($event_name, $event_label);
  } catch (Exception $e) {
    $this
      ->assertTrue($e instanceof ALProfilesException);
  }

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::ERROR,
      'message' => "Could not save event {$event_name} to Acquia Lift Profiles",
    ),
  );
  $this
    ->assertLogs($logs);

  // Now try with a non-empty customer site
  ALProfilesAPI::reset();
  $this->customerSite = 'TESTSITE';
  $acquia_lift_profiles_api = $this
    ->getALProfilesAPI();
  $acquia_lift_profiles_api
    ->saveEvent($event_name, $event_label);

  // Our request should now include a customerSite param in the querystring
  $canonicalRequest = "PUT\naccept:application/json\n/dashboard/rest/{$acquia_lift_profiles_api->getAccountName()}/events/{$event_name}?customerSite={$this->customerSite}&label={$event_label}&type=OTHER";
  $hmac = base64_encode(hash_hmac('sha1', $canonicalRequest, $this->secretKey, TRUE));
  $auth_header = 'HMAC ' . $this->accessKey . ':' . $hmac;
  $encoded_label = rawurlencode($event_label);
  $requests = array(
    array(
      'type' => 'put',
      'uri' => "{$acquia_lift_profiles_api->getApiUrl()}/dashboard/rest/{$acquia_lift_profiles_api->getAccountName()}/events/{$event_name}?customerSite={$this->customerSite}&label={$encoded_label}&type=OTHER",
      'headers' => array(
        'Authorization' => $auth_header,
        'Accept' => 'application/json',
      ),
      'options' => array(),
      'body' => NULL,
    ),
  );

  // Confirm the expected requests were made.
  $this
    ->assertAPIRequests($requests);
  $logs = array(
    array(
      'level' => PersonalizeLogLevel::INFO,
      'message' => "The event {$event_name} has been saved to Acquia Lift Profiles",
    ),
  );
  $this
    ->assertLogs($logs);
  $this->customerSite = '';
  ALProfilesAPI::reset();
}