SortingTest.php in Drupal 9
File
core/modules/views/tests/src/Functional/Wizard/SortingTest.php
View source
<?php
namespace Drupal\Tests\views\Functional\Wizard;
class SortingTest extends WizardTestBase {
protected $defaultTheme = 'stark';
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->drupalPlaceBlock('page_title_block');
}
public function testSorting() {
$this
->drupalCreateContentType([
'type' => 'page',
]);
$node1 = $this
->drupalCreateNode([
'created' => REQUEST_TIME,
]);
$node2 = $this
->drupalCreateNode([
'created' => REQUEST_TIME + 1,
]);
$node3 = $this
->drupalCreateNode([
'created' => REQUEST_TIME + 2,
]);
$view1 = [];
$view1['label'] = $this
->randomMachineName(16);
$view1['id'] = strtolower($this
->randomMachineName(16));
$view1['description'] = $this
->randomMachineName(16);
$view1['show[sort]'] = 'node_field_data-created:ASC';
$view1['page[create]'] = 1;
$view1['page[title]'] = $this
->randomMachineName(16);
$view1['page[path]'] = $this
->randomMachineName(16);
$this
->drupalGet('admin/structure/views/add');
$this
->submitForm($view1, 'Save and edit');
$this
->drupalGet($view1['page[path]']);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($view1['page[path]']);
$this
->assertSession()
->pageTextContains($view1['page[title]']);
$content = $this
->getSession()
->getPage()
->getContent();
$this
->assertSession()
->pageTextContains($node1
->label());
$this
->assertSession()
->pageTextContains($node2
->label());
$this
->assertSession()
->pageTextContains($node3
->label());
$pos1 = strpos($content, $node1
->label());
$pos2 = strpos($content, $node2
->label());
$pos3 = strpos($content, $node3
->label());
$this
->assertGreaterThan($pos1, $pos2);
$this
->assertGreaterThan($pos2, $pos3);
$view2 = [];
$view2['label'] = $this
->randomMachineName(16);
$view2['id'] = strtolower($this
->randomMachineName(16));
$view2['description'] = $this
->randomMachineName(16);
$view2['show[sort]'] = 'node_field_data-created:DESC';
$view2['page[create]'] = 1;
$view2['page[title]'] = $this
->randomMachineName(16);
$view2['page[path]'] = $this
->randomMachineName(16);
$this
->drupalGet('admin/structure/views/add');
$this
->submitForm($view2, 'Save and edit');
$this
->drupalGet($view2['page[path]']);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($view2['page[path]']);
$this
->assertSession()
->pageTextContains($view2['page[title]']);
$content = $this
->getSession()
->getPage()
->getContent();
$this
->assertSession()
->pageTextContains($node3
->label());
$this
->assertSession()
->pageTextContains($node2
->label());
$this
->assertSession()
->pageTextContains($node1
->label());
$pos3 = strpos($content, $node3
->label());
$pos2 = strpos($content, $node2
->label());
$pos1 = strpos($content, $node1
->label());
$this
->assertGreaterThan($pos3, $pos2);
$this
->assertGreaterThan($pos2, $pos1);
}
}
Classes
Name |
Description |
SortingTest |
Tests the ability of the views wizard to create views with sorts. |