You are here

public function DynamicEntityReferenceTest::testFieldFormatterViewModes in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 tests/src/FunctionalJavascript/DynamicEntityReferenceTest.php \Drupal\Tests\dynamic_entity_reference\FunctionalJavascript\DynamicEntityReferenceTest::testFieldFormatterViewModes()

Tests view modes in formatter of dynamic entity reference field.

File

tests/src/FunctionalJavascript/DynamicEntityReferenceTest.php, line 188

Class

DynamicEntityReferenceTest
Ensures that Dynamic Entity References field works correctly.

Namespace

Drupal\Tests\dynamic_entity_reference\FunctionalJavascript

Code

public function testFieldFormatterViewModes() {
  $assert_session = $this
    ->assertSession();
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalCreateContentType([
    'type' => 'test_content',
  ]);
  $this
    ->drupalGet('/admin/structure/types/manage/test_content/fields/add-field');
  $select = $assert_session
    ->selectExists('new_storage_type');
  $select
    ->selectOption('dynamic_entity_reference');
  $label = $assert_session
    ->fieldExists('label');
  $label
    ->setValue('Foobar');

  // Wait for the machine name.
  $assert_session
    ->waitForElementVisible('css', '[name="label"] + * .machine-name-value');
  $this
    ->submitForm([], t('Save and continue'), 'field-ui-field-storage-add-form');
  $page = $this
    ->getSession()
    ->getPage();
  $entity_type_ids_select = $assert_session
    ->selectExists('settings[entity_type_ids][]', $page);
  $entity_type_ids_select
    ->selectOption('user');
  $assert_session
    ->selectExists('cardinality', $page)
    ->selectOption(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  $page
    ->uncheckField('settings[exclude_entity_types]');
  $this
    ->submitForm([], t('Save field settings'), 'field-storage-config-edit-form');
  $this
    ->drupalGet('admin/structure/types/manage/test_content/display');
  $page = $this
    ->getSession()
    ->getPage();
  $formats = $assert_session
    ->selectExists('fields[field_foobar][type]', $page);
  $formats
    ->selectOption('dynamic_entity_reference_entity_view');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->pressButton('Edit');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session
    ->selectExists('fields[field_foobar][settings_edit_form][settings][user][view_mode]', $page);
  $assert_session
    ->optionExists('fields[field_foobar][settings_edit_form][settings][user][view_mode]', 'compact', $page);
  $assert_session
    ->optionExists('fields[field_foobar][settings_edit_form][settings][user][view_mode]', 'full', $page);

  // Edit field, turn on exclude entity types and check display again.
  $this
    ->drupalGet('admin/structure/types/manage/test_content/fields/node.test_content.field_foobar/storage');
  $page
    ->checkField('settings[exclude_entity_types]');
  $this
    ->submitForm([], t('Save field settings'), 'field-storage-config-edit-form');
  $this
    ->drupalGet('admin/structure/types/manage/test_content/display');
  $page = $this
    ->getSession()
    ->getPage();
  $formats = $assert_session
    ->selectExists('fields[field_foobar][type]', $page);
  $formats
    ->selectOption('dynamic_entity_reference_entity_view');
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Assert node view mode is set on default.
  $assert_session
    ->responseContains("Content view mode: default");
  $page
    ->pressButton('Edit');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page = $this
    ->getSession()
    ->getPage();

  // Assert we have multi select form items for view mode settings.
  $assert_session
    ->selectExists('fields[field_foobar][settings_edit_form][settings][entity_test_with_bundle][view_mode]', $page);
  $assert_session
    ->responseContains("View mode for <em class=\"placeholder\">Test entity with bundle</em>");
  $assert_session
    ->optionExists('fields[field_foobar][settings_edit_form][settings][entity_test_with_bundle][view_mode]', 'default', $page);
  $assert_session
    ->optionNotExists('fields[field_foobar][settings_edit_form][settings][entity_test_with_bundle][view_mode]', 'rss', $page);
  $node_view_modes = $assert_session
    ->selectExists('fields[field_foobar][settings_edit_form][settings][node][view_mode]', $page);
  $assert_session
    ->responseContains("View mode for <em class=\"placeholder\">Content</em>");
  $assert_session
    ->optionExists('fields[field_foobar][settings_edit_form][settings][node][view_mode]', 'default', $page);
  $assert_session
    ->optionExists('fields[field_foobar][settings_edit_form][settings][node][view_mode]', 'full', $page);
  $assert_session
    ->optionExists('fields[field_foobar][settings_edit_form][settings][node][view_mode]', 'rss', $page);
  $assert_session
    ->optionExists('fields[field_foobar][settings_edit_form][settings][node][view_mode]', 'teaser', $page);

  // Select different select options and assert summary is changed properly.
  $node_view_modes
    ->selectOption('teaser');
  $page
    ->pressButton('Update');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->responseContains("Content view mode: teaser");
  $page
    ->pressButton('Edit');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $node_view_modes
    ->selectOption('rss');
  $page
    ->pressButton('Update');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->responseContains("Content view mode: rss");
}