You are here

public function SchedulerFieldsDisplayTest::testVerticalTabOrFieldset in Scheduler 2.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/SchedulerFieldsDisplayTest.php \Drupal\Tests\scheduler\Functional\SchedulerFieldsDisplayTest::testVerticalTabOrFieldset()

Tests date input is displayed as vertical tab or an expandable fieldset.

This test covers _scheduler_entity_form_alter().

@dataProvider dataStandardEntityTypes()

File

tests/src/Functional/SchedulerFieldsDisplayTest.php, line 43

Class

SchedulerFieldsDisplayTest
Tests the display of date entry fields and form elements.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testVerticalTabOrFieldset($entityTypeId, $bundle) {
  $this
    ->drupalLogin($this->adminUser);
  $entityType = $this
    ->entityTypeObject($entityTypeId, $bundle);

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();

  // For rendering of vertical tabs, node and media entity forms have a div
  // with class 'js-form-type-vertical-tabs'. However, the Commerce Product
  // module does things differently and does not have this class, but instead
  // has a class 'layout-region-product-secondary' (for vertical tabs) and
  // 'layout-region-product-main' if in the main form not in vertical tabs. So
  // to cover all entity types we can check for either of these classes as an
  // ancestor of the 'edit-scheduler-settings' section.
  $vertical_tab_xpath = '//div[contains(@class, "form-type-vertical-tabs") or contains(@class, "-secondary")]//details[@id = "edit-scheduler-settings"]';

  // The 'open' and 'closed' xpath searches do apply to vertical tabs, even if
  // the theme does not actually make use of it (such as in Bartik and Stark).
  $details_open_xpath = '//details[@id = "edit-scheduler-settings" and @open = "open"]';
  $details_closed_xpath = '//details[@id = "edit-scheduler-settings" and not(@open = "open")]';

  // Check that the dates are shown in a vertical tab by default.
  $add_url = $this
    ->entityAddUrl($entityTypeId, $bundle);
  $this
    ->drupalGet($add_url);
  $assert
    ->elementExists('xpath', $vertical_tab_xpath);
  $assert
    ->elementExists('xpath', $details_closed_xpath);

  // Check that the dates are shown as a fieldset when configured to do so,
  // and that fieldset is collapsed by default.
  $entityType
    ->setThirdPartySetting('scheduler', 'fields_display_mode', 'fieldset')
    ->save();
  $this
    ->drupalGet($add_url);
  $assert
    ->elementNotExists('xpath', $vertical_tab_xpath);
  $assert
    ->elementExists('xpath', $details_closed_xpath);

  // Check that the fieldset is expanded if either of the scheduling dates
  // are required.
  $entityType
    ->setThirdPartySetting('scheduler', 'publish_required', TRUE)
    ->save();
  $this
    ->drupalGet($add_url);
  $assert
    ->elementExists('xpath', $details_open_xpath);
  $entityType
    ->setThirdPartySetting('scheduler', 'publish_required', FALSE)
    ->setThirdPartySetting('scheduler', 'unpublish_required', TRUE)
    ->save();
  $this
    ->drupalGet($add_url);
  $assert
    ->elementExists('xpath', $details_open_xpath);

  // Check that the fieldset is expanded if the 'always' option is set.
  $entityType
    ->setThirdPartySetting('scheduler', 'publish_required', FALSE)
    ->setThirdPartySetting('scheduler', 'unpublish_required', FALSE)
    ->setThirdPartySetting('scheduler', 'expand_fieldset', 'always')
    ->save();
  $this
    ->drupalGet($add_url);
  $assert
    ->elementExists('xpath', $details_open_xpath);

  // Check that the fieldset is expanded if the entity already has a
  // publish-on date. This requires editing an existing scheduled entity.
  $entityType
    ->setThirdPartySetting('scheduler', 'expand_fieldset', 'when_required')
    ->save();
  $options = [
    'title' => 'Contains Publish-on date ' . $this
      ->randomMachineName(10),
    'publish_on' => strtotime('+1 day'),
  ];
  $entity = $this
    ->createEntity($entityTypeId, $bundle, $options);
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $assert
    ->elementExists('xpath', $details_open_xpath);

  // Repeat the check with a timestamp value of zero. This is a valid date
  // so the fieldset should be opened. It will not be used much on real sites
  // but can occur when testing Rules which fail to set the date correctly and
  // we get zero. Debugging Rules is easier if the fieldset opens as expected.
  $options = [
    'title' => 'Contains Publish-on date with timestamp value zero - ' . $this
      ->randomMachineName(10),
    'publish_on' => 0,
  ];
  $entity = $this
    ->createEntity($entityTypeId, $bundle, $options);
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $assert
    ->elementExists('xpath', $details_open_xpath);

  // Check that the fieldset is expanded if there is an unpublish-on date.
  $options = [
    'title' => 'Contains Unpublish-on date ' . $this
      ->randomMachineName(10),
    'unpublish_on' => strtotime('+1 day'),
  ];
  $entity = $this
    ->createEntity($entityTypeId, $bundle, $options);
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $assert
    ->elementExists('xpath', $details_open_xpath);

  // Repeat with a timestamp value of zero.
  $options = [
    'title' => 'Contains Unpublish-on date with timestamp value zero - ' . $this
      ->randomMachineName(10),
    'unpublish_on' => 0,
  ];
  $entity = $this
    ->createEntity($entityTypeId, $bundle, $options);
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $assert
    ->elementExists('xpath', $details_open_xpath);

  // Check that the display reverts to a vertical tab again when specifically
  // configured to do so.
  $entityType
    ->setThirdPartySetting('scheduler', 'fields_display_mode', 'vertical_tab')
    ->save();
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $assert
    ->elementExists('xpath', $vertical_tab_xpath);
  $assert
    ->elementExists('xpath', $details_open_xpath);
}