View source
<?php
namespace Drupal\Tests\views\Unit;
use Drupal\Core\Language\Language;
use Drupal\Tests\UnitTestCase;
use Drupal\views\ViewsData;
use Drupal\views\Tests\ViewTestData;
class ViewsDataTest extends UnitTestCase {
protected $cacheBackend;
protected $cacheTagsInvalidator;
protected $moduleHandler;
protected $configFactory;
protected $languageManager;
protected $viewsData;
protected function setUp() {
$this->cacheTagsInvalidator = $this
->createMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
$this->cacheBackend = $this
->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
$this
->getContainerWithCacheTagsInvalidator($this->cacheTagsInvalidator);
$configs = [];
$configs['views.settings']['skip_cache'] = FALSE;
$this->configFactory = $this
->getConfigFactoryStub($configs);
$this->moduleHandler = $this
->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->languageManager = $this
->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
$this->languageManager
->expects($this
->any())
->method('getCurrentLanguage')
->will($this
->returnValue(new Language([
'id' => 'en',
])));
$this->viewsData = new ViewsData($this->cacheBackend, $this->configFactory, $this->moduleHandler, $this->languageManager);
}
protected function viewsData() {
$data = ViewTestData::viewsData();
unset($data['views_test_data']['id']['field']);
unset($data['views_test_data']['name']['argument']);
unset($data['views_test_data']['age']['filter']);
unset($data['views_test_data']['job']['sort']);
$data['views_test_data']['created']['area']['id'] = 'text';
$data['views_test_data']['age']['area']['id'] = 'text';
$data['views_test_data']['age']['area']['sub_type'] = 'header';
$data['views_test_data']['job']['area']['id'] = 'text';
$data['views_test_data']['job']['area']['sub_type'] = [
'header',
'footer',
];
$data['views_test_data_2'] = $data['views_test_data'];
$data['views_test_data_2']['table']['base']['weight'] = 50;
$data['views_test_data_3'] = $data['views_test_data'];
$data['views_test_data_3']['table']['base']['weight'] = -50;
$data['views_test_data_4'] = $data['views_test_data'];
$data['views_test_data_4']['table']['base']['title'] = 'A different title';
$data['views_test_data_5'] = $data['views_test_data'];
$data['views_test_data_5']['table']['base']['title'] = 'Z different title';
$data['views_test_data_6'] = $data['views_test_data'];
return $data;
}
protected function viewsDataWithProvider() {
$views_data = static::viewsData();
foreach (array_keys($views_data) as $table) {
$views_data[$table]['table']['provider'] = 'views_test_data';
}
return $views_data;
}
protected function setupMockedModuleHandler() {
$views_data = $this
->viewsData();
$this->moduleHandler
->expects($this
->at(0))
->method('getImplementations')
->with('views_data')
->willReturn([
'views_test_data',
]);
$this->moduleHandler
->expects($this
->at(1))
->method('invoke')
->with('views_test_data', 'views_data')
->willReturn($views_data);
}
public function testFetchBaseTables() {
$this
->setupMockedModuleHandler();
$data = $this->viewsData
->getAll();
$base_tables = $this->viewsData
->fetchBaseTables();
foreach (array_keys($base_tables) as $base_table) {
$this
->assertEquals('views_test_data', $data[$base_table]['table']['provider']);
}
$this
->assertCount(6, $base_tables, 'The correct amount of base tables were returned.');
$base_tables_keys = array_keys($base_tables);
for ($i = 1; $i < count($base_tables); ++$i) {
$prev = $base_tables[$base_tables_keys[$i - 1]];
$current = $base_tables[$base_tables_keys[$i]];
$this
->assertTrue($prev['weight'] <= $current['weight'] && $prev['title'] <= $prev['title'], 'The tables are sorted as expected.');
}
$defaults = [
'title' => '',
'help' => '',
'weight' => 0,
];
foreach ($base_tables as $base_table => $info) {
$expected = $data[$base_table]['table']['base'] += $defaults;
foreach ($defaults as $key => $default) {
$this
->assertSame($info[$key], $expected[$key]);
}
}
}
public function testGetOnFirstCall() {
$this
->setupMockedModuleHandler();
$this->moduleHandler
->expects($this
->at(2))
->method('alter')
->with('views_data', $this
->viewsDataWithProvider());
$this->cacheBackend
->expects($this
->once())
->method('get')
->with("views_data:en")
->will($this
->returnValue(FALSE));
$expected_views_data = $this
->viewsDataWithProvider();
$views_data = $this->viewsData
->getAll();
$this
->assertSame($expected_views_data, $views_data);
}
public function testFullAndTableGetCache() {
$expected_views_data = $this
->viewsDataWithProvider();
$table_name = 'views_test_data';
$table_name_2 = 'views_test_data_2';
$random_table_name = $this
->randomMachineName();
$this->moduleHandler
->expects($this
->at(0))
->method('getImplementations')
->with('views_data')
->willReturn([
'views_test_data',
]);
$this->moduleHandler
->expects($this
->at(1))
->method('invoke')
->with('views_test_data', 'views_data')
->willReturn($this
->viewsData());
$this->moduleHandler
->expects($this
->at(2))
->method('alter')
->with('views_data', $expected_views_data);
$this->moduleHandler
->expects($this
->at(3))
->method('getImplementations')
->with('views_data')
->willReturn([
'views_test_data',
]);
$this->moduleHandler
->expects($this
->at(4))
->method('invoke')
->with('views_test_data', 'views_data')
->willReturn($this
->viewsData());
$this->moduleHandler
->expects($this
->at(5))
->method('alter')
->with('views_data', $expected_views_data);
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with("views_data:en")
->will($this
->returnValue(FALSE));
$this->cacheBackend
->expects($this
->at(1))
->method('set')
->with("views_data:en", $expected_views_data);
$this->cacheBackend
->expects($this
->at(2))
->method('get')
->with("views_data:{$random_table_name}:en")
->will($this
->returnValue(FALSE));
$this->cacheBackend
->expects($this
->at(3))
->method('set')
->with("views_data:{$random_table_name}:en", []);
$this->cacheTagsInvalidator
->expects($this
->once())
->method('invalidateTags')
->with([
'views_data',
]);
$this->cacheBackend
->expects($this
->at(4))
->method('get')
->with("views_data:en")
->will($this
->returnValue(FALSE));
$this->cacheBackend
->expects($this
->at(5))
->method('set')
->with("views_data:en", $expected_views_data);
$this->cacheBackend
->expects($this
->at(6))
->method('get')
->with("views_data:{$random_table_name}:en")
->will($this
->returnValue(FALSE));
$this->cacheBackend
->expects($this
->at(7))
->method('set')
->with("views_data:{$random_table_name}:en", []);
$views_data = $this->viewsData
->getAll();
$this
->assertSame($expected_views_data, $views_data);
$views_data = $this->viewsData
->get($table_name);
$this
->assertSame($expected_views_data[$table_name], $views_data);
$views_data = $this->viewsData
->get($table_name_2);
$this
->assertSame($expected_views_data[$table_name_2], $views_data);
$views_data = $this->viewsData
->get($random_table_name);
$this
->assertSame([], $views_data);
$this->viewsData
->clear();
$this->viewsData
->getAll();
$this->viewsData
->get($table_name);
$this->viewsData
->get($table_name_2);
$this->viewsData
->get($random_table_name);
}
public function testFullGetCache() {
$expected_views_data = $this
->viewsDataWithProvider();
$this
->setupMockedModuleHandler();
$this->moduleHandler
->expects($this
->once())
->method('alter')
->with('views_data', $expected_views_data);
$this->cacheBackend
->expects($this
->once())
->method('get')
->with("views_data:en")
->will($this
->returnValue(FALSE));
$views_data = $this->viewsData
->getAll();
$this
->assertSame($expected_views_data, $views_data);
$views_data = $this->viewsData
->getAll();
$this
->assertSame($expected_views_data, $views_data);
}
public function testSingleTableGetCache() {
$table_name = 'views_test_data';
$expected_views_data = $this
->viewsDataWithProvider();
$this
->setupMockedModuleHandler();
$this->moduleHandler
->expects($this
->once())
->method('alter')
->with('views_data', $this
->viewsDataWithProvider());
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with("views_data:{$table_name}:en")
->will($this
->returnValue(FALSE));
$this->cacheBackend
->expects($this
->at(1))
->method('get')
->with("views_data:en")
->will($this
->returnValue(FALSE));
$views_data = $this->viewsData
->get($table_name);
$this
->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching views data by table works as expected.');
$views_data = $this->viewsData
->get($table_name);
$this
->assertSame($expected_views_data[$table_name], $views_data, 'Make sure fetching cached views data by table works as expected.');
$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.');
}
public function testNonExistingTableGetCache() {
$random_table_name = $this
->randomMachineName();
$this
->setupMockedModuleHandler();
$this->moduleHandler
->expects($this
->once())
->method('alter')
->with('views_data', $this
->viewsDataWithProvider());
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with("views_data:{$random_table_name}:en")
->will($this
->returnValue(FALSE));
$this->cacheBackend
->expects($this
->at(1))
->method('get')
->with("views_data:en")
->will($this
->returnValue(FALSE));
$views_data = $this->viewsData
->get($random_table_name);
$this
->assertSame([], $views_data, 'Make sure fetching views data for an invalid table returns an empty array.');
$views_data = $this->viewsData
->get($random_table_name);
$this
->assertSame([], $views_data, 'Make sure fetching views data for an invalid table returns an empty array.');
}
public function testCacheCallsWithSameTableMultipleTimes() {
$expected_views_data = $this
->viewsDataWithProvider();
$this
->setupMockedModuleHandler();
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with('views_data:views_test_data:en');
$this->cacheBackend
->expects($this
->at(1))
->method('get')
->with('views_data:en');
$this->cacheBackend
->expects($this
->at(2))
->method('set')
->with('views_data:en', $expected_views_data);
$this->cacheBackend
->expects($this
->at(3))
->method('set')
->with('views_data:views_test_data:en', $expected_views_data['views_test_data']);
$table_name = 'views_test_data';
for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData
->get($table_name);
$this
->assertSame($expected_views_data['views_test_data'], $views_data);
}
}
public function testCacheCallsWithSameTableMultipleTimesAndWarmCache() {
$expected_views_data = $this
->viewsDataWithProvider();
$this->moduleHandler
->expects($this
->never())
->method('getImplementations');
$this->cacheBackend
->expects($this
->once())
->method('get')
->with('views_data:views_test_data:en')
->will($this
->returnValue((object) [
'data' => $expected_views_data['views_test_data'],
]));
$this->cacheBackend
->expects($this
->never())
->method('set');
for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData
->get('views_test_data');
$this
->assertSame($expected_views_data['views_test_data'], $views_data);
}
}
public function testCacheCallsWithWarmCacheAndDifferentTable() {
$expected_views_data = $this
->viewsDataWithProvider();
$this->moduleHandler
->expects($this
->never())
->method('getImplementations');
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with('views_data:views_test_data_2:en');
$this->cacheBackend
->expects($this
->at(1))
->method('get')
->with('views_data:en')
->will($this
->returnValue((object) [
'data' => $expected_views_data,
]));
$this->cacheBackend
->expects($this
->at(2))
->method('set')
->with('views_data:views_test_data_2:en', $expected_views_data['views_test_data_2']);
for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData
->get('views_test_data_2');
$this
->assertSame($expected_views_data['views_test_data_2'], $views_data);
}
}
public function testCacheCallsWithWarmCacheAndInvalidTable() {
$expected_views_data = $this
->viewsDataWithProvider();
$non_existing_table = $this
->randomMachineName();
$this->moduleHandler
->expects($this
->never())
->method('getImplementations');
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with("views_data:{$non_existing_table}:en");
$this->cacheBackend
->expects($this
->at(1))
->method('get')
->with('views_data:en')
->will($this
->returnValue((object) [
'data' => $expected_views_data,
]));
$this->cacheBackend
->expects($this
->at(2))
->method('set')
->with("views_data:{$non_existing_table}:en", []);
for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData
->get($non_existing_table);
$this
->assertSame([], $views_data);
}
}
public function testCacheCallsWithWarmCacheForInvalidTable() {
$non_existing_table = $this
->randomMachineName();
$this->moduleHandler
->expects($this
->never())
->method('getImplementations');
$this->cacheBackend
->expects($this
->once())
->method('get')
->with("views_data:{$non_existing_table}:en")
->will($this
->returnValue((object) [
'data' => [],
]));
$this->cacheBackend
->expects($this
->never())
->method('set');
for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData
->get($non_existing_table);
$this
->assertSame([], $views_data);
}
}
public function testCacheCallsWithoutWarmCacheAndGetAllTables() {
$expected_views_data = $this
->viewsDataWithProvider();
$this
->setupMockedModuleHandler();
$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);
for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData
->getAll();
$this
->assertSame($expected_views_data, $views_data);
}
}
public function testCacheCallsWithWarmCacheAndGetAllTables() {
$expected_views_data = $this
->viewsDataWithProvider();
$this->moduleHandler
->expects($this
->never())
->method('getImplementations');
$this->cacheBackend
->expects($this
->once())
->method('get')
->with("views_data:en")
->will($this
->returnValue((object) [
'data' => $expected_views_data,
]));
$this->cacheBackend
->expects($this
->never())
->method('set');
for ($i = 0; $i < 5; $i++) {
$views_data = $this->viewsData
->getAll();
$this
->assertSame($expected_views_data, $views_data);
}
}
public function testCacheCallsWithoutWarmCacheAndGetMultipleTables() {
$expected_views_data = $this
->viewsDataWithProvider();
$table_name = 'views_test_data';
$table_name_2 = 'views_test_data_2';
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with("views_data:{$table_name}:en")
->will($this
->returnValue(FALSE));
$this->cacheBackend
->expects($this
->at(1))
->method('get')
->with('views_data:en')
->will($this
->returnValue((object) [
'data' => $expected_views_data,
]));
$this->cacheBackend
->expects($this
->at(2))
->method('set')
->with("views_data:{$table_name}:en", $expected_views_data[$table_name]);
$this->cacheBackend
->expects($this
->at(3))
->method('get')
->with("views_data:{$table_name_2}:en")
->will($this
->returnValue(FALSE));
$this->cacheBackend
->expects($this
->at(4))
->method('set')
->with("views_data:{$table_name_2}:en", $expected_views_data[$table_name_2]);
$this
->assertSame($expected_views_data[$table_name], $this->viewsData
->get($table_name));
$this
->assertSame($expected_views_data[$table_name_2], $this->viewsData
->get($table_name_2));
$this
->assertSame($expected_views_data[$table_name], $this->viewsData
->get($table_name));
$this
->assertSame($expected_views_data[$table_name_2], $this->viewsData
->get($table_name_2));
}
public function testGetAllEqualsToGetNull() {
$expected_views_data = $this
->viewsDataWithProvider();
$this
->setupMockedModuleHandler();
$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);
for ($i = 0; $i < 5; $i++) {
$this
->assertSame($expected_views_data, $this->viewsData
->getAll());
$this
->assertSame($expected_views_data, $this->viewsData
->get());
}
}
}