View source
<?php
namespace Drupal\Tests\lightning_media\Functional;
use Drupal\editor\Entity\Editor;
use Drupal\entity_browser\Element\EntityBrowserElement;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\lightning_media\Update\Update400;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\Console\Style\StyleInterface;
class Update400Test extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'ckeditor',
'lightning_media',
'user',
];
public function test() {
$this
->createContentType([
'type' => 'article',
'name' => 'Article',
]);
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_migrate',
'entity_type' => 'node',
'type' => 'entity_reference',
'settings' => [
'target_type' => 'media',
],
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'label' => 'Field to migrate',
])
->save();
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_ignore',
'entity_type' => 'node',
'type' => 'entity_reference',
'settings' => [
'target_type' => 'media',
],
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'label' => 'Field to ignore',
])
->save();
$this->container
->get('entity_display.repository')
->getFormDisplay('node', 'article')
->setComponent('field_migrate', [
'type' => 'entity_browser_entity_reference',
'settings' => [
'entity_browser' => 'media_browser',
'field_widget_display' => 'rendered_entity',
'field_widget_edit' => TRUE,
'field_widget_remove' => TRUE,
'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
'field_widget_display_settings' => [
'view_mode' => 'thumbnail',
],
'open' => TRUE,
],
'region' => 'content',
])
->setComponent('field_ignore', [
'type' => 'entity_reference_autocomplete',
'region' => 'content',
])
->save();
$editor = Editor::load('rich_text');
$settings = $editor
->getSettings();
$settings['toolbar']['rows'][0][3]['items'][3] = 'media_browser';
$editor
->setSettings($settings)
->save();
$format = $editor
->getFilterFormat();
$format
->setFilterConfig('filter_html', [
'settings' => [
'allowed_html' => '<a href hreflang>',
],
'status' => TRUE,
]);
$format
->filters();
$format
->removeFilter('media_embed');
$format
->save();
$io = $this
->prophesize(StyleInterface::class);
$io
->confirm('Do you want to convert the Field to migrate field of the Article content type to use the media library in the default form mode?')
->willReturn(TRUE)
->shouldBeCalled();
$io
->confirm('Do you want to convert the Field to ignore field of the Article content type to use the media library in the default form mode?')
->shouldNotBeCalled();
$io
->confirm('Do you want to convert the Rich text WYSIWYG editor to use the media library?')
->willReturn(TRUE)
->shouldBeCalled();
$io = $io
->reveal();
$updater = Update400::create($this->container);
$updater
->convertMediaReferenceFieldsToMediaLibrary($io);
$updater
->convertEditorsToMediaLibrary($io);
$components = $this->container
->get('entity_display.repository')
->getFormDisplay('node', 'article')
->getComponents();
$this
->assertSame('media_library_widget', $components['field_migrate']['type']);
$this
->assertSame('entity_reference_autocomplete', $components['field_ignore']['type']);
$editor = Editor::load('rich_text');
$settings = $editor
->getSettings();
$this
->assertSame('DrupalMediaLibrary', $settings['toolbar']['rows'][0][3]['items'][3]);
$filters = $editor
->getFilterFormat()
->filters();
$configuration = $filters
->get('media_embed')
->getConfiguration();
$this
->assertTrue($configuration['status']);
$this
->assertSame('embedded', $configuration['settings']['default_view_mode']);
$configuration = $filters
->get('filter_html')
->getConfiguration();
$this
->assertTrue($configuration['status']);
$this
->assertStringContainsString('<drupal-media data-entity-type data-entity-uuid data-view-mode data-align data-caption alt>', $configuration['settings']['allowed_html']);
}
}