View source
<?php
namespace Drupal\Tests\views\Unit\Plugin\query;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityType;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Plugin\views\query\DateSqlInterface;
use Drupal\views\Plugin\views\query\Sql;
use Drupal\views\Plugin\views\relationship\RelationshipPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewEntityInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\ViewsData;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class SqlTest extends UnitTestCase {
public function testGetCacheTags() {
$view = $this
->prophesize('Drupal\\views\\ViewExecutable')
->reveal();
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$date_sql = $this
->prophesize(DateSqlInterface::class);
$messenger = $this
->prophesize(MessengerInterface::class);
$query = new Sql([], 'sql', [], $entity_type_manager
->reveal(), $date_sql
->reveal(), $messenger
->reveal());
$query->view = $view;
$result = [];
$view->result = $result;
$row = new ResultRow();
$prophecy = $this
->prophesize('Drupal\\Core\\Entity\\EntityInterface');
$prophecy
->getCacheTags()
->willReturn([
'entity_test:123',
]);
$entity = $prophecy
->reveal();
$row->_entity = $entity;
$result[] = $row;
$view->result = $result;
$row = new ResultRow();
$prophecy = $this
->prophesize('Drupal\\Core\\Entity\\EntityInterface');
$prophecy
->getCacheTags()
->willReturn([
'entity_test:124',
]);
$entity = $prophecy
->reveal();
$row->_entity = $entity;
$prophecy = $this
->prophesize('Drupal\\Core\\Entity\\EntityInterface');
$prophecy
->getCacheTags()
->willReturn([
'entity_test:125',
]);
$entity = $prophecy
->reveal();
$row->_relationship_entities[] = $entity;
$prophecy = $this
->prophesize('Drupal\\Core\\Entity\\EntityInterface');
$prophecy
->getCacheTags()
->willReturn([
'entity_test:126',
]);
$entity = $prophecy
->reveal();
$row->_relationship_entities[] = $entity;
$result[] = $row;
$view->result = $result;
$this
->assertEqualsCanonicalizing([
'entity_test:123',
'entity_test:124',
'entity_test:125',
'entity_test:126',
], $query
->getCacheTags());
}
public function testGetCacheMaxAge() {
$view = $this
->prophesize('Drupal\\views\\ViewExecutable')
->reveal();
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$date_sql = $this
->prophesize(DateSqlInterface::class);
$messenger = $this
->prophesize(MessengerInterface::class);
$query = new Sql([], 'sql', [], $entity_type_manager
->reveal(), $date_sql
->reveal(), $messenger
->reveal());
$query->view = $view;
$view->result = [];
$row = new ResultRow();
$prophecy = $this
->prophesize('Drupal\\Core\\Entity\\EntityInterface');
$prophecy
->getCacheMaxAge()
->willReturn(10);
$entity = $prophecy
->reveal();
$row->_entity = $entity;
$view->result[] = $row;
$row = new ResultRow();
$prophecy = $this
->prophesize('Drupal\\Core\\Entity\\EntityInterface');
$prophecy
->getCacheMaxAge()
->willReturn(20);
$entity = $prophecy
->reveal();
$row->_entity = $entity;
$prophecy = $this
->prophesize('Drupal\\Core\\Entity\\EntityInterface');
$prophecy
->getCacheMaxAge()
->willReturn(30);
$entity = $prophecy
->reveal();
$row->_relationship_entities[] = $entity;
$prophecy = $this
->prophesize('Drupal\\Core\\Entity\\EntityInterface');
$prophecy
->getCacheMaxAge()
->willReturn(40);
$entity = $prophecy
->reveal();
$row->_relationship_entities[] = $entity;
$this
->assertEquals(10, $query
->getCacheMaxAge());
}
protected function setupViewsData(ViewsData $views_data) {
$container = \Drupal::hasContainer() ? \Drupal::getContainer() : new ContainerBuilder();
$container
->set('views.views_data', $views_data);
\Drupal::setContainer($container);
}
protected function setupEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
$container = \Drupal::hasContainer() ? \Drupal::getContainer() : new ContainerBuilder();
$container
->set('entity_type.manager', $entity_type_manager);
\Drupal::setContainer($container);
}
protected function setupEntityTypes($entities_by_type = [], $entity_revisions_by_type = []) {
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$entity_type0 = new EntityType([
'label' => 'First',
'id' => 'first',
'base_table' => 'entity_first',
'revision_table' => 'entity_first__revision',
'entity_keys' => [
'id' => 'id',
'revision' => 'vid',
],
]);
$entity_type1 = new EntityType([
'label' => 'second',
'id' => 'second',
'base_table' => 'entity_second',
'revision_table' => 'entity_second__revision',
'entity_keys' => [
'id' => 'id',
'revision' => 'vid',
],
]);
$entity_type_manager
->getDefinitions()
->willReturn([
'first' => $entity_type0,
'second' => $entity_type1,
'base_table' => 'entity_second',
]);
$entity_type_manager
->getDefinition('first')
->willReturn($entity_type0);
$entity_type_manager
->getDefinition('second')
->willReturn($entity_type1);
$views_data = $this
->prophesize(ViewsData::class);
$views_data
->get('entity_first')
->willReturn([
'table' => [
'entity type' => 'first',
'entity revision' => FALSE,
],
]);
$views_data
->get('entity_first__revision')
->willReturn([
'table' => [
'entity type' => 'first',
'entity revision' => TRUE,
],
]);
$views_data
->get('entity_second')
->willReturn([
'table' => [
'entity type' => 'second',
'entity revision' => FALSE,
],
]);
$views_data
->get('entity_second__revision')
->willReturn([
'table' => [
'entity type' => 'second',
'entity revision' => TRUE,
],
]);
$views_data
->get('entity_first_field_data')
->willReturn([
'table' => [
'entity type' => 'first',
'entity revision' => FALSE,
],
]);
$this
->setupViewsData($views_data
->reveal());
$entity_storages = [
'first' => $this
->prophesize(EntityStorageInterface::class),
'second' => $this
->prophesize(EntityStorageInterface::class),
];
foreach ($entities_by_type as $entity_type_id => $entities) {
foreach ($entities as $entity_id => $entity) {
$entity_storages[$entity_type_id]
->load($entity_id)
->willReturn($entity);
}
$entity_storages[$entity_type_id]
->loadMultiple(array_keys($entities))
->willReturn($entities);
}
foreach ($entity_revisions_by_type as $entity_type_id => $entity_revisions) {
foreach ($entity_revisions as $revision_id => $revision) {
$entity_storages[$entity_type_id]
->loadRevision($revision_id)
->willReturn($revision);
}
}
$entity_type_manager
->getStorage('first')
->willReturn($entity_storages['first']);
$entity_type_manager
->getStorage('second')
->willReturn($entity_storages['second']);
$this
->setupEntityTypeManager($entity_type_manager
->reveal());
return $entity_type_manager;
}
public function testLoadEntitiesWithEmptyResult() {
$view = $this
->prophesize('Drupal\\views\\ViewExecutable')
->reveal();
$view_entity = $this
->prophesize(ViewEntityInterface::class);
$view_entity
->get('base_table')
->willReturn('entity_first');
$view_entity
->get('base_field')
->willReturn('id');
$view->storage = $view_entity
->reveal();
$entity_type_manager = $this
->setupEntityTypes();
$date_sql = $this
->prophesize(DateSqlInterface::class);
$messenger = $this
->prophesize(MessengerInterface::class);
$query = new Sql([], 'sql', [], $entity_type_manager
->reveal(), $date_sql
->reveal(), $messenger
->reveal());
$query->view = $view;
$result = [];
$query
->addField('entity_first', 'id', 'id');
$query
->loadEntities($result);
$this
->assertEmpty($result);
}
public function testLoadEntitiesWithNoRelationshipAndNoRevision() {
$view = $this
->prophesize('Drupal\\views\\ViewExecutable')
->reveal();
$view_entity = $this
->prophesize(ViewEntityInterface::class);
$view_entity
->get('base_table')
->willReturn('entity_first');
$view_entity
->get('base_field')
->willReturn('id');
$view->storage = $view_entity
->reveal();
$entities = [
'first' => [
1 => $this
->prophesize(EntityInterface::class)
->reveal(),
2 => $this
->prophesize(EntityInterface::class)
->reveal(),
],
];
$entity_type_manager = $this
->setupEntityTypes($entities);
$date_sql = $this
->prophesize(DateSqlInterface::class);
$messenger = $this
->prophesize(MessengerInterface::class);
$query = new Sql([], 'sql', [], $entity_type_manager
->reveal(), $date_sql
->reveal(), $messenger
->reveal());
$query->view = $view;
$result = [];
$result[] = new ResultRow([
'id' => 1,
]);
$result[] = new ResultRow([
'id' => 2,
]);
$result[] = new ResultRow([
'id' => 2,
]);
$query
->addField('entity_first', 'id', 'id');
$query
->loadEntities($result);
$this
->assertSame($entities['first'][1], $result[0]->_entity);
$this
->assertSame($entities['first'][2], $result[1]->_entity);
$this
->assertSame($entities['first'][2], $result[2]->_entity);
}
protected function setupViewWithRelationships(ViewExecutable $view, $base = 'entity_second') {
$relationship = $this
->getMockBuilder(RelationshipPluginBase::class)
->disableOriginalConstructor()
->getMock();
$relationship->definition['base'] = $base;
$relationship->tableAlias = $base;
$relationship->alias = $base;
$view->relationship[$base] = $relationship;
}
public function testLoadEntitiesWithRelationship() {
$view = $this
->getMockBuilder(ViewExecutable::class)
->disableOriginalConstructor()
->getMock();
$this
->setupViewWithRelationships($view);
$view_entity = $this
->prophesize(ViewEntityInterface::class);
$view_entity
->get('base_table')
->willReturn('entity_first');
$view_entity
->get('base_field')
->willReturn('id');
$view->storage = $view_entity
->reveal();
$entities = [
'first' => [
1 => $this
->prophesize(EntityInterface::class)
->reveal(),
2 => $this
->prophesize(EntityInterface::class)
->reveal(),
],
'second' => [
11 => $this
->prophesize(EntityInterface::class)
->reveal(),
12 => $this
->prophesize(EntityInterface::class)
->reveal(),
],
];
$entity_type_manager = $this
->setupEntityTypes($entities);
$date_sql = $this
->prophesize(DateSqlInterface::class);
$messenger = $this
->prophesize(MessengerInterface::class);
$query = new Sql([], 'sql', [], $entity_type_manager
->reveal(), $date_sql
->reveal(), $messenger
->reveal());
$query->view = $view;
$result = [];
$result[] = new ResultRow([
'id' => 1,
'entity_second__id' => 11,
]);
$result[] = new ResultRow([
'id' => 2,
'entity_second__id' => NULL,
]);
$result[] = new ResultRow([
'id' => 2,
'entity_second__id' => 12,
]);
$query
->addField('entity_first', 'id', 'id');
$query
->addField('entity_second', 'id', 'entity_second__id');
$query
->loadEntities($result);
$this
->assertSame($entities['first'][1], $result[0]->_entity);
$this
->assertSame($entities['first'][2], $result[1]->_entity);
$this
->assertSame($entities['first'][2], $result[2]->_entity);
$this
->assertSame($entities['second'][11], $result[0]->_relationship_entities['entity_second']);
$this
->assertEquals([], $result[1]->_relationship_entities);
$this
->assertSame($entities['second'][12], $result[2]->_relationship_entities['entity_second']);
}
public function testLoadEntitiesWithNonEntityRelationship() {
$view = $this
->getMockBuilder(ViewExecutable::class)
->disableOriginalConstructor()
->getMock();
$this
->setupViewWithRelationships($view, 'entity_first_field_data');
$view_entity = $this
->prophesize(ViewEntityInterface::class);
$view_entity
->get('base_table')
->willReturn('entity_first');
$view_entity
->get('base_field')
->willReturn('id');
$view->storage = $view_entity
->reveal();
$entities = [
'first' => [
1 => $this
->prophesize(EntityInterface::class)
->reveal(),
2 => $this
->prophesize(EntityInterface::class)
->reveal(),
],
];
$entity_type_manager = $this
->setupEntityTypes($entities);
$date_sql = $this
->prophesize(DateSqlInterface::class);
$messenger = $this
->prophesize(MessengerInterface::class);
$query = new Sql([], 'sql', [], $entity_type_manager
->reveal(), $date_sql
->reveal(), $messenger
->reveal());
$query->view = $view;
$result = [];
$result[] = new ResultRow([
'id' => 1,
]);
$result[] = new ResultRow([
'id' => 2,
]);
$query
->addField('entity_first', 'id', 'id');
$query
->loadEntities($result);
$entity_information = $query
->getEntityTableInfo();
$this
->assertSame($entities['first'][1], $result[0]->_entity);
$this
->assertSame($entities['first'][2], $result[1]->_entity);
$this
->assertEquals([], $result[0]->_relationship_entities);
$this
->assertEquals([], $result[1]->_relationship_entities);
$this
->assertContains('first', array_keys($entity_information));
$this
->assertNotContains('entity_first_field_data__entity_first_field_data', array_keys($entity_information));
}
public function testLoadEntitiesWithRevision() {
$view = $this
->getMockBuilder(ViewExecutable::class)
->disableOriginalConstructor()
->getMock();
$view_entity = $this
->prophesize(ViewEntityInterface::class);
$view_entity
->get('base_table')
->willReturn('entity_first__revision');
$view_entity
->get('base_field')
->willReturn('vid');
$view->storage = $view_entity
->reveal();
$entity_revisions = [
'first' => [
1 => $this
->prophesize(EntityInterface::class)
->reveal(),
3 => $this
->prophesize(EntityInterface::class)
->reveal(),
],
];
$entity_type_manager = $this
->setupEntityTypes([], $entity_revisions);
$date_sql = $this
->prophesize(DateSqlInterface::class);
$messenger = $this
->prophesize(MessengerInterface::class);
$query = new Sql([], 'sql', [], $entity_type_manager
->reveal(), $date_sql
->reveal(), $messenger
->reveal());
$query->view = $view;
$result = [];
$result[] = new ResultRow([
'vid' => 1,
]);
$result[] = new ResultRow([
'vid' => 1,
]);
$result[] = new ResultRow([
'vid' => 3,
]);
$query
->addField('entity_first__revision', 'vid', 'vid');
$query
->loadEntities($result);
$this
->assertSame($entity_revisions['first'][1], $result[0]->_entity);
$this
->assertSame($entity_revisions['first'][1], $result[1]->_entity);
$this
->assertSame($entity_revisions['first'][3], $result[2]->_entity);
}
public function testLoadEntitiesWithRevisionOfSameEntityType() {
$view = $this
->getMockBuilder(ViewExecutable::class)
->disableOriginalConstructor()
->getMock();
$this
->setupViewWithRelationships($view, 'entity_first__revision');
$view_entity = $this
->prophesize(ViewEntityInterface::class);
$view_entity
->get('base_table')
->willReturn('entity_first');
$view_entity
->get('base_field')
->willReturn('id');
$view->storage = $view_entity
->reveal();
$entity = [
'first' => [
1 => $this
->prophesize(EntityInterface::class)
->reveal(),
2 => $this
->prophesize(EntityInterface::class)
->reveal(),
],
];
$entity_revisions = [
'first' => [
1 => $this
->prophesize(EntityInterface::class)
->reveal(),
2 => $this
->prophesize(EntityInterface::class)
->reveal(),
3 => $this
->prophesize(EntityInterface::class)
->reveal(),
],
];
$entity_type_manager = $this
->setupEntityTypes($entity, $entity_revisions);
$date_sql = $this
->prophesize(DateSqlInterface::class);
$messenger = $this
->prophesize(MessengerInterface::class);
$query = new Sql([], 'sql', [], $entity_type_manager
->reveal(), $date_sql
->reveal(), $messenger
->reveal());
$query->view = $view;
$result = [];
$result[] = new ResultRow([
'id' => 1,
'entity_first__revision__vid' => 1,
]);
$result[] = new ResultRow([
'id' => 2,
'entity_first__revision__vid' => 2,
]);
$result[] = new ResultRow([
'id' => 2,
'entity_first__revision__vid' => 3,
]);
$query
->addField('entity_first', 'id', 'id');
$query
->addField('entity_first__revision', 'vid', 'entity_first__revision__vid');
$query
->loadEntities($result);
$this
->assertSame($entity['first'][1], $result[0]->_entity);
$this
->assertSame($entity['first'][2], $result[1]->_entity);
$this
->assertSame($entity['first'][2], $result[2]->_entity);
$this
->assertSame($entity_revisions['first'][1], $result[0]->_relationship_entities['entity_first__revision']);
$this
->assertSame($entity_revisions['first'][2], $result[1]->_relationship_entities['entity_first__revision']);
$this
->assertSame($entity_revisions['first'][3], $result[2]->_relationship_entities['entity_first__revision']);
}
public function testLoadEntitiesWithRelationshipAndRevision() {
$view = $this
->getMockBuilder(ViewExecutable::class)
->disableOriginalConstructor()
->getMock();
$this
->setupViewWithRelationships($view);
$view_entity = $this
->prophesize(ViewEntityInterface::class);
$view_entity
->get('base_table')
->willReturn('entity_first__revision');
$view_entity
->get('base_field')
->willReturn('vid');
$view->storage = $view_entity
->reveal();
$entities = [
'second' => [
11 => $this
->prophesize(EntityInterface::class)
->reveal(),
12 => $this
->prophesize(EntityInterface::class)
->reveal(),
],
];
$entity_revisions = [
'first' => [
1 => $this
->prophesize(EntityInterface::class)
->reveal(),
3 => $this
->prophesize(EntityInterface::class)
->reveal(),
],
];
$entity_type_manager = $this
->setupEntityTypes($entities, $entity_revisions);
$date_sql = $this
->prophesize(DateSqlInterface::class);
$messenger = $this
->prophesize(MessengerInterface::class);
$query = new Sql([], 'sql', [], $entity_type_manager
->reveal(), $date_sql
->reveal(), $messenger
->reveal());
$query->view = $view;
$result = [];
$result[] = new ResultRow([
'vid' => 1,
'entity_second__id' => 11,
]);
$result[] = new ResultRow([
'vid' => 1,
'entity_second__id' => NULL,
]);
$result[] = new ResultRow([
'vid' => 3,
'entity_second__id' => 12,
]);
$query
->addField('entity_first__revision', 'vid', 'vid');
$query
->addField('entity_second', 'id', 'entity_second__id');
$query
->loadEntities($result);
$this
->assertSame($entity_revisions['first'][1], $result[0]->_entity);
$this
->assertSame($entity_revisions['first'][1], $result[1]->_entity);
$this
->assertSame($entity_revisions['first'][3], $result[2]->_entity);
$this
->assertSame($entities['second'][11], $result[0]->_relationship_entities['entity_second']);
$this
->assertEquals([], $result[1]->_relationship_entities);
$this
->assertSame($entities['second'][12], $result[2]->_relationship_entities['entity_second']);
}
}