Commit 1dbc0554 authored by Joe Kersey's avatar Joe Kersey Committed by Jonathan Smith
Browse files

Issue #2644208 by joekers, jonathan1055: Convert tokens to Drupal 8

parent b11b1009
Loading
Loading
Loading
Loading
+7 −8
Changes for scheduler.tokens.inc: 7 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -3,12 +3,10 @@
/**
 * @file
 * Builds placeholder replacement tokens for node scheduler data.
 *
 * @todo This still needs to be converted to Drupal 8.
 * @see https://www.drupal.org/node/2644208
 */

use Drupal\Component\Utility;
use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_token_info().
@@ -31,7 +29,8 @@ function scheduler_token_info() {
/**
 * Implements hook_tokens().
 */
function scheduler_tokens($type, $tokens, array $data = array(), array $options = array()) {
function scheduler_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $token_service = \Drupal::token();
  $date_formatter = \Drupal::service('date.formatter');
  $language_code = isset($options['language']) ? $options['language']->language : NULL;
  $replacements = array();
@@ -55,11 +54,11 @@ function scheduler_tokens($type, $tokens, array $data = array(), array $options
    }

    // Chained token replacement.
    if (isset($node->publish_on->value) && $publish_tokens = \Drupal::token()->findWithPrefix($tokens, 'scheduler-publish')) {
      $replacements += token_generate('date', $publish_tokens, array('date' => $node->publish_on->value), $options);
    if (isset($node->publish_on->value) && $publish_tokens = $token_service->findWithPrefix($tokens, 'scheduler-publish')) {
      $replacements += $token_service->generate('date', $publish_tokens, array('date' => $node->publish_on->value), $options, $bubbleable_metadata);
    }
    if (isset($node->unpublish_on->value) && $unpublish_tokens = \Drupal::token()->findWithPrefix($tokens, 'scheduler-unpublish')) {
      $replacements += token_generate('date', $unpublish_tokens, array('date' => $node->unpublish_on->value), $options);
    if (isset($node->unpublish_on->value) && $unpublish_tokens = $token_service->findWithPrefix($tokens, 'scheduler-unpublish')) {
      $replacements += $token_service->generate('date', $unpublish_tokens, array('date' => $node->unpublish_on->value), $options, $bubbleable_metadata);
    }
  }

+1 −0
Changes for src/Tests/SchedulerTestBase.php: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ abstract class SchedulerTestBase extends WebTestBase {
    $this->adminUser = $this->drupalCreateUser([
      'administer nodes',
      'access content',
      'access content overview',
      'create page content',
      'edit own page content',
      'delete own page content',
+68 −0
Changes for src/Tests/SchedulerTokenReplaceTest.php: 68 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
/**
 * @file
 * Contains \Drupal\scheduler\Tests\SchedulerTokenReplaceTest.
 */

namespace Drupal\scheduler\Tests;

/**
 * Generates text using placeholders to check scheduler token replacement.
 *
 * @group scheduler
 */
class SchedulerTokenReplaceTest extends SchedulerTestBase {

  /**
   * Creates a node, then tests the tokens generated from it.
   */
  function testSchedulerTokenReplacement() {
    $this->drupalLogin($this->adminUser);
    $node_storage = $this->container->get('entity.manager')->getStorage('node');
    $date_formatter = \Drupal::service('date.formatter');

    // Define timestamps for consistent use when repeated throughout this test.
    $publish_on_timestamp = REQUEST_TIME + 3600;
    $unpublish_on_timestamp = REQUEST_TIME + 7200;

    // Create an unpublished page with scheduled dates.
    $settings = [
      'type' => 'page',
      'status' => FALSE,
      'publish_on' => $publish_on_timestamp,
      'unpublish_on' => $unpublish_on_timestamp,
    ];
    $node = $this->drupalCreateNode($settings);
    // Show that the node is scheduled.
    $this->drupalGet('admin/content/scheduled');

    // Create array of test case data.
    $test_cases = array(
      ['token_format' => '', 'date_format' => 'medium', 'custom' => ''],
      ['token_format' => ':long', 'date_format' => 'long','custom' => ''],
      ['token_format' => ':raw', 'date_format' => 'custom', 'custom' => 'U'],
      ['token_format' => ':custom:jS F g:ia e O', 'date_format' => 'custom', 'custom' => 'jS F g:ia e O'],
    );

    foreach ($test_cases as $test_data) {
      // Edit the node and set the body tokens to use the format being tested.
      $edit = [
        'body[0][value]' => 'Publish on: [node:scheduler-publish' . $test_data['token_format'] . ']. Unpublish on: [node:scheduler-unpublish' . $test_data['token_format'] . '].',
      ];
      $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep unpublished'));
      $this->drupalGet('node/' . $node->id());

      // Refresh the node and get the body output value.
      $node_storage->resetCache(array($node->id()));
      $node = $node_storage->load($node->id());
      $body_output = \Drupal::token()->replace($node->body->value, array('node' => $node));

      // Create the expected text for the body.
      $publish_on_date = $date_formatter->format($publish_on_timestamp, $test_data['date_format'], $test_data['custom']);
      $unpublish_on_date = $date_formatter->format($unpublish_on_timestamp, $test_data['date_format'], $test_data['custom']);
      $expected_output = 'Publish on: ' . $publish_on_date . '. Unpublish on: ' . $unpublish_on_date . '.';
      // Check that the actual text matches the expected value.
      $this->assertEqual($body_output, $expected_output, 'Scheduler tokens replaced correctly for ' . $test_data['token_format'] . ' format.');
    }
  }
}