View source
<?php
namespace Drupal\Tests\entity_browser\FunctionalJavascript;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageException;
class PluginsTest extends EntityBrowserWebDriverTestBase {
public function testIframeDisplayPlugin() {
$browser = $this
->getEntityBrowser('test_entity_browser_file', 'iframe', 'single', 'no_display');
$image = $this
->createFile('lama');
$this
->drupalGet('node/add/article');
$this
->assertSession()
->linkExists('Select entities');
$this
->getSession()
->getPage()
->clickLink('Select entities');
$this
->getSession()
->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
$this
->getSession()
->getPage()
->checkField('entity_browser_select[file:' . $image
->id() . ']');
$this
->getSession()
->getPage()
->pressButton('Select entities');
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->waitForElement('css', '.field--type-entity-reference .button');
$this
->assertSession()
->pageTextContains('lama.jpg');
$upload_widget = $browser
->getWidget('2dc1ab07-2f8f-42c9-aab7-7eef7f8b7d87');
$upload_widget
->setWeight(0);
$browser
->save();
$this
->drupalGet('node/add/article');
$this
->assertSession()
->linkExists('Select entities');
$this
->getSession()
->getPage()
->clickLink('Select entities');
$this
->getSession()
->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
$this
->getSession()
->getPage()
->checkField('entity_browser_select[file:' . $image
->id() . ']');
$this
->getSession()
->getPage()
->pressButton('Select entities');
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->waitForElement('css', '.field--type-entity-reference .button');
$this
->assertSession()
->pageTextContains('lama.jpg');
$this
->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'no_display');
$this
->drupalGet('node/add/article');
$this
->assertSession()
->linkExists('Select entities');
$this
->getSession()
->getPage()
->clickLink('Select entities');
$this
->getSession()
->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
$this
->assertSession()
->linkExists('view');
$this
->assertSession()
->linkExists('upload');
$this
->clickLink('view');
$this
->getSession()
->getPage()
->checkField('entity_browser_select[file:' . $image
->id() . ']');
$this
->getSession()
->getPage()
->pressButton('Select entities');
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->waitForElement('css', '.field--type-entity-reference .button');
$this
->assertSession()
->pageTextContains('lama.jpg');
$this
->drupalGet('node/add/article');
$this
->getSession()
->getPage()
->clickLink('Select entities');
$this
->getSession()
->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
$this
->clickLink('upload');
$this
->getSession()
->getPage()
->attachFileToField('edit-upload-upload', $this->container
->get('file_system')
->realpath($image
->getFileUri()));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$image2 = $this
->getLastUploadedFile();
$this
->assertEquals('lama_0.jpg', $image2
->label());
$this
->getSession()
->getPage()
->checkField('upload[file_' . $image2
->id() . '][selected]');
$this
->getSession()
->getPage()
->pressButton('Select files');
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('lama_0.jpg');
$this
->getEntityBrowser('test_entity_browser_file', 'iframe', 'drop_down', 'no_display');
$view = $this->container
->get('entity_type.manager')
->getStorage('view')
->load('files_entity_browser');
$display =& $view
->getDisplay('default');
$display['display_options']['filters'] = [];
$view
->save();
$this
->drupalGet('node/add/article');
$this
->getSession()
->getPage()
->clickLink('Select entities');
$this
->getSession()
->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
$this
->assertSession()
->selectExists('edit-widget');
$this
->getSession()
->getPage()
->checkField('entity_browser_select[file:' . $image
->id() . ']');
$this
->getSession()
->getPage()
->pressButton('Select entities');
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->waitForElement('css', '.field--type-entity-reference .button');
$this
->assertSession()
->pageTextContains('lama.jpg');
$this
->drupalGet('node/add/article');
$this
->getSession()
->getPage()
->clickLink('Select entities');
$this
->getSession()
->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
$this
->getSession()
->getPage()
->selectFieldOption('edit-widget', '2dc1ab07-2f8f-42c9-aab7-7eef7f8b7d87');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->getSession()
->getPage()
->attachFileToField('files[upload][]', $this->container
->get('file_system')
->realpath($image
->getFileUri()));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$image3 = $this
->getLastUploadedFile();
$this
->getSession()
->getPage()
->checkField('upload[file_' . $image3
->id() . '][selected]');
$this
->getSession()
->getPage()
->pressButton('Select files');
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains($image3
->label());
$view_configuration = [
'view' => 'test_selection_display_view',
'view_display' => 'entity_browser_1',
];
$browser = $this
->getEntityBrowser('test_entity_browser_file', 'iframe', 'single', 'view', [], [], $view_configuration);
$this
->drupalGet('node/add/article');
$this
->getSession()
->getPage()
->clickLink('Select entities');
$this
->getSession()
->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
$dragon_image = $this
->createFile('dragon');
$unicorn_image = $this
->createFile('unicorn');
$upload_widget = $browser
->getWidget('2dc1ab07-2f8f-42c9-aab7-7eef7f8b7d87');
$upload_widget
->setWeight(-9);
$browser
->save();
$multistep_configuration = [
'entity_type' => 'file',
'display' => 'label',
'display_settings' => [],
'select_text' => 'Use selected',
'selection_hidden' => 0,
];
$browser = $this
->getEntityBrowser('test_entity_browser_file', 'iframe', 'tabs', 'multi_step_display', [], [], $multistep_configuration);
$upload_widget = $browser
->getWidget('774798f1-5ec5-4b63-84bd-124cd51ec07d');
$upload_widget
->setWeight(0);
$browser
->save();
$this
->drupalGet('node/add/article');
$this
->getSession()
->getPage()
->clickLink('Select entities');
$this
->getSession()
->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
$this
->getSession()
->getPage()
->attachFileToField('files[upload][]', $this->container
->get('file_system')
->realpath($dragon_image
->getFileUri()));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->getSession()
->getPage()
->pressButton('Select files');
$image4 = $this
->getLastUploadedFile();
$this
->assertEquals('dragon_0.jpg', $image4
->label());
$this
->assertSession()
->pageTextContains('dragon_0.jpg');
$this
->assertSession()
->pageTextNotContains('unicorn.jpg');
$this
->getSession()
->getPage()
->clickLink('view');
$this
->assertSession()
->waitForField('entity_browser_select[file:' . $unicorn_image
->id() . ']')
->check();
$this
->getSession()
->getPage()
->pressButton('Select entities');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->waitForElement('css', '#edit-selected-items-2-1-remove-button');
$this
->assertSession()
->waitForElement('css', '#edit-selected-items-1-0-remove-button');
$this
->getSession()
->getPage()
->pressButton('Use selected');
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('dragon_0.jpg');
$this
->assertSession()
->pageTextContains('unicorn.jpg');
}
public function testModalDisplay() {
$modal_display_config = [
'width' => '650',
'height' => '500',
'link_text' => 'Select entities',
];
$this
->getEntityBrowser('test_entity_browser_file', 'modal', 'single', 'no_display', $modal_display_config);
$image = $this
->createFile('lama');
$this
->drupalGet('node/add/article');
$this
->assertSession()
->buttonExists('Select entities');
$this
->getSession()
->getPage()
->pressButton('Select entities');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->getSession()
->switchToIFrame('entity_browser_iframe_test_entity_browser_file');
$this
->getSession()
->getPage()
->checkField('entity_browser_select[file:' . $image
->id() . ']');
$this
->getSession()
->getPage()
->pressButton('Select entities');
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('lama');
}
public function testStandaloneDisplay() {
$image = $this
->createFile('lama');
$standalone_configuration = [
'entity_browser_id' => 'test_entity_browser_file',
'path' => 'test',
];
$this
->getEntityBrowser('test_entity_browser_file', 'standalone', 'single', 'no_display', $standalone_configuration);
$this
->drupalGet('test');
$this
->assertSession()
->buttonExists('Select entities');
$this
->getSession()
->getPage()
->pressButton('Select entities');
$this
->getSession()
->getPage()
->checkField('entity_browser_select[file:' . $image
->id() . ']');
$this
->getSession()
->getPage()
->pressButton('Select entities');
}
protected function getLastUploadedFile() {
$entity_type_manager = \Drupal::service('entity_type.manager');
$results = $entity_type_manager
->getStorage('file')
->getQuery()
->range(0, 1)
->sort('fid', 'DESC')
->execute();
if (!empty($results)) {
return $entity_type_manager
->getStorage('file')
->load(reset($results));
}
else {
throw new SqlContentEntityStorageException('File not found');
}
}
}