3 namespace Drupal\Tests\views\Functional\Wizard;
6 * Tests the ability of the views wizard to specify the number of items per
11 class ItemsPerPageTest extends WizardTestBase {
13 protected function setUp($import_test_views = TRUE) {
14 parent::setUp($import_test_views);
16 $this->drupalPlaceBlock('page_title_block');
20 * Tests the number of items per page.
22 public function testItemsPerPage() {
23 $this->drupalCreateContentType(['type' => 'article']);
25 // Create articles, each with a different creation time so that we can do a
27 $node1 = $this->drupalCreateNode(['type' => 'article', 'created' => REQUEST_TIME]);
28 $node2 = $this->drupalCreateNode(['type' => 'article', 'created' => REQUEST_TIME + 1]);
29 $node3 = $this->drupalCreateNode(['type' => 'article', 'created' => REQUEST_TIME + 2]);
30 $node4 = $this->drupalCreateNode(['type' => 'article', 'created' => REQUEST_TIME + 3]);
31 $node5 = $this->drupalCreateNode(['type' => 'article', 'created' => REQUEST_TIME + 4]);
33 // Create a page. This should never appear in the view created below.
34 $page_node = $this->drupalCreateNode(['type' => 'page', 'created' => REQUEST_TIME + 2]);
36 // Create a view that sorts newest first, and shows 4 items in the page and
39 $view['label'] = $this->randomMachineName(16);
40 $view['id'] = strtolower($this->randomMachineName(16));
41 $view['description'] = $this->randomMachineName(16);
42 $view['show[wizard_key]'] = 'node';
43 $view['show[type]'] = 'article';
44 $view['show[sort]'] = 'node_field_data-created:DESC';
45 $view['page[create]'] = 1;
46 $view['page[title]'] = $this->randomMachineName(16);
47 $view['page[path]'] = $this->randomMachineName(16);
48 $view['page[items_per_page]'] = 4;
49 $view['block[create]'] = 1;
50 $view['block[title]'] = $this->randomMachineName(16);
51 $view['block[items_per_page]'] = 3;
52 $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
53 $this->drupalGet($view['page[path]']);
54 $this->assertResponse(200);
56 // Make sure the page display shows the nodes we expect, and that they
57 // appear in the expected order.
58 $this->assertUrl($view['page[path]']);
59 $this->assertText($view['page[title]']);
60 $content = $this->getSession()->getPage()->getContent();
61 $this->assertText($node5->label());
62 $this->assertText($node4->label());
63 $this->assertText($node3->label());
64 $this->assertText($node2->label());
65 $this->assertNoText($node1->label());
66 $this->assertNoText($page_node->label());
67 $pos5 = strpos($content, $node5->label());
68 $pos4 = strpos($content, $node4->label());
69 $pos3 = strpos($content, $node3->label());
70 $pos2 = strpos($content, $node2->label());
71 $this->assertTrue($pos5 < $pos4 && $pos4 < $pos3 && $pos3 < $pos2, 'The nodes appear in the expected order in the page display.');
73 // Confirm that the block is listed in the block administration UI.
74 $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
75 $this->clickLink('Place block');
76 $this->assertText($view['label']);
78 // Place the block, visit a page that displays the block, and check that the
79 // nodes we expect appear in the correct order.
80 $this->drupalPlaceBlock("views_block:{$view['id']}-block_1");
82 $this->drupalGet('user');
83 $content = $this->getSession()->getPage()->getContent();
84 $this->assertText($node5->label());
85 $this->assertText($node4->label());
86 $this->assertText($node3->label());
87 $this->assertNoText($node2->label());
88 $this->assertNoText($node1->label());
89 $this->assertNoText($page_node->label());
90 $pos5 = strpos($content, $node5->label());
91 $pos4 = strpos($content, $node4->label());
92 $pos3 = strpos($content, $node3->label());
93 $this->assertTrue($pos5 < $pos4 && $pos4 < $pos3, 'The nodes appear in the expected order in the block display.');