6f209d13ae09cf8ce97d5c931a05c50a4d49891c
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / EntityRevisionConverterTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\workflows\Entity\Workflow;
10
11 /**
12  * @coversDefaultClass \Drupal\content_moderation\ParamConverter\EntityRevisionConverter
13  * @group content_moderation
14  */
15 class EntityRevisionConverterTest extends KernelTestBase {
16
17   public static $modules = [
18     'user',
19     'entity_test',
20     'system',
21     'content_moderation',
22     'node',
23     'workflows',
24   ];
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31
32     $this->installEntitySchema('entity_test');
33     $this->installEntitySchema('node');
34     $this->installEntitySchema('user');
35     $this->installEntitySchema('content_moderation_state');
36     $this->installSchema('system', 'router');
37     $this->installSchema('system', 'sequences');
38     $this->installSchema('node', 'node_access');
39     \Drupal::service('router.builder')->rebuild();
40   }
41
42   /**
43    * @covers ::convert
44    */
45   public function testConvertNonRevisionableEntityType() {
46     $entity_test = EntityTest::create([
47       'name' => 'test',
48     ]);
49
50     $entity_test->save();
51
52     /** @var \Symfony\Component\Routing\RouterInterface $router */
53     $router = \Drupal::service('router.no_access_checks');
54     $result = $router->match('/entity_test/' . $entity_test->id());
55
56     $this->assertInstanceOf(EntityTest::class, $result['entity_test']);
57     $this->assertEquals($entity_test->getRevisionId(), $result['entity_test']->getRevisionId());
58   }
59
60   /**
61    * @covers ::convert
62    */
63   public function testConvertWithRevisionableEntityType() {
64     $this->installConfig(['content_moderation']);
65     $node_type = NodeType::create([
66       'type' => 'article',
67     ]);
68     $node_type->save();
69     $workflow = Workflow::load('editorial');
70     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'article');
71     $workflow->save();
72
73     $revision_ids = [];
74     $node = Node::create([
75       'title' => 'test',
76       'type' => 'article',
77     ]);
78     $node->moderation_state->value = 'published';
79     $node->save();
80
81     $revision_ids[] = $node->getRevisionId();
82
83     $node->setNewRevision(TRUE);
84     $node->save();
85     $revision_ids[] = $node->getRevisionId();
86
87     $node->setNewRevision(TRUE);
88     $node->moderation_state->value = 'draft';
89     $node->save();
90     $revision_ids[] = $node->getRevisionId();
91
92     /** @var \Symfony\Component\Routing\RouterInterface $router */
93     $router = \Drupal::service('router.no_access_checks');
94     $result = $router->match('/node/' . $node->id() . '/edit');
95
96     $this->assertInstanceOf(Node::class, $result['node']);
97     $this->assertEquals($revision_ids[2], $result['node']->getRevisionId());
98     $this->assertFalse($result['node']->isDefaultRevision());
99   }
100
101 }