rebuildContainer example

$this->assertCacheContext('user.node_grants:view');

    // The root user has the 'bypass node access' permission, which means the     // node grants cache context is not necessary.     $this->drupalLogin($this->rootUser);
    $this->drupalGet(new Url('node_access_test_auto_bubbling'));
    $this->assertNoCacheContext('user.node_grants:view');
    $this->drupalLogout();

    // Uninstall the module with the only hook_node_grants() implementation.     $this->container->get('module_installer')->uninstall(['node_access_test']);
    $this->rebuildContainer();

    // Because there are no node grants defined, there also is no need for the     // node grants cache context to be bubbled.     $this->drupalGet(new Url('node_access_test_auto_bubbling'));
    $this->assertNoCacheContext('user.node_grants:view');
  }

  /** * Tests that the user cache contexts are correctly set. */
  public function testNodeAccessCacheContext() {
    
$this->installConfig('system');
    $this->installConfig('language');

    /** @var \Drupal\language\Config\LanguageConfigFactoryOverride $config_factory_override */
    $config_factory_override = \Drupal::service('language.config_factory_override');
    $this->assertEquals('en', $config_factory_override->getLanguage()->getId());

    ConfigurableLanguage::createFromLangcode('de')->save();

    // Invalidate the container.     $this->config('system.site')->set('default_langcode', 'de')->save();
    $this->container->get('kernel')->rebuildContainer();

    $config_factory_override = \Drupal::service('language.config_factory_override');
    $this->assertEquals('de', $config_factory_override->getLanguage()->getId());
  }

}
/** * Confirms entity is shown in user's language by default. */
  public function testEntityLanguage() {
    $language = ConfigurableLanguage::createFromLangcode('de');
    $language->save();
    \Drupal::configFactory()->getEditable('language.negotiation')
      ->set('url.prefixes', ['de' => 'de'])
      ->save();

    // The container must be recreated after adding a new language.     $this->rebuildContainer();

    $node = $this->drupalCreateNode(['title' => 'English label']);
    $translation = $node->addTranslation('de');
    $translation->setTitle('Deutscher Titel')->save();

    $this->drupalGet("/paramconverter_test/node/" . $node->id() . "/test_language");
    $this->assertSession()->pageTextContains("English label");
    $this->drupalGet("paramconverter_test/node/" . $node->id() . "/test_language", ['language' => $language]);
    $this->assertSession()->pageTextContains("Deutscher Titel");
  }

}
$this->assertSession()->statusCodeEquals(200);
      $langcode_url = 'admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language';
      $this->assertSession()->linkByHrefNotExists($langcode_url);
      $assert_session->linkNotExistsExact('Content language selected for page');
      $this->assertSession()->linkNotExists('Content language of view row');
    }

    // Make the site multilingual and test the options again.     $this->container->get('module_installer')->install(['language', 'content_translation']);
    ConfigurableLanguage::createFromLangcode('hu')->save();
    $this->resetAll();
    $this->rebuildContainer();

    // Language options should now exist with entity language the default.     foreach ($test_views as $view_name => $display) {
      $this->drupalGet('admin/structure/views/view/' . $view_name);
      $this->assertSession()->statusCodeEquals(200);
      $langcode_url = 'admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language';
      if ($view_name == 'test_view') {
        $this->assertSession()->linkByHrefNotExists($langcode_url);
        $assert_session->linkNotExistsExact('Content language selected for page');
        $this->assertSession()->linkNotExists('Content language of view row');
      }
      
'#template' => 'test-with-context <label>{{ unsafe_content }}</label>',
      '#context' => ['unsafe_content' => $unsafe_string],
    ];
    $this->assertSame('test-with-context <label>' . Html::escape($unsafe_string) . '</label>', (string) $renderer->renderRoot($element));

    // Enable twig_auto_reload and twig_debug.     $settings = Settings::getAll();
    $settings['twig_debug'] = TRUE;
    $settings['twig_auto_reload'] = TRUE;

    new Settings($settings);
    $this->container = \Drupal::service('kernel')->rebuildContainer();
    \Drupal::setContainer($this->container);

    $element = [];
    $element['test'] = [
      '#type' => 'inline_template',
      '#template' => 'test-with-context {{ llama }}',
      '#context' => ['llama' => 'muuh'],
    ];
    $element_copy = $element;
    // Render it twice so that twig caching is triggered.     $this->assertEquals('test-with-context muuh', $renderer->renderRoot($element));
    
