resetCache example

$this->submitForm($edit, 'Save (this translation)');

    // Clear the path lookup cache.     $this->container->get('path_alias.manager')->cacheClear();

    // Languages are cached on many levels, and we need to clear those caches.     $this->container->get('language_manager')->reset();
    $this->rebuildContainer();
    $languages = $this->container->get('language_manager')->getLanguages();

    // Ensure the node was created.     $node_storage->resetCache([$english_node->id()]);
    $english_node = $node_storage->load($english_node->id());
    $english_node_french_translation = $english_node->getTranslation('fr');
    $this->assertTrue($english_node->hasTranslation('fr'), 'Node found in database.');

    // Confirm that the alias works.     $this->drupalGet('fr' . $edit['path[0][alias]']);
    $this->assertSession()->pageTextContains($english_node_french_translation->body->value);

    // Confirm that the alias is returned for the URL. Languages are cached on     // many levels, and we need to clear those caches.     $this->container->get('language_manager')->reset();
    
// Look for a test shortcut weight select form element.       $this->assertSession()->fieldExists('shortcuts[links][' . $shortcut->id() . '][weight]');

      // Change the weight of the shortcut.       $edit['shortcuts[links][' . $shortcut->id() . '][weight]'] = $weight;
      $weight--;
    }

    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains("The shortcut set has been updated.");

    \Drupal::entityTypeManager()->getStorage('shortcut')->resetCache();
    // Check to ensure that the shortcut weights have changed and that     // ShortcutSet::.getShortcuts() returns shortcuts in the new order.     $this->assertSame(array_reverse(array_keys($shortcuts))array_keys($set->getShortcuts()));
  }

  /** * Tests switching a user's own shortcut set. */
  public function testShortcutSetSwitchOwn() {
    $new_set = $this->generateShortcutSet($this->randomMachineName());

    
'type' => 'display_build_alter_bundle',
      ]);
      $entity->save();
      $entity_ids[] = $entity->id();
    }

    /** @var \Drupal\entity_test\EntityTestViewBuilder $view_builder */
    $view_builder = $this->container->get('entity_type.manager')->getViewBuilder('entity_test');

    /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
    $storage->resetCache();
    $entities = $storage->loadMultiple($entity_ids);

    $build = $view_builder->viewMultiple($entities);

    $output = $renderer->renderRoot($build);
    $this->setRawContent($output->__toString());
    // Confirm that the content added in     // entity_test_entity_display_build_alter() appears multiple times, not     // just for the final entity.     foreach ($entity_ids as $id) {
      $this->assertText('Content added in hook_entity_display_build_alter for entity id ' . $id);
    }
/** * Checks whether the view has calculated its cacheability metadata. * * @param bool $expected_result * TRUE if it is expected that the cacheability metadata has been * calculated. FALSE otherwise. * * @internal */
  protected function assertCacheableMetadataHasBeenCalculated(bool $expected_result): void {
    $this->state->resetCache();
    $this->assertEquals($expected_result$this->state->get('views_test_cacheable_metadata_has_been_accessed'));
  }

  /** * Resets the state so we are ready for a new test. */
  protected function resetState() {
    $this->state->set('views_test_cacheable_metadata_has_been_accessed', FALSE);
  }

}

  protected function renderTestEntity($id$view_mode = 'full', $reset = TRUE) {
    if ($reset) {
      $this->container->get('entity_type.manager')->getStorage('entity_test')->resetCache([$id]);
    }
    $entity = EntityTest::load($id);
    $display = EntityViewDisplay::collectRenderDisplay($entity$view_mode);
    $build = $display->build($entity);
    return (string) $this->container->get('renderer')->renderRoot($build);
  }

  /** * Sets the site timezone to a given timezone. * * @param string $timezone * The timezone identifier to set. */
