You are here

public function CustomProvidersUiTest::testCustomProviderForm in oEmbed Providers 1.0.x

Same name and namespace in other branches
  1. 2.x tests/src/FunctionalJavascript/CustomProvidersUiTest.php \Drupal\Tests\oembed_providers\FunctionalJavascript\CustomProvidersUiTest::testCustomProviderForm()
  2. 1.1.x tests/src/FunctionalJavascript/CustomProvidersUiTest.php \Drupal\Tests\oembed_providers\FunctionalJavascript\CustomProvidersUiTest::testCustomProviderForm()

Tests Custom Providers add/edit form.

File

tests/src/FunctionalJavascript/CustomProvidersUiTest.php, line 55

Class

CustomProvidersUiTest
Class CustomProvidersUiTest.

Namespace

Drupal\Tests\oembed_providers\FunctionalJavascript

Code

public function testCustomProviderForm() {
  $this
    ->getSession()
    ->resizeWindow(1200, 2000);
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->drupalGet('/admin/config/media/oembed-providers/custom-providers/add');
  $assert_session
    ->pageTextContains('Endpoint #1');

  // Fill in data for Endpoint 1.
  // Cause no validation issues.
  $page
    ->findField('label')
    ->setValue('Test Provider');

  // For some reason the machine name field won't auto-populate, so make
  // it visible and manually populate it.
  $this
    ->assertJsCondition("jQuery('.js-form-type-machine-name').removeClass('visually-hidden')");
  $page
    ->findField('provider_url')
    ->setValue('https://test-provider.com');
  $page
    ->findField('id')
    ->setValue('test_provider');
  $page
    ->findField('endpoints[endpoint-1][schemes]')
    ->setValue('https://test-provider.com/media/*');
  $page
    ->findField('endpoints[endpoint-1][url]')
    ->setValue('https://test-provider.com/oembed/v1/media');
  $page
    ->findField('endpoints[endpoint-1][discovery]')
    ->setValue(1);
  $page
    ->pressButton('Add an endpoint');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->pageTextContains('Endpoint #2');

  // Fill in data for Endpoint 2.
  // Cause validation issues.
  $page
    ->findField('endpoints[endpoint-2][schemes]')
    ->setValue('invalid URL');
  $page
    ->findField('endpoints[endpoint-2][url]')
    ->setValue('https://test-provider.com/oembed/v1/');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('A valid URL is required on line 1.');
  $assert_session
    ->pageTextContains('If discovery is disabled, then one or more formats must be explicitly defined for an endpoint.');
  $page
    ->pressButton('Add an endpoint');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->pageTextContains('Endpoint #3');

  // Fill in data for Endpoint 3.
  // Cause no validation issues.
  $page
    ->findField('endpoints[endpoint-3][schemes]')
    ->setValue('https://test-provider.com/media1/*' . PHP_EOL . 'http://test-provider.com/media1/*');
  $page
    ->findField('endpoints[endpoint-3][url]')
    ->setValue('https://test-provider.com/oembed/v1/media1');
  $page
    ->findField('endpoints[endpoint-3][formats][json]')
    ->setValue('json');

  // Remove endpoint 2.
  $page
    ->pressButton('remove-endpoint2');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->pageTextContains('Endpoint #1');
  $assert_session
    ->pageTextNotContains('Endpoint #2');
  $assert_session
    ->pageTextContains('Endpoint #3');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains("The Test Provider oEmbed provider was created.");
  $this
    ->assertInstanceOf(OembedProvider::class, OembedProvider::load('test_provider'));

  // Load resulting config entity and compare to expected values.
  $entity = \Drupal::entityTypeManager()
    ->getStorage('oembed_provider')
    ->load('test_provider');
  $this
    ->AssertSame($entity
    ->get('label'), 'Test Provider');
  $this
    ->AssertSame($entity
    ->get('id'), 'test_provider');
  $this
    ->AssertSame($entity
    ->get('provider_url'), 'https://test-provider.com');
  $endpoints = [
    [
      'schemes' => [
        'https://test-provider.com/media/*',
      ],
      'url' => 'https://test-provider.com/oembed/v1/media',
      'discovery' => TRUE,
      'formats' => [
        'json' => FALSE,
        'xml' => FALSE,
      ],
    ],
    [
      'schemes' => [
        'https://test-provider.com/media1/*',
        'http://test-provider.com/media1/*',
      ],
      'url' => 'https://test-provider.com/oembed/v1/media1',
      'discovery' => FALSE,
      'formats' => [
        'json' => TRUE,
        'xml' => FALSE,
      ],
    ],
  ];
  $this
    ->AssertSame($entity
    ->get('endpoints'), $endpoints);

  // Verify cached providers are cleared.
  $this
    ->AssertFalse(\Drupal::cache()
    ->get('oembed_providers:oembed_providers'));

  // Load edit page.
  $this
    ->drupalGet('/admin/config/media/oembed-providers/custom-providers/test_provider/edit');

  // Verify re-indexing of endpoints.
  $assert_session
    ->pageTextContains('Endpoint #1');
  $assert_session
    ->pageTextContains('Endpoint #2');
  $value = $page
    ->findField('endpoints[endpoint-1][url]')
    ->getValue();
  $this
    ->AssertSame($value, 'https://test-provider.com/oembed/v1/media');
  $value = $page
    ->findField('endpoints[endpoint-2][url]')
    ->getValue();
  $this
    ->AssertSame($value, 'https://test-provider.com/oembed/v1/media1');
}