scheduled_publish-8.x-3.9/tests/src/Functional/ScheduledStateChangeEventTest.php

tests/src/Functional/ScheduledStateChangeEventTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\scheduled_publish\Functional;

use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\workflows\Entity\Workflow;

/**
 * Test event that occurs when an entity changes moderation state on schedule.
 *
 * @see \Drupal\scheduled_publish_test\EventSubscriber\ScheduledStateChange
 *
 * @group scheduled_publish
 */
final class ScheduledStateChangeEventTest extends BrowserTestBase {
  use ContentModerationTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
    'field',
    'field_ui',
    'views',
    'scheduled_publish_test',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    // Setup: Create a content type with a scheduled publish field and a test
    // text field.
    $this->drupalCreateContentType(['type' => 'page']);
    FieldStorageConfig::create([
      'field_name' => 'field_publish',
      'type' => 'scheduled_publish',
      'entity_type' => 'node',
      'cardinality' => 1,
    ])->save();
    FieldConfig::create([
      'field_name' => 'field_publish',
      'entity_type' => 'node',
      'bundle' => 'page',
      'label' => 'Publish',
    ])->save();
    EntityFormDisplay::load('node.page.default')->setComponent('field_publish', [
      'type' => 'scheduled_publish',
    ])->save();
    FieldStorageConfig::create([
      'field_name' => 'field_test',
      'type' => 'string_long',
      'entity_type' => 'node',
      'cardinality' => 1,
    ])->save();
    FieldConfig::create([
      'field_name' => 'field_test',
      'entity_type' => 'node',
      'bundle' => 'page',
      'label' => 'Test',
    ])->save();
    EntityFormDisplay::load('node.page.default')->setComponent('field_test', [
      'type' => 'string_textarea',
    ])->save();

    // Setup: Establish an editorial workflow.
    $this->createEditorialWorkflow();
    $editorialWorkflow = Workflow::load('editorial');
    $typeSettings = $editorialWorkflow->get('type_settings');
    $typeSettings['entity_types'] = ['node' => ['page']];
    $editorialWorkflow->set('type_settings', $typeSettings)->save();

    // Setup: Create an admin user and log in.
    $adminUser = $this->drupalCreateUser();
    $adminUser->addRole($this->createAdminRole('admin', 'admin'));
    $adminUser->save();
    $this->drupalLogin($adminUser);
  }

  /**
   * Test that the ScheduledStateChangeEvent is dispatched at the right time.
   */
  public function testEventIsDispatchedWhenContentChangesStateOnSchedule(): void {
    // Setup: Create an initial string value.
    $initialString = $this->randomString();

    // Setup: Create a node in the 'draft' state, and schedule the node to be
    // published a day ago.
    $date = DateTimePlus::createFromTimestamp(\strtotime('-1 day'));
    $node1 = $this->drupalCreateNode([
      'type' => 'page',
      'title' => $this->randomString(),
      'moderation_state' => 'draft',
      'field_test' => $initialString,
      'field_publish' => [
        [
          'moderation_state' => 'published',
          'value' => $date->format('Y-m-d\TH:i:s'),
        ],
      ],
    ]);
    $node1->save();

    // SUT: Run the scheduled publishing process. Then, re-load the node.
    $this->container->get('scheduled_publish.update')->doUpdate();
    $node2 = Node::load($node1->id());

    // Assert: The ScheduledStateChange subscriber in the test module has
    // modified the text in the test string field with data that was present in
    // \Drupal\scheduled_publish\Service\ScheduledPublishCron::updateEntity().
    $expectedString = \implode('//', [
      $initialString,
      'node',
      $node1->id(),
      'draft',
      'published',
      'field_publish',
      '',
    ]);
    $this->assertEquals($expectedString, $node2->get('field_test')->getString());
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc