function ViewsDataTest::testFullyLoadedSetByGetMethod

Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Unit/ViewsDataTest.php \Drupal\Tests\views\Unit\ViewsDataTest::testFullyLoadedSetByGetMethod()

Tests fullyLoaded with get() method triggering full data load.

When get() is called for a specific table and no cache exists, it triggers getData() which should set fullyLoaded to TRUE.

File

core/modules/views/tests/src/Unit/ViewsDataTest.php, line 824

Class

ViewsDataTest
Tests Drupal\views\ViewsData.

Namespace

Drupal\Tests\views\Unit

Code

public function testFullyLoadedSetByGetMethod() : void {
  $table_name = 'views_test_data';
  $expected_views_data = $this->viewsDataWithProvider();
  // Use reflection to access the protected fullyLoaded property.
  $reflection = new \ReflectionClass($this->viewsData);
  $fullyLoaded_property = $reflection->getProperty('fullyLoaded');
  $this->assertFalse($fullyLoaded_property->getValue($this->viewsData));
  $this->setupMockedModuleHandler();
  $this->moduleHandler
    ->expects($this->once())
    ->method('alter')
    ->with('views_data', $expected_views_data);
  // No table-specific or full cache exists.
  $gets = [
    "views_data:{$table_name}:en",
    'views_data:en',
  ];
  $this->cacheBackend
    ->expects($this->exactly(count($gets)))
    ->method('get')
    ->with($this->callback(function (string $key) use (&$gets) : bool {
    return $key === array_shift($gets);
  }))
    ->willReturn(FALSE);
  // Get specific table data, which triggers full data load.
  $views_data = $this->viewsData
    ->get($table_name);
  // After get() triggers getData(), fullyLoaded should be TRUE.
  $this->assertTrue($fullyLoaded_property->getValue($this->viewsData));
  $this->assertSame($expected_views_data[$table_name], $views_data);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.