View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\preview_link\Functional;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\preview_link\Entity\PreviewLink;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\workflows\Entity\Workflow;
class PreviewLinkForwardRevisionTest extends BrowserTestBase {
use ContentModerationTestTrait;
protected $defaultTheme = 'classy';
public static $modules = [
'preview_link',
'node',
'text',
'filter',
'content_moderation',
'paragraphs',
'entity_reference_revisions',
];
public function setUp() {
parent::setUp();
$this
->createEditorialWorkflow();
$this
->createContentType([
'type' => 'page',
]);
$workflow = Workflow::load('editorial');
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'page');
$workflow
->save();
}
public function testForwardRevision() : void {
$original_random_text = 'Original Title';
$latest_random_text = 'Latest Title';
$node = $this
->createNode([
'title' => $original_random_text,
'moderation_state' => 'published',
]);
$node
->setTitle($latest_random_text);
$node->moderation_state = 'draft';
$node
->save();
$previewLink = PreviewLink::create()
->addEntity($node);
$previewLink
->save();
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->pageTextNotContains($latest_random_text);
$this
->assertSession()
->pageTextContains($original_random_text);
$this
->drupalGet($previewLink
->getUrl($node));
$this
->assertSession()
->pageTextContains($latest_random_text);
$this
->assertSession()
->pageTextNotContains($original_random_text);
}
public function testDraftRevisionWithParagraphField() : void {
$this
->setupParagraphTypeAndField();
$paragraph = Paragraph::create([
'type' => 'section',
'section' => 'A section title',
'status' => 1,
]);
$paragraph
->save();
$node = $this
->createNode([
'title' => 'A draft with paragraph content',
'paragraphs' => [
$paragraph,
],
'moderation_state' => 'draft',
]);
$previewLink = PreviewLink::create()
->addEntity($node);
$previewLink
->save();
$this
->drupalLogin($this
->drupalCreateUser(array_keys($this->container
->get('user.permissions')
->getPermissions())));
$this
->drupalGet($node
->toUrl());
$assert = $this
->assertSession();
$assert
->pageTextContains('A section title');
$this
->drupalLogout();
$this
->drupalGet($previewLink
->getUrl($node));
$assert
->pageTextContains('A section title');
}
protected function setupParagraphTypeAndField() : void {
$paragraph_type = ParagraphsType::create([
'id' => 'section',
'label' => 'Section',
]);
$paragraph_type
->save();
$storage = FieldStorageConfig::create([
'entity_type' => 'paragraph',
'type' => 'text',
'field_name' => 'section',
]);
$storage
->save();
$field = FieldConfig::create([
'entity_type' => 'paragraph',
'field_name' => 'section',
'bundle' => 'section',
'type' => 'text',
'label' => 'Section',
]);
$field
->save();
$field_storage = FieldStorageConfig::create([
'field_name' => 'paragraphs',
'entity_type' => 'node',
'type' => 'entity_reference_revisions',
'cardinality' => '-1',
'settings' => [
'target_type' => 'paragraph',
],
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'page',
'field_name' => 'paragraphs',
'settings' => [
'handler' => 'default:paragraph',
],
]);
$field
->save();
$view_display = EntityViewDisplay::load('node.page.default')
->setComponent('paragraphs', [
'type' => 'entity_reference_revisions_entity_view',
]);
$view_display
->save();
$view_display = EntityViewDisplay::create([
'targetEntityType' => 'paragraph',
'bundle' => 'section',
'mode' => 'default',
'status' => TRUE,
])
->setComponent('section', [
'type' => 'text_default',
]);
$view_display
->save();
}
}