You are here

public function BynderCreateMediaTest::testCreateMediaEntities in Bynder 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/BynderCreateMediaTest.php \Drupal\Tests\bynder\Functional\BynderCreateMediaTest::testCreateMediaEntities()
  2. 8 tests/src/Functional/BynderCreateMediaTest.php \Drupal\Tests\bynder\Functional\BynderCreateMediaTest::testCreateMediaEntities()
  3. 4.0.x tests/src/Functional/BynderCreateMediaTest.php \Drupal\Tests\bynder\Functional\BynderCreateMediaTest::testCreateMediaEntities()

Test create media entities on Bynder search widget.

File

tests/src/Functional/BynderCreateMediaTest.php, line 101

Class

BynderCreateMediaTest
Tests duplicate media on Bynder search widget.

Namespace

Drupal\Tests\bynder\Functional

Code

public function testCreateMediaEntities() {
  \Drupal::state()
    ->set('bynder.bynder_test_access_token', TRUE);
  $metaproperties = [
    'test_filter' => [
      'label' => 'Test filter',
      'name' => 'test_filter',
      'isFilterable' => TRUE,
      'isMultiselect' => FALSE,
      'isRequired' => FALSE,
      'zindex' => 1,
      'options' => [
        'option1' => [
          'label' => 'option',
          'id' => 'option1',
        ],
      ],
    ],
    'test_another_filter' => [
      'label' => 'Test another filter',
      'name' => 'test_another_filter',
      'isFilterable' => TRUE,
      'isMultiselect' => FALSE,
      'isRequired' => FALSE,
      'zindex' => 1,
      'options' => [
        'option1' => [
          'label' => 'option',
          'id' => 'option1',
        ],
      ],
    ],
    'test_not_filterable' => [
      'label' => 'Test not filterable',
      'name' => 'test_not_filterable',
      'isFilterable' => FALSE,
      'isMultiselect' => FALSE,
      'isRequired' => FALSE,
      'zindex' => 1,
      'options' => [
        'option1' => [
          'label' => 'option',
          'id' => 'option1',
        ],
      ],
    ],
    'test_empty_options' => [
      'label' => 'Test no options',
      'isFilterable' => FALSE,
      'zindex' => 1,
      'options' => [],
    ],
  ];
  \Drupal::state()
    ->set('bynder.bynder_test_metaproperties', $metaproperties);
  $image_1 = $this
    ->getTestFiles('image')[1];
  $image_2 = $this
    ->getTestFiles('image')[2];
  $bynder_data = [
    'media' => [
      [
        'type' => 'image',
        'id' => '4DFD39C5-1234-1234-8714AFEE1A617618',
        'name' => 'Photo from London',
        'tags' => [
          0 => 'startups',
          1 => 'london',
        ],
        'extension' => [
          0 => 'jpeg',
        ],
        'keyword' => 'london',
        'thumbnails' => [
          'mini' => file_create_url($image_1->uri),
          'webimage' => file_create_url($image_1->uri),
          'thul' => file_create_url($image_1->uri),
        ],
      ],
      [
        'type' => 'image',
        'id' => '4DFD39C5-4321-4321-8714AFFF1A617618',
        'name' => 'Photo from Paris',
        'tags' => [
          0 => 'start',
          1 => 'paris',
        ],
        'extension' => [
          0 => 'jpeg',
        ],
        'keyword' => 'paris',
        'thumbnails' => [
          'mini' => file_create_url($image_2->uri),
          'webimage' => file_create_url($image_2->uri),
          'thul' => file_create_url($image_2->uri),
        ],
      ],
    ],
    'total' => 2,
  ];
  \Drupal::state()
    ->set('bynder.bynder_test_media_list', $bynder_data);

  // Assert that we have no media created yet.
  $this
    ->assertEquals(0, count(\Drupal::entityTypeManager()
    ->getStorage('media')
    ->loadMultiple()));

  // Fill form with Bynder assets and check if media entities are created.
  $this
    ->drupalGet('entity-browser/modal/bynder');
  $this
    ->getSession()
    ->getPage()
    ->checkField('selection[4DFD39C5-1234-1234-8714AFEE1A617618]');
  $this
    ->getSession()
    ->getPage()
    ->checkField('selection[4DFD39C5-4321-4321-8714AFFF1A617618]');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Select assets');
  $this
    ->assertEquals(2, count(\Drupal::entityTypeManager()
    ->getStorage('media')
    ->loadMultiple()));

  // Use the same Bynder assets and check if entities are not re-created.
  $this
    ->drupalGet('entity-browser/modal/bynder');
  $this
    ->getSession()
    ->getPage()
    ->checkField('selection[4DFD39C5-1234-1234-8714AFEE1A617618]');
  $this
    ->getSession()
    ->getPage()
    ->checkField('selection[4DFD39C5-4321-4321-8714AFFF1A617618]');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Select assets');
  $this
    ->assertEquals(2, count(\Drupal::entityTypeManager()
    ->getStorage('media')
    ->loadMultiple()));
}