View source
<?php
namespace Drupal\Tests\geshifield\Functional;
use Drupal\geshifilter\GeshiFilter;
use Drupal\Tests\BrowserTestBase;
class GeshiFieldTest extends BrowserTestBase {
protected $adminUser;
protected $config;
public static $modules = [
'node',
'geshifilter',
'filter',
'geshifield',
'field_ui',
];
public function setUp() {
parent::setUp();
$this->config = \Drupal::configFactory()
->getEditable('geshifilter.settings');
$this->config
->set('geshi_dir', '/libraries/geshi');
$settings = [
'type' => 'geshifilter_content_type',
'name' => 'Geshifilter Content',
];
$this
->drupalCreateContentType($settings);
$this->adminUser = $this
->drupalCreateUser([], NULL, TRUE);
$this
->drupalLogin($this->adminUser);
$this
->createTextFormat('geshifilter_text_format', [
'filter_geshifilter',
]);
$this->config
->set('default_highlighting', GeshiFilter::DEFAULT_PLAINTEXT);
$this->config
->set('use_format_specific_options', FALSE);
$this->config
->set('tag_styles', [
GeshiFilter::BRACKETS_ANGLE => GeshiFilter::BRACKETS_ANGLE,
GeshiFilter::BRACKETS_SQUARE => GeshiFilter::BRACKETS_SQUARE,
]);
$this->config
->set('default_line_numbering', GeshiFilter::LINE_NUMBERS_DEFAULT_NONE);
$this->config
->save();
}
protected function createTextFormat($format_name, array $filters) {
$edit = [];
$edit['format'] = $format_name;
$edit['name'] = $this
->randomMachineName();
$edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = 1;
foreach ($filters as $filter) {
$edit['filters[' . $filter . '][status]'] = TRUE;
}
$this
->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
$this
->assertRaw(t('Added text format %format.', [
'%format' => $edit['name'],
]), 'New filter created.');
$this
->drupalGet('admin/config/content/formats');
}
public function testAddField() {
$this
->addField('geshifield', 'geshi', 'GeshiFilter Field');
$this
->addNode('title', 'body', '<?php echo("hi"); ?>', 'php');
}
protected function addNode($title, $body, $sourcecode, $language) {
$node = [
'title[0][value]' => $title,
'body[0][value]' => $body,
'field_geshi[0][sourcecode]' => $sourcecode,
'field_geshi[0][language]' => $language,
];
$this
->drupalPostForm('node/add/geshifilter_content_type', $node, 'Save');
$this
->drupalGet('node/1');
}
private function addField($type, $name, $label, array $values = [], array $instance = []) {
$edit = [];
$edit['new_storage_type'] = $type;
$edit['label'] = $label;
$edit['field_name'] = $name;
$this
->drupalPostForm('admin/structure/types/manage/geshifilter_content_type/fields/add-field', $edit, 'Save and continue');
$this
->drupalPostForm(NULL, $values, 'Save field settings');
$this
->drupalPostForm(NULL, $instance, 'Save settings');
}
}