You are here

public function AcquiaAgentTestCase::testAcquiaAgentSubscription in Acquia Connector 7.3

Same name and namespace in other branches
  1. 7.2 acquia_agent/tests/acquia_agent.test \AcquiaAgentTestCase::testAcquiaAgentSubscription()

Test Agent subscription methods.

File

acquia_agent/tests/acquia_agent.test, line 238
Tests the functionality of the Acquia Agent module.

Class

AcquiaAgentTestCase
AcquiaAgentTestCase.

Code

public function testAcquiaAgentSubscription() {

  // Starts as inactive.
  $active = acquia_agent_subscription_is_active();
  $this
    ->assertFalse($active, 'Subscription is not currently active.');

  // Confirm HTTP request count is 0 because without credentials no request
  // should have been made.
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 0);
  $subscription = acquia_agent_check_subscription();
  $this
    ->assertFalse($subscription, 'Subscription is currently false.');

  // Confirm HTTP request count is still 0.
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 0);

  // Fail a connection.
  $random_id = $this
    ->randomName();
  $edit_fields = array(
    'acquia_identifier' => $random_id,
    'acquia_key' => $this
      ->randomName(),
  );
  $submit_button = 'Connect';
  $this
    ->drupalPost($this->credentialsPath, $edit_fields, $submit_button);

  // Confirm HTTP request count is 1.
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 1, 'Made 1 HTTP request in attempt to connect subscription.');
  $active = acquia_agent_subscription_is_active();
  $this
    ->assertFalse($active, 'Subscription is not active after failed attempt to connect.');
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 1, 'Still have made only 1 HTTP request');
  $subscription = acquia_agent_check_subscription();
  $this
    ->assertFalse($subscription, 'Subscription is false after failed attempt to connect.');
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 1, 'Still have made only 1 HTTP request');

  // Test default from acquia_agent_settings().
  $stored = acquia_agent_settings('acquia_subscription_data');

  // Not identical since acquia_agent_has_credentials() causes stored to be
  // deleted.
  $this
    ->assertNotIdentical($subscription, $stored, 'Stored subscription data not same before connected subscription.');
  $this
    ->assertTrue($stored['active'] === FALSE, 'Default is inactive.');

  // Reset HTTP request counter;.
  variable_set('acquia_connector_test-requests', 0);

  // Connect.
  $edit_fields = array(
    'acquia_identifier' => ACQUIA_CONNECTOR_TEST_ID,
    'acquia_key' => ACQUIA_CONNECTOR_TEST_KEY,
  );
  $this
    ->drupalPost($this->credentialsPath, $edit_fields, $submit_button);

  // HTTP requests should now be 3 (acquia.agent.subscription.name and
  // acquia.agent.subscription and acquia.agent.validate.
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 3, '3 HTTP requests were made during first connection.');
  $active = acquia_agent_subscription_is_active();
  $this
    ->assertTrue($active, 'Subscription is active after successful connection.');
  $subscription = acquia_agent_check_subscription();
  $this
    ->assertTrue(is_array($subscription), 'Subscription is array after successful connection.');

  // Now stored subscription data should match.
  $stored = acquia_agent_settings('acquia_subscription_data');
  $this
    ->assertIdentical($subscription, $stored, 'Stored expected subscription data.');
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 4, '1 additional HTTP request made via acquia_agent_check_subscription().');
  $this
    ->drupalGet('/');
  $this
    ->drupalGet('admin');
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 4, 'No extra requests made during visits to other pages.');

  // Reset HTTP request counter;.
  variable_set('acquia_connector_test-requests', 0);

  // Connect on expired subscription.
  $edit_fields = array(
    'acquia_identifier' => ACQUIA_CONNECTOR_TEST_EXPIRED_ID,
    'acquia_key' => ACQUIA_CONNECTOR_TEST_EXPIRED_KEY,
  );
  $this
    ->drupalPost($this->credentialsPath, $edit_fields, $submit_button);
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 3, '3 HTTP requests were made during expired connection attempt.');
  $active = acquia_agent_subscription_is_active();
  $this
    ->assertFalse($active, 'Subscription is not active after connection with expired subscription.');
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 3, 'No additional HTTP requests made via acquia_agent_subscription_is_active().');
  $this
    ->drupalGet('/');
  $this
    ->drupalGet('admin');
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 3, 'No HTTP requests made during visits to other pages.');

  // Stored subscription data will now be the expired integer.
  $subscription = acquia_agent_check_subscription();
  $this
    ->assertIdentical($subscription, ACQUIA_AGENT_SUBSCRIPTION_EXPIRED, 'Subscription is expired after connection with expired subscription.');
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 4, '1 additional request made via acquia_agent_check_subscription().');
  $stored = acquia_agent_settings('acquia_subscription_data');
  $this
    ->assertIdentical($subscription, $stored, 'Stored expected subscription data.');

  // Reset HTTP request counter;.
  variable_set('acquia_connector_test-requests', 0);

  // Connect on subscription that will trigger a 503 response..
  $edit_fields = array(
    'acquia_identifier' => ACQUIA_CONNECTOR_TEST_503_ID,
    'acquia_key' => ACQUIA_CONNECTOR_TEST_503_KEY,
  );
  $this
    ->drupalPost($this->credentialsPath, $edit_fields, $submit_button);
  $active = acquia_agent_subscription_is_active();
  $this
    ->assertTrue($active, 'Subscription is active after successful connection.');

  // Hold onto subcription data for comparison.
  $stored = acquia_agent_settings('acquia_subscription_data');

  // Make another request which will trigger 503 server error.
  $subscription = acquia_agent_check_subscription();
  $this
    ->assertNotIdentical($subscription, '503', 'Subscription is not storing 503.');
  $this
    ->assertTrue(is_array($subscription), 'Storing subscription array data.');
  $this
    ->assertIdentical($stored, $subscription, 'Subscription data is the same.');
  $this
    ->assertIdentical(variable_get('acquia_connector_test-requests', 0), 4, 'Have made 4 HTTP requests so far.');
}