function OptionsFieldUITest::testNodeDisplay

Same name in other branches
  1. 8.9.x core/modules/options/tests/src/Functional/OptionsFieldUITest.php \Drupal\Tests\options\Functional\OptionsFieldUITest::testNodeDisplay()
  2. 10 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 342

Class

OptionsFieldUITest
Tests the Options field UI functionality.

Namespace

Drupal\Tests\options\Functional

Code

public function testNodeDisplay() {
    $this->fieldName = strtolower($this->randomMachineName());
    $this->createOptionsField('list_integer');
    $node = $this->drupalCreateNode([
        'type' => $this->type,
    ]);
    $on = $this->randomMachineName();
    $off = $this->randomMachineName();
    $edit = [
        'settings[allowed_values]' => "1|{$on}" . PHP_EOL . "0|{$off}",
    ];
    $this->drupalGet($this->adminPath);
    $this->submitForm($edit, 'Save field settings');
    $this->assertSession()
        ->pageTextContains('Updated field ' . $this->fieldName . ' field settings.');
    // 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.