forked from drupal/drupal
/
EditIntegrationTest.php
207 lines (179 loc) · 7.16 KB
/
EditIntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* @file
* Contains \Drupal\editor\Tests\EditIntegrationTest.
*/
namespace Drupal\editor\Tests;
use Drupal\Component\Utility\Json;
use Drupal\Core\Language\Language;
use Drupal\edit\EditorSelector;
use Drupal\edit\MetadataGenerator;
use Drupal\edit\Plugin\InPlaceEditorManager;
use Drupal\edit\Tests\EditTestBase;
use Drupal\edit_test\MockEditEntityFieldAccessCheck;
use Drupal\editor\EditorController;
use Symfony\Component\HttpFoundation\Request;
/**
* Tests Edit module integration (Editor module's inline editing support).
*/
class EditIntegrationTest extends EditTestBase {
/**
* The manager for editor plug-ins.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $editorManager;
/**
* The metadata generator object to be tested.
*
* @var \Drupal\edit\MetadataGeneratorInterface.php
*/
protected $metadataGenerator;
/**
* The editor selector object to be used by the metadata generator object.
*
* @var \Drupal\edit\EditorSelectorInterface
*/
protected $editorSelector;
/**
* The access checker object to be used by the metadata generator object.
*
* @var \Drupal\edit\Access\EditEntityFieldAccessCheckInterface
*/
protected $accessChecker;
/**
* The name of the field ued for tests.
*
* @var string
*/
protected $field_name;
public static function getInfo() {
return array(
'name' => 'In-place text editors (Edit module integration)',
'description' => 'Tests Edit module integration (Editor module\'s inline editing support).',
'group' => 'Text Editor',
);
}
public function setUp() {
parent::setUp();
// Install the Filter module.
$this->installSchema('system', 'url_alias');
$this->installSchema('user', array('users'));
$this->enableModules(array('user', 'filter'));
// Enable the Text Editor and Text Editor Test module.
$this->enableModules(array('editor', 'editor_test'));
// Create a field.
$this->field_name = 'field_textarea';
$this->createFieldWithInstance(
$this->field_name, 'text', 1, 'Long text field',
// Instance settings.
array('text_processing' => 1),
// Widget type & settings.
'text_textarea',
array('size' => 42),
// 'default' formatter type & settings.
'text_default',
array()
);
// Create text format.
$full_html_format = entity_create('filter_format', array(
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => array(),
));
$full_html_format->save();
// Associate text editor with text format.
$editor = entity_create('editor', array(
'format' => $full_html_format->format,
'editor' => 'unicorn',
));
$editor->save();
}
/**
* Returns the in-place editor that Edit selects.
*/
protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'default') {
$entity = entity_load('entity_test', $entity_id, TRUE);
$items = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_name);
$options = entity_get_display('entity_test', 'entity_test', $view_mode)->getComponent($field_name);
return $this->editorSelector->getEditor($options['type'], $items);
}
/**
* Tests editor selection when the Editor module is present.
*
* Tests a textual field, with text processing, with cardinality 1 and >1,
* always with a ProcessedTextEditor plug-in present, but with varying text
* format compatibility.
*/
public function testEditorSelection() {
$this->editorManager = $this->container->get('plugin.manager.edit.editor');
$this->editorSelector = $this->container->get('edit.editor.selector');
// Create an entity with values for this text field.
$this->entity = entity_create('entity_test');
$this->entity->{$this->field_name}->value = 'Hello, world!';
$this->entity->{$this->field_name}->format = 'filtered_html';
$this->entity->save();
// Editor selection w/ cardinality 1, text format w/o associated text editor.
$this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $this->field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected.");
// Editor selection w/ cardinality 1, text format w/ associated text editor.
$this->entity->{$this->field_name}->format = 'full_html';
$this->entity->save();
$this->assertEqual('editor', $this->getSelectedEditor($this->entity->id(), $this->field_name), "With cardinality 1, and the full_html text format, the 'editor' editor is selected.");
// Editor selection with text processing, cardinality >1
$this->field_textarea_field->cardinality = 2;
$this->field_textarea_field->save();
$this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $this->field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected.");
}
/**
* Tests (custom) metadata when the formatted text editor is used.
*/
public function testMetadata() {
$this->editorManager = $this->container->get('plugin.manager.edit.editor');
$this->accessChecker = new MockEditEntityFieldAccessCheck();
$this->editorSelector = $this->container->get('edit.editor.selector');
$this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
// Create an entity with values for the field.
$this->entity = entity_create('entity_test');
$this->entity->{$this->field_name}->value = 'Test';
$this->entity->{$this->field_name}->format = 'full_html';
$this->entity->save();
$entity = entity_load('entity_test', $this->entity->id());
// Verify metadata.
$items = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($this->field_name);
$metadata = $this->metadataGenerator->generateFieldMetadata($items, 'default');
$expected = array(
'access' => TRUE,
'label' => 'Long text field',
'editor' => 'editor',
'aria' => 'Entity entity_test 1, field Long text field',
'custom' => array(
'format' => 'full_html',
'formatHasTransformations' => FALSE,
),
);
$this->assertEqual($expected, $metadata, 'The correct metadata (including custom metadata) is generated.');
}
/**
* Tests GetUntransformedTextCommand AJAX command.
*/
public function testGetUntransformedTextCommand() {
// Create an entity with values for the field.
$this->entity = entity_create('entity_test');
$this->entity->{$this->field_name}->value = 'Test';
$this->entity->{$this->field_name}->format = 'full_html';
$this->entity->save();
$entity = entity_load('entity_test', $this->entity->id());
// Verify AJAX response.
$controller = new EditorController();
$request = new Request();
$response = $controller->getUntransformedText($entity, $this->field_name, Language::LANGCODE_NOT_SPECIFIED, 'default');
$expected = array(
array(
'command' => 'editorGetUntransformedText',
'data' => 'Test',
)
);
$this->assertEqual(Json::encode($expected), $response->prepare($request)->getContent(), 'The GetUntransformedTextCommand AJAX command works correctly.');
}
}