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