View source
<?php
namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\entity_test\Entity\EntityTestNoBundle;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
use Drupal\workflows\Entity\Workflow;
class ViewsModerationStateFilterTest extends ViewsKernelTestBase {
use ContentModerationTestTrait;
use UserCreationTrait;
protected static $modules = [
'content_moderation_test_views',
'node',
'content_moderation',
'workflows',
'workflow_type_test',
'entity_test',
'language',
'content_translation',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp(FALSE);
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('content_moderation_state');
$this
->installEntitySchema('entity_test_no_bundle');
$this
->installSchema('node', 'node_access');
$this
->installConfig('content_moderation');
$node_type = NodeType::create([
'type' => 'example',
]);
$node_type
->save();
$node_type = NodeType::create([
'type' => 'another_example',
]);
$node_type
->save();
$node_type = NodeType::create([
'type' => 'example_non_moderated',
]);
$node_type
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$this
->installConfig('content_moderation_test_views');
ConfigurableLanguage::createFromLangcode('fr')
->save();
}
public function testStateFilterViewsRelationship() {
$workflow = Workflow::load('editorial');
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->getTypePlugin()
->addState('translated_draft', 'Bar');
$configuration = $workflow
->getTypePlugin()
->getConfiguration();
$configuration['states']['translated_draft'] += [
'published' => FALSE,
'default_revision' => FALSE,
];
$workflow
->getTypePlugin()
->setConfiguration($configuration);
$workflow
->save();
$node = Node::create([
'type' => 'example',
'title' => 'Test Node',
'moderation_state' => 'published',
]);
$node
->save();
$node
->setNewRevision();
$node->moderation_state = 'draft';
$node
->save();
$second_node = Node::create([
'type' => 'example',
'title' => 'Second Node',
'moderation_state' => 'draft',
]);
$second_node
->save();
$third_node = Node::create([
'type' => 'example',
'title' => 'Third node',
'moderation_state' => 'published',
]);
$third_node
->save();
$fourth_node = Node::create([
'type' => 'example_non_moderated',
'title' => 'Fourth node',
]);
$fourth_node
->save();
$translated_forward_revision = $third_node
->addTranslation('fr');
$translated_forward_revision->title = 'Translated Node';
$translated_forward_revision
->setNewRevision(TRUE);
$translated_forward_revision->moderation_state = 'translated_draft';
$translated_forward_revision
->save();
$base_table_views = [
'test_content_moderation_state_filter_base_table',
'test_content_moderation_state_filter_base_table_filter_group_or',
];
foreach ($base_table_views as $view_id) {
$this
->assertNodesWithFilters([
$node,
$second_node,
$third_node,
], [], $view_id);
$this
->assertNodesWithFilters([
$node,
$third_node,
], [
'default_revision_state' => 'editorial-published',
], $view_id);
$this
->assertNodesWithFilters([
$second_node,
], [
'default_revision_state' => 'editorial-draft',
], $view_id);
}
$this
->assertNodesWithFilters([
$node,
$second_node,
], [
'moderation_state' => 'editorial-draft',
], 'test_content_moderation_state_filter_revision_table');
$this
->assertNodesWithFilters([
$node,
$third_node,
$third_node,
], [
'moderation_state' => 'editorial-published',
], 'test_content_moderation_state_filter_revision_table');
$this
->assertNodesWithFilters([
$translated_forward_revision,
], [
'moderation_state' => 'editorial-translated_draft',
], 'test_content_moderation_state_filter_revision_table');
}
public function testNonTranslatableEntityType() {
$workflow = Workflow::load('editorial');
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('entity_test_no_bundle', 'entity_test_no_bundle');
$workflow
->save();
$test_entity = EntityTestNoBundle::create([
'moderation_state' => 'draft',
]);
$test_entity
->save();
$view = Views::getView('test_content_moderation_state_filter_entity_test');
$view
->setExposedInput([
'moderation_state' => 'editorial-draft',
]);
$view
->execute();
$this
->assertIdenticalResultset($view, [
[
'id' => $test_entity
->id(),
],
], [
'id' => 'id',
]);
}
public function testModerationStateFilterOnJoinedEntity() {
$workflow = Workflow::load('editorial');
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$user = $this
->createUser([], 'Test user');
$node = Node::create([
'type' => 'example',
'title' => 'Test node',
'moderation_state' => 'published',
'uid' => $user
->id(),
]);
$node
->save();
$view = Views::getView('test_content_moderation_filter_via_relationship');
$view
->setExposedInput([
'moderation_state' => 'editorial-published',
]);
$view
->execute();
$this
->assertIdenticalResultset($view, [
[
'name' => 'Test user',
'title' => 'Test node',
'moderation_state' => 'published',
],
], [
'name' => 'name',
'title' => 'title',
'moderation_state' => 'moderation_state',
]);
$view = Views::getView('test_content_moderation_filter_via_relationship');
$view
->setExposedInput([
'moderation_state' => 'editorial-draft',
]);
$view
->execute();
$this
->assertIdenticalResultset($view, [], [
'name' => 'name',
]);
}
public function testStateFilterStatesList() {
$workflow = Workflow::load('editorial');
$workflow
->getTypePlugin()
->removeEntityTypeAndBundle('node', 'example');
$workflow
->save();
$this
->assertPluginStates([]);
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$this
->assertPluginStates([
'Editorial' => [
'editorial-draft' => 'Draft',
'editorial-published' => 'Published',
'editorial-archived' => 'Archived',
],
]);
$workflow = Workflow::create([
'id' => 'test',
'type' => 'workflow_type_complex_test',
]);
$workflow
->getTypePlugin()
->addState('draft', 'Draft');
$workflow
->save();
$this
->assertPluginStates([
'Editorial' => [
'editorial-draft' => 'Draft',
'editorial-published' => 'Published',
'editorial-archived' => 'Archived',
],
]);
$workflow = Workflow::create([
'id' => 'moderation_test',
'type' => 'content_moderation',
'label' => 'Moderation test',
]);
$workflow
->getTypePlugin()
->addState('foo', 'Foo State');
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'example');
$workflow
->save();
$this
->assertPluginStates([
'Editorial' => [
'editorial-draft' => 'Draft',
'editorial-published' => 'Published',
'editorial-archived' => 'Archived',
],
'Moderation test' => [
'moderation_test-foo' => 'Foo State',
'moderation_test-draft' => 'Draft',
'moderation_test-published' => 'Published',
],
]);
$workflow = Workflow::load('moderation_test');
$workflow
->delete();
$this
->assertPluginStates([
'Editorial' => [
'editorial-draft' => 'Draft',
'editorial-published' => 'Published',
'editorial-archived' => 'Archived',
],
]);
$workflow = Workflow::load('editorial');
$workflow
->getTypePlugin()
->deleteState('archived');
$workflow
->save();
$this
->assertPluginStates([
'Editorial' => [
'editorial-draft' => 'Draft',
'editorial-published' => 'Published',
],
]);
}
protected function assertPluginStates($states) {
$plugin = Views::pluginManager('filter')
->createInstance('moderation_state_filter', []);
$view = Views::getView('test_content_moderation_state_filter_base_table');
$plugin
->init($view, $view
->getDisplay());
$this
->assertEquals($states, $plugin
->getValueOptions());
}
protected function assertNodesWithFilters(array $nodes, array $filters, $view_id = 'test_content_moderation_state_filter_base_table') {
$view = Views::getView($view_id);
$view
->setExposedInput($filters);
$view
->execute();
$query = $view
->getQuery();
$join = $query
->getTableInfo('content_moderation_state')['join'];
$configuration = $join->configuration;
$this
->assertEquals('content_moderation_state_field_revision', $configuration['table']);
$this
->assertEquals('content_entity_revision_id', $configuration['field']);
$this
->assertEquals('vid', $configuration['left_field']);
$this
->assertEquals('content_entity_type_id', $configuration['extra'][0]['field']);
$this
->assertEquals('node', $configuration['extra'][0]['value']);
$this
->assertEquals('content_entity_id', $configuration['extra'][1]['field']);
$this
->assertEquals('nid', $configuration['extra'][1]['left_field']);
$this
->assertEquals('langcode', $configuration['extra'][2]['field']);
$this
->assertEquals('langcode', $configuration['extra'][2]['left_field']);
$expected_result = [];
foreach ($nodes as $node) {
$expected_result[] = [
'nid' => $node
->id(),
];
}
$this
->assertIdenticalResultset($view, $expected_result, [
'nid' => 'nid',
]);
}
}