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

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 namespace Drupal\Tests\node\Kernel;
Chris@14 4
Chris@14 5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
Chris@14 6 use Drupal\node\Entity\Node;
Chris@14 7 use Drupal\node\Entity\NodeType;
Chris@14 8 use Drupal\user\Entity\User;
Chris@14 9
Chris@14 10 /**
Chris@14 11 * Tests the node view builder.
Chris@14 12 *
Chris@14 13 * @group node
Chris@14 14 *
Chris@14 15 * @coversDefaultClass \Drupal\node\NodeViewBuilder
Chris@14 16 */
Chris@14 17 class NodeViewBuilderTest extends EntityKernelTestBase {
Chris@14 18
Chris@14 19 /**
Chris@14 20 * {@inheritdoc}
Chris@14 21 */
Chris@14 22 public static $modules = ['node'];
Chris@14 23
Chris@14 24 /**
Chris@14 25 * The node storage.
Chris@14 26 *
Chris@14 27 * @var \Drupal\node\NodeStorageInterface
Chris@14 28 */
Chris@14 29 protected $storage;
Chris@14 30
Chris@14 31 /**
Chris@14 32 * The node view builder.
Chris@14 33 *
Chris@14 34 * @var \Drupal\Core\Entity\EntityViewBuilderInterface
Chris@14 35 */
Chris@14 36 protected $viewBuilder;
Chris@14 37
Chris@14 38 /**
Chris@14 39 * The renderer.
Chris@14 40 *
Chris@14 41 * @var \Drupal\Core\Render\RendererInterface
Chris@14 42 */
Chris@14 43 protected $renderer;
Chris@14 44
Chris@14 45 /**
Chris@14 46 * {@inheritdoc}
Chris@14 47 */
Chris@14 48 protected function setUp() {
Chris@14 49 parent::setUp();
Chris@14 50
Chris@14 51 $this->storage = $this->entityManager->getStorage('node');
Chris@14 52 $this->viewBuilder = $this->entityManager->getViewBuilder('node');
Chris@14 53 $this->renderer = $this->container->get('renderer');
Chris@14 54
Chris@14 55 $type = NodeType::create([
Chris@14 56 'type' => 'article',
Chris@14 57 'name' => 'Article',
Chris@14 58 ]);
Chris@14 59 $type->save();
Chris@14 60
Chris@14 61 $this->installSchema('node', 'node_access');
Chris@14 62 $this->installConfig(['system', 'node']);
Chris@14 63 }
Chris@14 64
Chris@14 65 /**
Chris@14 66 * Tests that node links are displayed correctly in pending revisions.
Chris@14 67 *
Chris@14 68 * @covers ::buildComponents
Chris@14 69 * @covers ::renderLinks
Chris@14 70 * @covers ::buildLinks
Chris@14 71 */
Chris@14 72 public function testPendingRevisionLinks() {
Chris@14 73 $account = User::create([
Chris@14 74 'name' => $this->randomString(),
Chris@14 75 ]);
Chris@14 76 $account->save();
Chris@14 77
Chris@14 78 $title = $this->randomMachineName();
Chris@14 79 $node = Node::create([
Chris@14 80 'type' => 'article',
Chris@14 81 'title' => $title,
Chris@14 82 'uid' => $account->id(),
Chris@14 83 ]);
Chris@14 84 $node->save();
Chris@14 85
Chris@14 86 /** @var \Drupal\node\NodeInterface $pending_revision */
Chris@14 87 $pending_revision = $this->storage->createRevision($node, FALSE);
Chris@14 88 $draft_title = $title . ' draft';
Chris@14 89 $pending_revision->setTitle($draft_title);
Chris@14 90 $pending_revision->save();
Chris@14 91
Chris@14 92 $build = $this->viewBuilder->view($node, 'teaser');
Chris@14 93 $output = (string) $this->renderer->renderPlain($build);
Chris@14 94 $this->assertContains("title=\"$title\"", $output);
Chris@14 95
Chris@14 96 $build = $this->viewBuilder->view($pending_revision, 'teaser');
Chris@14 97 $output = (string) $this->renderer->renderPlain($build);
Chris@14 98 $this->assertContains("title=\"$draft_title\"", $output);
Chris@14 99 }
Chris@14 100
Chris@14 101 }