ContentPreprocessTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Unit;
  3. use Drupal\content_moderation\ContentPreprocess;
  4. use Drupal\Core\Routing\CurrentRouteMatch;
  5. use Drupal\node\Entity\Node;
  6. use Drupal\Tests\UnitTestCase;
  7. /**
  8. * @coversDefaultClass \Drupal\content_moderation\ContentPreprocess
  9. *
  10. * @group content_moderation
  11. */
  12. class ContentPreprocessTest extends UnitTestCase {
  13. /**
  14. * @covers ::isLatestVersionPage
  15. * @dataProvider routeNodeProvider
  16. */
  17. public function testIsLatestVersionPage($route_name, $route_nid, $check_nid, $result, $message) {
  18. $content_preprocess = new ContentPreprocess($this->setupCurrentRouteMatch($route_name, $route_nid));
  19. $node = $this->setupNode($check_nid);
  20. $this->assertEquals($result, $content_preprocess->isLatestVersionPage($node), $message);
  21. }
  22. /**
  23. * Data provider for self::testIsLatestVersionPage().
  24. */
  25. public function routeNodeProvider() {
  26. return [
  27. ['entity.node.canonical', 1, 1, FALSE, 'Not on the latest version tab route.'],
  28. ['entity.node.latest_version', 1, 1, TRUE, 'On the latest version tab route, with the route node.'],
  29. ['entity.node.latest_version', 1, 2, FALSE, 'On the latest version tab route, with a different node.'],
  30. ];
  31. }
  32. /**
  33. * Mock the current route matching object.
  34. *
  35. * @param string $route_name
  36. * The route to mock.
  37. * @param int $nid
  38. * The node ID for mocking.
  39. *
  40. * @return \Drupal\Core\Routing\CurrentRouteMatch
  41. * The mocked current route match object.
  42. */
  43. protected function setupCurrentRouteMatch($route_name, $nid) {
  44. $route_match = $this->prophesize(CurrentRouteMatch::class);
  45. $route_match->getRouteName()->willReturn($route_name);
  46. $route_match->getParameter('node')->willReturn($this->setupNode($nid));
  47. return $route_match->reveal();
  48. }
  49. /**
  50. * Mock a node object.
  51. *
  52. * @param int $nid
  53. * The node ID to mock.
  54. *
  55. * @return \Drupal\node\Entity\Node
  56. * The mocked node.
  57. */
  58. protected function setupNode($nid) {
  59. $node = $this->prophesize(Node::class);
  60. $node->id()->willReturn($nid);
  61. return $node->reveal();
  62. }
  63. }