invalidateCaches example

$view = Views::getView('test_taxonomy_term_name');
    $view->initDisplay();
    $this->executeView($view);

    $this->assertEquals($this->term1->getName()$view->getStyle()->getField(0, 'name'));
    $this->assertEquals($this->term2->getName()$view->getStyle()->getField(1, 'name'));

    $view = Views::getView('test_taxonomy_term_name');
    $display =& $view->storage->getDisplay('default');
    $display['display_options']['fields']['name']['convert_spaces'] = TRUE;
    $view->storage->invalidateCaches();
    $this->executeView($view);

    $this->assertEquals(str_replace(' ', '-', $this->term1->getName())$view->getStyle()->getField(0, 'name'));
    $this->assertEquals($this->term2->getName()$view->getStyle()->getField(1, 'name'));

    // Enable link_to_entity option and ensure that title is displayed properly.     $view = Views::getView('test_taxonomy_term_name');
    $display =& $view->storage->getDisplay('default');
    $display['display_options']['fields']['name']['convert_spaces'] = TRUE;
    $display['display_options']['fields']['name']['settings']['link_to_entity'] = TRUE;
    $view->storage->invalidateCaches();
    
      $this->assertSame(implode('<h2>test</h2>', $items)(string) $rendered_field);
    }
  }

  /** * Sets up the testing view with random field data. * * @param \Drupal\views\ViewExecutable $view * The view to add field data to. */
  protected function prepareView(ViewExecutable $view) {
    $view->storage->invalidateCaches();
    $view->initDisplay();
    foreach ($this->fieldStorages as $field_storage) {
      $field_name = $field_storage->getName();
      $view->display_handler->options['fields'][$field_name]['id'] = $field_name;
      $view->display_handler->options['fields'][$field_name]['table'] = 'node__' . $field_name;
      $view->display_handler->options['fields'][$field_name]['field'] = $field_name;
    }
  }

  /** * Creates the testing fields. */


  /** * Tests the various testcases of empty block rendering. */
  public function testBlockEmptyRendering() {
    $url = new Url('test_page_test.test_page');
    // Remove all views_test_data entries.     \Drupal::database()->truncate('views_test_data')->execute();
    /** @var \Drupal\views\ViewEntityInterface $view */
    $view = View::load('test_view_block');
    $view->invalidateCaches();

    $block = $this->drupalPlaceBlock('views_block:test_view_block-block_1', ['label' => 'test_view_block-block_1:1', 'views_label' => 'Custom title']);
    $block_xpath = $this->assertSession()->buildXPathQuery('//div[@id = :id]', [
      ':id' => 'block-' . $block->id(),
    ]);
    $this->drupalGet('');
    $this->assertSession()->elementsCount('xpath', $block_xpath, 1);

    $display = &$view->getDisplay('block_1');
    $display['display_options']['block_hide_empty'] = TRUE;
    $view->save();

    
      'grid' => '//div[contains(@class, "views-col") or contains(@class, "views-view-grid__item-inner")]',
      'html_list' => '//div[@class="item-list"]//li',
    ];

    $themes = ['olivero', 'stable9', 'stark', 'claro'];

    foreach ($themes as $theme) {
      \Drupal::service('theme_installer')->install([$theme]);
      \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme($theme));
      foreach ($styles as $type => $xpath) {
        $view = Views::getView('test_page_display');
        $view->storage->invalidateCaches();
        $view->initDisplay();
        $view->setDisplay('empty_row');
        $view->displayHandlers->get('empty_row')->default_display->options['style']['type'] = $type;
        $view->initStyle();
        $this->executeView($view);
        $output = $view->preview();
        $output = \Drupal::service('renderer')->renderRoot($output);
        $this->setRawContent($output);
        $this->assertCount(5, $this->xpath("{$xpath}[not(text()) and not(node())]"), "Empty rows in theme '$theme', type '$type'.");
      }
    }
  }

  protected function assertTranslations(string $display, string $renderer_id, array $expected, string $message = ''): void {
    $view = Views::getView('test_entity_row_renderers');
    $view->storage->invalidateCaches();
    $view->setDisplay($display);
    $view->getDisplay()->setOption('rendering_language', $renderer_id);
    $view->preview();

    $result = FALSE;
    foreach ($expected as $index => $expected_output) {
      if (!empty($view->result[$index])) {
        $build = $view->rowPlugin->render($view->result[$index]);
        $output = \Drupal::service('renderer')->renderRoot($build);
        $result = str_contains($output$expected_output);
        if (!$result) {
          

    ]);
    $view->preview();

    $counter = $view->style_plugin->getField(0, 'counter');
    $this->assertEquals('1', $counternew FormattableMarkup('Make sure the expected number (@expected) patches with the rendered number (@counter)', ['@expected' => 1, '@counter' => $counter]));
    $counter = $view->style_plugin->getField(1, 'counter');
    $this->assertEquals('2', $counternew FormattableMarkup('Make sure the expected number (@expected) patches with the rendered number (@counter)', ['@expected' => 2, '@counter' => $counter]));
    $counter = $view->style_plugin->getField(2, 'counter');
    $this->assertEquals('3', $counternew FormattableMarkup('Make sure the expected number (@expected) patches with the rendered number (@counter)', ['@expected' => 3, '@counter' => $counter]));
    $view->destroy();
    $view->storage->invalidateCaches();

    $view->setDisplay();
    $rand_start = rand(5, 10);
    $view->displayHandlers->get('default')->overrideOption('fields', [
      'counter' => [
        'id' => 'counter',
        'table' => 'views',
        'field' => 'counter',
        'relationship' => 'none',
        'counter_start' => $rand_start,
      ],
      
    $executable->setDisplay($current_display);
  }

  /** * {@inheritdoc} */
  public function postSave(EntityStorageInterface $storage$update = TRUE) {
    parent::postSave($storage$update);

    // @todo Remove if views implements a view_builder controller.     views_invalidate_cache();
    $this->invalidateCaches();

    // Rebuild the router if this is a new view, or its status changed.     if (!isset($this->original) || ($this->status() != $this->original->status())) {
      \Drupal::service('router.builder')->setRebuildNeeded();
    }
  }

  /** * {@inheritdoc} */
  public static function postLoad(EntityStorageInterface $storage, array &$entities) {
    
      if ($index == 2) {
        // Make sure that unsafe link differs from the rendered link, so we know         // that some filtering actually happened. We use assertNotSame and cast         // values to strings since HTML tags are significant.         $this->assertNotSame((string) $entry['variables']['link'](string) $link_field);
      }
      $this->assertSame(Xss::filterAdmin($entry['variables']['link'])(string) $link_field);
    }

    // Disable replacing variables and check that the tokens aren't replaced.     $view->destroy();
    $view->storage->invalidateCaches();
    $view->initHandlers();
    $this->executeView($view);
    $view->initStyle();
    $view->field['message']->options['replace_variables'] = FALSE;
    foreach ($entries as $index => $entry) {
      $this->assertEquals($entry['message']$view->style_plugin->getField($index, 'message'));
    }
  }

  /** * Tests the relationship with the users_field_data table. */
    // belongs to an authenticated user the name field has no value.     $comment_author = $this->xpath('//div[contains(@class, :class)]/span[normalize-space(text())=""]', [
      ':class' => 'views-field-subject',
    ]);
    $this->assertNotEmpty($comment_author);
    // When comment belongs to an anonymous user the name field has a value and     // it is rendered correctly.     $this->assertLink('barry (not verified)');

    $account_switcher->switchTo(new AnonymousUserSession());
    $executable = Views::getView($view_id);
    $executable->storage->invalidateCaches();

    $build = $executable->preview();
    $this->setRawContent($renderer->renderRoot($build));

    // No access to user-profiles, so shouldn't be able to see links.     $this->assertNoLink($this->adminUser->label());
    // Note: External users aren't pointing to drupal user profiles.     $this->assertLink('barry (not verified)');
    $this->assertLink('My comment title');
    $this->assertLink('Anonymous comment title');
  }

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