protected function drupalLogin(AccountInterface $account) {
    parent::drupalLogin($account);
    $session_data = $this->container->get('session_handler.write_safe')->read($this->getSession()->getCookie($this->getSessionName()));
    $csrf_token_seed = unserialize(explode('_sf2_meta|', $session_data)[1])['s'];
    $this->container->get('session_manager.metadata_bag')->setCsrfTokenSeed($csrf_token_seed);
  }

  /** * {@inheritdoc} */
  protected function rebuildContainer() {
    parent::rebuildContainer();

    // Ensure that the CSRF token seed is reset on container rebuild.     if ($this->loggedInUser) {
      $current_user = $this->loggedInUser;
      $this->drupalLogout();
      $this->drupalLogin($current_user);
    }
  }

  /** * {@inheritdoc} */
/** * {@inheritdoc} */
  protected function setUp($import_test_views = TRUE): void {
    parent::setUp($import_test_views);
    ConfigurableLanguage::createFromLangcode('de')->save();
    $this->installSchema('node', 'node_access');
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');

    // $this->installConfig('node');     $this->container->get('kernel')->rebuildContainer();

    $node_type = NodeType::create(['type' => 'article']);
    $node_type->save();

    FieldStorageConfig::create([
      'field_name' => 'text',
      'entity_type' => 'node',
      'type' => 'string',
    ])->save();

    FieldConfig::create([
      
$user = $this->drupalCreateUser(['administer modules']);
    $this->drupalLogin($user);
    $this->drupalGet('admin/modules/uninstall');
    $this->assertSession()->fieldDisabled('uninstall[dblog]');
    $this->getSession()->getPage()->checkField('uninstall[ban]');
    $this->click('#edit-submit');
    // Click the confirm button.     $this->click('#edit-submit');
    $this->assertSession()->responseContains('The selected modules have been uninstalled.');
    // We've uninstalled a module therefore we need to rebuild the container in     // the test runner.     $this->rebuildContainer();
    $this->assertFalse($this->container->get('module_handler')->moduleExists('ban'));
    try {
      $this->container->get('module_installer')->uninstall(['dblog']);
      $this->fail('Uninstalled dblog module.');
    }
    catch (ModuleUninstallValidatorException $e) {
      $this->assertStringContainsString('The Testing install profile dependencies module is required', $e->getMessage());
    }
  }

}
$this->assertTermAccess($unpublished_term, 'update', FALSE, "The following permissions are required: 'edit terms in {$vocabulary->id()}' OR 'administer taxonomy'.");

    $this->drupalGet('taxonomy/term/' . $published_term->id() . '/delete');
    $assert_session->statusCodeEquals(403);
    $this->assertTermAccess($published_term, 'delete', FALSE, "The following permissions are required: 'delete terms in {$vocabulary->id()}' OR 'administer taxonomy'.");
    $this->drupalGet('taxonomy/term/' . $unpublished_term->id() . '/delete');
    $assert_session->statusCodeEquals(403);
    $this->assertTermAccess($unpublished_term, 'delete', FALSE, "The following permissions are required: 'delete terms in {$vocabulary->id()}' OR 'administer taxonomy'.");

    // Install the Views module and repeat the checks for the 'view' permission.     \Drupal::service('module_installer')->install(['views'], TRUE);
    $this->rebuildContainer();

    $this->drupalGet('taxonomy/term/' . $published_term->id());
    $assert_session->statusCodeEquals(200);

    // @todo Change this assertion to expect a 403 status code when     // https://www.drupal.org/project/drupal/issues/2983070 is fixed.     $this->drupalGet('taxonomy/term/' . $unpublished_term->id());
    $assert_session->statusCodeEquals(404);
  }

  /** * Checks access on taxonomy term. * * @param \Drupal\taxonomy\TermInterface $term * A taxonomy term entity. * @param string $access_operation * The entity operation, e.g. 'view', 'edit', 'delete', etc. * @param bool $access_allowed * Whether the current use has access to the given operation or not. * @param string $access_reason * (optional) The reason of the access result. * * @internal */
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    ConfigurableLanguage::create(['id' => 'es'])->save();
    ConfigurableLanguage::create(['id' => 'fr'])->save();

    // In order to reflect the changes for a multilingual site in the container     // we have to rebuild it.     $this->rebuildContainer();

    $this->createTranslatableEntity();

    $user = $this->drupalCreateUser(['view test entity']);
    $this->drupalLogin($user);
  }

  /** * Tests default with content language remaining same as interface language. */
  public function testDefaultConfiguration() {
    

  protected function setInstallProfile($profile) {
    $this->container->get('config.factory')
      ->getEditable('core.extension')
      ->set('profile', $profile)
      ->save();

    // The installation profile is provided by a container parameter. Saving     // the configuration doesn't automatically trigger invalidation     $this->container->get('kernel')->rebuildContainer();
  }

  /** * Stops test execution. */
  protected function stop() {
    $this->getTestResultObject()->stop();
  }

  /** * Dumps the current state of the virtual filesystem to STDOUT. */
$this->drupalPlaceBlock('local_tasks_block', ['id' => 'test_role_admin_test_local_tasks_block']);

    // Create and log in user.     $this->adminUser = $this->drupalCreateUser($permissions);
    $this->drupalLogin($this->adminUser);

    // Add languages.     foreach ($this->langcodes as $langcode) {
      ConfigurableLanguage::createFromLangcode($langcode)->save();
    }
    $this->resetAll();
    $this->rebuildContainer();
  }

  public function testTranslatedStrings() {
    $translation_url = 'admin/structure/views/view/files/translate/fr/add';
    $edit_url = 'admin/structure/views/view/files';

    // Check the original string.     $this->drupalGet($edit_url);
    $this->assertSession()->titleEquals('Files (File) | Drupal');

    // Translate the label of the view.

  protected function enableViewsTestModule() {
    // Define the schema and views data variable before enabling the test module.     \Drupal::state()->set('views_test_data_schema', $this->schemaDefinition());
    \Drupal::state()->set('views_test_data_views_data', $this->viewsData());

    \Drupal::service('module_installer')->install(['views_test_data']);
    $this->resetAll();
    $this->rebuildContainer();
    $this->container->get('module_handler')->reload();

    // Load the test dataset.     $data_set = $this->dataSet();
    $query = Database::getConnection()->insert('views_test_data')
      ->fields(array_keys($data_set[0]));
    foreach ($data_set as $record) {
      $query->values($record);
    }
    $query->execute();
  }

  
// Verify that getEntityClass with a non-existing bundle returns the entity     // class.     $entity_class = $this->storage->getEntityClass('custom');
    $this->assertEquals(EntityTest::class$entity_class);
  }

  /** * Tests making use of a custom bundle class for an entity without bundles. */
  public function testEntityNoBundleSubclass() {
    $this->container->get('state')->set('entity_test_bundle_class_enable_user_class', TRUE);
    $this->container->get('kernel')->rebuildContainer();
    $this->entityTypeManager->clearCachedDefinitions();
    $this->drupalSetUpCurrentUser();
    $entity = User::load(1);
    $this->assertInstanceOf(EntityTestUserClass::class$entity);
  }

  /** * Checks exception is thrown if two bundles share the same bundle class. * * @covers Drupal\Core\Entity\ContentEntityStorageBase::create */
  
