annotate core/modules/text/tests/src/Kernel/TextWithSummaryItemTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\text\Kernel;
Chris@0 4
Chris@0 5 use Drupal\Core\Field\FieldItemListInterface;
Chris@0 6 use Drupal\Core\Field\FieldItemInterface;
Chris@0 7 use Drupal\field\Entity\FieldConfig;
Chris@0 8 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
Chris@0 9 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 10 use Drupal\filter\Entity\FilterFormat;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Tests using entity fields of the text summary field type.
Chris@0 14 *
Chris@0 15 * @group text
Chris@0 16 */
Chris@0 17 class TextWithSummaryItemTest extends FieldKernelTestBase {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Modules to enable.
Chris@0 21 *
Chris@0 22 * @var array
Chris@0 23 */
Chris@0 24 public static $modules = ['filter'];
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Field storage entity.
Chris@0 28 *
Chris@0 29 * @var \Drupal\field\Entity\FieldStorageConfig.
Chris@0 30 */
Chris@0 31 protected $fieldStorage;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Field entity.
Chris@0 35 *
Chris@0 36 * @var \Drupal\field\Entity\FieldConfig
Chris@0 37 */
Chris@0 38 protected $field;
Chris@0 39
Chris@0 40
Chris@0 41 protected function setUp() {
Chris@0 42 parent::setUp();
Chris@0 43
Chris@0 44 $this->installEntitySchema('entity_test_rev');
Chris@0 45
Chris@0 46 // Create the necessary formats.
Chris@0 47 $this->installConfig(['filter']);
Chris@0 48 FilterFormat::create([
Chris@0 49 'format' => 'no_filters',
Chris@0 50 'filters' => [],
Chris@0 51 ])->save();
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Tests processed properties.
Chris@0 56 */
Chris@0 57 public function testCrudAndUpdate() {
Chris@0 58 $entity_type = 'entity_test';
Chris@0 59 $this->createField($entity_type);
Chris@0 60
Chris@0 61 // Create an entity with a summary and no text format.
Chris@0 62 $storage = $this->container->get('entity_type.manager')
Chris@0 63 ->getStorage($entity_type);
Chris@0 64 $entity = $storage->create();
Chris@0 65 $entity->summary_field->value = $value = $this->randomMachineName();
Chris@0 66 $entity->summary_field->summary = $summary = $this->randomMachineName();
Chris@0 67 $entity->summary_field->format = NULL;
Chris@0 68 $entity->name->value = $this->randomMachineName();
Chris@0 69 $entity->save();
Chris@0 70
Chris@0 71 $entity = $storage->load($entity->id());
Chris@0 72 $this->assertTrue($entity->summary_field instanceof FieldItemListInterface, 'Field implements interface.');
Chris@0 73 $this->assertTrue($entity->summary_field[0] instanceof FieldItemInterface, 'Field item implements interface.');
Chris@0 74 $this->assertEqual($entity->summary_field->value, $value);
Chris@0 75 $this->assertEqual($entity->summary_field->summary, $summary);
Chris@0 76 $this->assertNull($entity->summary_field->format);
Chris@0 77 // Even if no format is given, if text processing is enabled, the default
Chris@0 78 // format is used.
Chris@0 79 $this->assertEqual($entity->summary_field->processed, "<p>$value</p>\n");
Chris@0 80 $this->assertEqual($entity->summary_field->summary_processed, "<p>$summary</p>\n");
Chris@0 81
Chris@0 82 // Change the format, this should update the processed properties.
Chris@0 83 $entity->summary_field->format = 'no_filters';
Chris@0 84 $this->assertEqual($entity->summary_field->processed, $value);
Chris@0 85 $this->assertEqual($entity->summary_field->summary_processed, $summary);
Chris@0 86
Chris@0 87 // Test the generateSampleValue() method.
Chris@0 88 $entity = $this->container->get('entity_type.manager')
Chris@0 89 ->getStorage($entity_type)
Chris@0 90 ->create();
Chris@0 91 $entity->summary_field->generateSampleItems();
Chris@0 92 $this->entityValidateAndSave($entity);
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Creates a text_with_summary field storage and field.
Chris@0 97 *
Chris@0 98 * @param string $entity_type
Chris@0 99 * Entity type for which the field should be created.
Chris@0 100 */
Chris@0 101 protected function createField($entity_type) {
Chris@0 102 // Create a field .
Chris@0 103 $this->fieldStorage = FieldStorageConfig::create([
Chris@0 104 'field_name' => 'summary_field',
Chris@0 105 'entity_type' => $entity_type,
Chris@0 106 'type' => 'text_with_summary',
Chris@0 107 'settings' => [
Chris@0 108 'max_length' => 10,
Chris@0 109 ]
Chris@0 110 ]);
Chris@0 111 $this->fieldStorage->save();
Chris@0 112 $this->field = FieldConfig::create([
Chris@0 113 'field_storage' => $this->fieldStorage,
Chris@0 114 'bundle' => $entity_type,
Chris@0 115 ]);
Chris@0 116 $this->field->save();
Chris@0 117 }
Chris@0 118
Chris@0 119 }