Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / node / tests / src / Kernel / SummaryLengthTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Kernel;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Datetime\Entity\DateFormat;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\node\Entity\Node;
9 use Drupal\simpletest\ContentTypeCreationTrait;
10 use Drupal\simpletest\NodeCreationTrait;
11 use Drupal\simpletest\UserCreationTrait;
12 use Drupal\Tests\EntityViewTrait;
13
14 /**
15  * Tests summary length.
16  *
17  * @group node
18  */
19 class SummaryLengthTest extends KernelTestBase {
20
21   use NodeCreationTrait {
22     getNodeByTitle as drupalGetNodeByTitle;
23     createNode as drupalCreateNode;
24   }
25   use UserCreationTrait {
26     createUser as drupalCreateUser;
27     createRole as drupalCreateRole;
28     createAdminRole as drupalCreateAdminRole;
29   }
30   use ContentTypeCreationTrait {
31     createContentType as drupalCreateContentType;
32   }
33   use EntityViewTrait {
34     buildEntityView as drupalBuildEntityView;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static $modules = [
41     'node',
42     'datetime',
43     'user',
44     'system',
45     'filter',
46     'field',
47     'text',
48   ];
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function setUp() {
54     parent::setUp();
55     $this->installSchema('system', 'sequences');
56     $this->installSchema('node', 'node_access');
57     $this->installEntitySchema('user');
58     $this->installEntitySchema('node');
59     $this->installEntitySchema('date_format');
60     $this->installConfig('filter');
61     $this->installConfig('node');
62
63     // Create a node type.
64     $this->drupalCreateContentType([
65       'type' => 'page',
66       'name' => 'Basic page',
67       'display_submitted' => FALSE,
68     ]);
69
70     DateFormat::create([
71       'id' => 'fallback',
72       'label' => 'Fallback',
73       'pattern' => 'Y-m-d',
74     ])->save();
75
76     // Enable multibyte support.
77     Unicode::setStatus(Unicode::STATUS_MULTIBYTE);
78   }
79
80   /**
81    * Tests the node summary length functionality.
82    */
83   public function testSummaryLength() {
84     /** @var \Drupal\Core\Render\RendererInterface $renderer */
85     $renderer = $this->container->get('renderer');
86
87     // Create a node to view.
88     $settings = [
89       'body' => [['value' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae arcu at leo cursus laoreet. Curabitur dui tortor, adipiscing malesuada tempor in, bibendum ac diam. Cras non tellus a libero pellentesque condimentum. What is a Drupalism? Suspendisse ac lacus libero. Ut non est vel nisl faucibus interdum nec sed leo. Pellentesque sem risus, vulputate eu semper eget, auctor in libero. Ut fermentum est vitae metus convallis scelerisque. Phasellus pellentesque rhoncus tellus, eu dignissim purus posuere id. Quisque eu fringilla ligula. Morbi ullamcorper, lorem et mattis egestas, tortor neque pretium velit, eget eleifend odio turpis eu purus. Donec vitae metus quis leo pretium tincidunt a pulvinar sem. Morbi adipiscing laoreet mauris vel placerat. Nullam elementum, nisl sit amet scelerisque malesuada, dolor nunc hendrerit quam, eu ultrices erat est in orci. Curabitur feugiat egestas nisl sed accumsan.']],
90       'promote' => 1,
91     ];
92     $node = $this->drupalCreateNode($settings);
93     $this->assertTrue(Node::load($node->id()), 'Node created.');
94
95     // Render the node as a teaser.
96     $content = $this->drupalBuildEntityView($node, 'teaser');
97     $this->assertTrue(strlen($content['body'][0]['#markup']) < 600, 'Teaser is less than 600 characters long.');
98     $this->setRawContent($renderer->renderRoot($content));
99     // The string 'What is a Drupalism?' is between the 200th and 600th
100     // characters of the node body, so it should be included if the summary is
101     // 600 characters long.
102     $expected = 'What is a Drupalism?';
103     $this->assertRaw($expected);
104
105     // Change the teaser length for "Basic page" content type.
106     $display = entity_get_display('node', $node->getType(), 'teaser');
107     $display_options = $display->getComponent('body');
108     $display_options['settings']['trim_length'] = 200;
109     $display->setComponent('body', $display_options)
110       ->save();
111
112     // Render the node as a teaser again and check that the summary is now only
113     // 200 characters in length and so does not include 'What is a Drupalism?'.
114     $content = $this->drupalBuildEntityView($node, 'teaser');
115     $this->assertTrue(strlen($content['body'][0]['#markup']) < 200, 'Teaser is less than 200 characters long.');
116     $this->setRawContent($renderer->renderRoot($content));
117     $this->assertText($node->label());
118     $this->assertNoRaw($expected);
119   }
120
121 }