Unverified Commit 46deaa34 authored by Alex Pott's avatar Alex Pott
Browse files

task: #2877924 Remove ContentPreprocess and associated code

By: sam152
By: timmillwood
By: _utsavsharma
By: smustgrave
By: acbramley
By: alexpott
By: berdir
parent 4e58746c
Loading
Loading
Loading
Loading
Loading
+0 −18
Original line number Diff line number Diff line
@@ -13391,18 +13391,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/contact/tests/src/Unit/MailHandlerTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\content_moderation\\\\ContentPreprocess\\:\\:create\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/content_moderation/src/ContentPreprocess.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\content_moderation\\\\ContentPreprocess\\:\\:preprocessNode\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/content_moderation/src/ContentPreprocess.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\content_moderation\\\\Entity\\\\ContentModerationState\\:\\:updateOrCreateFromEntity\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
@@ -13829,12 +13817,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/content_moderation/tests/src/Unit/ContentModerationRouteSubscriberTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Tests\\\\content_moderation\\\\Unit\\\\ContentPreprocessTest\\:\\:routeNodeProvider\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/content_moderation/tests/src/Unit/ContentPreprocessTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Tests\\\\content_moderation\\\\Unit\\\\LatestRevisionCheckTest\\:\\:accessSituationProvider\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+0 −70
Original line number Diff line number Diff line
<?php

namespace Drupal\content_moderation;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Determines whether a route is the "Latest version" tab of a node.
 *
 * @internal
 */
class ContentPreprocess implements ContainerInjectionInterface {

  /**
   * The route match service.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   Current route match service.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('current_route_match')
    );
  }

  /**
   * @param array $variables
   *   Theme variables to preprocess.
   *
   * @see hook_preprocess_HOOK()
   */
  public function preprocessNode(array &$variables) {
    // Set the 'page' template variable when the node is being displayed on the
    // "Latest version" tab provided by content_moderation.
    $variables['page'] = $variables['page'] || $this->isLatestVersionPage($variables['node']);
  }

  /**
   * Checks whether a route is the "Latest version" tab of a node.
   *
   * @param \Drupal\node\Entity\Node $node
   *   A node.
   *
   * @return bool
   *   True if the current route is the latest version tab of the given node.
   */
  public function isLatestVersionPage(Node $node) {
    return $this->routeMatch->getRouteName() == 'entity.node.latest_version'
           && ($pageNode = $this->routeMatch->getParameter('node'))
           && $pageNode->id() == $node->id();
  }

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

namespace Drupal\content_moderation\Hook;

use Drupal\content_moderation\ContentPreprocess;
use Drupal\Core\Hook\Attribute\Hook;

/**
 * Hook implementations for content_moderation.
 */
class ContentModerationThemeHooks {

  /**
   * Implements hook_preprocess_HOOK().
   */
  #[Hook('preprocess_node')]
  public function preprocessNode(&$variables): void {
    \Drupal::service('class_resolver')->getInstanceFromDefinition(ContentPreprocess::class)->preprocessNode($variables);
  }

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

declare(strict_types=1);

namespace Drupal\Tests\content_moderation\Unit;

use Drupal\content_moderation\ContentPreprocess;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\node\Entity\Node;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests Drupal\content_moderation\ContentPreprocess.
 */
#[CoversClass(ContentPreprocess::class)]
#[Group('content_moderation')]
class ContentPreprocessTest extends UnitTestCase {

  /**
   * Tests is latest version page.
   */
  #[DataProvider('routeNodeProvider')]
  public function testIsLatestVersionPage($route_name, $route_nid, $check_nid, $result, $message): void {
    $content_preprocess = new ContentPreprocess($this->setupCurrentRouteMatch($route_name, $route_nid));
    $node = $this->setupNode($check_nid);
    $this->assertEquals($result, $content_preprocess->isLatestVersionPage($node), $message);
  }

  /**
   * Data provider for self::testIsLatestVersionPage().
   */
  public static function routeNodeProvider() {
    return [
      ['entity.node.canonical', 1, 1, FALSE, 'Not on the latest version tab route.'],
      ['entity.node.latest_version', 1, 1, TRUE, 'On the latest version tab route, with the route node.'],
      ['entity.node.latest_version', 1, 2, FALSE, 'On the latest version tab route, with a different node.'],
    ];
  }

  /**
   * Mock the current route matching object.
   *
   * @param string $route_name
   *   The route to mock.
   * @param int $nid
   *   The node ID for mocking.
   *
   * @return \Drupal\Core\Routing\CurrentRouteMatch
   *   The mocked current route match object.
   */
  protected function setupCurrentRouteMatch($route_name, $nid) {
    $route_match = $this->prophesize(CurrentRouteMatch::class);
    $route_match->getRouteName()->willReturn($route_name);
    $route_match->getParameter('node')->willReturn($this->setupNode($nid));

    return $route_match->reveal();
  }

  /**
   * Mock a node object.
   *
   * @param int $nid
   *   The node ID to mock.
   *
   * @return \Drupal\node\Entity\Node
   *   The mocked node.
   */
  protected function setupNode($nid) {
    $node = $this->prophesize(Node::class);
    $node->id()->willReturn($nid);

    return $node->reveal();
  }

}