You are here

public function SchedulerMultilingualTest::testPublishingTranslations in Scheduler 8

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

Test creating translations with independent scheduling.

@dataProvider dataPublishingTranslations()

File

tests/src/Functional/SchedulerMultilingualTest.php, line 125

Class

SchedulerMultilingualTest
Tests the scheduling functions for node translations.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testPublishingTranslations($publish_on_translatable, $unpublish_on_translatable, $status_translatable, array $expected_status_values_before, array $expected_status_values_after) {

  // Show the languages, this is for info and debug only.
  $this
    ->drupalGet('admin/config/regional/language');

  // Set the scheduler fields to be translatable yes/no depending on the
  // parameters passed in.
  $this
    ->drupalGet('admin/config/regional/content-language');
  $settings = [
    'edit-settings-node-' . $this->type . '-settings-language-language-alterable' => TRUE,
    'edit-settings-node-' . $this->type . '-fields-publish-on' => $publish_on_translatable,
    'edit-settings-node-' . $this->type . '-fields-unpublish-on' => $unpublish_on_translatable,
    'edit-settings-node-' . $this->type . '-fields-status' => $status_translatable,
  ];

  // The submit shows the updated values, so no need for second get.
  $this
    ->submitForm($settings, 'Save configuration');
  $early_return = FALSE;
  if ($publish_on_translatable != $status_translatable) {

    // Check for validation form error on status and publish_on.
    $this
      ->assertSession()
      ->elementExists('xpath', '//input[@id = "edit-settings-node-' . $this->type . '-fields-publish-on" and contains(@class, "error")]');
    $this
      ->assertSession()
      ->elementExists('xpath', '//input[@id = "edit-settings-node-' . $this->type . '-fields-status" and contains(@class, "error")]');
    $early_return = TRUE;
  }
  if ($unpublish_on_translatable != $status_translatable) {

    // Check for validation form error on status and unpublish_on.
    $this
      ->assertSession()
      ->elementExists('xpath', '//input[@id = "edit-settings-node-' . $this->type . '-fields-unpublish-on" and contains(@class, "error")]');
    $this
      ->assertSession()
      ->elementExists('xpath', '//input[@id = "edit-settings-node-' . $this->type . '-fields-status" and contains(@class, "error")]');
    $early_return = TRUE;
  }
  if ($early_return) {

    // The rest of the test is meaningless so skip it and move to the next.
    return;
  }

  // Create a node in the 'original' language, before any translations. It is
  // unpublished with no scheduled date.
  $create = [
    'type' => $this->type,
    'title' => $this->languages[0]['name'] . '(0) - Unpublished and not scheduled',
    'langcode' => $this->languages[0]['code'],
    'status' => FALSE,
  ];
  $node = $this
    ->drupalCreateNode($create);
  $nid = $node
    ->id();

  // Create the first translation, published now with no scheduled dates.
  $this
    ->drupalGet('node/' . $nid . '/translations/add/' . $this->languages[0]['code'] . '/' . $this->languages[1]['code']);
  $edit = [
    'title[0][value]' => $this->languages[1]['name'] . '(1) - Published now',
    'publish_on[0][value][date]' => '',
    'publish_on[0][value][time]' => '',
    'status[value]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save');

  // Create second translation, for publishing and unpublising in the future.
  $this
    ->drupalGet('node/' . $nid . '/translations/add/' . $this->languages[0]['code'] . '/' . $this->languages[2]['code']);
  $edit = [
    'title[0][value]' => $this->languages[2]['name'] . '(2) - Publish in the future',
    'publish_on[0][value][date]' => date('Y-m-d', strtotime('+2 day', $this->requestTime)),
    'publish_on[0][value][time]' => date('H:i:s', strtotime('+2 day', $this->requestTime)),
    'unpublish_on[0][value][date]' => date('Y-m-d', strtotime('+3 day', $this->requestTime)),
    'unpublish_on[0][value][time]' => date('H:i:s', strtotime('+3 day', $this->requestTime)),
  ];
  $this
    ->submitForm($edit, 'Save');

  // Reset the cache, reload the node, and check if the dates of translation
  // 3 have been synchronized to the other translations, or not, as required.
  $this->nodeStorage
    ->resetCache([
    $nid,
  ]);
  $node = $this->nodeStorage
    ->load($nid);
  $translation1 = $node
    ->getTranslation($this->languages[1]['code']);
  $translation2 = $node
    ->getTranslation($this->languages[2]['code']);
  if ($publish_on_translatable) {
    $this
      ->assertNotEquals($translation2->publish_on->value, $node->publish_on->value, 'Node publish_on');
    $this
      ->assertNotEquals($translation2->unpublish_on->value, $node->unpublish_on->value, 'Node unpublish_on');
  }
  else {
    $this
      ->assertEquals($translation2->publish_on->value, $node->publish_on->value, 'Node publish_on');
    $this
      ->assertEquals($translation2->unpublish_on->value, $node->unpublish_on->value, 'Node unpublish_on');
    $this
      ->assertEquals($translation2->publish_on->value, $translation1->publish_on->value, 'Translation 1 publish_on');
    $this
      ->assertEquals($translation2->unpublish_on->value, $translation1->unpublish_on->value, 'Translation 1 unpublish_on');
  }

  // Create the third translation, to be published in the past.
  $this
    ->drupalGet('node/' . $nid . '/translations/add/' . $this->languages[0]['code'] . '/' . $this->languages[3]['code']);
  $edit = [
    'title[0][value]' => $this->languages[3]['name'] . '(3) - Publish in the past',
    'publish_on[0][value][date]' => date('Y-m-d', strtotime('-2 day', $this->requestTime)),
    'publish_on[0][value][time]' => date('H:i:s', strtotime('-2 day', $this->requestTime)),
  ];
  $this
    ->submitForm($edit, 'Save');

  // Reset the cache, reload the node, and check if the dates of translation
  // 3 have been synchronized to the other translations, or not, as required.
  $this->nodeStorage
    ->resetCache([
    $nid,
  ]);
  $node = $this->nodeStorage
    ->load($nid);
  $translation1 = $node
    ->getTranslation($this->languages[1]['code']);
  $translation2 = $node
    ->getTranslation($this->languages[2]['code']);
  $translation3 = $node
    ->getTranslation($this->languages[3]['code']);
  if ($publish_on_translatable) {
    $this
      ->assertNotEquals($translation3->publish_on->value, $translation2->publish_on->value, 'Node publish_on');
    $this
      ->assertNotEquals($translation3->unpublish_on->value, $translation2->unpublish_on->value, 'Node unpublish_on');
  }
  else {

    // The scheduer dates should be synchronized across all translations.
    $this
      ->assertEquals($translation3->publish_on->value, $node->publish_on->value, 'Node publish_on');
    $this
      ->assertEquals($translation3->unpublish_on->value, $node->unpublish_on->value, 'Node unpublish_on');
    $this
      ->assertEquals($translation3->publish_on->value, $translation1->publish_on->value, 'Translation 1 publish_on');
    $this
      ->assertEquals($translation3->unpublish_on->value, $translation1->unpublish_on->value, 'Translation 1 unpublish_on');
    $this
      ->assertEquals($translation3->publish_on->value, $translation2->publish_on->value, 'Translation 2 publish_on');
    $this
      ->assertEquals($translation3->unpublish_on->value, $translation2->unpublish_on->value, 'Translation 2 unpublish_on');
  }

  // For info only.
  $this
    ->drupalGet($this->languages[0]['code'] . '/node/' . $nid . '/translations');
  $this
    ->drupalGet('admin/content/scheduled');

  // Check that the status of all four pieces of content before running cron
  // match the expected values.
  $this
    ->checkStatus($nid, 'Before cron', $expected_status_values_before);

  // Check that the status after running cron matches the expected values.
  $this
    ->cronRun();
  $this
    ->checkStatus($nid, 'After cron', $expected_status_values_after);

  // For info only.
  $this
    ->drupalGet('admin/content/scheduled');
  $this
    ->drupalGet('admin/content');
  $this
    ->drupalGet('admin/reports/dblog');
  $this
    ->drupalGet($this->languages[0]['code'] . '/node/' . $nid . '/translations');
}