annotate core/modules/node/tests/src/Kernel/SummaryLengthTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\node\Kernel;
Chris@0 4
Chris@0 5 use Drupal\Core\Datetime\Entity\DateFormat;
Chris@0 6 use Drupal\KernelTests\KernelTestBase;
Chris@0 7 use Drupal\node\Entity\Node;
Chris@0 8 use Drupal\Tests\EntityViewTrait;
Chris@18 9 use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
Chris@18 10 use Drupal\Tests\node\Traits\NodeCreationTrait;
Chris@18 11 use Drupal\Tests\user\Traits\UserCreationTrait;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Tests summary length.
Chris@0 15 *
Chris@0 16 * @group node
Chris@0 17 */
Chris@0 18 class SummaryLengthTest extends KernelTestBase {
Chris@0 19
Chris@0 20 use NodeCreationTrait {
Chris@0 21 getNodeByTitle as drupalGetNodeByTitle;
Chris@0 22 createNode as drupalCreateNode;
Chris@0 23 }
Chris@0 24 use UserCreationTrait {
Chris@0 25 createUser as drupalCreateUser;
Chris@0 26 createRole as drupalCreateRole;
Chris@0 27 createAdminRole as drupalCreateAdminRole;
Chris@0 28 }
Chris@0 29 use ContentTypeCreationTrait {
Chris@0 30 createContentType as drupalCreateContentType;
Chris@0 31 }
Chris@0 32 use EntityViewTrait {
Chris@0 33 buildEntityView as drupalBuildEntityView;
Chris@0 34 }
Chris@0 35
Chris@0 36 /**
Chris@0 37 * {@inheritdoc}
Chris@0 38 */
Chris@0 39 public static $modules = [
Chris@0 40 'node',
Chris@0 41 'datetime',
Chris@0 42 'user',
Chris@0 43 'system',
Chris@0 44 'filter',
Chris@0 45 'field',
Chris@0 46 'text',
Chris@0 47 ];
Chris@0 48
Chris@0 49 /**
Chris@0 50 * {@inheritdoc}
Chris@0 51 */
Chris@0 52 protected function setUp() {
Chris@0 53 parent::setUp();
Chris@0 54 $this->installSchema('system', 'sequences');
Chris@0 55 $this->installSchema('node', 'node_access');
Chris@0 56 $this->installEntitySchema('user');
Chris@0 57 $this->installEntitySchema('node');
Chris@0 58 $this->installEntitySchema('date_format');
Chris@0 59 $this->installConfig('filter');
Chris@0 60 $this->installConfig('node');
Chris@0 61
Chris@0 62 // Create a node type.
Chris@0 63 $this->drupalCreateContentType([
Chris@0 64 'type' => 'page',
Chris@0 65 'name' => 'Basic page',
Chris@0 66 'display_submitted' => FALSE,
Chris@0 67 ]);
Chris@0 68
Chris@0 69 DateFormat::create([
Chris@0 70 'id' => 'fallback',
Chris@0 71 'label' => 'Fallback',
Chris@0 72 'pattern' => 'Y-m-d',
Chris@0 73 ])->save();
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Tests the node summary length functionality.
Chris@0 78 */
Chris@0 79 public function testSummaryLength() {
Chris@0 80 /** @var \Drupal\Core\Render\RendererInterface $renderer */
Chris@0 81 $renderer = $this->container->get('renderer');
Chris@0 82
Chris@0 83 // Create a node to view.
Chris@0 84 $settings = [
Chris@0 85 '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.']],
Chris@0 86 'promote' => 1,
Chris@0 87 ];
Chris@0 88 $node = $this->drupalCreateNode($settings);
Chris@0 89 $this->assertTrue(Node::load($node->id()), 'Node created.');
Chris@0 90
Chris@0 91 // Render the node as a teaser.
Chris@0 92 $content = $this->drupalBuildEntityView($node, 'teaser');
Chris@0 93 $this->assertTrue(strlen($content['body'][0]['#markup']) < 600, 'Teaser is less than 600 characters long.');
Chris@0 94 $this->setRawContent($renderer->renderRoot($content));
Chris@0 95 // The string 'What is a Drupalism?' is between the 200th and 600th
Chris@0 96 // characters of the node body, so it should be included if the summary is
Chris@0 97 // 600 characters long.
Chris@0 98 $expected = 'What is a Drupalism?';
Chris@0 99 $this->assertRaw($expected);
Chris@0 100
Chris@0 101 // Change the teaser length for "Basic page" content type.
Chris@0 102 $display = entity_get_display('node', $node->getType(), 'teaser');
Chris@0 103 $display_options = $display->getComponent('body');
Chris@0 104 $display_options['settings']['trim_length'] = 200;
Chris@0 105 $display->setComponent('body', $display_options)
Chris@0 106 ->save();
Chris@0 107
Chris@0 108 // Render the node as a teaser again and check that the summary is now only
Chris@0 109 // 200 characters in length and so does not include 'What is a Drupalism?'.
Chris@0 110 $content = $this->drupalBuildEntityView($node, 'teaser');
Chris@0 111 $this->assertTrue(strlen($content['body'][0]['#markup']) < 200, 'Teaser is less than 200 characters long.');
Chris@0 112 $this->setRawContent($renderer->renderRoot($content));
Chris@0 113 $this->assertText($node->label());
Chris@0 114 $this->assertNoRaw($expected);
Chris@0 115 }
Chris@0 116
Chris@0 117 }