View source
<?php
namespace Drupal\Tests\depcalc\Kernel\EventSubscriber\DependencyCollector;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\depcalc\DependencyStack;
use Drupal\depcalc\DependentEntityWrapper;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class EntityFormDisplayDependencyCollectorTest extends KernelTestBase {
use NodeCreationTrait;
use ContentTypeCreationTrait;
public static $modules = [
'depcalc',
'field',
'filter',
'node',
'path_alias',
'system',
'text',
'user',
];
private $calculator;
protected function setUp() {
parent::setUp();
$this
->installConfig('node');
$this
->installConfig('field');
$this
->installConfig('filter');
$this
->installSchema('node', 'node_access');
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('path_alias');
$this->calculator = \Drupal::service('entity.dependency.calculator');
}
public function testEntityFormDisplayDependencyCollection() {
$bundle = 'depcalc_dummy_content_type';
$entityType = 'node';
$this
->createContentType([
'type' => $bundle,
'name' => 'Depcalc. Dummy content type',
])
->save();
$enabledFormDisplay = $this
->createFormDisplay($entityType, $bundle, 'depcalc_dummy_form_mode', TRUE);
$disabledFormDisplay = $this
->createFormDisplay($entityType, $bundle, 'depcalc_dummy_form_mode_disabled', FALSE);
$node = $this
->createNode([
'type' => 'depcalc_dummy_content_type',
]);
$node
->save();
$dependencies = $this->calculator
->calculateDependencies(new DependentEntityWrapper($node), new DependencyStack());
$this
->assertNotEmpty($dependencies);
$this
->assertArrayHasKey($enabledFormDisplay
->uuid(), $dependencies);
$this
->assertArrayNotHasKey($disabledFormDisplay
->uuid(), $dependencies);
$formDisplayEntityWrapper = $dependencies[$enabledFormDisplay
->uuid()];
$this
->assertEqual($formDisplayEntityWrapper
->getUuid(), $enabledFormDisplay
->uuid());
$this
->assertEqual($enabledFormDisplay
->getEntityTypeId(), 'entity_form_display');
}
private function createFormDisplay(string $targetEntityType, string $bundle, string $mode, bool $status) {
EntityFormMode::create([
'id' => sprintf('%s.%s', $targetEntityType, $mode),
'targetEntityType' => $targetEntityType,
'bundle' => $bundle,
'mode' => $mode,
])
->save();
$formDisplay = EntityFormDisplay::create([
'targetEntityType' => $targetEntityType,
'bundle' => $bundle,
'mode' => $mode,
'status' => $status,
]);
$formDisplay
->save();
return $formDisplay;
}
}