You are here

public function ViewsModerationStateSortTest::testSortBaseTable in Drupal 9

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

Tests sorting with a standard data base table.

File

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

Class

ViewsModerationStateSortTest
Tests the views moderation_state field sorting integration.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

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

  // Create two revisions. The sorted revision will be 'zz_draft' since it
  // will be attached to the default revision of the entity.
  $first_node = Node::create([
    'type' => 'example',
    'title' => 'Foo',
    'moderation_state' => 'aa_draft',
  ]);
  $first_node
    ->save();
  $first_node->moderation_state = 'zz_draft';
  $first_node
    ->save();

  // Create a second published node, which falls between aa_draft and zz_draft
  // for the purposes of testing.
  $second_node = Node::create([
    'type' => 'example',
    'title' => 'Foo',
    'moderation_state' => 'published',
  ]);
  $second_node
    ->save();

  // Ascending order will see 'published' followed by 'zz_draft'.
  $this
    ->assertSortResults('test_content_moderation_state_sort_base_table', 'nid', 'ASC', [
    [
      'nid' => $second_node
        ->id(),
    ],
    [
      'nid' => $first_node
        ->id(),
    ],
  ]);

  // Descending will reverse the order.
  $this
    ->assertSortResults('test_content_moderation_state_sort_base_table', 'nid', 'DESC', [
    [
      'nid' => $first_node
        ->id(),
    ],
    [
      'nid' => $second_node
        ->id(),
    ],
  ]);
}