You are here

public function SchedulerRequiredTest::testRequiredScheduling in Scheduler 8

Same name and namespace in other branches
  1. 2.x tests/src/Functional/SchedulerRequiredTest.php \Drupal\Tests\scheduler\Functional\SchedulerRequiredTest::testRequiredScheduling()

Tests creating and editing nodes with required scheduling enabled.

@dataProvider dataRequiredScheduling()

File

tests/src/Functional/SchedulerRequiredTest.php, line 17

Class

SchedulerRequiredTest
Tests the options for scheduling dates to be required during add/edit.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testRequiredScheduling($id, $publish_required, $unpublish_required, $operation, $scheduled, $status, $publish_expected, $unpublish_expected, $message) {
  $this
    ->drupalLogin($this->schedulerUser);
  $fields = $this->container
    ->get('entity_field.manager')
    ->getFieldDefinitions('node', $this->type);

  // Set required (un)publishing as stipulated by the test case.
  $this->nodetype
    ->setThirdPartySetting('scheduler', 'publish_required', $publish_required)
    ->setThirdPartySetting('scheduler', 'unpublish_required', $unpublish_required)
    ->save();

  // To assist viewing and analysing the generated test result pages create a
  // text string showing all the test case parameters.
  $title_data = [
    'id = ' . $id,
    $publish_required ? 'Publishing required' : '',
    $unpublish_required ? 'Unpublishing required' : '',
    'on ' . $operation,
    $status ? 'published' : 'unpublished',
    $scheduled ? 'scheduled' : 'not scheduled',
  ];

  // Remove any empty items.
  $title_data = array_filter($title_data);
  $title = implode(', ', $title_data);

  // If the scenario requires editing a node, we need to create one first.
  if ($operation == 'edit') {

    // Note: The key names in the $options parameter for drupalCreateNode()
    // are the plain field names i.e. 'title' not title[0][value].
    $options = [
      'title' => $title,
      'type' => $this->type,
      'status' => $status,
      'publish_on' => $scheduled ? strtotime('+1 day') : NULL,
      'body' => $message,
    ];
    $node = $this
      ->drupalCreateNode($options);

    // Define the path and button to use for editing the node.
    $path = 'node/' . $node
      ->id() . '/edit';
  }
  else {

    // Set the default status, used when testing creation of the new node.
    $fields['status']
      ->getConfig($this->type)
      ->setDefaultValue($status)
      ->save();

    // Define the path and button to use for creating the node.
    $path = 'node/add/' . $this->type;
  }

  // Make sure that both date fields are empty so we can check if they throw
  // validation errors when the fields are required.
  $values = [
    'title[0][value]' => $title,
    'publish_on[0][value][date]' => '',
    'publish_on[0][value][time]' => '',
    'unpublish_on[0][value][date]' => '',
    'unpublish_on[0][value][time]' => '',
  ];

  // Add or edit the node.
  $this
    ->drupalPostForm($path, $values, 'Save');

  // Check for the expected result.
  if ($publish_expected) {
    $string = sprintf('The %s date is required.', ucfirst('publish') . ' on');
    $this
      ->assertSession()
      ->pageTextContains($string);
  }
  if ($unpublish_expected) {
    $string = sprintf('The %s date is required.', ucfirst('unpublish') . ' on');
    $this
      ->assertSession()
      ->pageTextContains($string);
  }
  if (!$publish_expected && !$unpublish_expected) {
    $string = sprintf('%s %s has been %s.', $this->typeName, $title, $operation == 'add' ? 'created' : 'updated');
    $this
      ->assertSession()
      ->pageTextContains($string);
  }
}