BooleanFormatterSettingsTest.php 3.99 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\field\Functional\Boolean;
4 5 6

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
7
use Drupal\Tests\BrowserTestBase;
8 9 10 11 12 13

/**
 * Tests the Boolean field formatter settings.
 *
 * @group field
 */
14
class BooleanFormatterSettingsTest extends BrowserTestBase {
15 16 17 18 19 20

  /**
   * Modules to enable.
   *
   * @var array
   */
21
  protected static $modules = ['field', 'field_ui', 'text', 'node', 'user'];
22

23 24 25 26 27
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  /**
   * The name of the entity bundle that is created in the test.
   *
   * @var string
   */
  protected $bundle;

  /**
   * The name of the Boolean field to use for testing.
   *
   * @var string
   */
  protected $fieldName;

  /**
   * {@inheritdoc}
   */
45
  protected function setUp(): void {
46 47 48
    parent::setUp();

    // Create a content type. Use Node because it has Field UI pages that work.
49
    $type_name = mb_strtolower($this->randomMachineName(8)) . '_test';
50
    $type = $this->drupalCreateContentType(['name' => $type_name, 'type' => $type_name]);
51 52
    $this->bundle = $type->id();

53 54 55 56 57 58 59 60
    $admin_user = $this->drupalCreateUser([
      'access content',
      'administer content types',
      'administer node fields',
      'administer node display',
      'bypass node access',
      'administer nodes',
    ]);
61 62
    $this->drupalLogin($admin_user);

63
    $this->fieldName = mb_strtolower($this->randomMachineName(8));
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    $field_storage = FieldStorageConfig::create([
      'field_name' => $this->fieldName,
      'entity_type' => 'node',
      'type' => 'boolean',
    ]);
    $field_storage->save();

    $instance = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $this->bundle,
      'label' => $this->randomMachineName(),
    ]);
    $instance->save();

79 80
    \Drupal::service('entity_display.repository')
      ->getViewDisplay('node', $this->bundle)
81 82 83
      ->setComponent($this->fieldName, [
        'type' => 'boolean',
        'settings' => [],
84 85
      ])
      ->save();
86 87 88 89 90
  }

  /**
   * Tests the formatter settings page for the Boolean formatter.
   */
91
  public function testBooleanFormatterSettings() {
92 93
    // List the options we expect to see on the settings form. Omit the one
    // with the Unicode check/x characters, which does not appear to work
94
    // well in BrowserTestBase.
95
    $options = [
96 97 98 99 100 101
      'Yes / No',
      'True / False',
      'On / Off',
      'Enabled / Disabled',
      '1 / 0',
      'Custom',
102
    ];
103 104 105

    // For several different values of the field settings, test that the
    // options, including default, are shown correctly.
106 107 108 109 110
    $settings = [
      ['Yes', 'No'],
      ['On', 'Off'],
      ['TRUE', 'FALSE'],
    ];
111

112
    $assert_session = $this->assertSession();
113 114
    foreach ($settings as $values) {
      // Set up the field settings.
115
      $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/fields/node.' . $this->bundle . '.' . $this->fieldName);
116
      $this->submitForm([
117 118
        'settings[on_label]' => $values[0],
        'settings[off_label]' => $values[1],
119
      ], 'Save settings');
120 121 122

      // Open the Manage Display page and trigger the field settings form.
      $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/display');
123
      $this->submitForm([], $this->fieldName . '_settings_edit');
124 125 126

      // Test that the settings options are present in the correct format.
      foreach ($options as $string) {
127
        $assert_session->pageTextContains($string);
128
      }
129
      $assert_session->pageTextContains(t('Field settings (@on_label / @off_label)', ['@on_label' => $values[0], '@off_label' => $values[1]]));
130

131
      // Test that the settings summary are present in the correct format.
132
      $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/display');
133 134
      $this->assertSession()->elementExists('xpath', "//div[contains(@class, 'field-plugin-summary')]");
      $this->assertSession()->elementTextEquals('xpath', "//div[contains(@class, 'field-plugin-summary')]", "Display: {$values[0]} / {$values[1]}");
135
    }
136 137 138
  }

}