3 namespace Drupal\Tests\views\Functional\Wizard;
6 * Tests the ability of the views wizard to create views with sorts.
10 class SortingTest extends WizardTestBase {
12 protected function setUp($import_test_views = TRUE) {
13 parent::setUp($import_test_views);
15 $this->drupalPlaceBlock('page_title_block');
19 * Tests the sorting functionality.
21 public function testSorting() {
22 // Create nodes, each with a different creation time so that we can do a
24 $this->drupalCreateContentType(['type' => 'page']);
25 $node1 = $this->drupalCreateNode(['created' => REQUEST_TIME]);
26 $node2 = $this->drupalCreateNode(['created' => REQUEST_TIME + 1]);
27 $node3 = $this->drupalCreateNode(['created' => REQUEST_TIME + 2]);
29 // Create a view that sorts oldest first.
31 $view1['label'] = $this->randomMachineName(16);
32 $view1['id'] = strtolower($this->randomMachineName(16));
33 $view1['description'] = $this->randomMachineName(16);
34 $view1['show[sort]'] = 'node_field_data-created:ASC';
35 $view1['page[create]'] = 1;
36 $view1['page[title]'] = $this->randomMachineName(16);
37 $view1['page[path]'] = $this->randomMachineName(16);
38 $this->drupalPostForm('admin/structure/views/add', $view1, t('Save and edit'));
39 $this->drupalGet($view1['page[path]']);
40 $this->assertResponse(200);
42 // Make sure the view shows the nodes in the expected order.
43 $this->assertUrl($view1['page[path]']);
44 $this->assertText($view1['page[title]']);
45 $content = $this->getRawContent();
46 $this->assertText($node1->label());
47 $this->assertText($node2->label());
48 $this->assertText($node3->label());
49 $pos1 = strpos($content, $node1->label());
50 $pos2 = strpos($content, $node2->label());
51 $pos3 = strpos($content, $node3->label());
52 $this->assertTrue($pos1 < $pos2 && $pos2 < $pos3, 'The nodes appear in the expected order in a view that sorts by oldest first.');
54 // Create a view that sorts newest first.
56 $view2['label'] = $this->randomMachineName(16);
57 $view2['id'] = strtolower($this->randomMachineName(16));
58 $view2['description'] = $this->randomMachineName(16);
59 $view2['show[sort]'] = 'node_field_data-created:DESC';
60 $view2['page[create]'] = 1;
61 $view2['page[title]'] = $this->randomMachineName(16);
62 $view2['page[path]'] = $this->randomMachineName(16);
63 $this->drupalPostForm('admin/structure/views/add', $view2, t('Save and edit'));
64 $this->drupalGet($view2['page[path]']);
65 $this->assertResponse(200);
67 // Make sure the view shows the nodes in the expected order.
68 $this->assertUrl($view2['page[path]']);
69 $this->assertText($view2['page[title]']);
70 $content = $this->getRawContent();
71 $this->assertText($node3->label());
72 $this->assertText($node2->label());
73 $this->assertText($node1->label());
74 $pos3 = strpos($content, $node3->label());
75 $pos2 = strpos($content, $node2->label());
76 $pos1 = strpos($content, $node1->label());
77 $this->assertTrue($pos3 < $pos2 && $pos2 < $pos1, 'The nodes appear in the expected order in a view that sorts by newest first.');