View source
<?php
namespace Drupal\views\Tests\EventSubscriber;
use Drupal\Core\Entity\EntityTypeEvent;
use Drupal\Core\Entity\EntityTypeEvents;
use Drupal\system\Tests\Entity\EntityDefinitionTestTrait;
use Drupal\views\Tests\ViewKernelTestBase;
class ViewsEntitySchemaSubscriberIntegrationTest extends ViewKernelTestBase {
use EntityDefinitionTestTrait;
protected $entityDefinitionUpdateManager;
public static $modules = [
'entity_test',
'user',
'text',
];
public static $testViews = [
'test_view_entity_test',
'test_view_entity_test_revision',
'test_view_entity_test_data',
'test_view_entity_test_additional_base_field',
];
protected $eventDispatcher;
protected $eventSubscriber;
protected $entityManager;
protected $state;
protected function setUp() {
parent::setUp();
$this->eventDispatcher = $this->container
->get('event_dispatcher');
$this->eventSubscriber = $this->container
->get('views.entity_schema_subscriber');
$this->entityDefinitionUpdateManager = $this->container
->get('entity.definition_update_manager');
$this->entityManager = $this->container
->get('entity.manager');
$this->state = $this->container
->get('state');
$this->database = $this->container
->get('database');
foreach (array_diff_key($this->entityManager
->getDefinitions(), array_flip(array(
'user',
'entity_test',
))) as $entity_type_id => $entity_type) {
$this
->installEntitySchema($entity_type_id);
}
$this
->installSchema('system', 'key_value_expire');
}
public function testDeleteEntityType() {
$entity_storage = $this->entityManager
->getStorage('view');
$views = $entity_storage
->loadMultiple();
$this
->assertTrue(isset($views['test_view_entity_test']));
$this
->assertTrue(isset($views['test_view_entity_test_revision']));
$this
->assertTrue(isset($views['test_view_entity_test_data']));
$this
->assertTrue(isset($views['test_view_entity_test_additional_base_field']));
$event = new EntityTypeEvent($this->entityManager
->getDefinition('entity_test_update'));
$this->eventDispatcher
->dispatch(EntityTypeEvents::DELETE, $event);
$views = $entity_storage
->loadMultiple();
$this
->assertTrue(isset($views['test_view_entity_test']));
$this
->assertTrue(isset($views['test_view_entity_test_revision']));
$this
->assertTrue(isset($views['test_view_entity_test_data']));
$this
->assertTrue(isset($views['test_view_entity_test_additional_base_field']));
$this
->assertFalse($views['test_view_entity_test']
->status());
$this
->assertFalse($views['test_view_entity_test_revision']
->status());
$this
->assertFalse($views['test_view_entity_test_data']
->status());
$this
->assertFalse($views['test_view_entity_test_additional_base_field']
->status());
}
public function testBaseTableRename() {
$this
->renameBaseTable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load('test_view_entity_test');
$this
->assertEqual('entity_test_update_new', $view
->get('base_table'));
$display = $view
->getDisplay('default');
$this
->assertEqual('entity_test_update_new', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_new', $display['display_options']['fields']['name']['table']);
}
public function testDataTableRename() {
$this
->updateEntityTypeToTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load('test_view_entity_test_data');
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$display = $view
->getDisplay('default');
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
$this
->renameDataTable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load('test_view_entity_test_data');
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$display = $view
->getDisplay('default');
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_data_new', $display['display_options']['fields']['name']['table']);
}
public function testRevisionBaseTableRename() {
$this
->updateEntityTypeToRevisionable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load('test_view_entity_test_revision');
$this
->assertEqual('entity_test_update_revision', $view
->get('base_table'));
$display = $view
->getDisplay('default');
$this
->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_revision', $display['display_options']['fields']['name']['table']);
$this
->renameRevisionBaseTable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load('test_view_entity_test_revision');
$this
->assertEqual('entity_test_update_revision_new', $view
->get('base_table'));
$display = $view
->getDisplay('default');
$this
->assertEqual('entity_test_update_revision_new', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_revision_new', $display['display_options']['fields']['name']['table']);
}
public function testRevisionDataTableRename() {
$this
->updateEntityTypeToRevisionable();
$this->entityManager
->clearCachedDefinitions();
$this
->updateEntityTypeToTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load('test_view_entity_test_revision');
$this
->assertEqual('entity_test_update_revision', $view
->get('base_table'));
$display = $view
->getDisplay('default');
$this
->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_revision_data', $display['display_options']['fields']['name']['table']);
$this
->renameRevisionDataTable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load('test_view_entity_test_revision');
$this
->assertEqual('entity_test_update_revision', $view
->get('base_table'));
$display = $view
->getDisplay('default');
$this
->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_revision_data_new', $display['display_options']['fields']['name']['table']);
}
public function testDataTableAddition() {
$this
->updateEntityTypeToTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load('test_view_entity_test');
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$display = $view
->getDisplay('default');
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
}
public function testRevisionEnabling() {
$this
->updateEntityTypeToRevisionable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load('test_view_entity_test');
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$display = $view
->getDisplay('default');
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
}
public function testRevisionDisabling() {
$this
->updateEntityTypeToRevisionable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$this
->updateEntityTypeToNotRevisionable();
$this->entityDefinitionUpdateManager
->applyUpdates();
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load('test_view_entity_test_revision');
$this
->assertFalse($view
->status());
}
public function testVariousTableUpdates() {
$this
->updateEntityTypeToTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
$this
->updateEntityTypeToNotTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
$this
->resetEntityType();
$this
->updateEntityTypeToTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
$this
->updateEntityTypeToRevisionable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
$this
->updateEntityTypeToNotRevisionable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
$this
->resetEntityType();
$this
->updateEntityTypeToRevisionable();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
$this
->updateEntityTypeToTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
$this
->updateEntityTypeToNotTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
$this
->resetEntityType();
$this
->updateEntityTypeToRevisionable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
$this
->updateEntityTypeToNotRevisionable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
$this
->resetEntityType();
$this
->updateEntityTypeToRevisionable();
$this
->updateEntityTypeToTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_data', $display['display_options']['fields']['name']['table']);
$this
->updateEntityTypeToNotRevisionable();
$this
->updateEntityTypeToNotTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay();
$this
->assertEqual('entity_test_update', $view
->get('base_table'));
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update', $display['display_options']['fields']['name']['table']);
}
public function testVariousTableUpdatesForRevisionView() {
$this
->updateEntityTypeToRevisionable();
$this->entityManager
->clearCachedDefinitions();
list($view, $display) = $this
->getUpdatedViewAndDisplay(TRUE);
$this
->assertEqual('entity_test_update_revision', $view
->get('base_table'));
$this
->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_revision', $display['display_options']['fields']['name']['table']);
$this
->updateEntityTypeToTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay(TRUE);
$this
->assertEqual('entity_test_update_revision', $view
->get('base_table'));
$this
->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_revision_data', $display['display_options']['fields']['name']['table']);
$this
->updateEntityTypeToNotTranslatable();
$this->entityDefinitionUpdateManager
->applyUpdates();
list($view, $display) = $this
->getUpdatedViewAndDisplay(TRUE);
$this
->assertEqual('entity_test_update_revision', $view
->get('base_table'));
$this
->assertEqual('entity_test_update_revision', $display['display_options']['fields']['id']['table']);
$this
->assertEqual('entity_test_update_revision', $display['display_options']['fields']['name']['table']);
$this
->resetEntityType();
}
protected function getUpdatedViewAndDisplay($revision = FALSE) {
$entity_storage = $this->entityManager
->getStorage('view');
$view = $entity_storage
->load($revision ? 'test_view_entity_test_revision' : 'test_view_entity_test');
$display = $view
->getDisplay('default');
return [
$view,
$display,
];
}
}