View source
<?php
namespace Drupal\entityblock\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\WebTestBase;
class EntityBlockFieldTest extends WebTestBase {
public static $modules = [
'entity_test',
'entityblock',
'node',
'field_ui',
'block',
];
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'view test entity',
'administer entity_test content',
]));
}
function testEntityBlock() {
$field_name = Unicode::strtolower($this
->randomMachineName());
$title = $this
->randomString();
$this->fieldStorage = FieldStorageConfig::create(array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'entityblock',
'cardinality' => 1,
));
$this->fieldStorage
->save();
$this->field = FieldConfig::create(array(
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'label' => 'Block',
'required' => TRUE,
'settings' => array(
'enabled' => 1,
'title' => $title,
),
));
$this->field
->save();
\Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('entity_test.entity_test.default')
->setComponent($field_name, array(
'type' => 'entityblock_default',
'settings' => array(
'view_modes' => array(
'full' => 'full',
'teaser' => 'teaser',
),
'force_enabled' => FALSE,
'force_title' => FALSE,
),
))
->save();
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("{$field_name}[0][enabled]", '', 'Display as block');
$this
->drupalPostForm(NULL, [], t('Save'));
$this
->assertText('Display as block field is required.');
$this
->assertText('Block title field is required.');
$this
->assertText('Render the block with this view mode field is required.');
$edit = array(
"{$field_name}[0][enabled]" => TRUE,
"{$field_name}[0][title]" => TRUE,
"{$field_name}[0][view_mode]" => 'full',
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText("entity_test 1 has been created.", 'Entity saved');
}
}