View source
<?php
namespace Drupal\Tests\scheduler\Functional;
class SchedulerTokenReplaceTest extends SchedulerBrowserTestBase {
public function testSchedulerTokenReplacement($entityTypeId, $bundle) {
$this
->drupalLogin($this->schedulerUser);
$publish_on_timestamp = $this->requestTime + 3600;
$unpublish_on_timestamp = $this->requestTime + 7200;
$entity = $this
->createEntity($entityTypeId, $bundle, [
'status' => FALSE,
'publish_on' => $publish_on_timestamp,
'unpublish_on' => $unpublish_on_timestamp,
]);
$this
->assertFalse($entity
->isPublished(), 'The entity is not published');
$this
->assertNotEmpty($entity->publish_on->value, 'The entity has a publish_on date');
$this
->assertNotEmpty($entity->unpublish_on->value, 'The entity has an unpublish_on date');
$test_cases = [
[
'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',
],
];
$storage = $this
->entityStorageObject($entityTypeId);
foreach ($test_cases as $test_data) {
$edit = [
'body[0][value]' => "Publish on: [{$entityTypeId}:scheduler-publish{$test_data['token_format']}]. Unpublish on: [{$entityTypeId}:scheduler-unpublish{$test_data['token_format']}].",
];
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
$this
->drupalGet($entity
->toUrl());
$storage
->resetCache([
$entity
->id(),
]);
$entity = $storage
->load($entity
->id());
$body_output = \Drupal::token()
->replace($entity->body->value, [
"{$entityTypeId}" => $entity,
]);
$publish_on_date = $this->dateFormatter
->format($publish_on_timestamp, $test_data['date_format'], $test_data['custom']);
$unpublish_on_date = $this->dateFormatter
->format($unpublish_on_timestamp, $test_data['date_format'], $test_data['custom']);
$expected_output = 'Publish on: ' . $publish_on_date . '. Unpublish on: ' . $unpublish_on_date . '.';
$this
->assertEquals($expected_output, $body_output, 'Scheduler tokens replaced correctly for ' . $test_data['token_format'] . ' format.');
}
}
public function dataSchedulerTokenReplacement() {
$data = $this
->dataStandardEntityTypes();
unset($data['#media']);
return $data;
}
}