View source
<?php
namespace Drupal\Tests\sophron\Functional;
use Drupal\sophron\MimeMapManagerInterface;
use Drupal\Tests\BrowserTestBase;
class SophronTest extends BrowserTestBase {
protected static $modules = [
'sophron',
];
protected $defaultTheme = 'stark';
public function setUp() : void {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'administer site configuration',
]));
}
public function testFormAndSettings() : void {
$this
->assertSame(MimeMapManagerInterface::DRUPAL_MAP, \Drupal::configFactory()
->get('sophron.settings')
->get('map_option'));
$this
->assertSame('', \Drupal::configFactory()
->get('sophron.settings')
->get('map_class'));
$this
->drupalGet('admin/config/system/sophron');
$edit = [
'map_option' => MimeMapManagerInterface::DEFAULT_MAP,
];
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$this
->assertSession()
->responseContains('Mapping gaps');
$this
->assertSame(MimeMapManagerInterface::DEFAULT_MAP, \Drupal::configFactory()
->get('sophron.settings')
->get('map_option'));
$this
->assertSame('', \Drupal::configFactory()
->get('sophron.settings')
->get('map_class'));
$edit = [
'map_option' => MimeMapManagerInterface::CUSTOM_MAP,
'map_class' => BrowserTestBase::class,
];
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$this
->assertSession()
->responseContains('The map class is invalid.');
$edit = [
'map_option' => MimeMapManagerInterface::DEFAULT_MAP,
];
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$this
->assertEquals('application/octet-stream', \Drupal::service('sophron.mime_map.manager')
->getExtension('quxqux')
->getDefaultType(FALSE));
$this
->assertSession()
->fieldExists('map_commands');
$edit = [
'map_commands' => '- [addTypeExtensionMapping, [foo/bar, quxqux]]',
];
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$edit = [
'map_commands' => "- aaa\n- [addTypeExtensionMapping, [a/c, bbbb]]\n- [bbb, ccc]\n",
];
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$this
->assertSession()
->responseContains('The items at line(s) 1, 3 are wrongly typed.');
$edit = [
'map_commands' => "- aaa\nbbb\n",
];
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$this
->assertSession()
->responseContains('YAML syntax error');
$edit = [
'map_commands' => '- [aaa, [paramA, paramB]]',
];
$this
->drupalPostForm(NULL, $edit, 'Save configuration');
$this
->assertSession()
->responseContains('Mapping errors');
$this
->assertEquals([
[
'aaa',
[
'paramA',
'paramB',
],
],
], \Drupal::configFactory()
->get('sophron.settings')
->get('map_commands'));
}
}