'd6_term_node',
      'd6_term_node_translation',
    ]);
  }

  /** * Tests the Drupal 6 term-node association to Drupal 8 migration. */
  public function testTermNode() {
    $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->resetCache([18, 21]);

    // Test with translated content type employee. Vocabulary     // field_vocabulary_name_much_longe is a localized vocabulary and     // field_vocabulary_3_i_2_ is a per language vocabulary.     // An untranslated node.     $node = Node::load(18);
    // A localized vocabulary.     $this->assertSame('15', $node->field_vocabulary_name_much_longe[0]->target_id);
    // Per language vocabulary.     $this->assertSame('5', $node->field_vocabulary_3_i_2_[0]->target_id);

    
      2 => 1,
      // Page of comment 4       3 => 1,
      // Page of comment 4       4 => 1,
      // Page of comment 4       5 => 1,
      // Page of comment 0       6 => 0,
    ];

    \Drupal::entityTypeManager()->getStorage('node')->resetCache([$node->id()]);
    $node = Node::load($node->id());
    foreach ($expected_pages as $new_replies => $expected_page) {
      $returned_page = \Drupal::entityTypeManager()->getStorage('comment')
        ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies$node, 'comment');
      $this->assertEquals($expected_page$returned_pagenew FormattableMarkup('Threaded mode, @new replies: expected page @expected, returned page @returned.', ['@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page]));
    }
  }

  /** * Confirms comment paging works correctly with two pagers. */
  
// After previewing, make the alt field required. It cannot be required     // during preview because the form validation will fail.     $instance->setSetting('alt_field_required', 1);
    $instance->save();

    // Create alt text for the image.     $alt = $this->randomMachineName();

    // Save node.     $nid = $this->uploadNodeImage($test_image$field_name, 'article', $alt);
    $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);

    // Test that the default formatter is being used.     /** @var \Drupal\file\FileInterface $file */
    $file = $node->{$field_name}->entity;
    $image_uri = $file->getFileUri();
    $image = [
      '#theme' => 'image',
      '#uri' => $image_uri,
      '#width' => 40,
      '#height' => 20,
      
    $migration = $this->getMigration('d6_upload');
    $migration->set('requirements', []);
    $this->executeMigration($migration);
  }

  /** * Tests upload migration from Drupal 6 to Drupal 8. */
  public function testUpload() {
    $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->resetCache([1, 2, 12]);

    $nodes = Node::loadMultiple([1, 2, 12]);
    $node = $nodes[1];
    $this->assertEquals('en', $node->langcode->value);
    $this->assertCount(1, $node->upload);
    $this->assertSame('1', $node->upload[0]->target_id);
    $this->assertSame('file 1-1-1', $node->upload[0]->description);
    $this->assertFalse($node->upload[0]->isDisplayed());

    $node = $nodes[2];
    $this->assertEquals('en', $node->langcode->value);
    

  protected function assertMenuLink(array $expected_item$menu_plugin_id) {
    // Retrieve the menu link.     /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $menu_link_manager->resetDefinitions();
    // Reset the static load cache.     \Drupal::entityTypeManager()->getStorage('menu_link_content')->resetCache();
    $definition = $menu_link_manager->getDefinition($menu_plugin_id);

    $entity = NULL;

    // Pull the path from the menu link content.     if (str_starts_with($menu_plugin_id, 'menu_link_content')) {
      [$uuid] = explode(':', $menu_plugin_id, 2);
      /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $entity */
      $entity = \Drupal::service('entity.repository')
        ->loadEntityByUuid('menu_link_content', $uuid);
    }

    


  /** * {@inheritdoc} */
  public function updateLastLoginTimestamp(UserInterface $account) {
    $this->database->update($this->getDataTable())
      ->fields(['login' => $account->getLastLoginTime()])
      ->condition('uid', $account->id())
      ->execute();
    // Ensure that the entity cache is cleared.     $this->resetCache([$account->id()]);
  }

  /** * {@inheritdoc} */
  public function updateLastAccessTimestamp(AccountInterface $account$timestamp) {
    $this->database->update($this->getDataTable())
      ->fields([
        'access' => $timestamp,
      ])
      ->condition('uid', $account->id())
      
/** * Tests changing user permissions through the permissions pages. */
  public function testUserPermissionChanges() {
    $permissions_hash_generator = $this->container->get('user_permissions_hash_generator');

    $storage = $this->container->get('entity_type.manager')->getStorage('user_role');

    // Create an additional role and mark it as admin role.     Role::create(['is_admin' => TRUE, 'id' => 'administrator', 'label' => 'Administrator'])->save();
    $storage->resetCache();

    $this->drupalLogin($this->adminUser);
    $rid = $this->rid;
    $account = $this->adminUser;
    $previous_permissions_hash = $permissions_hash_generator->generate($account);
    $this->assertSame($previous_permissions_hash$permissions_hash_generator->generate($this->loggedInUser));

    // Add a permission.     $this->assertFalse($account->hasPermission('administer users'), 'User does not have "administer users" permission.');
    $edit = [];
    $edit[$rid . '[administer users]'] = TRUE;
    

  public function testTermReorder() {
    $assert = $this->assertSession();
    $this->createTerm($this->vocabulary);
    $this->createTerm($this->vocabulary);
    $this->createTerm($this->vocabulary);

    $taxonomy_storage = $this->container->get('entity_type.manager')->getStorage('taxonomy_term');

    // Fetch the created terms in the default alphabetical order, i.e. term1     // precedes term2 alphabetically, and term2 precedes term3.     $taxonomy_storage->resetCache();
    [$term1$term2$term3] = $taxonomy_storage->loadTree($this->vocabulary->id(), 0, NULL, TRUE);

    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');

    // Each term has four hidden fields, "tid:1:0[tid]", "tid:1:0[parent]",     // "tid:1:0[depth]", and "tid:1:0[weight]". Change the order to term2,     // term3, term1 by setting weight property, make term3 a child of term2 by     // setting the parent and depth properties, and update all hidden fields.     $hidden_edit = [
      'terms[tid:' . $term2->id() . ':0][term][tid]' => $term2->id(),
      'terms[tid:' . $term2->id() . ':0][term][parent]' => 0,
      
// The node author details must be open.     $this->assertSession()->responseContains('<details class="node-form-author js-form-wrapper form-wrapper" data-drupal-selector="edit-author" id="edit-author" open="open">');
    // Only one extra details element should now be open.     $open_details_elements++;
    $this->assertCount($open_details_elements$this->cssSelect('details[open="open"]'), 'Exactly one extra open &lt;details&gt; element found.');

    // Edit the same node, save it and verify it's unpublished after unchecking     // the 'Published' boolean_checkbox and clicking 'Save'.     $this->drupalGet("node/" . $node->id() . "/edit");
    $edit = ['status[value]' => FALSE];
    $this->submitForm($edit, 'Save');
    $this->nodeStorage->resetCache([$node->id()]);
    $node = $this->nodeStorage->load($node->id());
    $this->assertFalse($node->isPublished(), 'Node is unpublished');
  }

  /** * Tests changing a node's "authored by" field. */
  public function testNodeEditAuthoredBy() {
    $this->drupalLogin($this->adminUser);

    // Create node to edit.


    $result = $node->access('update', $account, TRUE);
    $this->assertFalse($result->isAllowed());
    $this->assertEqualsCanonicalizing(['user.permissions']$result->getCacheContexts());
    $this->assertEqualsCanonicalizing(['config:workflows.workflow.editorial', 'node:' . $node->id()]$result->getCacheTags());
    $this->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $result->getCacheMaxAge());

    $authenticated->grantPermission('use editorial transition create_new_draft');
    $authenticated->save();

    \Drupal::entityTypeManager()->getAccessControlHandler('node')->resetCache();
    $result = $node->access('update', $account, TRUE);
    $this->assertTrue($result->isAllowed());
    $this->assertEqualsCanonicalizing(['user.permissions']$result->getCacheContexts());
    $this->assertEqualsCanonicalizing(['config:workflows.workflow.editorial', 'node:' . $node->id()]$result->getCacheTags());
    $this->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $result->getCacheMaxAge());
  }

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