SettingsFormTest.php in oEmbed Providers 1.1.x
File
tests/src/Functional/SettingsFormTest.php
View source
<?php
namespace Drupal\Tests\oembed_providers\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\media\Traits\OEmbedTestTrait;
class SettingsFormTest extends BrowserTestBase {
use OEmbedTestTrait;
protected $defaultTheme = 'stark';
protected $adminUser;
protected $nonAdminUser;
public static $modules = [
'block',
'media',
'oembed_providers',
];
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer blocks',
'administer oembed providers',
]);
$this->nonAdminUser = $this
->drupalCreateUser([
'access administration pages',
]);
$this
->drupalPlaceBlock('system_messages_block');
}
public function testRoutePermissions() {
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->nonAdminUser);
$this
->drupalGet('/admin/config/media/oembed-providers');
$assert_session
->statusCodeEquals(403, "Non-admin user is unable to access settings page");
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/config/media/oembed-providers');
$assert_session
->statusCodeEquals(200, "Admin user is unable to access settings page");
}
public function testSettingsForm() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
\Drupal::cache()
->set('oembed_providers:oembed_providers', 'test value', REQUEST_TIME + 86400);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/config/media/oembed-providers');
$assert_session
->checkboxChecked('Enable external fetch of providers');
$this
->assertSame('https://oembed.com/providers.json', $page
->findField('oembed_providers_url')
->getValue());
$page
->findField('oembed_providers_url')
->setValue('https://example.com/providers.json');
$page
->pressButton('Save configuration');
$assert_session
->pageTextContains('The configuration options have been saved.');
$this
->assertSame('https://example.com/providers.json', $this
->config('media.settings')
->get('oembed_providers_url'));
$this
->AssertFalse(\Drupal::cache()
->get('oembed_providers:oembed_providers'));
$this
->drupalGet('/admin/config/media/oembed-providers');
$assert_session
->checkboxChecked('Enable external fetch of providers');
$this
->assertSame('https://example.com/providers.json', $page
->findField('oembed_providers_url')
->getValue());
$page
->findField('external_fetch')
->uncheck();
$page
->pressButton('Save configuration');
$assert_session
->pageTextContains('The configuration options have been saved.');
$this
->assertSame(FALSE, $this
->config('oembed_providers.settings')
->get('external_fetch'));
$this
->drupalGet('/admin/config/media/oembed-providers');
$assert_session
->checkboxNotChecked('Enable external fetch of providers');
$this
->drupalGet('/admin/config/media/oembed-providers');
$page
->findField('external_fetch')
->check();
$page
->findField('oembed_providers_url')
->setValue('');
$page
->pressButton('Save configuration');
$assert_session
->pageTextContains('The oEmbed Providers URL field is required.');
}
public function testAllowedProviders() {
$this
->useFixtureProviders();
$this
->lockHttpClientToFixtures();
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
\Drupal::service('cache.discovery')
->set('media_source_plugins', 'test value', REQUEST_TIME + 86400);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/config/media/oembed-providers/allowed-providers');
$assert_session
->checkboxNotChecked('Vimeo');
$assert_session
->checkboxNotChecked('YouTube');
$page
->findField('allowed_providers[Vimeo]')
->check();
$page
->pressButton('Save configuration');
$assert_session
->pageTextContains('The configuration options have been saved.');
$config_allowed_providers = \Drupal::config('oembed_providers.settings')
->get('allowed_providers');
$expected_allowed_providers = [
'Vimeo',
];
$this
->assertSame($config_allowed_providers, $expected_allowed_providers);
$this
->AssertFalse(\Drupal::service('cache.discovery')
->get('media_source_plugins'));
$media_sources = \Drupal::service('plugin.manager.media.source')
->getDefinitions();
$providers = $media_sources['oembed:video']['providers'];
$this
->AssertTrue(in_array('Vimeo', $providers));
$this
->AssertFalse(in_array('YouTube', $providers));
}
}