/** * Tests that late middlewares are automatically flagged lazy. */
  public function testLazyLateMiddlewares() {
    $this->assertFalse($this->container->getDefinition('http_middleware.reverse_proxy')->isLazy(), 'lazy flag on http_middleware.reverse_proxy definition is not set');
    $this->assertFalse($this->container->getDefinition('http_middleware.kernel_pre_handle')->isLazy(), 'lazy flag on http_middleware.kernel_pre_handle definition is not set');
    $this->assertFalse($this->container->getDefinition('http_middleware.session')->isLazy(), 'lazy flag on http_middleware.session definition is not set');
    $this->assertFalse($this->container->getDefinition('http_kernel.basic')->isLazy(), 'lazy flag on http_kernel.basic definition is not set');

    \Drupal::service('module_installer')->install(['page_cache']);
    $this->container = \Drupal::service('kernel')->rebuildContainer();

    $this->assertFalse($this->container->getDefinition('http_middleware.reverse_proxy')->isLazy(), 'lazy flag on http_middleware.reverse_proxy definition is not set');
    $this->assertFalse($this->container->getDefinition('http_middleware.page_cache')->isLazy(), 'lazy flag on http_middleware.page_cache definition is not set');
    $this->assertTrue($this->container->getDefinition('http_middleware.kernel_pre_handle')->isLazy(), 'lazy flag on http_middleware.kernel_pre_handle definition is automatically set if page_cache is enabled.');
    $this->assertTrue($this->container->getDefinition('http_middleware.session')->isLazy(), 'lazy flag on http_middleware.session definition is automatically set if page_cache is enabled.');
    $this->assertTrue($this->container->getDefinition('http_kernel.basic')->isLazy(), 'lazy flag on http_kernel.basic definition is automatically set if page_cache is enabled.');
  }

}
Home | Imprint | This part of the site doesn't use cookies.