Verified Commit 56a8ed21 authored by godotislate's avatar godotislate
Browse files

test: #3579629 Add procedural hooks to test theme to test collection and invoking legacy hooks

By: nicxvan
By: berdir
By: godotislate
(cherry picked from commit 036963e5)
parent 662dd45f
Loading
Loading
Loading
Loading
Loading
+67 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\system\Functional\Theme;

use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests low-level theme functions.
 */
#[Group('Theme')]
#[RunTestsInSeparateProcesses]
class ThemeProceduralTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['node'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'procedural_hook_theme';

  /**
   * Ensures preprocess functions run from procedural implementations.
   */
  public function testPreprocess(): void {
    $node_article_type = NodeType::create([
      'type' => 'article',
      'name' => 'Article',
    ]);
    $node_article_type->save();

    $node_basic_type = NodeType::create([
      'type' => 'basic',
      'name' => 'Basic',
    ]);
    $node_basic_type->save();

    $node = Node::create([
      'title' => 'placeholder_title',
      'type' => 'article',
      'uid' => 1,
    ]);
    $node->save();

    $node = Node::create([
      'title' => 'placeholder_title',
      'type' => 'basic',
      'uid' => 1,
    ]);
    $node->save();
    $this->drupalGet('node/1');
    $items = $this->cssSelect('.title');
    $this->assertEquals('Procedural Article Node Preprocess', $items[0]->getText());
    $this->drupalGet('node/2');
    $items = $this->cssSelect('.title');
    $this->assertEquals('Procedural Node Preprocess', $items[0]->getText());
  }

}
+46 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\system\Kernel\Theme;

use Drupal\Core\Theme\ThemeInitializationInterface;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests low-level theme functions.
 */
#[Group('Theme')]
#[RunTestsInSeparateProcesses]
class ThemeHookTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public function setUp(): void {
    parent::setUp();
    \Drupal::service('theme_installer')->install(['procedural_hook_theme']);
    \Drupal::theme()->setActiveTheme(\Drupal::service(ThemeInitializationInterface::class)->initTheme('procedural_hook_theme'));
  }

  /**
   * Tests that procedural hooks are collected and executed.
   */
  public function testProceduralHookCollection(): void {
    $args = [];
    \Drupal::theme()->alter('procedural', $args);
    $this->assertEquals(['Procedural theme hook executed.'], $args);
  }

  /**
   * Tests that procedural hooks with #[LegacyHook] are properly ignored.
   */
  public function testLegacyHookInThemes(): void {
    $args = [];
    \Drupal::theme()->alter('procedural_legacy', $args);
    $this->assertEquals(['OOP theme hook executed.'], $args);
  }

}
+5 −0
Original line number Diff line number Diff line
name: 'Procedural hook test'
type: theme
description: 'Test theme for procedural hooks.'
version: VERSION
base theme: false
+39 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Functions to support testing procedural theme hooks.
 */

declare(strict_types=1);

use Drupal\Core\Hook\Attribute\LegacyHook;

/**
 * Implements hook_procedural_alter().
 */
function procedural_hook_theme_procedural_alter(array &$args): void {
  $args[] = 'Procedural theme hook executed.';
}

/**
 * Implements hook_procedural_legacy_alter().
 */
#[LegacyHook]
function procedural_hook_theme_procedural_legacy_alter(array &$args): void {
  $args[] = 'Procedural theme hook did not execute.';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function procedural_hook_theme_preprocess_node(array &$variables): void {
  $variables['title'] = 'Procedural Node Preprocess';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function procedural_hook_theme_preprocess_node__article(array &$variables): void {
  $variables['title'] = 'Procedural Article Node Preprocess';
}
+19 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\procedural_hook_theme\Hook;

use Drupal\Core\Hook\Attribute\Hook;

/**
 * Confirms LegacyHook works with themes.
 */
class ProceduralHookThemeHooks {

  #[Hook('procedural_legacy_alter')]
  public function proceduralLegacyAlter(array &$args): void {
    $args[] = 'OOP theme hook executed.';
  }

}
Loading