Mercurial > hg > isophonics-drupal-site
comparison core/modules/field/src/Tests/FieldDefaultValueCallbackTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\field\Tests; | |
4 | |
5 use Drupal\field\Entity\FieldConfig; | |
6 use Drupal\field\Entity\FieldStorageConfig; | |
7 use Drupal\simpletest\WebTestBase; | |
8 | |
9 /** | |
10 * Tests the default value callback. | |
11 * | |
12 * @group field | |
13 */ | |
14 class FieldDefaultValueCallbackTest extends WebTestBase { | |
15 | |
16 /** | |
17 * Modules to enable. | |
18 * | |
19 * @var array | |
20 */ | |
21 public static $modules = ['node', 'field_test', 'field_ui']; | |
22 | |
23 /** | |
24 * The field name. | |
25 * | |
26 * @var string | |
27 */ | |
28 private $fieldName; | |
29 | |
30 /** | |
31 * {@inheritdoc} | |
32 */ | |
33 protected function setUp() { | |
34 parent::setUp(); | |
35 | |
36 $this->fieldName = 'field_test'; | |
37 | |
38 // Create Article node types. | |
39 if ($this->profile != 'standard') { | |
40 $this->drupalCreateContentType([ | |
41 'type' => 'article', | |
42 'name' => 'Article', | |
43 ]); | |
44 } | |
45 | |
46 } | |
47 | |
48 public function testDefaultValueCallbackForm() { | |
49 // Create a field and storage for checking. | |
50 /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */ | |
51 FieldStorageConfig::create([ | |
52 'field_name' => $this->fieldName, | |
53 'entity_type' => 'node', | |
54 'type' => 'text', | |
55 ])->save(); | |
56 /** @var \Drupal\field\Entity\FieldConfig $field_config */ | |
57 $field_config = FieldConfig::create([ | |
58 'entity_type' => 'node', | |
59 'field_name' => $this->fieldName, | |
60 'bundle' => 'article', | |
61 ]); | |
62 $field_config->save(); | |
63 | |
64 $this->drupalLogin($this->rootUser); | |
65 | |
66 // Check that the default field form is visible when no callback is set. | |
67 $this->drupalGet('/admin/structure/types/manage/article/fields/node.article.field_test'); | |
68 $this->assertFieldByName('default_value_input[field_test][0][value]', NULL, 'The default field form is visible.'); | |
69 | |
70 // Set a different field value, it should be on the field. | |
71 $default_value = $this->randomString(); | |
72 $field_config->setDefaultValue([['value' => $default_value]])->save(); | |
73 $this->drupalGet('/admin/structure/types/manage/article/fields/node.article.field_test'); | |
74 $this->assertFieldByName('default_value_input[field_test][0][value]', $default_value, 'The default field form is visible.'); | |
75 | |
76 // Set a different field value to the field directly, instead of an array. | |
77 $default_value = $this->randomString(); | |
78 $field_config->setDefaultValue($default_value)->save(); | |
79 $this->drupalGet('/admin/structure/types/manage/article/fields/node.article.field_test'); | |
80 $this->assertFieldByName('default_value_input[field_test][0][value]', $default_value, 'The default field form is visible.'); | |
81 | |
82 // Set a default value callback instead, and the default field form should | |
83 // not be visible. | |
84 $field_config->setDefaultValueCallback('\Drupal\field\Tests\FieldDefaultValueCallbackProvider::calculateDefaultValue')->save(); | |
85 $this->drupalGet('/admin/structure/types/manage/article/fields/node.article.field_test'); | |
86 $this->assertNoFieldByName('default_value_input[field_test][0][value]', 'Calculated default value', 'The default field form is not visible when a callback is defined.'); | |
87 } | |
88 | |
89 } |