fdedb79907a867c0fbb4871c85d7df1b21510a40
[yaffs-website] / web / core / modules / field / src / Tests / String / StringFieldTest.php
1 <?php
2
3 namespace Drupal\field\Tests\String;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\simpletest\WebTestBase;
9 use Drupal\field\Entity\FieldStorageConfig;
10
11 /**
12  * Tests the creation of string fields.
13  *
14  * @group text
15  */
16 class StringFieldTest extends WebTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['entity_test', 'file'];
24
25   /**
26    * A user without any special permissions.
27    *
28    * @var \Drupal\user\UserInterface
29    */
30   protected $webUser;
31
32   protected function setUp() {
33     parent::setUp();
34
35     $this->webUser = $this->drupalCreateUser(['view test entity', 'administer entity_test content', 'access content']);
36     $this->drupalLogin($this->webUser);
37   }
38
39   // Test fields.
40
41   /**
42    * Test widgets.
43    */
44   public function testTextfieldWidgets() {
45     $this->_testTextfieldWidgets('string', 'string_textfield');
46     $this->_testTextfieldWidgets('string_long', 'string_textarea');
47   }
48
49   /**
50    * Helper function for testTextfieldWidgets().
51    */
52   public function _testTextfieldWidgets($field_type, $widget_type) {
53     // Create a field.
54     $field_name = Unicode::strtolower($this->randomMachineName());
55     $field_storage = FieldStorageConfig::create([
56       'field_name' => $field_name,
57       'entity_type' => 'entity_test',
58       'type' => $field_type
59     ]);
60     $field_storage->save();
61     FieldConfig::create([
62       'field_storage' => $field_storage,
63       'bundle' => 'entity_test',
64       'label' => $this->randomMachineName() . '_label',
65     ])->save();
66     entity_get_form_display('entity_test', 'entity_test', 'default')
67       ->setComponent($field_name, [
68         'type' => $widget_type,
69         'settings' => [
70           'placeholder' => 'A placeholder on ' . $widget_type,
71         ],
72       ])
73       ->save();
74     entity_get_display('entity_test', 'entity_test', 'full')
75       ->setComponent($field_name)
76       ->save();
77
78     // Display creation form.
79     $this->drupalGet('entity_test/add');
80     $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
81     $this->assertNoFieldByName("{$field_name}[0][format]", '1', 'Format selector is not displayed');
82     $this->assertRaw(format_string('placeholder="A placeholder on @widget_type"', ['@widget_type' => $widget_type]));
83
84     // Submit with some value.
85     $value = $this->randomMachineName();
86     $edit = [
87       "{$field_name}[0][value]" => $value,
88     ];
89     $this->drupalPostForm(NULL, $edit, t('Save'));
90     preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
91     $id = $match[1];
92     $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
93
94     // Display the entity.
95     $entity = EntityTest::load($id);
96     $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
97     $content = $display->build($entity);
98     $this->setRawContent(\Drupal::service('renderer')->renderRoot($content));
99     $this->assertText($value, 'Filtered tags are not displayed');
100   }
101
102 }