You are here

public function ImageFieldTest::setUp in Entity Browser 8.2

Same name and namespace in other branches
  1. 8 tests/src/FunctionalJavascript/ImageFieldTest.php \Drupal\Tests\entity_browser\FunctionalJavascript\ImageFieldTest::setUp()

Overrides EntityBrowserWebDriverTestBase::setUp

File

tests/src/FunctionalJavascript/ImageFieldTest.php, line 32

Class

ImageFieldTest
Tests the image field widget.

Namespace

Drupal\Tests\entity_browser\FunctionalJavascript

Code

public function setUp() {
  parent::setUp();
  FieldStorageConfig::create([
    'field_name' => 'field_image',
    'type' => 'image',
    'entity_type' => 'node',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'field_image',
    'entity_type' => 'node',
    'bundle' => 'article',
    'label' => 'Images',
    'settings' => [
      'file_extensions' => 'jpg',
      'file_directory' => 'entity-browser-test',
      'max_resolution' => '40x40',
      'title_field' => TRUE,
    ],
  ])
    ->save();
  $test_files = $this
    ->getTestFiles('image');
  foreach ($test_files as $test_file) {
    if ($test_file->filename === 'image-test.jpg') {
      break;
    }
  }
  $file_system = $this->container
    ->get('file_system');
  $file_system
    ->copy($file_system
    ->realpath($test_file->uri), 'public://example.jpg');
  $this->image = File::create([
    'uri' => 'public://example.jpg',
  ]);
  $this->image
    ->save();

  // Register usage for this file to avoid validation erros when referencing
  // this file on node save.
  \Drupal::service('file.usage')
    ->add($this->image, 'entity_browser', 'test', '1');

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
  $form_display = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_form_display')
    ->load('node.article.default');
  $form_display
    ->setComponent('field_image', [
    'type' => 'entity_browser_file',
    'settings' => [
      'entity_browser' => 'test_entity_browser_iframe_view',
      'open' => TRUE,
      'field_widget_edit' => FALSE,
      'field_widget_remove' => TRUE,
      'field_widget_replace' => TRUE,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
      'view_mode' => 'default',
      'preview_image_style' => 'thumbnail',
    ],
  ])
    ->save();
  $display_config = [
    'width' => '650',
    'height' => '500',
    'link_text' => 'Select images',
  ];

  /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
  $browser = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_browser')
    ->load('test_entity_browser_iframe_view');
  $browser
    ->setDisplay('iframe');
  $browser
    ->getDisplay()
    ->setConfiguration($display_config);
  $browser
    ->addWidget([
    // These settings should get overridden by our field settings.
    'settings' => [
      'upload_location' => 'public://',
      'extensions' => 'png',
    ],
    'weight' => 1,
    'label' => 'Upload images',
    'id' => 'upload',
  ]);
  $browser
    ->setWidgetSelector('tabs');
  $browser
    ->save();
  $account = $this
    ->drupalCreateUser([
    'access test_entity_browser_iframe_view entity browser pages',
    'create article content',
    'edit own article content',
    'access content',
  ]);
  $this
    ->drupalLogin($account);
}