You are here

public function BynderUsageTest::testBynderUsage in Bynder 8

Same name and namespace in other branches
  1. 8.3 tests/src/FunctionalJavascript/BynderUsageTest.php \Drupal\Tests\bynder\FunctionalJavascript\BynderUsageTest::testBynderUsage()
  2. 8.2 tests/src/FunctionalJavascript/BynderUsageTest.php \Drupal\Tests\bynder\FunctionalJavascript\BynderUsageTest::testBynderUsage()
  3. 4.0.x tests/src/FunctionalJavascript/BynderUsageTest.php \Drupal\Tests\bynder\FunctionalJavascript\BynderUsageTest::testBynderUsage()

Test Bynder media usage info.

File

tests/src/FunctionalJavascript/BynderUsageTest.php, line 104

Class

BynderUsageTest
Test the Bynder media usage info.

Namespace

Drupal\Tests\bynder\FunctionalJavascript

Code

public function testBynderUsage() {
  $first_node = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->create([
    'title' => 'First node title',
    'field_entity_reference' => [],
    'type' => 'page',
  ]);
  $first_node
    ->save();
  $this
    ->drupalGet('node/' . $first_node
    ->id() . '/usage');

  // Test access is forbidden without permission.
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->assertSession()
    ->responseContains('Access denied');
  $this
    ->assertSession()
    ->responseContains('You are not authorized to access this page.');
  $this->testRole
    ->grantPermission('view bynder media usage');
  $this->testRole
    ->save();

  // Go on Bynder media usage tab and test message when table is empty.
  $this
    ->drupalGet('node/' . $first_node
    ->id() . '/usage');
  $this
    ->assertSession()
    ->responseContains('There are no Bynder media found on the page.');
  \Drupal::configFactory()
    ->getEditable('bynder.settings')
    ->set('account_domain', 'https://plugin.getbynder.com/')
    ->save(TRUE);
  $bynder_data = [
    'type' => 'image',
    'id' => '123',
    'name' => 'Bynder name',
    'propertyOptions' => [
      0 => "6EF40BA8-E011-4758-80C12BDCA70DDF4F",
    ],
  ];
  \Drupal::state()
    ->set('bynder.bynder_test_media_info', $bynder_data);
  $media = \Drupal::entityTypeManager()
    ->getStorage('media')
    ->create([
    'name' => 'Media name test',
    'field_media_uuid' => '123',
    'bundle' => 'media_type',
    'type' => 'bynder',
  ]);
  $media
    ->save();
  $node = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->create([
    'title' => 'Page title',
    'field_entity_reference' => $media
      ->id(),
    'type' => 'page',
  ]);
  $node
    ->save();

  // Assert referenced Bynder media appears on Bynder media usage tab.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/usage');
  $this
    ->assertSession()
    ->responseContains($bynder_data['name']);
  $this
    ->assertSession()
    ->responseContains($bynder_data['type']);
  $this
    ->assertSession()
    ->responseContains('N/A');
  $this
    ->assertSession()
    ->linkExists('Edit');
  $this
    ->assertSession()
    ->linkByHrefExists('/media/' . $media
    ->id() . '/edit');
  $this
    ->assertSession()
    ->linkExists('Edit on Bynder');
  $this
    ->assertSession()
    ->linkByHrefExists('/media?mediaId=' . $bynder_data['id']);
  \Drupal::configFactory()
    ->getEditable('bynder.settings')
    ->set('usage_metaproperty', '6EF40BA8-E011-4758-80C12BDCA1111111')
    ->set('restrictions.royalty_free', '6EF40BA8-E011-4758-80C12BDCA70DDF4F')
    ->set('restrictions.web_license', '6EF40BA8-E011-4758-80C12BDCA2222222')
    ->set('restrictions.print_license', '6EF40BA8-E011-4758-80C12BDCA3333333')
    ->save(TRUE);
  $this
    ->drupalGet('node/' . $node
    ->id() . '/usage');
  $this
    ->assertSession()
    ->responseContains('Royality free');
  $this
    ->assertSession()
    ->responseNotContains('N/A');
  \Drupal::configFactory()
    ->getEditable('bynder.settings')
    ->set('restrictions.royalty_free', '6EF40BA8-E011-4758-80C12BDCA2222222')
    ->set('restrictions.web_license', '6EF40BA8-E011-4758-80C12BDCA70DDF4F')
    ->save(TRUE);
  $this
    ->drupalGet('node/' . $node
    ->id() . '/usage');
  $this
    ->assertSession()
    ->responseNotContains('Royality free');
  $this
    ->assertSession()
    ->responseNotContains('N/A');
  $this
    ->assertSession()
    ->responseContains('Web licence');
}