Commit 073cc74c authored by plach's avatar plach

Issue #2987956 by mariancalinro, amateescu, vijaycs85, mheip, mallezie: Entity...

Issue #2987956 by mariancalinro, amateescu, vijaycs85, mheip, mallezie: Entity queries with sorting and ordering are not working in a non-default workspace
parent 0db61d15
...@@ -13,17 +13,6 @@ class Query extends BaseQuery { ...@@ -13,17 +13,6 @@ class Query extends BaseQuery {
prepare as traitPrepare; prepare as traitPrepare;
} }
/**
* Stores the SQL expressions used to build the SQL query.
*
* The array is keyed by the expression alias and the values are the actual
* expressions.
*
* @var array
* An array of expressions.
*/
protected $sqlExpressions = [];
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -42,21 +31,15 @@ public function prepare() { ...@@ -42,21 +31,15 @@ public function prepare() {
// relationship, and, as a consequence, the revision ID field is no longer // relationship, and, as a consequence, the revision ID field is no longer
// a simple SQL field but an expression. // a simple SQL field but an expression.
$this->sqlFields = []; $this->sqlFields = [];
$this->sqlExpressions[$revision_field] = "COALESCE(workspace_association.target_entity_revision_id, base_table.$revision_field)"; $this->sqlQuery->addExpression("COALESCE(workspace_association.target_entity_revision_id, base_table.$revision_field)", $revision_field);
$this->sqlExpressions[$id_field] = "base_table.$id_field"; $this->sqlQuery->addExpression("base_table.$id_field", $id_field);
$this->sqlGroupBy['workspace_association.target_entity_revision_id'] = 'workspace_association.target_entity_revision_id';
$this->sqlGroupBy["base_table.$id_field"] = "base_table.$id_field";
$this->sqlGroupBy["base_table.$revision_field"] = "base_table.$revision_field";
} }
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
protected function finish() {
foreach ($this->sqlExpressions as $alias => $expression) {
$this->sqlQuery->addExpression($expression, $alias);
}
return parent::finish();
}
} }
...@@ -69,4 +69,18 @@ public function prepare() { ...@@ -69,4 +69,18 @@ public function prepare() {
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
public function isSimpleQuery() {
// We declare that this is not a simple query in
// \Drupal\workspaces\EntityQuery\QueryTrait::prepare(), but that's not
// enough because the parent method can return TRUE in some circumstances.
if ($this->sqlQuery->getMetaData('active_workspace_id')) {
return FALSE;
}
return parent::isSimpleQuery();
}
} }
...@@ -44,6 +44,13 @@ class WorkspaceIntegrationTest extends KernelTestBase { ...@@ -44,6 +44,13 @@ class WorkspaceIntegrationTest extends KernelTestBase {
*/ */
protected $createdTimestamp; protected $createdTimestamp;
/**
* An array of nodes created before installing the Workspaces module.
*
* @var \Drupal\node\NodeInterface[]
*/
protected $nodes = [];
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -83,8 +90,8 @@ protected function setUp() { ...@@ -83,8 +90,8 @@ protected function setUp() {
// Create two nodes, a published and an unpublished one, so we can test the // Create two nodes, a published and an unpublished one, so we can test the
// behavior of the module with default/existing content. // behavior of the module with default/existing content.
$this->createdTimestamp = \Drupal::time()->getRequestTime(); $this->createdTimestamp = \Drupal::time()->getRequestTime();
$this->createNode(['title' => 'live - 1 - r1 - published', 'created' => $this->createdTimestamp++, 'status' => TRUE]); $this->nodes[] = $this->createNode(['title' => 'live - 1 - r1 - published', 'created' => $this->createdTimestamp++, 'status' => TRUE]);
$this->createNode(['title' => 'live - 2 - r2 - unpublished', 'created' => $this->createdTimestamp++, 'status' => FALSE]); $this->nodes[] = $this->createNode(['title' => 'live - 2 - r2 - unpublished', 'created' => $this->createdTimestamp++, 'status' => FALSE]);
} }
/** /**
...@@ -338,6 +345,32 @@ public function testWorkspaces() { ...@@ -338,6 +345,32 @@ public function testWorkspaces() {
$this->assertEmpty($workspace_publisher->getDifferringRevisionIdsOnSource()); $this->assertEmpty($workspace_publisher->getDifferringRevisionIdsOnSource());
} }
/**
* Tests entity query overrides without any conditions.
*/
public function testEntityQueryWithoutConditions() {
$this->initializeWorkspacesModule();
$this->switchToWorkspace('stage');
// Add a workspace-specific revision to a pre-existing node.
$this->nodes[1]->title->value = 'stage - 2 - r3 - published';
$this->nodes[1]->save();
$query = $this->entityTypeManager->getStorage('node')->getQuery();
$query->sort('nid');
$query->pager(1);
$result = $query->execute();
$this->assertSame([1 => '1'], $result);
$query = $this->entityTypeManager->getStorage('node')->getQuery();
$query->sort('nid', 'DESC');
$query->pager(10);
$result = $query->execute();
$this->assertSame([3 => '2', 1 => '1'], $result);
}
/** /**
* Tests the Entity Query relationship API with workspaces. * Tests the Entity Query relationship API with workspaces.
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment