outline-8.x-1.x-dev/tests/src/Kernel/PendingRevisionTest.php
tests/src/Kernel/PendingRevisionTest.php
<?php
namespace Drupal\Tests\outline\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\outline\Entity\Entry;
use Drupal\outline\Entity\Outline;
/**
* Kernel tests for outline pending revisions.
*
* @group outline
*/
class PendingRevisionTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'outline',
'node',
'user',
'text',
'field',
'system',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installEntitySchema('outline_entry');
$this->installSchema('node', 'node_access');
}
/**
* Tests that the outline index work correctly with pending revisions.
*/
public function testOutlineIndexWithPendingRevision() {
\Drupal::configFactory()->getEditable('outline.settings')->set('maintain_index_table', TRUE)->save();
Outline::create([
'name' => 'test',
'oid' => 'test',
])->save();
$entry = Entry::create([
'name' => 'entry1',
'oid' => 'test',
]);
$entry->save();
$entry2 = Entry::create([
'name' => 'entry2',
'oid' => 'test',
]);
$entry2->save();
NodeType::create([
'type' => 'page',
])->save();
FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'field_tags',
'type' => 'entity_reference',
'settings' => [
'target_type' => 'outline_entry',
],
])->save();
FieldConfig::create([
'field_name' => 'field_tags',
'entity_type' => 'node',
'bundle' => 'page',
])->save();
$node = Node::create([
'type' => 'page',
'title' => 'test_title',
'field_tags' => [$entry->id()],
]);
$node->save();
$outline_index = $this->getOutlineIndex();
$this->assertEquals($entry->id(), $outline_index[$node->id()]->tid);
// Normal new revision.
$node->setNewRevision(TRUE);
$node->isDefaultRevision(TRUE);
$node->field_tags->target_id = $entry2->id();
$node->save();
$outline_index = $this->getOutlineIndex();
$this->assertEquals($entry2->id(), $outline_index[$node->id()]->tid);
// Check that saving a pending revision does not affect the outline index.
$node->setNewRevision(TRUE);
$node->isDefaultRevision(FALSE);
$node->field_tags->target_id = $entry->id();
$node->save();
$outline_index = $this->getOutlineIndex();
$this->assertEquals($entry2->id(), $outline_index[$node->id()]->tid);
// Check that making the previously created pending revision the default
// revision updates the outline index correctly.
$node->isDefaultRevision(TRUE);
$node->save();
$outline_index = $this->getOutlineIndex();
$this->assertEquals($entry->id(), $outline_index[$node->id()]->tid);
}
protected function getOutlineIndex() {
return \Drupal::database()->select('outline_index')
->fields('outline_index')
->execute()
->fetchAllAssoc('nid');
}
}
