View source
<?php
namespace Drupal\Tests\form_mode_manager\Functional;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Tests\BrowserTestBase;
abstract class FormModeManagerBase extends BrowserTestBase {
use DisplayFormModeTestTrait;
protected static $modules = [
'entity_test',
'field',
'field_ui',
'media',
'block',
'block_content',
'node',
'user',
'form_mode_manager',
'taxonomy',
];
protected $defaultTheme = 'stark';
public static $uiLocalTabsExpected = [
'Settings',
'Local task settings',
];
protected $anonymousUser;
protected $adminUser;
protected $testUser;
protected $nodeTypeFmm1;
protected $nodeFormMode;
protected $userFormMode;
protected $formModeManager;
protected $strictConfigSchema = FALSE;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
$this->nodeTypeFmm1 = $this
->drupalCreateContentType([
'type' => 'fmm_test',
'name' => 'Form Mode Manager Test 1',
]);
$this->nodeFormMode = $this
->drupalCreateFormMode('node');
$this->userFormMode = $this
->drupalCreateFormMode('user');
$this->container
->get('router.builder')
->rebuildIfNeeded();
$this
->drupalLogin($this->rootUser);
$this
->setUpFormMode("admin/structure/types/manage/{$this->nodeTypeFmm1->id()}/form-display", $this->nodeFormMode
->id());
$this
->setUpFormMode("admin/config/people/accounts/form-display", $this->userFormMode
->id());
$this
->setUpUsers();
}
public function setUpUsers() {
$this->anonymousUser = $this
->drupalCreateUser([
'access content',
]);
$this->adminUser = $this
->drupalCreateUser([
'access content',
'access administration pages',
'administer site configuration',
'administer users',
'administer permissions',
'administer content types',
'administer node fields',
'administer node display',
'administer node form display',
'administer nodes',
'administer display modes',
'use node.default form mode',
'use user.default form mode',
'use ' . $this->nodeFormMode
->id() . ' form mode',
'use ' . $this->userFormMode
->id() . ' form mode',
'edit any ' . $this->nodeTypeFmm1
->id() . ' content',
'create ' . $this->nodeTypeFmm1
->id() . ' content',
]);
$this->testUser = $this
->drupalCreateUser([
'access content',
]);
}
public function setUpFormMode($path, $form_mode_id) {
$this
->drupalGet($path);
$this->formModeManager = $this->container
->get('form_mode.manager');
$edit = [
"display_modes_custom[{$this->formModeManager->getFormModeMachineName($form_mode_id)}]" => TRUE,
];
$this
->submitForm($edit, t('Save'));
}
public function setHiddenFieldFormMode($path, $field_name) {
$this
->drupalGet($path);
$edit = [
"fields[{$field_name}][region]" => 'hidden',
];
$this
->submitForm($edit, t('Save'));
}
public function assertLocalTasks($tabs_expected) {
foreach ($tabs_expected as $link) {
$this
->assertSession()
->linkExists($link);
}
}
protected function createBlockContentType($create_body = FALSE) {
$bundle = BlockContentType::create([
'id' => mb_strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'revision' => FALSE,
]);
$bundle
->save();
if ($create_body) {
block_content_add_body_field($bundle
->id());
}
return $bundle;
}
protected function createBlockContent(array $settings = []) {
$settings += [
'body' => [
[
'value' => $this
->randomMachineName(32),
'format' => filter_default_format(),
],
],
'info' => $this
->randomMachineName(),
'type' => 'basic',
'langcode' => 'en',
];
$block_content = BlockContent::create($settings);
$block_content
->save();
return $block_content;
}
}