Commit 6b94c5c9 authored by alexpott's avatar alexpott

Issue #2719335 by iMiksu, markdorison, mattc321, dawehner, mikeker, catch: Add...

Issue #2719335 by iMiksu, markdorison, mattc321, dawehner, mikeker, catch: Add \Drupal\views\ViewsData::getAll()
parent c19740f8
......@@ -81,7 +81,7 @@ class ViewsData {
protected $moduleHandler;
/**
* The language manager
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
......@@ -108,6 +108,26 @@ public function __construct(CacheBackendInterface $cache_backend, ConfigFactoryI
$this->skipCache = $config->get('views.settings')->get('skip_cache');
}
/**
* Gets all table data.
*
* @see https://www.drupal.org/node/2723553
*
* @return array $data
* An array of table data.
*/
public function getAll() {
if (!$this->fullyLoaded) {
$this->allStorage = $this->getData();
}
// Set storage from allStorage outside of the fullyLoaded check to prevent
// cache calls on requests that have requested all data to get a single
// tables data. Make sure $this->storage is populated in this case.
$this->storage = $this->allStorage;
return $this->allStorage;
}
/**
* Gets data for a particular table, or all tables.
*
......@@ -115,57 +135,50 @@ public function __construct(CacheBackendInterface $cache_backend, ConfigFactoryI
* The key of the cache entry to retrieve. Defaults to NULL, this will
* return all table data.
*
* @deprecated NULL $key deprecated in Drupal 8.2.x and will be removed in
* 9.0.0. Use getAll() instead.
*
* @see https://www.drupal.org/node/2723553
*
* @return array $data
* An array of table data.
*/
public function get($key = NULL) {
if ($key) {
if (!isset($this->storage[$key])) {
// Prepare a cache ID for get and set.
$cid = $this->baseCid . ':' . $key;
$from_cache = FALSE;
if ($data = $this->cacheGet($cid)) {
$this->storage[$key] = $data->data;
$from_cache = TRUE;
}
// If there is no cached entry and data is not already fully loaded,
// rebuild. This will stop requests for invalid tables calling getData.
elseif (!$this->fullyLoaded) {
$this->allStorage = $this->getData();
}
if (!$from_cache) {
if (!isset($this->allStorage[$key])) {
// Write an empty cache entry if no information for that table
// exists to avoid repeated cache get calls for this table and
// prevent loading all tables unnecessarily.
$this->storage[$key] = array();
$this->allStorage[$key] = array();
}
else {
$this->storage[$key] = $this->allStorage[$key];
}
// Create a cache entry for the requested table.
$this->cacheSet($cid, $this->allStorage[$key]);
}
}
return $this->storage[$key];
if (!$key) {
return $this->getAll();
}
else {
if (!$this->fullyLoaded) {
if (!isset($this->storage[$key])) {
// Prepare a cache ID for get and set.
$cid = $this->baseCid . ':' . $key;
$from_cache = FALSE;
if ($data = $this->cacheGet($cid)) {
$this->storage[$key] = $data->data;
$from_cache = TRUE;
}
// If there is no cached entry and data is not already fully loaded,
// rebuild. This will stop requests for invalid tables calling getData.
elseif (!$this->fullyLoaded) {
$this->allStorage = $this->getData();
}
// Set storage from allStorage outside of the fullyLoaded check to prevent
// cache calls on requests that have requested all data to get a single
// tables data. Make sure $this->storage is populated in this case.
$this->storage = $this->allStorage;
}
if (!$from_cache) {
if (!isset($this->allStorage[$key])) {
// Write an empty cache entry if no information for that table
// exists to avoid repeated cache get calls for this table and
// prevent loading all tables unnecessarily.
$this->storage[$key] = array();
$this->allStorage[$key] = array();
}
else {
$this->storage[$key] = $this->allStorage[$key];
}
return $this->allStorage;
// Create a cache entry for the requested table.
$this->cacheSet($cid, $this->allStorage[$key]);
}
}
return $this->storage[$key];
}
/**
......
......@@ -148,7 +148,7 @@ protected function setupMockedModuleHandler() {
*/
public function testFetchBaseTables() {
$this->setupMockedModuleHandler();
$data = $this->viewsData->get();
$data = $this->viewsData->getAll();
$base_tables = $this->viewsData->fetchBaseTables();
......@@ -198,7 +198,7 @@ public function testGetOnFirstCall() {
->will($this->returnValue(FALSE));
$expected_views_data = $this->viewsDataWithProvider();
$views_data = $this->viewsData->get();
$views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data);
}
......@@ -271,7 +271,7 @@ public function testFullAndTableGetCache() {
->method('set')
->with("views_data:$random_table_name:en", array());
$views_data = $this->viewsData->get();
$views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data);
// Request a specific table should be static cached.
......@@ -288,7 +288,7 @@ public function testFullAndTableGetCache() {
$this->viewsData->clear();
// Get the views data again.
$this->viewsData->get();
$this->viewsData->getAll();
$this->viewsData->get($table_name);
$this->viewsData->get($table_name_2);
$this->viewsData->get($random_table_name);
......@@ -312,10 +312,10 @@ public function testFullGetCache() {
->with("views_data:en")
->will($this->returnValue(FALSE));
$views_data = $this->viewsData->get();
$views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data);
$views_data = $this->viewsData->get();
$views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data);
}
......@@ -349,7 +349,7 @@ public function testSingleTableGetCache() {
$this->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching cached views data by table works as expected.');
// Test that this data is present if all views data is returned.
$views_data = $this->viewsData->get();
$views_data = $this->viewsData->getAll();
$this->assertArrayHasKey($table_name, $views_data, 'Make sure the views_test_data info appears in the total views data.');
$this->assertSame($expected_views_data[$table_name], $views_data[$table_name], 'Make sure the views_test_data has the expected values.');
......@@ -568,7 +568,7 @@ public function testCacheCallsWithoutWarmCacheAndGetAllTables() {
// Initialize the views data cache and repeat with no specified table. This
// should only load the cache entry for all tables.
for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData->get();
$views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data);
}
}
......@@ -595,7 +595,7 @@ public function testCacheCallsWithWarmCacheAndGetAllTables() {
// Initialize the views data cache and repeat with no specified table. This
// should only load the cache entry for all tables.
for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData->get();
$views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data);
}
}
......@@ -638,4 +638,30 @@ public function testCacheCallsWithoutWarmCacheAndGetMultipleTables() {
$this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2));
}
/**
* Tests that getting all data has same results as getting data with NULL
* logic.
*
* @covers ::getAll
*/
public function testGetAllEqualsToGetNull() {
$expected_views_data = $this->viewsDataWithProvider();
$this->setupMockedModuleHandler();
// Setup a warm cache backend for a single table.
$this->cacheBackend->expects($this->once())
->method('get')
->with("views_data:en");
$this->cacheBackend->expects($this->once())
->method('set')
->with('views_data:en', $expected_views_data);
// Initialize the views data cache and repeat with no specified table. This
// should only load the cache entry for all tables.
for ($i = 0; $i < 5; $i++) {
$this->assertSame($expected_views_data, $this->viewsData->getAll());
$this->assertSame($expected_views_data, $this->viewsData->get());
}
}
}
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