You are here

protected function ImageBrowserCardinalityTest::setUp in Lightning Media 8.4

Same name and namespace in other branches
  1. 8.3 modules/lightning_media_image/tests/src/FunctionalJavascript/ImageBrowserCardinalityTest.php \Drupal\Tests\lightning_media_image\FunctionalJavascript\ImageBrowserCardinalityTest::setUp()

Overrides BrowserTestBase::setUp

File

modules/lightning_media_image/tests/src/FunctionalJavascript/ImageBrowserCardinalityTest.php, line 43

Class

ImageBrowserCardinalityTest
Tests that the image browser handles field cardinality correctly.

Namespace

Drupal\Tests\lightning_media_image\FunctionalJavascript

Code

protected function setUp() {
  parent::setUp();
  $this
    ->createContentType([
    'type' => 'page',
  ]);
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_multi_image',
    'entity_type' => 'node',
    'type' => 'image',
    'cardinality' => 3,
  ]);
  $field_storage
    ->save();
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'page',
    'label' => 'Multi-Image',
  ])
    ->save();
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_unlimited_images',
    'entity_type' => 'node',
    'type' => 'image',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $field_storage
    ->save();
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'page',
    'label' => 'Unlimited Images',
  ])
    ->save();
  $this->container
    ->get('entity_display.repository')
    ->getFormDisplay('node', 'page')
    ->setComponent('field_multi_image', [
    'type' => 'entity_browser_file',
    'settings' => [
      'entity_browser' => 'image_browser',
      'field_widget_edit' => TRUE,
      'field_widget_remove' => TRUE,
      'view_mode' => 'default',
      'preview_image_style' => 'thumbnail',
      'open' => TRUE,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
    ],
    'region' => 'content',
  ])
    ->setComponent('field_unlimited_images', [
    'type' => 'entity_browser_file',
    'settings' => [
      'entity_browser' => 'image_browser',
      'field_widget_edit' => TRUE,
      'field_widget_remove' => TRUE,
      'view_mode' => 'default',
      'preview_image_style' => 'thumbnail',
      'open' => TRUE,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
    ],
    'region' => 'content',
  ])
    ->save();
  for ($i = 0; $i < 4; $i++) {
    $uri = $this
      ->getRandomGenerator()
      ->image(uniqid('public://random_') . '.png', '240x240', '640x480');
    $file = File::create([
      'uri' => $uri,
    ]);
    $file
      ->setMimeType('image/png');
    $file
      ->setTemporary();
    $file
      ->save();
    $media = Media::create([
      'bundle' => 'image',
      'name' => $this
        ->getRandomGenerator()
        ->name(32),
      'field_media_image' => $file
        ->id(),
      'field_media_in_library' => TRUE,
    ]);
    $media
      ->save();
  }
  $account = $this
    ->drupalCreateUser([
    'access media overview',
    'create page content',
    'access image_browser entity browser pages',
  ]);
  $this
    ->drupalLogin($account);
  module_load_install('lightning_media_image');
  lightning_media_image_install();
}