Version 1
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / DefaultRevisionStateTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\workflows\Entity\Workflow;
10
11 /**
12  * Tests the correct default revision is set.
13  *
14  * @group content_moderation
15  */
16 class DefaultRevisionStateTest extends KernelTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = [
22     'entity_test',
23     'node',
24     'block_content',
25     'content_moderation',
26     'user',
27     'system',
28     'language',
29     'content_translation',
30     'text',
31     'workflows',
32   ];
33
34   /**
35    * @var \Drupal\Core\Entity\EntityTypeManager
36    */
37   protected $entityTypeManager;
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44
45     $this->installSchema('node', 'node_access');
46     $this->installEntitySchema('node');
47     $this->installEntitySchema('user');
48     $this->installEntitySchema('entity_test_with_bundle');
49     $this->installEntitySchema('entity_test_rev');
50     $this->installEntitySchema('entity_test_mulrevpub');
51     $this->installEntitySchema('block_content');
52     $this->installEntitySchema('content_moderation_state');
53     $this->installConfig('content_moderation');
54
55     $this->entityTypeManager = $this->container->get('entity_type.manager');
56   }
57
58   /**
59    * Tests a translatable Node.
60    */
61   public function testMultilingual() {
62     // Enable French.
63     ConfigurableLanguage::createFromLangcode('fr')->save();
64     $node_type = NodeType::create([
65       'type' => 'example',
66     ]);
67     $node_type->save();
68
69     $this->container->get('content_translation.manager')->setEnabled('node', 'example', TRUE);
70
71     $workflow = Workflow::load('editorial');
72     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
73     $workflow->save();
74
75     $english_node = Node::create([
76       'type' => 'example',
77       'title' => 'Test title',
78     ]);
79     // Revision 1 (en).
80     $english_node
81       ->setUnpublished()
82       ->save();
83     $this->assertEquals('draft', $english_node->moderation_state->value);
84     $this->assertFalse($english_node->isPublished());
85     $this->assertTrue($english_node->isDefaultRevision());
86
87     // Revision 2 (fr)
88     $french_node = $english_node->addTranslation('fr', ['title' => 'French title']);
89     $french_node->moderation_state->value = 'published';
90     $french_node->save();
91     $this->assertTrue($french_node->isPublished());
92     $this->assertTrue($french_node->isDefaultRevision());
93
94     // Revision 3 (fr)
95     $node = Node::load($english_node->id())->getTranslation('fr');
96     $node->moderation_state->value = 'draft';
97     $node->save();
98     $this->assertFalse($node->isPublished());
99     $this->assertFalse($node->isDefaultRevision());
100
101     // Revision 4 (en)
102     $latest_revision = $this->entityTypeManager->getStorage('node')->loadRevision(3);
103     $latest_revision->moderation_state->value = 'draft';
104     $latest_revision->save();
105     $this->assertFalse($latest_revision->isPublished());
106     $this->assertFalse($latest_revision->isDefaultRevision());
107   }
108
109 }