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 { ...@@ -81,7 +81,7 @@ class ViewsData {
protected $moduleHandler; protected $moduleHandler;
/** /**
* The language manager * The language manager.
* *
* @var \Drupal\Core\Language\LanguageManagerInterface * @var \Drupal\Core\Language\LanguageManagerInterface
*/ */
...@@ -108,6 +108,26 @@ public function __construct(CacheBackendInterface $cache_backend, ConfigFactoryI ...@@ -108,6 +108,26 @@ public function __construct(CacheBackendInterface $cache_backend, ConfigFactoryI
$this->skipCache = $config->get('views.settings')->get('skip_cache'); $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. * Gets data for a particular table, or all tables.
* *
...@@ -115,57 +135,50 @@ public function __construct(CacheBackendInterface $cache_backend, ConfigFactoryI ...@@ -115,57 +135,50 @@ public function __construct(CacheBackendInterface $cache_backend, ConfigFactoryI
* The key of the cache entry to retrieve. Defaults to NULL, this will * The key of the cache entry to retrieve. Defaults to NULL, this will
* return all table data. * 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 * @return array $data
* An array of table data. * An array of table data.
*/ */
public function get($key = NULL) { public function get($key = NULL) {
if ($key) { if (!$key) {
if (!isset($this->storage[$key])) { return $this->getAll();
// 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];
} }
else { if (!isset($this->storage[$key])) {
if (!$this->fullyLoaded) { // 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(); $this->allStorage = $this->getData();
} }
// Set storage from allStorage outside of the fullyLoaded check to prevent if (!$from_cache) {
// cache calls on requests that have requested all data to get a single if (!isset($this->allStorage[$key])) {
// tables data. Make sure $this->storage is populated in this case. // Write an empty cache entry if no information for that table
$this->storage = $this->allStorage; // 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() { ...@@ -148,7 +148,7 @@ protected function setupMockedModuleHandler() {
*/ */
public function testFetchBaseTables() { public function testFetchBaseTables() {
$this->setupMockedModuleHandler(); $this->setupMockedModuleHandler();
$data = $this->viewsData->get(); $data = $this->viewsData->getAll();
$base_tables = $this->viewsData->fetchBaseTables(); $base_tables = $this->viewsData->fetchBaseTables();
...@@ -198,7 +198,7 @@ public function testGetOnFirstCall() { ...@@ -198,7 +198,7 @@ public function testGetOnFirstCall() {
->will($this->returnValue(FALSE)); ->will($this->returnValue(FALSE));
$expected_views_data = $this->viewsDataWithProvider(); $expected_views_data = $this->viewsDataWithProvider();
$views_data = $this->viewsData->get(); $views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data); $this->assertSame($expected_views_data, $views_data);
} }
...@@ -271,7 +271,7 @@ public function testFullAndTableGetCache() { ...@@ -271,7 +271,7 @@ public function testFullAndTableGetCache() {
->method('set') ->method('set')
->with("views_data:$random_table_name:en", array()); ->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); $this->assertSame($expected_views_data, $views_data);
// Request a specific table should be static cached. // Request a specific table should be static cached.
...@@ -288,7 +288,7 @@ public function testFullAndTableGetCache() { ...@@ -288,7 +288,7 @@ public function testFullAndTableGetCache() {
$this->viewsData->clear(); $this->viewsData->clear();
// Get the views data again. // Get the views data again.
$this->viewsData->get(); $this->viewsData->getAll();
$this->viewsData->get($table_name); $this->viewsData->get($table_name);
$this->viewsData->get($table_name_2); $this->viewsData->get($table_name_2);
$this->viewsData->get($random_table_name); $this->viewsData->get($random_table_name);
...@@ -312,10 +312,10 @@ public function testFullGetCache() { ...@@ -312,10 +312,10 @@ public function testFullGetCache() {
->with("views_data:en") ->with("views_data:en")
->will($this->returnValue(FALSE)); ->will($this->returnValue(FALSE));
$views_data = $this->viewsData->get(); $views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data); $this->assertSame($expected_views_data, $views_data);
$views_data = $this->viewsData->get(); $views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data); $this->assertSame($expected_views_data, $views_data);
} }
...@@ -349,7 +349,7 @@ public function testSingleTableGetCache() { ...@@ -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.'); $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. // 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->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.'); $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() { ...@@ -568,7 +568,7 @@ public function testCacheCallsWithoutWarmCacheAndGetAllTables() {
// Initialize the views data cache and repeat with no specified table. This // Initialize the views data cache and repeat with no specified table. This
// should only load the cache entry for all tables. // should only load the cache entry for all tables.
for ($i = 0; $i < 5; $i++) { for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData->get(); $views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data); $this->assertSame($expected_views_data, $views_data);
} }
} }
...@@ -595,7 +595,7 @@ public function testCacheCallsWithWarmCacheAndGetAllTables() { ...@@ -595,7 +595,7 @@ public function testCacheCallsWithWarmCacheAndGetAllTables() {
// Initialize the views data cache and repeat with no specified table. This // Initialize the views data cache and repeat with no specified table. This
// should only load the cache entry for all tables. // should only load the cache entry for all tables.
for ($i = 0; $i < 5; $i++) { for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData->get(); $views_data = $this->viewsData->getAll();
$this->assertSame($expected_views_data, $views_data); $this->assertSame($expected_views_data, $views_data);
} }
} }
...@@ -638,4 +638,30 @@ public function testCacheCallsWithoutWarmCacheAndGetMultipleTables() { ...@@ -638,4 +638,30 @@ public function testCacheCallsWithoutWarmCacheAndGetMultipleTables() {
$this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2)); $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