function OptionsFieldUITest::testNodeDisplay

Same name and namespace in other branches
  1. 9 core/modules/options/tests/src/Functional/OptionsFieldUITest.php \Drupal\Tests\options\Functional\OptionsFieldUITest::testNodeDisplay()
  2. 8.9.x core/modules/options/tests/src/Functional/OptionsFieldUITest.php \Drupal\Tests\options\Functional\OptionsFieldUITest::testNodeDisplay()
  3. 11.x core/modules/options/tests/src/Functional/OptionsFieldUITest.php \Drupal\Tests\options\Functional\OptionsFieldUITest::testNodeDisplay()

Tests normal and key formatter display on node display.

File

core/modules/options/tests/src/Functional/OptionsFieldUITest.php, line 394

Class

OptionsFieldUITest
Tests the Options field UI functionality.

Namespace

Drupal\Tests\options\Functional

Code

public function testNodeDisplay() : void {
  $this->fieldName = $this->randomMachineName();
  $this->createOptionsField('list_integer');
  $node = $this->drupalCreateNode([
    'type' => $this->type,
  ]);
  $on = $this->randomMachineName();
  $off = $this->randomMachineName();
  $edit = [
    'field_storage[subform][settings][allowed_values][table][0][item][key]' => 1,
    'field_storage[subform][settings][allowed_values][table][0][item][label]' => $on,
    'field_storage[subform][settings][allowed_values][table][1][item][key]' => 0,
    'field_storage[subform][settings][allowed_values][table][1][item][label]' => $off,
  ];
  $this->drupalGet($this->adminPath);
  $page = $this->getSession()
    ->getPage();
  $page->findButton('Add another item')
    ->click();
  $this->submitForm($edit, 'Save');
  // Select a default value.
  $edit = [
    $this->fieldName => '1',
  ];
  $this->drupalGet('node/' . $node->id() . '/edit');
  $this->submitForm($edit, 'Save');
  // Check the node page and see if the values are correct.
  $file_formatters = [
    'list_default',
    'list_key',
  ];
  foreach ($file_formatters as $formatter) {
    $edit = [
      "fields[{$this->fieldName}][type]" => $formatter,
      "fields[{$this->fieldName}][region]" => 'content',
    ];
    $this->drupalGet('admin/structure/types/manage/' . $this->typeName . '/display');
    $this->submitForm($edit, 'Save');
    $this->drupalGet('node/' . $node->id());
    if ($formatter == 'list_default') {
      $output = $on;
    }
    else {
      $output = '1';
    }
    // Verify that correct options are found.
    $this->assertSession()
      ->elementsCount('xpath', '//div[text()="' . $output . '"]', 1);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.