TextWithSummaryItemTest.php in Zircon Profile 8
File
core/modules/text/src/Tests/TextWithSummaryItemTest.php
View source
<?php
namespace Drupal\text\Tests;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\field\Tests\FieldUnitTestBase;
class TextWithSummaryItemTest extends FieldUnitTestBase {
public static $modules = array(
'filter',
);
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test_rev');
$this
->installConfig(array(
'filter',
));
entity_create('filter_format', array(
'format' => 'no_filters',
'filters' => array(),
))
->save();
}
public function testCrudAndUpdate() {
$entity_type = 'entity_test';
$this
->createField($entity_type);
$entity = entity_create($entity_type);
$entity->summary_field->value = $value = $this
->randomMachineName();
$entity->summary_field->summary = $summary = $this
->randomMachineName();
$entity->summary_field->format = NULL;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$entity = entity_load($entity_type, $entity
->id());
$this
->assertTrue($entity->summary_field instanceof FieldItemListInterface, 'Field implements interface.');
$this
->assertTrue($entity->summary_field[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this
->assertEqual($entity->summary_field->value, $value);
$this
->assertEqual($entity->summary_field->summary, $summary);
$this
->assertNull($entity->summary_field->format);
$this
->assertEqual($entity->summary_field->processed, "<p>{$value}</p>\n");
$this
->assertEqual($entity->summary_field->summary_processed, "<p>{$summary}</p>\n");
$entity->summary_field->format = 'no_filters';
$this
->assertEqual($entity->summary_field->processed, $value);
$this
->assertEqual($entity->summary_field->summary_processed, $summary);
$entity = entity_create($entity_type);
$entity->summary_field
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
protected function createField($entity_type) {
$this->fieldStorage = entity_create('field_storage_config', array(
'field_name' => 'summary_field',
'entity_type' => $entity_type,
'type' => 'text_with_summary',
'settings' => array(
'max_length' => 10,
),
));
$this->fieldStorage
->save();
$this->field = entity_create('field_config', array(
'field_storage' => $this->fieldStorage,
'bundle' => $entity_type,
));
$this->field
->save();
}
}