You are here

public function FormatterTest::testBooleanAndString in Double Field 4.x

Same name and namespace in other branches
  1. 8.3 tests/src/FunctionalJavascript/FormatterTest.php \Drupal\Tests\double_field\FunctionalJavascript\FormatterTest::testBooleanAndString()

Test callback.

File

tests/src/FunctionalJavascript/FormatterTest.php, line 24

Class

FormatterTest
A tests for Double Field formatter.

Namespace

Drupal\Tests\double_field\FunctionalJavascript

Code

public function testBooleanAndString() : void {
  $page = $this
    ->getSession()
    ->getPage();
  $settings = [];
  $settings['storage']['first']['type'] = 'boolean';
  $settings['storage']['second']['type'] = 'string';
  $settings['first']['on_label'] = 'Yes';
  $settings['first']['off_label'] = 'No';
  $settings['first']['required'] = FALSE;
  $settings['formatter']['type'] = 'double_field_unformatted_list';
  $this
    ->createField($settings);
  $this
    ->submitNode([
    NULL,
    'Bar',
  ]);

  // -- Default settings.
  $this
    ->drupalGet($this->displayAdminPath);
  $expected_summary = [
    'Display as inline element',
    '<b>First subfield - boolean</b>',
    'Hidden: no',
    '<b>Second subfield - text</b>',
    'Hidden: no',
  ];
  $this
    ->assertSettingsSummary($expected_summary);
  $this
    ->openSettingsForm();

  // Check default formatter form.
  $this
    ->assertXpath('//input[@type = "checkbox" and @name = "fields[{field_name}][settings_edit_form][settings][inline]" and @checked]');
  $this
    ->assertXpath('//details[1]/summary[text() = "First subfield - Boolean"][1]');
  $this
    ->assertXpath('//details[1]//div[1]/input[@name = "fields[{field_name}][settings_edit_form][settings][first][hidden]" and not(@checked)]');
  $this
    ->assertXpath('//details[2]/summary[text() = "Second subfield - Text"][1]');
  $this
    ->assertXpath('//details[2]//div[1]/input[@name = "fields[{field_name}][settings_edit_form][settings][second][hidden]" and not(@checked)]');
  $this
    ->drupalGet('node/1');
  $prefix = '//div[contains(@class, "double-field-unformatted-list") and contains(@class, "container-inline")]';
  $this
    ->assertXpath($prefix);
  $this
    ->assertXpath($prefix . '/div[@class = "double-field-first" and text() = "No"]');
  $this
    ->assertXpath($prefix . '/div[@class = "double-field-second" and text() = "Bar"]');

  // Test "on_label".
  $this
    ->drupalGet('node/1/edit');
  $page
    ->checkField($this->fieldName . '[0][first]');
  $page
    ->pressButton('Save');
  $prefix = '//div[contains(@class, "double-field-unformatted-list") and contains(@class, "container-inline")]';
  $this
    ->assertXpath($prefix . '/div[@class = "double-field-first" and text() = "Yes"]');
  $this
    ->assertXpath($prefix . '/div[@class = "double-field-second" and text() = "Bar"]');

  // -- Without "inline" option.
  $this
    ->drupalGet($this->displayAdminPath);
  $this
    ->openSettingsForm();
  $page
    ->uncheckField('Display as inline element');
  $this
    ->submitSettingsForm();
  $expected_summary = [
    '<b>First subfield - boolean</b>',
    'Hidden: no',
    '<b>Second subfield - text</b>',
    'Hidden: no',
  ];
  $this
    ->assertSettingsSummary($expected_summary);
  $this
    ->drupalGet('node/1');
  $prefix = '//div[contains(@class, "double-field-unformatted-list") and not(contains(@class, "container-inline"))]';
  $this
    ->assertXpath($prefix . '/div[@class = "double-field-first" and text() = "Yes"]');
  $this
    ->assertXpath($prefix . '/div[@class = "double-field-second" and text() = "Bar"]');

  // -- Hide first sub-field.
  $this
    ->drupalGet($this->displayAdminPath);
  $this
    ->openSettingsForm();
  $details_1 = $page
    ->find('xpath', '//details[1]');
  $details_1
    ->click();
  $details_1
    ->checkField($this->fieldPrefix . '[first][hidden]');
  $this
    ->submitSettingsForm();
  $expected_summary = [
    '<b>First subfield - boolean</b>',
    'Hidden: yes',
    '<b>Second subfield - text</b>',
    'Hidden: no',
  ];
  $this
    ->assertSettingsSummary($expected_summary);
  $this
    ->drupalGet('node/1');
  $prefix = '//div[contains(@class, "double-field-unformatted-list") and not(contains(@class, "container-inline"))]';
  $this
    ->assertNoXpath($prefix . '/div[@class = "double-field-first"]');
  $this
    ->assertXpath($prefix . '/div[@class = "double-field-second" and text() = "Bar"]');

  // -- Hide second sub-field.
  $this
    ->drupalGet($this->displayAdminPath);
  $this
    ->openSettingsForm();
  $details_2 = $page
    ->find('xpath', '//details[2]');
  $details_2
    ->click();
  $details_2
    ->checkField($this->fieldPrefix . '[second][hidden]');
  $this
    ->submitSettingsForm();
  $expected_summary = [
    '<b>First subfield - boolean</b>',
    'Hidden: yes',
    '<b>Second subfield - text</b>',
    'Hidden: yes',
  ];
  $this
    ->assertSettingsSummary($expected_summary);
  $this
    ->drupalGet('node/1');
  $prefix = '//div[contains(@class, "double-field-unformatted-list") and not(contains(@class, "container-inline"))]';
  $this
    ->assertNoXpath($prefix . '/div[@class = "double-field-first"]');
  $this
    ->assertNoXpath($prefix . '/div[@class = "double-field-second"]');

  // -- Show fields.
  $this
    ->drupalGet($this->displayAdminPath);
  $this
    ->openSettingsForm();
  $details_1 = $page
    ->find('xpath', '//details[1]');
  $details_1
    ->click();
  $details_1
    ->unCheckField($this->fieldPrefix . '[first][hidden]');
  $details_2 = $page
    ->find('xpath', '//details[2]');
  $details_2
    ->click();
  $details_2
    ->unCheckField($this->fieldPrefix . '[second][hidden]');
  $this
    ->submitSettingsForm();
  $expected_summary = [
    '<b>First subfield - boolean</b>',
    'Hidden: no',
    '<b>Second subfield - text</b>',
    'Hidden: no',
  ];
  $this
    ->assertSettingsSummary($expected_summary);
  $this
    ->drupalGet('node/1');
  $prefix = '//div[contains(@class, "double-field-unformatted-list")]';
  $this
    ->assertXpath($prefix . '//div[@class = "double-field-first" and contains(., "Yes")]');
  $this
    ->assertXpath($prefix . '//div[@class = "double-field-second" and contains(., "Bar")]');
}