Chris@0: installEntitySchema('comment'); Chris@0: $this->installSchema('comment', ['comment_entity_statistics']); Chris@0: $this->installConfig(['comment']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests using entity fields of the comment field type. Chris@0: */ Chris@0: public function testCommentItem() { Chris@0: $this->addDefaultCommentField('entity_test', 'entity_test', 'comment'); Chris@0: Chris@0: // Verify entity creation. Chris@0: $entity = EntityTest::create(); Chris@0: $entity->name->value = $this->randomMachineName(); Chris@0: $entity->save(); Chris@0: Chris@0: // Verify entity has been created properly. Chris@0: $id = $entity->id(); Chris@0: $storage = $this->container->get('entity_type.manager')->getStorage('entity_test'); Chris@0: $storage->resetCache([$id]); Chris@0: $entity = $storage->load($id); Chris@0: $this->assertTrue($entity->comment instanceof FieldItemListInterface, 'Field implements interface.'); Chris@0: $this->assertTrue($entity->comment[0] instanceof CommentItemInterface, 'Field item implements interface.'); Chris@0: Chris@0: // Test sample item generation. Chris@0: /** @var \Drupal\entity_test\Entity\EntityTest $entity */ Chris@0: $entity = EntityTest::create(); Chris@0: $entity->comment->generateSampleItems(); Chris@0: $this->entityValidateAndSave($entity); Chris@0: $this->assertTrue(in_array($entity->get('comment')->status, [ Chris@0: CommentItemInterface::HIDDEN, Chris@0: CommentItemInterface::CLOSED, Chris@0: CommentItemInterface::OPEN, Chris@0: ]), 'Comment status value in defined range'); Chris@0: Chris@0: $mainProperty = $entity->comment[0]->mainPropertyName(); Chris@0: $this->assertEqual('status', $mainProperty); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests comment author name. Chris@0: */ Chris@0: public function testCommentAuthorName() { Chris@0: $this->installEntitySchema('comment'); Chris@18: $this->addDefaultCommentField('entity_test', 'entity_test', 'comment'); Chris@0: Chris@12: $host = EntityTest::create(['name' => $this->randomString()]); Chris@12: $host->save(); Chris@12: Chris@0: // Create some comments. Chris@0: $comment = Comment::create([ Chris@0: 'subject' => 'My comment title', Chris@0: 'uid' => 1, Chris@0: 'name' => 'entity-test', Chris@0: 'mail' => 'entity@localhost', Chris@0: 'entity_type' => 'entity_test', Chris@18: 'field_name' => 'comment', Chris@12: 'entity_id' => $host->id(), Chris@0: 'comment_type' => 'entity_test', Chris@0: 'status' => 1, Chris@0: ]); Chris@0: $comment->save(); Chris@0: Chris@0: // The entity fields for name and mail have no meaning if the user is not Chris@0: // Anonymous. Chris@0: $this->assertNull($comment->name->value); Chris@0: $this->assertNull($comment->mail->value); Chris@0: Chris@0: $comment_anonymous = Comment::create([ Chris@0: 'subject' => 'Anonymous comment title', Chris@0: 'uid' => 0, Chris@0: 'name' => 'barry', Chris@0: 'mail' => 'test@example.com', Chris@0: 'homepage' => 'https://example.com', Chris@0: 'entity_type' => 'entity_test', Chris@18: 'field_name' => 'comment', Chris@12: 'entity_id' => $host->id(), Chris@0: 'comment_type' => 'entity_test', Chris@0: 'status' => 1, Chris@0: ]); Chris@0: $comment_anonymous->save(); Chris@0: Chris@0: // The entity fields for name and mail have retained their values when Chris@0: // comment belongs to an anonymous user. Chris@0: $this->assertNotNull($comment_anonymous->name->value); Chris@0: $this->assertNotNull($comment_anonymous->mail->value); Chris@0: Chris@0: $comment_anonymous->setOwnerId(1) Chris@0: ->save(); Chris@0: // The entity fields for name and mail have no meaning if the user is not Chris@0: // Anonymous. Chris@0: $this->assertNull($comment_anonymous->name->value); Chris@0: $this->assertNull($comment_anonymous->mail->value); Chris@0: } Chris@0: Chris@0: }