You are here

public function RebuildScriptTest::testRebuild in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/UpdateSystem/RebuildScriptTest.php \Drupal\Tests\system\Functional\UpdateSystem\RebuildScriptTest::testRebuild()
  2. 9 core/modules/system/tests/src/Functional/UpdateSystem/RebuildScriptTest.php \Drupal\Tests\system\Functional\UpdateSystem\RebuildScriptTest::testRebuild()

Tests redirect in rebuild.php.

File

core/modules/system/tests/src/Functional/UpdateSystem/RebuildScriptTest.php, line 29

Class

RebuildScriptTest
Tests the rebuild script access and functionality.

Namespace

Drupal\Tests\system\Functional\UpdateSystem

Code

public function testRebuild() {
  $cache = $this->container
    ->get('cache.default');
  $cache
    ->set('rebuild_test', TRUE);
  $this
    ->drupalGet(Url::fromUri('base:core/rebuild.php'));
  $this
    ->assertSession()
    ->addressEquals(new Url('<front>'));
  $this
    ->assertInstanceOf(\stdClass::class, $cache
    ->get('rebuild_test'));
  $settings['settings']['rebuild_access'] = (object) [
    'value' => TRUE,
    'required' => TRUE,
  ];
  $this
    ->writeSettings($settings);
  $this
    ->rebuildAll();
  $cache
    ->set('rebuild_test', TRUE);
  \Drupal::state()
    ->set('container_rebuild_test.count', 0);
  $this
    ->drupalGet(Url::fromUri('base:core/rebuild.php'));
  $this
    ->assertSession()
    ->addressEquals(new Url('<front>'));
  $this
    ->assertFalse($cache
    ->get('rebuild_test'));
  $this
    ->refreshVariables();
  $this
    ->assertSame(1, \Drupal::state()
    ->get('container_rebuild_test.count', 0));
  $this
    ->drupalGet('/container_rebuild_test/module_test/module_test_system_info_alter');
  $this
    ->assertSession()
    ->pageTextContains('module_test: core/modules/system/tests/modules/module_test');
  $this
    ->assertSession()
    ->pageTextContains('module_test_system_info_alter: true');

  // Move a module to ensure it does not break the rebuild.
  $file_system = new Filesystem();
  $file_system
    ->mirror('core/modules/system/tests/modules/module_test', $this->siteDirectory . '/modules/module_test');
  \Drupal::state()
    ->set('container_rebuild_test.count', 0);
  $this
    ->drupalGet(Url::fromUri('base:core/rebuild.php'));
  $this
    ->assertSession()
    ->addressEquals(new Url('<front>'));
  $this
    ->refreshVariables();
  $this
    ->assertSame(1, \Drupal::state()
    ->get('container_rebuild_test.count', 0));
  $this
    ->drupalGet('/container_rebuild_test/module_test/module_test_system_info_alter');
  $this
    ->assertSession()
    ->pageTextContains('module_test: ' . $this->siteDirectory . '/modules/module_test');
  $this
    ->assertSession()
    ->pageTextContains('module_test_system_info_alter: true');

  // Disable a module by writing to the core.extension list.
  $this
    ->config('core.extension')
    ->clear('module.module_test')
    ->save();
  \Drupal::state()
    ->set('container_rebuild_test.count', 0);
  $this
    ->drupalGet(Url::fromUri('base:core/rebuild.php'));
  $this
    ->assertSession()
    ->addressEquals(new Url('<front>'));
  $this
    ->refreshVariables();
  $this
    ->assertSame(1, \Drupal::state()
    ->get('container_rebuild_test.count', 0));
  $this
    ->drupalGet('/container_rebuild_test/module_test/module_test_system_info_alter');
  $this
    ->assertSession()
    ->pageTextContains('module_test: not installed');
  $this
    ->assertSession()
    ->pageTextContains('module_test_system_info_alter: false');

  // Enable a module by writing to the core.extension list.
  $modules = $this
    ->config('core.extension')
    ->get('module');
  $modules['module_test'] = 0;
  $this
    ->config('core.extension')
    ->set('module', module_config_sort($modules))
    ->save();
  \Drupal::state()
    ->set('container_rebuild_test.count', 0);
  $this
    ->drupalGet(Url::fromUri('base:core/rebuild.php'));
  $this
    ->assertSession()
    ->addressEquals(new Url('<front>'));
  $this
    ->refreshVariables();
  $this
    ->assertSame(1, \Drupal::state()
    ->get('container_rebuild_test.count', 0));
  $this
    ->drupalGet('/container_rebuild_test/module_test/module_test_system_info_alter');
  $this
    ->assertSession()
    ->pageTextContains('module_test: ' . $this->siteDirectory . '/modules/module_test');
  $this
    ->assertSession()
    ->pageTextContains('module_test_system_info_alter: true');

  // Test how many container rebuild occur when there is no cached container.
  \Drupal::state()
    ->set('container_rebuild_test.count', 0);
  \Drupal::service('kernel')
    ->invalidateContainer();
  $this
    ->drupalGet(Url::fromUri('base:core/rebuild.php'));
  $this
    ->assertSession()
    ->addressEquals(new Url('<front>'));
  $this
    ->assertFalse($cache
    ->get('rebuild_test'));
  $this
    ->refreshVariables();
  $this
    ->assertSame(1, \Drupal::state()
    ->get('container_rebuild_test.count', 0));
}