ViewsPreprocessTest.php

Same filename and directory in other branches
  1. 11.x core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php
  2. 10 core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php
  3. 9 core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php
  4. 8.9.x core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php

Namespace

Drupal\Tests\views\Kernel

File

core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\views\Kernel;

use Drupal\entity_test\Entity\EntityTest;
use Drupal\views\Hook\ViewsThemeHooks;
use Drupal\views\Views;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests the preprocessing functionality in views theme hooks.
 */
class ViewsPreprocessTest extends ViewsKernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  public static $testViews = [
    'test_preprocess',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'entity_test',
    'user',
    'node',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE) : void {
    parent::setUp();
    $this->installEntitySchema('entity_test');
  }
  
  /**
   * Tests css classes on displays are cleaned correctly.
   */
  public function testCssClassCleaning() : void {
    \Drupal::service('theme_installer')->install([
      'test_theme',
    ]);
    $this->config('system.theme')
      ->set('default', 'test_theme')
      ->save();
    $entity = EntityTest::create();
    $entity->save();
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $view = Views::getView('test_preprocess');
    $build = $view->buildRenderable();
    $renderer->renderRoot($build);
    $this->assertStringContainsString('class="entity-test__default', (string) $build['#markup']);
    $view->destroy();
    $view->setDisplay('display_2');
    $build = $view->buildRenderable();
    $renderer->renderRoot($build);
    $markup = (string) $build['#markup'];
    $this->assertStringContainsString('css_class: entity-test__default and-another-class', $markup);
    $this->assertStringContainsString('attributes: class="entity-test__default and-another-class', $markup);
  }
  
  /**
   * Tests the mini pager when an empty pagination_heading_level is passed.
   *
   * @legacy-covers ViewsThemeHooks::preprocessViewsMiniPager
   */
  public function testEmptyPaginationHeadingLevelSet() : void {
    $variables = [
      'tags' => [],
      'quantity' => 9,
      'element' => 0,
      'pagination_heading_level' => '',
      'parameters' => [],
    ];
    \Drupal::service(ViewsThemeHooks::class)->preprocessViewsMiniPager($variables);
    $this->assertEquals('h4', $variables['pagination_heading_level']);
  }
  
  /**
   * Tests the mini pager when no pagination_heading_level is passed.
   *
   * @legacy-covers ViewsThemeHooks::preprocessViewsMiniPager
   */
  public function testPaginationHeadingLevelNotSet() : void {
    $variables = [
      'tags' => [],
      'quantity' => 9,
      'element' => 0,
      'parameters' => [],
    ];
    \Drupal::service(ViewsThemeHooks::class)->preprocessViewsMiniPager($variables);
    $this->assertEquals('h4', $variables['pagination_heading_level']);
  }
  
  /**
   * Tests the mini pager when a pagination_heading_level value is passed.
   *
   * @legacy-covers ViewsThemeHooks::preprocessViewsMiniPager
   */
  public function testPaginationHeadingLevelSet() : void {
    $variables = [
      'tags' => [],
      'quantity' => 9,
      'element' => 0,
      'pagination_heading_level' => 'h5',
      'parameters' => [],
    ];
    \Drupal::service(ViewsThemeHooks::class)->preprocessViewsMiniPager($variables);
    $this->assertEquals('h5', $variables['pagination_heading_level']);
  }

}

Classes

Title Deprecated Summary
ViewsPreprocessTest Tests the preprocessing functionality in views theme hooks.

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