You are here

public function ViewsModerationStateSortTest::testSortRevisionBaseTable in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/ViewsModerationStateSortTest.php \Drupal\Tests\content_moderation\Kernel\ViewsModerationStateSortTest::testSortRevisionBaseTable()

Test sorting with the revision base table.

File

core/modules/content_moderation/tests/src/Kernel/ViewsModerationStateSortTest.php, line 104

Class

ViewsModerationStateSortTest
Tests the views moderation_state field sorting integration.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testSortRevisionBaseTable() {
  $this
    ->enableModules([
    'content_moderation_test_views',
  ]);
  $this
    ->installConfig([
    'content_moderation_test_views',
  ]);

  // Create a series of node revisions in different states and store
  // each revision ID at the given state.
  $node = Node::create([
    'type' => 'example',
    'title' => 'Foo',
    'moderation_state' => 'published',
  ]);
  $node
    ->save();
  $published_revision_id = $node
    ->getRevisionId();
  $node->moderation_state = 'draft';
  $node
    ->save();
  $draft_revision_id = $node
    ->getRevisionId();
  $node->moderation_state = 'aa_draft';
  $node
    ->save();
  $aa_draft_revision_id = $node
    ->getRevisionId();
  $translated = $node
    ->addTranslation('fr');
  $translated->moderation_state = 'zz_draft';
  $translated->title = 'Translated';
  $translated
    ->save();
  $zz_draft_revision_id = $translated
    ->getRevisionId();

  // A second aa_draft revision will be created for the non-translated
  // revision. Since in this case there will be two revisions with "aa_draft"
  // we add another sort in content_moderation_test_views_views_query_alter.
  // Secondary sorting is not an option in views when using exposed sorting
  // and table click sorting, so in order to maintain the same level of
  // coverage this is required.
  $second_aa_draft_revision_id = $translated
    ->getRevisionId();
  $this
    ->assertSortResults('test_content_moderation_state_sort_revision_table', 'vid', 'ASC', [
    [
      'vid' => $aa_draft_revision_id,
    ],
    [
      'vid' => $second_aa_draft_revision_id,
    ],
    [
      'vid' => $draft_revision_id,
    ],
    [
      'vid' => $published_revision_id,
    ],
    [
      'vid' => $zz_draft_revision_id,
    ],
  ]);
  $this
    ->assertSortResults('test_content_moderation_state_sort_revision_table', 'vid', 'DESC', [
    [
      'vid' => $zz_draft_revision_id,
    ],
    [
      'vid' => $published_revision_id,
    ],
    [
      'vid' => $draft_revision_id,
    ],
    [
      'vid' => $aa_draft_revision_id,
    ],
    [
      'vid' => $second_aa_draft_revision_id,
    ],
  ]);
}