NodeViewBuilderTest.php in Drupal 10
File
core/modules/node/tests/src/Kernel/NodeViewBuilderTest.php
View source
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\User;
class NodeViewBuilderTest extends EntityKernelTestBase {
protected static $modules = [
'node',
];
protected $storage;
protected $viewBuilder;
protected $renderer;
protected function setUp() : void {
parent::setUp();
$this->storage = $this->entityTypeManager
->getStorage('node');
$this->viewBuilder = $this->entityTypeManager
->getViewBuilder('node');
$this->renderer = $this->container
->get('renderer');
$type = NodeType::create([
'type' => 'article',
'name' => 'Article',
]);
$type
->save();
$this
->installSchema('node', 'node_access');
$this
->installConfig([
'system',
'node',
]);
}
public function testPendingRevisionLinks() {
$account = User::create([
'name' => $this
->randomString(),
]);
$account
->save();
$title = $this
->randomMachineName();
$node = Node::create([
'type' => 'article',
'title' => $title,
'uid' => $account
->id(),
]);
$node
->save();
$pending_revision = $this->storage
->createRevision($node, FALSE);
$draft_title = $title . ' draft';
$pending_revision
->setTitle($draft_title);
$pending_revision
->save();
$build = $this->viewBuilder
->view($node, 'teaser');
$output = (string) $this->renderer
->renderPlain($build);
$this
->assertStringContainsString("title=\"{$title}\"", $output);
$build = $this->viewBuilder
->view($pending_revision, 'teaser');
$output = (string) $this->renderer
->renderPlain($build);
$this
->assertStringContainsString("title=\"{$draft_title}\"", $output);
}
}