You are here

public function DeveloperAppUITest::testCallbackUrlValidationClientSide in Apigee Edge 8

Tests callback url validation on the client-side.

File

tests/src/FunctionalJavascript/DeveloperAppUITest.php, line 74

Class

DeveloperAppUITest
Developer app UI Javascript tests.

Namespace

Drupal\Tests\apigee_edge\FunctionalJavascript

Code

public function testCallbackUrlValidationClientSide() {
  $isValidInput = function () : bool {
    return $this
      ->getSession()
      ->evaluateScript('document.getElementById("edit-callbackurl-0-value").checkValidity()');
  };
  $checkValidationMessage = function (string $expected) : void {
    $this
      ->assertEquals($expected, $this
      ->getSession()
      ->evaluateScript('document.getElementById("edit-callbackurl-0-value").validationMessage'));
  };

  // Override default configuration.
  $pattern_error_message = 'It must be https://example.com';
  $this
    ->config('apigee_edge.common_app_settings')
    ->set('callback_url_pattern', '^https:\\/\\/example.com')
    ->set('callback_url_pattern_error_message', $pattern_error_message)
    ->save();
  $app = $this
    ->createDeveloperApp([
    'name' => $this
      ->randomMachineName(),
    'displayName' => $this
      ->randomString(),
    'callbackUrl' => $this
      ->randomMachineName(),
  ], $this->account, [
    $this->products[0]
      ->id(),
  ]);
  $app_edit_url = $app
    ->toUrl('edit-form-for-developer');
  $this
    ->drupalGet($app_edit_url);
  $this
    ->drupalPostForm($app_edit_url, [], 'Save');
  $this
    ->createScreenshot('DeveloperAppUITest-' . __FUNCTION__);
  $this
    ->assertFalse($isValidInput());
  $checkValidationMessage('Please enter a URL.');
  $this
    ->drupalPostForm($app_edit_url, [
    'callbackUrl[0][value]' => 'http://example.com',
  ], 'Save');
  $this
    ->createScreenshot('DeveloperAppUITest-' . __FUNCTION__);
  $this
    ->assertFalse($isValidInput());

  // The format in Firefox is different, it is only one line:
  // "Please match the requested format: {$pattern_description}.".
  $checkValidationMessage('Please match the requested format.');
  $this
    ->assertEquals($pattern_error_message, $this
    ->getSession()
    ->evaluateScript('document.getElementById("edit-callbackurl-0-value").title'));
  $this
    ->drupalPostForm($app_edit_url, [
    'callbackUrl[0][value]' => 'https://example.com',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('App has been successfully updated.');
  $this
    ->assertSession()
    ->pageTextContains('https://example.com');
}