summaryrefslogtreecommitdiffstatshomepage
path: root/core/tests/Drupal/KernelTests/Components/ComponentKernelTestBase.php
blob: d8d8073d1d117ebd54485c9d9eaa2e79373d606d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

declare(strict_types=1);

namespace Drupal\KernelTests\Components;

use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Theme\ComponentNegotiator;
use Drupal\Core\Theme\ComponentPluginManager;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\DomCrawler\Crawler;

/**
 * Defines a base class for component kernel tests.
 */
abstract class ComponentKernelTestBase extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'user',
    'serialization',
  ];

  /**
   * Themes to install.
   *
   * @var string[]
   */
  protected static $themes = [];

  /**
   * The component negotiator.
   *
   * @return \Drupal\Core\Theme\ComponentNegotiator
   */
  protected ComponentNegotiator $negotiator;

  /**
   * The component plugin manager.
   *
   * @var \Drupal\Core\Theme\ComponentPluginManager
   */
  protected ComponentPluginManager $manager;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    if (empty(static::$themes)) {
      throw new \Exception('You need to set the protected static $themes property on your test class, with the first item being the default theme.');
    }
    $this->container->get('theme_installer')->install(static::$themes);
    $this->installConfig('system');

    $system_theme_config = $this->container->get('config.factory')->getEditable('system.theme');
    $theme_name = reset(static::$themes);
    $system_theme_config
      ->set('default', $theme_name)
      ->save();
    $theme_manager = \Drupal::service('theme.manager');
    $active_theme = \Drupal::service('theme.initialization')->initTheme($theme_name);
    $theme_manager->setActiveTheme($active_theme);

    $this->negotiator = new ComponentNegotiator($theme_manager);
    $this->manager = \Drupal::service('plugin.manager.sdc');
  }

  /**
   * Renders a component for testing sake.
   *
   * @param array $component
   *   Component render array.
   * @param \Drupal\Core\Render\BubbleableMetadata|null $metadata
   *   Bubble metadata.
   *
   * @return \Symfony\Component\DomCrawler\Crawler
   *   Crawler for introspecting the rendered component.
   */
  protected function renderComponentRenderArray(array $component, ?BubbleableMetadata $metadata = NULL): Crawler {
    $component = [
      '#type' => 'container',
      '#attributes' => [
        'id' => 'sdc-wrapper',
      ],
      'component' => $component,
    ];
    $metadata = $metadata ?: new BubbleableMetadata();
    $context = new RenderContext();
    $renderer = \Drupal::service('renderer');
    $output = $renderer->executeInRenderContext($context, fn () => $renderer->render($component));
    if (!$context->isEmpty()) {
      $metadata->addCacheableDependency($context->pop());
    }
    return new Crawler((string) $output);
  }

}