EditorConfigEntityUnitTest.php in Drupal 9
File
core/modules/editor/tests/src/Unit/EditorConfigEntityUnitTest.php
View source
<?php
namespace Drupal\Tests\editor\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\editor\Entity\Editor;
use Drupal\Tests\UnitTestCase;
class EditorConfigEntityUnitTest extends UnitTestCase {
protected $entityType;
protected $entityTypeManager;
protected $entityTypeId;
protected $uuid;
protected $editorPluginManager;
protected $editorId;
protected function setUp() : void {
$this->editorId = $this
->randomMachineName();
$this->entityTypeId = $this
->randomMachineName();
$this->entityType = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$this->entityType
->expects($this
->any())
->method('getProvider')
->will($this
->returnValue('editor'));
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->entityTypeManager
->expects($this
->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this
->returnValue($this->entityType));
$this->uuid = $this
->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
$this->editorPluginManager = $this
->getMockBuilder('Drupal\\editor\\Plugin\\EditorManager')
->disableOriginalConstructor()
->getMock();
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $this->entityTypeManager);
$container
->set('uuid', $this->uuid);
$container
->set('plugin.manager.editor', $this->editorPluginManager);
\Drupal::setContainer($container);
}
public function testCalculateDependencies() {
$format_id = 'filter.format.test';
$values = [
'editor' => $this->editorId,
'format' => $format_id,
];
$plugin = $this
->getMockBuilder('Drupal\\editor\\Plugin\\EditorPluginInterface')
->disableOriginalConstructor()
->getMock();
$plugin
->expects($this
->once())
->method('getPluginDefinition')
->will($this
->returnValue([
'provider' => 'test_module',
]));
$plugin
->expects($this
->once())
->method('getDefaultSettings')
->will($this
->returnValue([]));
$this->editorPluginManager
->expects($this
->any())
->method('createInstance')
->with($this->editorId)
->will($this
->returnValue($plugin));
$entity = new Editor($values, $this->entityTypeId);
$filter_format = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
$filter_format
->expects($this
->once())
->method('getConfigDependencyName')
->will($this
->returnValue('filter.format.test'));
$storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$storage
->expects($this
->once())
->method('load')
->with($format_id)
->will($this
->returnValue($filter_format));
$this->entityTypeManager
->expects($this
->once())
->method('getStorage')
->with('filter_format')
->will($this
->returnValue($storage));
$dependencies = $entity
->calculateDependencies()
->getDependencies();
$this
->assertContains('test_module', $dependencies['module']);
$this
->assertContains('filter.format.test', $dependencies['config']);
}
}