View source
<?php
namespace Drupal\Tests\field_example\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\examples\Functional\ExamplesBrowserTestBase;
abstract class FieldExampleBrowserTestBase extends ExamplesBrowserTestBase {
protected $defaultTheme = 'classy';
protected $contentTypeName;
protected $administratorAccount;
protected $authorAccount;
protected $fieldName;
public static $modules = [
'block',
'node',
'field_ui',
'field_example',
];
protected function setUp() {
parent::setUp();
$permissions = [
'administer content types',
'administer node fields',
'administer node form display',
'administer node display',
];
$this->administratorAccount = $this
->drupalCreateUser($permissions);
parent::drupalLogin($this->administratorAccount);
$this->contentTypeName = strtolower($this
->randomMachineName(10));
$this
->drupalGet('admin/structure/types/add');
$edit = [
'name' => $this->contentTypeName,
'type' => $this->contentTypeName,
];
$this
->drupalPostForm(NULL, $edit, 'Save and manage fields');
$this
->assertText((string) new FormattableMarkup('The content type @name has been added.', [
'@name' => $this->contentTypeName,
]));
$create_permission = 'create ' . $this->contentTypeName . ' content';
$this
->checkPermissions([
$create_permission,
]);
$this->authorAccount = $this
->drupalCreateUser([
$create_permission,
]);
}
protected function createField($type = 'field_example_rgb', $widget_type = 'field_example_text', $cardinality = '1', $fieldFormatter = 'field_example_simple_text') {
$assert = $this
->assertSession();
$this
->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/fields');
$this
->clickLink('Add field');
$field_name = strtolower($this
->randomMachineName(10));
$edit = [
'new_storage_type' => $type,
'field_name' => $field_name,
'label' => $field_name,
];
$this
->drupalPostForm(NULL, $edit, 'Save and continue');
$edit = [
'cardinality' => 'number',
'cardinality_number' => (string) $cardinality,
];
if (-1 == $cardinality) {
$edit = [
'cardinality' => '-1',
'cardinality_number' => '1',
];
}
$this
->drupalPostForm(NULL, $edit, 'Save field settings');
$this
->verbose((string) new FormattableMarkup('Saved settings for field %field_name with widget %widget_type and cardinality %cardinality', [
'%field_name' => $field_name,
'%widget_type' => $widget_type,
'%cardinality' => $cardinality,
]));
$assert
->pageTextContains((string) new FormattableMarkup('Updated field @name field settings.', [
'@name' => $field_name,
]));
$this
->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/form-display');
$edit = [
'fields[field_' . $field_name . '][type]' => $widget_type,
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/display');
$edit1 = [
'fields[field_' . $field_name . '][type]' => $fieldFormatter,
];
$this
->drupalPostForm(NULL, $edit1, 'Save');
return $field_name;
}
}