function ViewsDataTest::testFullyLoadedPreventsRedundantLoading

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

Tests that fullyLoaded prevents redundant data loading.

Once fullyLoaded is TRUE, the subsequent calls to getAll() should not trigger data rebuilding or cache lookups.

File

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

Class

ViewsDataTest
Tests Drupal\views\ViewsData.

Namespace

Drupal\Tests\views\Unit

Code

public function testFullyLoadedPreventsRedundantLoading() : void {
  $expected_views_data = $this->viewsDataWithProvider();
  $this->setupMockedModuleHandler();
  $this->moduleHandler
    ->expects($this->once())
    ->method('alter')
    ->with('views_data', $expected_views_data);
  // Cache should only be checked once.
  $this->cacheBackend
    ->expects($this->once())
    ->method('get')
    ->with("views_data:en")
    ->willReturn(FALSE);
  // First call loads the data.
  $views_data = $this->viewsData
    ->getAll();
  $this->assertSame($expected_views_data, $views_data);
  // Use reflection to verify fullyLoaded is now TRUE.
  $reflection = new \ReflectionClass($this->viewsData);
  $fullyLoaded_property = $reflection->getProperty('fullyLoaded');
  $this->assertTrue($fullyLoaded_property->getValue($this->viewsData));
  // The Second call should use stored data without cache checks.
  $views_data = $this->viewsData
    ->getAll();
  $this->assertSame($expected_views_data, $views_data);
  // Third call to verify consistency.
  $views_data = $this->viewsData
    ->getAll();
  $this->assertSame($expected_views_data, $views_data);
}

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