SettingsFormTest.php in oEmbed Providers 2.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;
protected static $modules = [
'block',
'media',
'oembed_providers',
];
protected function setUp() : void {
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);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/config/media/oembed-providers');
$assert_session
->statusCodeEquals(200);
}
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.');
}
}