View source
<?php
namespace Drupal\Tests\profile\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\profile\Entity\ProfileType;
use Drupal\profile\ProfileTestTrait;
use Drupal\Tests\BrowserTestBase;
abstract class ProfileTestBase extends BrowserTestBase {
use ProfileTestTrait;
public static $modules = [
'profile',
'field_ui',
'text',
'block',
];
protected $type;
protected $display;
protected $form;
protected $field;
protected $adminUser;
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('page_title_block');
$user_form_display = EntityFormDisplay::load("user.user.default");
if (!$user_form_display) {
$user_form_display = EntityFormDisplay::create([
'targetEntityType' => 'user',
'bundle' => 'user',
'mode' => 'default',
'status' => TRUE,
]);
$user_form_display
->save();
}
$this->type = $this
->createProfileType('test', 'Test profile', TRUE);
$id = $this->type
->id();
$field_storage = FieldStorageConfig::create([
'field_name' => 'profile_fullname',
'entity_type' => 'profile',
'type' => 'text',
]);
$field_storage
->save();
$this->field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $this->type
->id(),
'label' => 'Full name',
]);
$this->field
->save();
$user_display = EntityViewDisplay::load("user.user.default");
if (!$user_display) {
$user_display = EntityViewDisplay::create([
'targetEntityType' => 'user',
'bundle' => 'user',
'mode' => 'default',
'status' => TRUE,
]);
$user_display
->save();
}
$this->display = EntityViewDisplay::load("profile.{$this->type->id()}.default");
if (!$this->display) {
$this->display = EntityViewDisplay::create([
'targetEntityType' => 'profile',
'bundle' => $this->type
->id(),
'mode' => 'default',
'status' => TRUE,
]);
$this->display
->save();
}
$this->display
->setComponent($this->field
->getName(), [
'type' => 'string',
])
->save();
$profile_types = ProfileType::loadMultiple();
foreach ($profile_types as $profile_type) {
$field_name = $profile_type
->id() . '_profiles';
$user_display
->setComponent($field_name, [
'label' => 'above',
'type' => 'entity_reference_entity_view',
'settings' => [
'view_mode' => $this->display
->id(),
],
])
->save();
}
$this->form = EntityFormDisplay::load("profile.{$this->type->id()}.default");
if (!$this->form) {
$this->form = EntityFormDisplay::create([
'targetEntityType' => 'profile',
'bundle' => $this->type
->id(),
'mode' => 'default',
'status' => TRUE,
]);
$this->form
->save();
}
$this->form
->setComponent($this->field
->getName(), [
'type' => 'string_textfield',
])
->save();
$this
->checkPermissions([
'administer profile types',
"create {$id} profile",
"view own {$id} profile",
"view any {$id} profile",
"update own {$id} profile",
"update any {$id} profile",
"delete own {$id} profile",
"delete any {$id} profile",
]);
$this->adminUser = $this
->drupalCreateUser([
'administer profile types',
'administer profile',
"view any {$id} profile",
"create {$id} profile",
"update any {$id} profile",
"delete any {$id} profile",
]);
}
}