View source
<?php
namespace Drupal\Tests\entity_reference_actions\FunctionalJavascript;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\media\Entity\Media;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
class WidgetTest extends WebDriverTestBase {
use EntityReferenceTestTrait;
use MediaTypeCreationTrait;
protected static $modules = [
'entity_test',
'media_library',
'entity_reference_actions',
];
protected $defaultTheme = 'stark';
protected $mediaType;
protected $media;
protected function setUp() : void {
parent::setUp();
$this->mediaType = $this
->createMediaType('image');
$this->media = Media::create([
'bundle' => $this->mediaType
->id(),
'published' => TRUE,
]);
$handler_settings = [
'target_bundles' => [
$this->mediaType
->id() => $this->mediaType
->id(),
],
];
$this
->createEntityReferenceField('entity_test', 'entity_test', 'field_test', 'Test', 'media', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$entity = EntityTest::create();
$entity->field_test = [
$this->media,
];
$entity
->save();
$this
->drupalLogin($this
->createUser([
'administer entity_test content',
'administer media',
]));
}
public function testWidgets($widget) {
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent('field_test', [
'type' => $widget,
'third_party_settings' => [
'entity_reference_actions' => [
'enabled' => TRUE,
],
],
])
->save();
$this
->drupalGet('/entity_test/manage/1/edit');
$this
->assertTrue($this->media
->isPublished());
$this
->getSession()
->getPage()
->find('css', 'li.dropbutton-toggle button')
->click();
$this
->getSession()
->getPage()
->pressButton('Unpublish all media items');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Unpublish all media items was successfully applied');
$this->media = Media::load($this->media
->id());
$this
->assertFalse($this->media
->isPublished());
$this
->getSession()
->getPage()
->pressButton('Save');
$entity = EntityTest::load(1);
$this
->assertNotEmpty($entity->field_test);
}
public function providerTestWidgets() {
return [
[
'entity_reference_autocomplete_tags',
],
[
'entity_reference_autocomplete',
],
[
'options_select',
],
[
'options_buttons',
],
[
'media_library_widget',
],
];
}
public function testConfirmationAction() {
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent('field_test', [
'type' => 'media_library_widget',
'third_party_settings' => [
'entity_reference_actions' => [
'enabled' => TRUE,
],
],
])
->save();
$this
->drupalGet('/entity_test/manage/1/edit');
$this
->getSession()
->getPage()
->pressButton('Delete all media items');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->elementExists('css', '.ui-dialog-buttonpane')
->pressButton('Delete');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Action was successfully applied');
$this
->assertEmpty(Media::load($this->media
->id()));
}
}