View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
class NodeRevisionsUiTest extends NodeTestBase {
protected $defaultTheme = 'stark';
protected $editor;
protected function setUp() : void {
parent::setUp();
$this->editor = $this
->drupalCreateUser([
'administer nodes',
'edit any page content',
'view page revisions',
'access user profiles',
]);
}
public function testNodeFormSaveWithoutRevision() {
$this
->drupalLogin($this->editor);
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$type = NodeType::load('page');
$type
->setNewRevision(TRUE);
$type
->save();
$node = $this
->drupalCreateNode();
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->checkboxChecked('edit-revision');
$edit = [
'revision' => FALSE,
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$node_storage
->resetCache([
$node
->id(),
]);
$node_revision = $node_storage
->load($node
->id(), TRUE);
$this
->assertEquals($node
->getRevisionId(), $node_revision
->getRevisionId(), "After an existing node is saved with 'Create new revision' unchecked, a new revision is not created.");
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->checkboxChecked('edit-revision');
$edit = [];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$node_storage
->resetCache([
$node
->id(),
]);
$node_revision = $node_storage
->load($node
->id());
$this
->assertNotEquals($node
->getRevisionId(), $node_revision
->getRevisionId(), "After an existing node is saved with 'Create new revision' checked, a new revision is created.");
}
public function testNodeRevisionDoubleEscapeFix() {
$this
->drupalLogin($this->editor);
$nodes = [];
$node = $this
->drupalCreateNode();
$username = [
'#theme' => 'username',
'#account' => $this->editor,
];
$editor = \Drupal::service('renderer')
->renderPlain($username);
$nodes[] = clone $node;
$node->title = $this
->randomMachineName();
$node->body = [
'value' => $this
->randomMachineName(32),
'format' => filter_default_format(),
];
$node
->setNewRevision();
$revision_log = 'Revision <em>message</em> with markup.';
$node->revision_log->value = $revision_log;
$node
->save();
$node = Node::load($node
->id());
$nodes[] = clone $node;
$this
->drupalGet('node/' . $node
->id() . '/revisions');
$date = $this->container
->get('date.formatter')
->format($nodes[0]->revision_timestamp->value, 'short');
$url = new Url('entity.node.revision', [
'node' => $nodes[0]
->id(),
'node_revision' => $nodes[0]
->getRevisionId(),
]);
$this
->assertSession()
->responseContains(Link::fromTextAndUrl($date, $url)
->toString() . ' by ' . $editor);
$date = $this->container
->get('date.formatter')
->format($nodes[1]->revision_timestamp->value, 'short');
$this
->assertSession()
->responseContains($nodes[1]
->toLink($date)
->toString() . ' by ' . $editor . '<p class="revision-log">' . $revision_log . '</p>');
}
public function testNodeRevisionsTabWithDefaultRevision() {
$this
->drupalLogin($this->editor);
$node = $this
->drupalCreateNode();
$storage = \Drupal::entityTypeManager()
->getStorage($node
->getEntityTypeId());
$node = $storage
->load($node
->id());
$node
->setNewRevision(TRUE);
$node
->save();
$node = $storage
->load($node
->id());
$node
->setNewRevision(TRUE);
$node
->save();
$node = $storage
->load($node
->id());
$node
->setTitle($this
->randomString());
$node
->isDefaultRevision(FALSE);
$node
->setNewRevision(TRUE);
$node
->save();
$node = $storage
->load($node
->id());
$node
->isDefaultRevision(FALSE);
$node
->setNewRevision(TRUE);
$node
->save();
$node_id = $node
->id();
$this
->drupalGet('node/' . $node_id . '/revisions');
$this
->assertSession()
->linkByHrefNotExists('/node/' . $node_id . '/revisions/1/revert');
$this
->assertSession()
->elementAttributeContains('xpath', '//tr[contains(@class, "revision-current")]/td/a[1]', 'href', '/node/1');
$this
->assertSession()
->linkByHrefExists('/node/' . $node_id . '/revisions/4/revert');
$this
->assertSession()
->linkByHrefNotExists('/node/' . $node_id . '/revisions/2/revert');
$this
->assertSession()
->linkByHrefNotExists('/node/' . $node_id . '/revisions/3/revert');
$this
->assertSession()
->linkByHrefNotExists('/node/' . $node_id . '/revisions/5/revert');
}
}