You are here

public function AcquiaConnectorModuleTest::testAcquiaConnectorSubscriptionTests in Acquia Connector 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/AcquiaConnectorModuleTest.php \Drupal\Tests\acquia_connector\Functional\AcquiaConnectorModuleTest::testAcquiaConnectorSubscriptionTests()
  2. 8 tests/src/Functional/AcquiaConnectorModuleTest.php \Drupal\Tests\acquia_connector\Functional\AcquiaConnectorModuleTest::testAcquiaConnectorSubscriptionTests()

Test Connector subscription methods.

File

tests/src/Functional/AcquiaConnectorModuleTest.php, line 382

Class

AcquiaConnectorModuleTest
Tests the functionality of the Acquia Connector module.

Namespace

Drupal\Tests\acquia_connector\Functional

Code

public function testAcquiaConnectorSubscriptionTests() {
  $subscription = new Subscription();

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

  // Confirm HTTP request count is 0 because without credentials no request
  // should have been made.
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 0);
  $check_subscription = $subscription
    ->update();
  \Drupal::state()
    ->resetCache();
  $this
    ->assertFalse($check_subscription, 'Subscription is currently false.');

  // Confirm HTTP request count is still 0.
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 0);

  // Fail a connection.
  $random_id = $this
    ->randomString();
  $edit_fields = [
    'acquia_identifier' => $random_id,
    'acquia_key' => $this
      ->randomString(),
  ];
  $submit_button = 'Connect';
  $this
    ->drupalPostForm($this->credentialsPath, $edit_fields, $submit_button);

  // Confirm HTTP request count is 1.
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 1, 'Made 1 HTTP request in attempt to connect subscription.');
  $is_active = $subscription
    ->isActive();
  $this
    ->assertFalse($is_active, 'Subscription is not active after failed attempt to connect.');
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 1, 'Still have made only 1 HTTP request');
  $check_subscription = $subscription
    ->update();
  \Drupal::state()
    ->resetCache();
  $this
    ->assertFalse($check_subscription, 'Subscription is false after failed attempt to connect.');
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 1, 'Still have made only 1 HTTP request');

  // Test default from acquia_agent_settings().
  $stored = \Drupal::config('acquia_connector.settings');
  $current_subscription = \Drupal::state()
    ->get('acquia_subscription_data');

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

  // Reset HTTP request counter;.
  \Drupal::state()
    ->set('acquia_connector_test_request_count', 0);

  // Connect.
  $edit_fields = [
    'acquia_identifier' => $this->acqtestId,
    'acquia_key' => $this->acqtestKey,
  ];
  $this
    ->drupalPostForm($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(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 3, '3 HTTP requests were made during first connection.');
  $is_active = $subscription
    ->isActive();
  $this
    ->assertTrue($is_active, 'Subscription is active after successful connection.');
  $check_subscription = $subscription
    ->update();
  \Drupal::state()
    ->resetCache();
  $this
    ->assertIsArray($check_subscription, 'Subscription is array after successful connection.');

  // Now stored subscription data should match.
  $stored = \Drupal::config('acquia_connector.settings');
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 4, '1 additional HTTP request made via acquia_agent_check_subscription().');
  $this
    ->drupalGet($this->baseUrl);
  $this
    ->drupalGet('admin');
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 4, 'No extra requests made during visits to other pages.');

  // Reset HTTP request counter;.
  \Drupal::state()
    ->set('acquia_connector_test_request_count', 0);

  // Connect on expired subscription.
  $edit_fields = [
    'acquia_identifier' => $this->acqtestExpiredId,
    'acquia_key' => $this->acqtestExpiredKey,
  ];
  $this
    ->drupalPostForm($this->credentialsPath, $edit_fields, $submit_button);
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 3, '3 HTTP requests were made during expired connection attempt.');
  $is_active = $subscription
    ->isActive();
  $this
    ->assertFalse($is_active, 'Subscription is not active after connection with expired subscription.');
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 3, 'No additional HTTP requests made via acquia_agent_subscription_is_active().');
  $this
    ->drupalGet($this->baseUrl);
  $this
    ->drupalGet('admin');
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 3, 'No HTTP requests made during visits to other pages.');

  // Stored subscription data will now be the expired integer.
  $check_subscription = $subscription
    ->update();
  \Drupal::state()
    ->resetCache();
  $this
    ->assertIdentical($check_subscription, 1200, 'Subscription is expired after connection with expired subscription.');
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 4, '1 additional request made via acquia_agent_check_subscription().');
  $stored = \Drupal::config('acquia_connector.settings');
  $current_subscription = \Drupal::state()
    ->get('acquia_subscription_data');
  $this
    ->assertIdentical($check_subscription, $current_subscription, 'Stored expected subscription data.');

  // Reset HTTP request counter;.
  \Drupal::state()
    ->set('acquia_connector_test_request_count', 0);

  // Connect on subscription that will trigger a 503 response..
  $edit_fields = [
    'acquia_identifier' => $this->acqtest503Id,
    'acquia_key' => $this->acqtest503Key,
  ];
  $this
    ->drupalPostForm($this->credentialsPath, $edit_fields, $submit_button);
  $is_active = $subscription
    ->isActive();
  $this
    ->assertTrue($is_active, 'Subscription is active after successful connection.');

  // Make another request which will trigger 503 server error.
  $check_subscription = $subscription
    ->update();
  \Drupal::state()
    ->resetCache();

  // Hold onto subcription data for comparison.
  $stored = \Drupal::config('acquia_connector.settings');
  $this
    ->assertNotIdentical($check_subscription, '503', 'Subscription is not storing 503.');
  $this
    ->assertIsArray($check_subscription, 'Storing subscription array data.');
  $this
    ->assertIdentical(\Drupal::state()
    ->get('acquia_connector_test_request_count', 0), 4, 'Have made 4 HTTP requests so far.');
}