View source
<?php
namespace Drupal\Tests\nopremium\Kernel;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
class NodeViewTest extends NopremiumKernelTestBase {
protected $viewBuilder;
protected $renderer;
protected function setUp() {
parent::setUp();
$content_admin_user = $this
->createUser([
'uid' => 2,
], [
'administer nodes',
]);
$this->container
->get('entity_type.manager')
->getStorage('entity_view_display')
->load('node.article.teaser')
->removeComponent('body')
->save();
$this->viewBuilder = $this->entityTypeManager
->getViewBuilder('node');
$this->renderer = $this->container
->get('renderer');
}
protected function createNodeWithBodyValue($body, array $values = []) {
$values += [
'type' => 'article',
'body' => [
[
'value' => $body,
'format' => filter_default_format(),
],
],
'uid' => 2,
];
return $this
->createNode($values);
}
protected function createViewModeAndDisplay($view_mode) {
EntityViewMode::create([
'id' => 'node.' . $view_mode,
'targetEntityType' => 'node',
])
->save();
EntityViewDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'article',
'mode' => $view_mode,
'status' => TRUE,
])
->setComponent('body')
->save();
}
public function testViewPremiumNode() {
$node = $this
->createNodeWithBodyValue('Lorem ipsum', [
'premium' => TRUE,
]);
$build = $this->viewBuilder
->view($node, 'full');
$output = (string) $this->renderer
->renderPlain($build);
$this
->assertStringContainsString('The full content of this page is available to premium users only.', $output);
$this
->assertStringNotContainsString('Lorem ipsum', $output);
}
public function testWithCustomViewMode() {
$this
->createViewModeAndDisplay('foo');
$node = $this
->createNodeWithBodyValue('Lorem ipsum', [
'premium' => TRUE,
]);
$build = $this->viewBuilder
->view($node, 'foo');
$output = (string) $this->renderer
->renderPlain($build);
$this
->assertStringContainsString('The full content of this page is available to premium users only.', $output);
$this
->assertStringNotContainsString('Lorem ipsum', $output);
}
public function testViewPremiumNodeInTeaserViewMode() {
$node = $this
->createNodeWithBodyValue('Lorem ipsum', [
'premium' => TRUE,
]);
$build = $this->viewBuilder
->view($node, 'teaser');
$output = (string) $this->renderer
->renderPlain($build);
$this
->assertStringContainsString('Read more', $output);
}
public function testViewNonPremiumNodeWithCustomViewMode() {
$this
->createViewModeAndDisplay('foo');
$node = $this
->createNodeWithBodyValue('Lorem ipsum');
$build = $this->viewBuilder
->view($node, 'foo');
$output = (string) $this->renderer
->renderPlain($build);
$this
->assertStringNotContainsString('The full content of this page is available to premium users only.', $output);
$this
->assertStringContainsString('Lorem ipsum', $output);
}
public function testViewModesSetting() {
$this
->createViewModeAndDisplay('foo');
$this
->createViewModeAndDisplay('bar');
$this
->config('nopremium.settings')
->set('view_modes', [
'foo' => 'foo',
])
->save();
$node = $this
->createNodeWithBodyValue('Lorem ipsum', [
'premium' => TRUE,
]);
$build = $this->viewBuilder
->view($node, 'foo');
$output = (string) $this->renderer
->renderPlain($build);
$this
->assertStringContainsString('The full content of this page is available to premium users only.', $output);
$this
->assertStringNotContainsString('Lorem ipsum', $output);
$build = $this->viewBuilder
->view($node, 'bar');
$output = (string) $this->renderer
->renderPlain($build);
$this
->assertStringNotContainsString('The full content of this page is available to premium users only.', $output);
$this
->assertStringContainsString('Lorem ipsum', $output);
}
}