Issue #2422101 by andypost, pcambra, penyaskito: CommentItem should override the generateSampleValue method and provide sample values
parent
25c41d0a6d
commit
0972ecb314
|
@ -9,6 +9,7 @@ namespace Drupal\comment\Plugin\Field\FieldType;
|
|||
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Render\Element;
|
||||
|
@ -193,4 +194,18 @@ class CommentItem extends FieldItemBase implements CommentItemInterface {
|
|||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
|
||||
$statuses = [
|
||||
CommentItemInterface::HIDDEN,
|
||||
CommentItemInterface::CLOSED,
|
||||
CommentItemInterface::OPEN,
|
||||
];
|
||||
return [
|
||||
'status' => $statuses[mt_rand(0, count($statuses) - 1)],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentItemTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\field\Tests\FieldUnitTestBase;
|
||||
|
||||
/**
|
||||
* Tests the new entity API for the comment field type.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentItemTest extends FieldUnitTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['comment', 'entity_test', 'user'];
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installSchema('comment', ['comment_entity_statistics']);
|
||||
$this->installConfig(['comment']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests using entity fields of the comment field type.
|
||||
*/
|
||||
public function testCommentItem() {
|
||||
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
|
||||
|
||||
// Verify entity creation.
|
||||
$entity = entity_create('entity_test');
|
||||
$entity->name->value = $this->randomMachineName();
|
||||
$entity->save();
|
||||
|
||||
// Verify entity has been created properly.
|
||||
$id = $entity->id();
|
||||
$entity = entity_load('entity_test', $id, TRUE);
|
||||
$this->assertTrue($entity->comment instanceof FieldItemListInterface, 'Field implements interface.');
|
||||
$this->assertTrue($entity->comment[0] instanceof CommentItemInterface, 'Field item implements interface.');
|
||||
|
||||
// Test sample item generation.
|
||||
/** @var \Drupal\entity_test\Entity\EntityTest $entity */
|
||||
$entity = entity_create('entity_test');
|
||||
$entity->comment->generateSampleItems();
|
||||
$this->entityValidateAndSave($entity);
|
||||
$this->assertTrue(in_array($entity->get('comment')->status, [
|
||||
CommentItemInterface::HIDDEN,
|
||||
CommentItemInterface::CLOSED,
|
||||
CommentItemInterface::OPEN,
|
||||
]), 'Comment status value in defined range');
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue