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