function ViewsDataTest::testClearResetsFullyLoaded

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

Tests that clear() resets the fullyLoaded property.

After calling clear(), fullyLoaded should be reset to FALSE so that data can be reloaded on the next request.

File

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

Class

ViewsDataTest
Tests Drupal\views\ViewsData.

Namespace

Drupal\Tests\views\Unit

Code

public function testClearResetsFullyLoaded() : void {
  $expected_views_data = $this->viewsDataWithProvider();
  $this->setupMockedModuleHandler();
  $this->moduleHandler
    ->expects($this->exactly(2))
    ->method('alter')
    ->with('views_data', $expected_views_data);
  // Cache will be checked twice (before and after clear).
  $this->cacheBackend
    ->expects($this->exactly(2))
    ->method('get')
    ->with("views_data:en")
    ->willReturn(FALSE);
  $this->cacheTagsInvalidator
    ->expects($this->once())
    ->method('invalidateTags')
    ->with([
    'views_data',
  ]);
  // Use reflection to access the protected fullyLoaded property.
  $reflection = new \ReflectionClass($this->viewsData);
  $fullyLoaded_property = $reflection->getProperty('fullyLoaded');
  // Load data initially.
  $this->viewsData
    ->getAll();
  $this->assertTrue($fullyLoaded_property->getValue($this->viewsData));
  // Clear the data.
  $this->viewsData
    ->clear();
  // After clear, fullyLoaded should be FALSE again.
  $this->assertFalse($fullyLoaded_property->getValue($this->viewsData));
  // Load data again to verify it works after clear.
  $views_data = $this->viewsData
    ->getAll();
  $this->assertSame($expected_views_data, $views_data);
  $this->assertTrue($fullyLoaded_property->getValue($this->viewsData));
}

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