comparison core/modules/comment/tests/src/Kernel/CommentItemTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\comment\Kernel;
4
5 use Drupal\comment\Entity\Comment;
6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
7 use Drupal\comment\Tests\CommentTestTrait;
8 use Drupal\Core\Field\FieldItemListInterface;
9 use Drupal\entity_test\Entity\EntityTest;
10 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
11
12 /**
13 * Tests the new entity API for the comment field type.
14 *
15 * @group comment
16 */
17 class CommentItemTest extends FieldKernelTestBase {
18
19 use CommentTestTrait;
20
21 /**
22 * Modules to enable.
23 *
24 * @var array
25 */
26 public static $modules = ['comment', 'entity_test', 'user'];
27
28 protected function setUp() {
29 parent::setUp();
30 $this->installSchema('comment', ['comment_entity_statistics']);
31 $this->installConfig(['comment']);
32 }
33
34 /**
35 * Tests using entity fields of the comment field type.
36 */
37 public function testCommentItem() {
38 $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
39
40 // Verify entity creation.
41 $entity = EntityTest::create();
42 $entity->name->value = $this->randomMachineName();
43 $entity->save();
44
45 // Verify entity has been created properly.
46 $id = $entity->id();
47 $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
48 $storage->resetCache([$id]);
49 $entity = $storage->load($id);
50 $this->assertTrue($entity->comment instanceof FieldItemListInterface, 'Field implements interface.');
51 $this->assertTrue($entity->comment[0] instanceof CommentItemInterface, 'Field item implements interface.');
52
53 // Test sample item generation.
54 /** @var \Drupal\entity_test\Entity\EntityTest $entity */
55 $entity = EntityTest::create();
56 $entity->comment->generateSampleItems();
57 $this->entityValidateAndSave($entity);
58 $this->assertTrue(in_array($entity->get('comment')->status, [
59 CommentItemInterface::HIDDEN,
60 CommentItemInterface::CLOSED,
61 CommentItemInterface::OPEN,
62 ]), 'Comment status value in defined range');
63
64 $mainProperty = $entity->comment[0]->mainPropertyName();
65 $this->assertEqual('status', $mainProperty);
66 }
67
68 /**
69 * Tests comment author name.
70 */
71 public function testCommentAuthorName() {
72 $this->installEntitySchema('comment');
73
74 // Create some comments.
75 $comment = Comment::create([
76 'subject' => 'My comment title',
77 'uid' => 1,
78 'name' => 'entity-test',
79 'mail' => 'entity@localhost',
80 'entity_type' => 'entity_test',
81 'comment_type' => 'entity_test',
82 'status' => 1,
83 ]);
84 $comment->save();
85
86 // The entity fields for name and mail have no meaning if the user is not
87 // Anonymous.
88 $this->assertNull($comment->name->value);
89 $this->assertNull($comment->mail->value);
90
91 $comment_anonymous = Comment::create([
92 'subject' => 'Anonymous comment title',
93 'uid' => 0,
94 'name' => 'barry',
95 'mail' => 'test@example.com',
96 'homepage' => 'https://example.com',
97 'entity_type' => 'entity_test',
98 'comment_type' => 'entity_test',
99 'status' => 1,
100 ]);
101 $comment_anonymous->save();
102
103 // The entity fields for name and mail have retained their values when
104 // comment belongs to an anonymous user.
105 $this->assertNotNull($comment_anonymous->name->value);
106 $this->assertNotNull($comment_anonymous->mail->value);
107
108 $comment_anonymous->setOwnerId(1)
109 ->save();
110 // The entity fields for name and mail have no meaning if the user is not
111 // Anonymous.
112 $this->assertNull($comment_anonymous->name->value);
113 $this->assertNull($comment_anonymous->mail->value);
114 }
115
116 }