View source
<?php
namespace Drupal\entityblock\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field_ui\Tests\FieldUiTestTrait;
use Drupal\simpletest\WebTestBase;
class EntityBlockFieldUITest extends WebTestBase {
use FieldUiTestTrait;
public static $modules = [
'node',
'entityblock',
'field_ui',
'block',
];
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'administer content types',
'administer node fields',
'administer node display',
'administer node form display',
]));
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('page_title_block');
}
function testFieldUI() {
$view_modes = \Drupal::service('entity_display.repository')
->getViewModes('node');
$type1 = $this
->drupalCreateContentType();
$content_type1 = $type1
->id();
$bundle_path1 = 'admin/structure/types/manage/' . $type1
->id();
$field_label = $this
->randomMachineName();
$field_name = Unicode::strtolower($field_label);
$this
->fieldUIAddNewField($bundle_path1, $field_name, $field_label, 'entityblock');
$this
->drupalGet("{$bundle_path1}/form-display");
$this
->assertRaw(t('View modes: %view_modes', array(
'%view_modes' => $view_modes['full']['label'],
)));
$type2 = $this
->drupalCreateContentType();
$content_type2 = $type2
->id();
$bundle_path2 = 'admin/structure/types/manage/' . $content_type2;
$this
->fieldUIAddExistingField($bundle_path2, 'field_' . $field_name, $field_label);
$this
->drupalGet("{$bundle_path2}/form-display");
$this
->assertRaw(t('View modes: %view_modes', array(
'%view_modes' => $view_modes['full']['label'],
)));
\Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('node.' . $content_type2 . '.default')
->setComponent('field_' . $field_name, array(
'type' => 'entityblock_default',
'settings' => array(
'view_modes' => array(
'teaser' => 'teaser',
),
'force_enabled' => TRUE,
'force_title' => TRUE,
),
))
->save();
$this
->drupalGet("{$bundle_path2}/form-display");
$this
->assertRaw(t('View modes: %view_modes', array(
'%view_modes' => $view_modes['teaser']['label'],
)));
$this
->assertText(t('Force "Display as block"'));
$this
->assertText(t('Force "Block title"'));
$this
->fieldUIDeleteField($bundle_path1, 'node.' . $content_type1 . '.field_' . $field_name, $field_label, $type1
->label());
$this
->fieldUIDeleteField($bundle_path2, 'node.' . $content_type2 . '.field_' . $field_name, $field_label, $type2
->label());
$this
->assertNull(FieldConfig::loadByName('node', $type1
->id(), $field_name), 'Field was deleted.');
$this
->assertNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was deleted.');
$this
->assertNull(FieldConfig::loadByName('node', $type2
->id(), $field_name), 'Field was deleted.');
$this
->assertNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was deleted.');
}
}