|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Tests\Driver\Kernel\Drupal8\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Driver\Wrapper\Entity\DriverEntityDrupal8; |
|
6
|
|
|
use Drupal\Tests\Driver\Kernel\Drupal8\Entity\DriverEntityKernelTestBase; |
|
7
|
|
|
use Drupal\comment\Entity\CommentType; |
|
8
|
|
|
use Drupal\comment\Tests\CommentTestTrait; |
|
9
|
|
|
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Tests the driver's handling of comment entities. |
|
13
|
|
|
* They have the peculiarity of having 'entity_type' as a field name. |
|
14
|
|
|
* |
|
15
|
|
|
* @group Driver |
|
16
|
|
|
*/ |
|
17
|
|
|
class CommentTest extends DriverEntityKernelTestBase |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
use CommentTestTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Modules to install. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
public static $modules = ['comment', 'user']; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Machine name of the entity type being tested. |
|
31
|
|
|
* |
|
32
|
|
|
* @string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $entityType = 'comment'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function setUp() |
|
40
|
|
|
{ |
|
41
|
|
|
parent::setUp(); |
|
42
|
|
|
$this->installConfig(['user', 'comment']); |
|
43
|
|
|
$this->installSchema('comment', ['comment_entity_statistics']); |
|
44
|
|
|
|
|
45
|
|
|
// Create a comment type. |
|
46
|
|
|
$comment_type = CommentType::create([ |
|
47
|
|
|
'id' => 'testcomment', |
|
48
|
|
|
'label' => 'Default comments', |
|
49
|
|
|
'description' => 'Default comment field', |
|
50
|
|
|
'target_entity_type_id' => 'user', |
|
51
|
|
|
]); |
|
52
|
|
|
$comment_type->save(); |
|
53
|
|
|
|
|
54
|
|
|
// Add a comment field to the user entity. |
|
55
|
|
|
$this->addDefaultCommentField('user', 'user', 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'testcomment'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Test that a comment can be created and deleted. |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
public function testCommentCreateDelete() |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
// Create a comment on a test user. |
|
64
|
|
|
$user = $this->createUser(); |
|
65
|
|
|
$subject = $this->randomString(); |
|
66
|
|
|
$comment = (object) [ |
|
67
|
|
|
'subject' => $subject, |
|
68
|
|
|
'entity_type' => 'user', |
|
69
|
|
|
'entity_id' => $user->getUsername(), |
|
70
|
|
|
'step_bundle' => 'testcomment' |
|
71
|
|
|
]; |
|
72
|
|
|
$comment = $this->driver->createEntity('comment', $comment); |
|
73
|
|
|
|
|
74
|
|
|
$entities = $this->storage->loadByProperties(['subject' => $subject]); |
|
75
|
|
|
$this->assertEquals(1, count($entities)); |
|
76
|
|
|
|
|
77
|
|
|
// Check the id of the new comment has been added to the returned object. |
|
78
|
|
|
$entity = reset($entities); |
|
79
|
|
|
$this->assertEquals($entity->id(), $comment->id); |
|
80
|
|
|
|
|
81
|
|
|
// Check the comment can be deleted. |
|
82
|
|
|
$this->driver->entityDelete('comment', $comment); |
|
83
|
|
|
$entities = $this->storage->loadByProperties(['subject' => $subject]); |
|
84
|
|
|
$this->assertEquals(0, count($entities)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Test that a comment can be created and deleted. |
|
89
|
|
|
*/ |
|
90
|
|
View Code Duplication |
public function testCommentCreateDeleteByWrapper() |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
// Create a comment on a test user. |
|
93
|
|
|
$user = $this->createUser(); |
|
94
|
|
|
$subject = $this->randomString(); |
|
95
|
|
|
|
|
96
|
|
|
$fields = [ |
|
97
|
|
|
'subject' => $subject, |
|
98
|
|
|
'entity_type' => 'user', |
|
99
|
|
|
'entity_id' => $user->getUsername(), |
|
100
|
|
|
'comment_type' => 'testcomment' |
|
101
|
|
|
]; |
|
102
|
|
|
$comment = DriverEntityDrupal8::create($fields, $this->entityType)->save(); |
|
103
|
|
|
|
|
104
|
|
|
$entities = $this->storage->loadByProperties(['subject' => $subject]); |
|
105
|
|
|
$this->assertEquals(1, count($entities)); |
|
106
|
|
|
|
|
107
|
|
|
// Check the id of the new comment has been added to the returned object. |
|
108
|
|
|
$entity = reset($entities); |
|
109
|
|
|
$this->assertEquals($entity->id(), $comment->id); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
// Check the comment can be deleted. |
|
112
|
|
|
$comment->delete(); |
|
113
|
|
|
$entities = $this->storage->loadByProperties(['subject' => $subject]); |
|
114
|
|
|
$this->assertEquals(0, count($entities)); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.