View source
<?php
namespace Drupal\Tests\lightning_scheduler\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\lightning_scheduler\Traits\SchedulerUiTrait;
use Drupal\workflows\Entity\Workflow;
class InlineEntityFormTest extends BrowserTestBase {
use SchedulerUiTrait;
protected $defaultTheme = 'stark';
protected static $modules = [
'inline_entity_form',
'lightning_scheduler',
'lightning_workflow',
];
protected function setUp() {
parent::setUp();
$this
->createContentType([
'type' => 'alpha',
]);
$this
->createContentType([
'type' => 'beta',
]);
$field_storage = FieldStorageConfig::create([
'type' => 'entity_reference',
'entity_type' => 'user',
'settings' => [
'target_type' => 'node',
],
'field_name' => 'field_inline_entity',
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'user',
'settings' => [
'handler_settings' => [
'target_bundles' => [
'alpha' => 'alpha',
],
],
],
'label' => 'Inline entity',
])
->save();
lightning_workflow_entity_get_form_display('user', 'user', 'default')
->setComponent('field_inline_entity', [
'type' => 'inline_entity_form_simple',
])
->save();
$field_storage = FieldStorageConfig::create([
'type' => 'entity_reference',
'entity_type' => 'node',
'settings' => [
'target_type' => 'node',
],
'field_name' => 'field_inline_entity',
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'alpha',
'settings' => [
'handler_settings' => [
'target_bundles' => [
'beta' => 'beta',
],
],
],
'label' => 'Inline entity',
])
->save();
lightning_workflow_entity_get_form_display('node', 'alpha', 'default')
->setComponent('field_inline_entity', [
'type' => 'inline_entity_form_simple',
])
->save();
$workflow = Workflow::load('editorial');
$plugin = $workflow
->getTypePlugin();
$plugin
->addEntityTypeAndBundle('node', 'alpha');
$plugin
->addEntityTypeAndBundle('node', 'beta');
$workflow
->save();
$this
->drupalLogin($this->rootUser);
}
private function assertInlineEntityForm() {
return $this
->assertSession()
->elementExists('css', '#edit-field-inline-entity-wrapper');
}
public function testHostEntityWithoutModeration() {
$assert_session = $this
->assertSession();
$this
->drupalGet('/user/' . $this->rootUser
->id() . '/edit');
$assert_session
->statusCodeEquals(200);
$inline_entity_form = $this
->assertInlineEntityForm();
$inline_entity_form
->fillField('Title', 'Kaboom?');
$assert_session
->selectExists('field_inline_entity[0][inline_entity_form][moderation_state][0][state]', $inline_entity_form);
$this
->getSession()
->getPage()
->pressButton('Save');
$assert_session
->statusCodeEquals(200);
}
public function testHostEntityWithModeration() {
$page = $this
->getSession()
->getPage();
$this
->drupalGet('node/add/alpha');
$page
->fillField('Title', 'Foobar');
$this
->assertInlineEntityForm()
->fillField('Title', 'Foobar');
$host_field = 'moderation_state[0][scheduled_transitions][data]';
$inline_field = 'field_inline_entity[0][inline_entity_form][moderation_state][0][scheduled_transitions][data]';
$transition_1 = [
[
'state' => 'published',
'when' => time() + 100,
],
];
$transition_2 = [
[
'state' => 'published',
'when' => time() + 200,
],
];
$this
->setTransitionData($host_field, $transition_1);
$this
->setTransitionData($inline_field, $transition_2);
$page
->pressButton('Save');
$storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$alpha = $storage
->loadByProperties([
'type' => 'alpha',
]);
$beta = $storage
->loadByProperties([
'type' => 'beta',
]);
$this
->assertCount(1, $alpha);
$this
->assertCount(1, $beta);
$this
->drupalGet(reset($alpha)
->toUrl('edit-form'));
$this
->assertTransitionData($host_field, $transition_1);
$this
->drupalGet(reset($beta)
->toUrl('edit-form'));
$this
->assertTransitionData($host_field, $transition_2);
}
}
function lightning_workflow_entity_get_form_display($entity_type, $bundle, $form_mode = 'default') {
$entity_form_display = EntityFormDisplay::load($entity_type . '.' . $bundle . '.' . $form_mode);
if (!$entity_form_display) {
$entity_form_display = EntityFormDisplay::create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $form_mode,
'status' => TRUE,
]);
}
return $entity_form_display;
}