You are here

protected function EntityBrowserTest::createTests in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/Extension/EntityBrowserTest.php \Drupal\Tests\entity_browser\Kernel\Extension\EntityBrowserTest::createTests()

Tests the creation of entity_browser.

1 call to EntityBrowserTest::createTests()
EntityBrowserTest::testEntityBrowserCrud in tests/src/Kernel/Extension/EntityBrowserTest.php
Tests CRUD operations.

File

tests/src/Kernel/Extension/EntityBrowserTest.php, line 91

Class

EntityBrowserTest
Tests the entity_browser config entity.

Namespace

Drupal\Tests\entity_browser\Kernel\Extension

Code

protected function createTests() {
  $plugin = [
    'name' => 'test_browser',
    'label' => 'Testing entity browser instance',
    'display' => 'standalone',
    'display_configuration' => [
      'path' => 'test-browser-test',
    ],
    'selection_display' => 'no_display',
    'selection_display_configuration' => [],
    'widget_selector' => 'single',
    'widget_selector_configuration' => [],
    'widgets' => [
      $this->widgetUUID => [
        'id' => 'view',
        'label' => 'View widget',
        'uuid' => $this->widgetUUID,
        'weight' => 0,
        'settings' => [
          'view' => 'test_view',
          'view_display' => 'test_display',
        ],
      ],
    ],
  ];
  foreach ([
    'display' => 'getDisplay',
    'selection_display' => 'getSelectionDisplay',
    'widget_selector' => 'getWidgetSelector',
  ] as $plugin_type => $function_name) {
    $current_plugin = $plugin;
    unset($current_plugin[$plugin_type]);

    // Attempt to create an entity_browser without required plugin.
    try {
      $entity = $this->controller
        ->create($current_plugin);
      $entity
        ->{$function_name}();
      $this
        ->fail('An entity browser without required ' . $plugin_type . ' created with no exception thrown.');
    } catch (PluginException $e) {
      $this
        ->assertContains('The "" plugin does not exist.', $e
        ->getMessage(), 'An exception was thrown when an entity_browser was created without a ' . $plugin_type . ' plugin.');
    }
  }

  // Try to create an entity browser w/o the ID.
  $current_plugin = $plugin;
  unset($current_plugin['name']);
  try {
    $entity = $this->controller
      ->create($current_plugin);
    $entity
      ->save();
    $this
      ->fail('An entity browser without required name created with no exception thrown.');
  } catch (EntityMalformedException $e) {
    $this
      ->assertEquals('The entity does not have an ID.', $e
      ->getMessage(), 'An exception was thrown when an entity_browser was created without a name.');
  }

  // Create an entity_browser with required values.
  $entity = $this->controller
    ->create($plugin);
  $entity
    ->save();
  $this
    ->assertTrue($entity instanceof EntityBrowserInterface, 'The newly created entity is an Entity browser.');

  // Verify all of the properties.
  $actual_properties = $this->container
    ->get('config.factory')
    ->get('entity_browser.browser.test_browser')
    ->get();
  $this
    ->assertTrue(!empty($actual_properties['uuid']), 'The entity browser UUID is set.');
  unset($actual_properties['uuid']);

  // Ensure that default values are filled in.
  $expected_properties = [
    'langcode' => $this->container
      ->get('language_manager')
      ->getDefaultLanguage()
      ->getId(),
    'status' => TRUE,
    'dependencies' => [
      'config' => [
        'views.view.test_view',
      ],
      'module' => [
        'views',
      ],
    ],
    'name' => 'test_browser',
    'label' => 'Testing entity browser instance',
    'display' => 'standalone',
    'display_configuration' => [
      'path' => 'test-browser-test',
    ],
    'selection_display' => 'no_display',
    'selection_display_configuration' => [],
    'widget_selector' => 'single',
    'widget_selector_configuration' => [],
    'widgets' => [
      $this->widgetUUID => [
        'id' => 'view',
        'label' => 'View widget',
        'uuid' => $this->widgetUUID,
        'weight' => 0,
        'settings' => [
          'view' => 'test_view',
          'view_display' => 'test_display',
          'submit_text' => 'Select entities',
          'auto_select' => FALSE,
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($actual_properties, $expected_properties, 'Actual config properties are structured as expected.');

  // Ensure that rebuilding routes works.
  $route = $this->routeProvider
    ->getRoutesByPattern('/test-browser-test');
  $this
    ->assertTrue($route, 'Route exists.');
}