ViewsPreprocessTest.php in Drupal 9
File
core/modules/views/tests/src/Kernel/ViewsPreprocessTest.php
View source
<?php
namespace Drupal\Tests\views\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\views\Views;
class ViewsPreprocessTest extends ViewsKernelTestBase {
public static $testViews = [
'test_preprocess',
];
protected static $modules = [
'entity_test',
'user',
'node',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp();
$this
->installEntitySchema('entity_test');
}
public function testCssClassCleaning() {
\Drupal::service('theme_installer')
->install([
'test_theme',
]);
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
$entity = EntityTest::create();
$entity
->save();
$renderer = \Drupal::service('renderer');
$view = Views::getview('test_preprocess');
$build = $view
->buildRenderable();
$renderer
->renderRoot($build);
$this
->assertStringContainsString('class="entity-test--default 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 entity-test__default', $markup);
$this
->assertStringContainsString('attributes: class="entity-test--default and-another-class entity-test__default', $markup);
}
}