Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
158
core/modules/quickedit/src/Tests/EditorSelectionTest.php
Normal file
158
core/modules/quickedit/src/Tests/EditorSelectionTest.php
Normal file
|
@ -0,0 +1,158 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\quickedit\Tests\EditorSelectionTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\quickedit\Tests;
|
||||
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\quickedit\EditorSelector;
|
||||
|
||||
/**
|
||||
* Tests in-place field editor selection.
|
||||
*
|
||||
* @group quickedit
|
||||
*/
|
||||
class EditorSelectionTest extends QuickEditTestBase {
|
||||
|
||||
/**
|
||||
* The manager for editor plugins.
|
||||
*
|
||||
* @var \Drupal\Component\Plugin\PluginManagerInterface
|
||||
*/
|
||||
protected $editorManager;
|
||||
|
||||
/**
|
||||
* The editor selector object to be tested.
|
||||
*
|
||||
* @var \Drupal\quickedit\EditorSelectorInterface
|
||||
*/
|
||||
protected $editorSelector;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
|
||||
$this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the in-place editor that Quick Edit selects.
|
||||
*/
|
||||
protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'default') {
|
||||
$entity = entity_load('entity_test', $entity_id, TRUE);
|
||||
$items = $entity->getTranslation(LanguageInterface::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 a string (plain text) field, with cardinality 1 and >1.
|
||||
*/
|
||||
public function testText() {
|
||||
$field_name = 'field_text';
|
||||
$this->createFieldWithStorage(
|
||||
$field_name, 'string', 1, 'Simple text field',
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'string_textfield',
|
||||
array('size' => 42),
|
||||
// 'default' formatter type & settings.
|
||||
'string',
|
||||
array()
|
||||
);
|
||||
|
||||
// Create an entity with values for this text field.
|
||||
$entity = entity_create('entity_test');
|
||||
$entity->{$field_name}->value = 'Hello, world!';
|
||||
$entity->save();
|
||||
|
||||
// With cardinality 1.
|
||||
$this->assertEqual('plain_text', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality 1, the 'plain_text' editor is selected.");
|
||||
|
||||
// With cardinality >1
|
||||
$this->fields->field_text_field_storage->setCardinality(2);
|
||||
$this->fields->field_text_field_storage->save();
|
||||
$this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality >1, the 'form' editor is selected.");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a textual field, with text filtering, with cardinality 1 and >1,
|
||||
* always with an Editor plugin present that supports textual fields with text
|
||||
* filtering, but with varying text format compatibility.
|
||||
*/
|
||||
public function testTextWysiwyg() {
|
||||
// Enable edit_test module so that the 'wysiwyg' editor becomes available.
|
||||
$this->enableModules(array('quickedit_test'));
|
||||
$this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
|
||||
$this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter'));
|
||||
|
||||
$field_name = 'field_textarea';
|
||||
$this->createFieldWithStorage(
|
||||
$field_name, 'text', 1, 'Long text field',
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'text_textarea',
|
||||
array('size' => 42),
|
||||
// 'default' formatter type & settings.
|
||||
'text_default',
|
||||
array()
|
||||
);
|
||||
|
||||
// Create an entity with values for this text field.
|
||||
$entity = entity_create('entity_test');
|
||||
$entity->{$field_name}->value = 'Hello, world!';
|
||||
$entity->{$field_name}->format = 'filtered_html';
|
||||
$entity->save();
|
||||
|
||||
// Editor selection w/ cardinality 1, text format w/o associated text editor.
|
||||
$this->assertEqual('form', $this->getSelectedEditor($entity->id(), $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.
|
||||
$entity->{$field_name}->format = 'full_html';
|
||||
$entity->save();
|
||||
$this->assertEqual('wysiwyg', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality 1, and the full_html text format, the 'wysiwyg' editor is selected.");
|
||||
|
||||
// Editor selection with text field, cardinality >1.
|
||||
$this->fields->field_textarea_field_storage->setCardinality(2);
|
||||
$this->fields->field_textarea_field_storage->save();
|
||||
$this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a number field, with cardinality 1 and >1.
|
||||
*/
|
||||
public function testNumber() {
|
||||
$field_name = 'field_nr';
|
||||
$this->createFieldWithStorage(
|
||||
$field_name, 'integer', 1, 'Simple number field',
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'number',
|
||||
array(),
|
||||
// 'default' formatter type & settings.
|
||||
'number_integer',
|
||||
array()
|
||||
);
|
||||
|
||||
// Create an entity with values for this text field.
|
||||
$entity = entity_create('entity_test');
|
||||
$entity->{$field_name}->value = 42;
|
||||
$entity->save();
|
||||
|
||||
// Editor selection with cardinality 1.
|
||||
$this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality 1, the 'form' editor is selected.");
|
||||
|
||||
// Editor selection with cardinality >1.
|
||||
$this->fields->field_nr_field_storage->setCardinality(2);
|
||||
$this->fields->field_nr_field_storage->save();
|
||||
$this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality >1, the 'form' editor is selected.");
|
||||
}
|
||||
|
||||
}
|
188
core/modules/quickedit/src/Tests/MetadataGeneratorTest.php
Normal file
188
core/modules/quickedit/src/Tests/MetadataGeneratorTest.php
Normal file
|
@ -0,0 +1,188 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\quickedit\Tests\MetadataGeneratorTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\quickedit\Tests;
|
||||
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\quickedit\EditorSelector;
|
||||
use Drupal\quickedit\MetadataGenerator;
|
||||
use Drupal\quickedit\Plugin\InPlaceEditorManager;
|
||||
use Drupal\quickedit_test\MockEditEntityFieldAccessCheck;
|
||||
|
||||
/**
|
||||
* Tests in-place field editing metadata.
|
||||
*
|
||||
* @group quickedit
|
||||
*/
|
||||
class MetadataGeneratorTest extends QuickEditTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('quickedit_test');
|
||||
|
||||
/**
|
||||
* The manager for editor plugins.
|
||||
*
|
||||
* @var \Drupal\Component\Plugin\PluginManagerInterface
|
||||
*/
|
||||
protected $editorManager;
|
||||
|
||||
/**
|
||||
* The metadata generator object to be tested.
|
||||
*
|
||||
* @var \Drupal\quickedit\MetadataGeneratorInterface.php
|
||||
*/
|
||||
protected $metadataGenerator;
|
||||
|
||||
/**
|
||||
* The editor selector object to be used by the metadata generator object.
|
||||
*
|
||||
* @var \Drupal\quickedit\EditorSelectorInterface
|
||||
*/
|
||||
protected $editorSelector;
|
||||
|
||||
/**
|
||||
* The access checker object to be used by the metadata generator object.
|
||||
*
|
||||
* @var \Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface
|
||||
*/
|
||||
protected $accessChecker;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
|
||||
$this->accessChecker = new MockEditEntityFieldAccessCheck();
|
||||
$this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter'));
|
||||
$this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a simple entity type, with two different simple fields.
|
||||
*/
|
||||
public function testSimpleEntityType() {
|
||||
$field_1_name = 'field_text';
|
||||
$field_1_label = 'Plain text field';
|
||||
$this->createFieldWithStorage(
|
||||
$field_1_name, 'string', 1, $field_1_label,
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'string_textfield',
|
||||
array('size' => 42),
|
||||
// 'default' formatter type & settings.
|
||||
'string',
|
||||
array()
|
||||
);
|
||||
$field_2_name = 'field_nr';
|
||||
$field_2_label = 'Simple number field';
|
||||
$this->createFieldWithStorage(
|
||||
$field_2_name, 'integer', 1, $field_2_label,
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'number',
|
||||
array(),
|
||||
// 'default' formatter type & settings.
|
||||
'number_integer',
|
||||
array()
|
||||
);
|
||||
|
||||
// Create an entity with values for this text field.
|
||||
$entity = entity_create('entity_test');
|
||||
$entity->{$field_1_name}->value = 'Test';
|
||||
$entity->{$field_2_name}->value = 42;
|
||||
$entity->save();
|
||||
$entity = entity_load('entity_test', $entity->id());
|
||||
|
||||
// Verify metadata for field 1.
|
||||
$items_1 = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($field_1_name);
|
||||
$metadata_1 = $this->metadataGenerator->generateFieldMetadata($items_1, 'default');
|
||||
$expected_1 = array(
|
||||
'access' => TRUE,
|
||||
'label' => 'Plain text field',
|
||||
'editor' => 'plain_text',
|
||||
'aria' => 'Entity entity_test 1, field Plain text field',
|
||||
);
|
||||
$this->assertEqual($expected_1, $metadata_1, 'The correct metadata is generated for the first field.');
|
||||
|
||||
// Verify metadata for field 2.
|
||||
$items_2 = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($field_2_name);
|
||||
$metadata_2 = $this->metadataGenerator->generateFieldMetadata($items_2, 'default');
|
||||
$expected_2 = array(
|
||||
'access' => TRUE,
|
||||
'label' => 'Simple number field',
|
||||
'editor' => 'form',
|
||||
'aria' => 'Entity entity_test 1, field Simple number field',
|
||||
);
|
||||
$this->assertEqual($expected_2, $metadata_2, 'The correct metadata is generated for the second field.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a field whose associated in-place editor generates custom metadata.
|
||||
*/
|
||||
public function testEditorWithCustomMetadata() {
|
||||
$this->installSchema('system', 'url_alias');
|
||||
|
||||
$this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
|
||||
$this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter'));
|
||||
$this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
|
||||
|
||||
$this->editorManager = $this->container->get('plugin.manager.quickedit.editor');
|
||||
$this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter'));
|
||||
$this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager);
|
||||
|
||||
// Create a rich text field.
|
||||
$field_name = 'field_rich';
|
||||
$field_label = 'Rich text field';
|
||||
$this->createFieldWithStorage(
|
||||
$field_name, 'text', 1, $field_label,
|
||||
// Instance settings.
|
||||
array(),
|
||||
// Widget type & settings.
|
||||
'text_textfield',
|
||||
array('size' => 42),
|
||||
// 'default' formatter type & settings.
|
||||
'text_default',
|
||||
array()
|
||||
);
|
||||
|
||||
// Create a text format.
|
||||
$full_html_format = entity_create('filter_format', array(
|
||||
'format' => 'full_html',
|
||||
'name' => 'Full HTML',
|
||||
'weight' => 1,
|
||||
'filters' => array(
|
||||
'filter_htmlcorrector' => array('status' => 1),
|
||||
),
|
||||
));
|
||||
$full_html_format->save();
|
||||
|
||||
// Create an entity with values for this rich text field.
|
||||
$entity = entity_create('entity_test');
|
||||
$entity->{$field_name}->value = 'Test';
|
||||
$entity->{$field_name}->format = 'full_html';
|
||||
$entity->save();
|
||||
$entity = entity_load('entity_test', $entity->id());
|
||||
|
||||
// Verify metadata.
|
||||
$items = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($field_name);
|
||||
$metadata = $this->metadataGenerator->generateFieldMetadata($items, 'default');
|
||||
$expected = array(
|
||||
'access' => TRUE,
|
||||
'label' => 'Rich text field',
|
||||
'editor' => 'wysiwyg',
|
||||
'aria' => 'Entity entity_test 1, field Rich text field',
|
||||
'custom' => array(
|
||||
'format' => 'full_html'
|
||||
),
|
||||
);
|
||||
$this->assertEqual($expected, $metadata); //, 'The correct metadata (including custom metadata) is generated.');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,217 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\quickedit\Tests\QuickEditAutocompleteTermTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\quickedit\Tests;
|
||||
|
||||
use Drupal\Component\Serialization\Json;
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests in-place editing of autocomplete tags.
|
||||
*
|
||||
* @group quickedit
|
||||
*/
|
||||
class QuickEditAutocompleteTermTest extends WebTestBase {
|
||||
|
||||
use EntityReferenceTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'taxonomy', 'quickedit');
|
||||
|
||||
/**
|
||||
* Stores the node used for the tests.
|
||||
*
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $node;
|
||||
|
||||
/**
|
||||
* Stores the vocabulary used in the tests.
|
||||
*
|
||||
* @var \Drupal\taxonomy\VocabularyInterface
|
||||
*/
|
||||
protected $vocabulary;
|
||||
|
||||
/**
|
||||
* Stores the first term used in the tests.
|
||||
*
|
||||
* @var \Drupal\taxonomy\TermInterface
|
||||
*/
|
||||
protected $term1;
|
||||
|
||||
/**
|
||||
* Stores the second term used in the tests.
|
||||
*
|
||||
* @var \Drupal\taxonomy\TermInterface
|
||||
*/
|
||||
protected $term2;
|
||||
|
||||
/**
|
||||
* Stores the field name for the autocomplete field.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fieldName;
|
||||
|
||||
/**
|
||||
* An user with permissions to access in-place editor.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $editorUser;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalCreateContentType(array(
|
||||
'type' => 'article',
|
||||
));
|
||||
// Create the vocabulary for the tag field.
|
||||
$this->vocabulary = entity_create('taxonomy_vocabulary', [
|
||||
'name' => 'quickedit testing tags',
|
||||
'vid' => 'quickedit_testing_tags',
|
||||
]);
|
||||
$this->vocabulary->save();
|
||||
$this->fieldName = 'field_' . $this->vocabulary->id();
|
||||
|
||||
$handler_settings = array(
|
||||
'target_bundles' => array(
|
||||
$this->vocabulary->id() => $this->vocabulary->id(),
|
||||
),
|
||||
'auto_create' => TRUE,
|
||||
);
|
||||
$this->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
|
||||
|
||||
entity_get_form_display('node', 'article', 'default')
|
||||
->setComponent($this->fieldName, [
|
||||
'type' => 'entity_reference_autocomplete_tags',
|
||||
'weight' => -4,
|
||||
])
|
||||
->save();
|
||||
|
||||
entity_get_display('node', 'article', 'default')
|
||||
->setComponent($this->fieldName, [
|
||||
'type' => 'entity_reference_label',
|
||||
'weight' => 10,
|
||||
])
|
||||
->save();
|
||||
entity_get_display('node', 'article', 'teaser')
|
||||
->setComponent($this->fieldName, [
|
||||
'type' => 'entity_reference_label',
|
||||
'weight' => 10,
|
||||
])
|
||||
->save();
|
||||
|
||||
$this->term1 = $this->createTerm();
|
||||
$this->term2 = $this->createTerm();
|
||||
|
||||
$node = array();
|
||||
$node['type'] = 'article';
|
||||
$node[$this->fieldName][]['target_id'] = $this->term1->id();
|
||||
$node[$this->fieldName][]['target_id'] = $this->term2->id();
|
||||
$this->node = $this->drupalCreateNode($node);
|
||||
|
||||
$this->editorUser = $this->drupalCreateUser(['access content', 'create article content', 'edit any article content', 'access in-place editing']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests Quick Edit autocomplete term behavior.
|
||||
*/
|
||||
public function testAutocompleteQuickEdit() {
|
||||
$this->drupalLogin($this->editorUser);
|
||||
|
||||
$quickedit_uri = 'quickedit/form/node/'. $this->node->id() . '/' . $this->fieldName . '/' . $this->node->language()->getId() . '/full';
|
||||
$post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData();
|
||||
$response = $this->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post);
|
||||
$ajax_commands = Json::decode($response);
|
||||
|
||||
// Prepare form values for submission. drupalPostAJAX() is not suitable for
|
||||
// handling pages with JSON responses, so we need our own solution here.
|
||||
$form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
|
||||
$this->assertTrue($form_tokens_found, 'Form tokens found in output.');
|
||||
|
||||
if ($form_tokens_found) {
|
||||
$post = array(
|
||||
'form_id' => 'quickedit_field_form',
|
||||
'form_token' => $token_match[1],
|
||||
'form_build_id' => $build_id_match[1],
|
||||
$this->fieldName . '[target_id]' => implode(', ', array($this->term1->getName(), 'new term', $this->term2->getName())),
|
||||
'op' => t('Save'),
|
||||
);
|
||||
|
||||
// Submit field form and check response. Should render back all the terms.
|
||||
$response = $this->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->setRawContent($ajax_commands[0]['data']);
|
||||
$this->assertLink($this->term1->getName());
|
||||
$this->assertLink($this->term2->getName());
|
||||
$this->assertText('new term');
|
||||
$this->assertNoLink('new term');
|
||||
|
||||
// Load the form again, which should now get it back from
|
||||
// PrivateTempStore.
|
||||
$quickedit_uri = 'quickedit/form/node/'. $this->node->id() . '/' . $this->fieldName . '/' . $this->node->language()->getId() . '/full';
|
||||
$post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData();
|
||||
$response = $this->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post);
|
||||
$ajax_commands = Json::decode($response);
|
||||
|
||||
// The AjaxResponse's first command is an InsertCommand which contains
|
||||
// the form to edit the taxonomy term field, it should contain all three
|
||||
// taxonomy terms, including the one that has just been newly created and
|
||||
// which is not yet stored.
|
||||
$this->setRawContent($ajax_commands[0]['data']);
|
||||
$expected = array(
|
||||
$this->term1->getName() . ' (' . $this->term1->id() . ')',
|
||||
'new term',
|
||||
$this->term2->getName() . ' (' . $this->term2->id() . ')',
|
||||
);
|
||||
$this->assertFieldByName($this->fieldName . '[target_id]', implode(', ', $expected));
|
||||
|
||||
// Save the entity.
|
||||
$post = array('nocssjs' => 'true');
|
||||
$response = $this->drupalPostWithFormat('quickedit/entity/node/' . $this->node->id(), 'json', $post);
|
||||
$this->assertResponse(200);
|
||||
|
||||
// The full node display should now link to all entities, with the new
|
||||
// one created in the database as well.
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertLink($this->term1->getName());
|
||||
$this->assertLink($this->term2->getName());
|
||||
$this->assertLink('new term');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new term with random name and description in $this->vocabulary.
|
||||
*
|
||||
* @return \Drupal\taxonomy\TermInterface
|
||||
* The created taxonomy term.
|
||||
*/
|
||||
protected function createTerm() {
|
||||
$filter_formats = filter_formats();
|
||||
$format = array_pop($filter_formats);
|
||||
$term = entity_create('taxonomy_term', array(
|
||||
'name' => $this->randomMachineName(),
|
||||
'description' => $this->randomMachineName(),
|
||||
// Use the first available text format.
|
||||
'format' => $format->id(),
|
||||
'vid' => $this->vocabulary->id(),
|
||||
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
||||
));
|
||||
$term->save();
|
||||
return $term;
|
||||
}
|
||||
|
||||
}
|
580
core/modules/quickedit/src/Tests/QuickEditLoadingTest.php
Normal file
580
core/modules/quickedit/src/Tests/QuickEditLoadingTest.php
Normal file
|
@ -0,0 +1,580 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\quickedit\Tests\QuickEditLoadingTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\quickedit\Tests;
|
||||
|
||||
use Drupal\Component\Serialization\Json;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\block_content\Entity\BlockContent;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests loading of in-place editing functionality and lazy loading of its
|
||||
* in-place editors.
|
||||
*
|
||||
* @group quickedit
|
||||
*/
|
||||
class QuickEditLoadingTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array(
|
||||
'contextual',
|
||||
'quickedit',
|
||||
'filter',
|
||||
'node',
|
||||
'image',
|
||||
);
|
||||
|
||||
/**
|
||||
* An user with permissions to create and edit articles.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $authorUser;
|
||||
|
||||
/**
|
||||
* A author user with permissions to access in-place editor.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $editorUser;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create a text format.
|
||||
$filtered_html_format = entity_create('filter_format', array(
|
||||
'format' => 'filtered_html',
|
||||
'name' => 'Filtered HTML',
|
||||
'weight' => 0,
|
||||
'filters' => array(),
|
||||
));
|
||||
$filtered_html_format->save();
|
||||
|
||||
// Create a node type.
|
||||
$this->drupalCreateContentType(array(
|
||||
'type' => 'article',
|
||||
'name' => 'Article',
|
||||
));
|
||||
|
||||
// Create one node of the above node type using the above text format.
|
||||
$this->drupalCreateNode(array(
|
||||
'type' => 'article',
|
||||
'body' => array(
|
||||
0 => array(
|
||||
'value' => '<p>How are you?</p>',
|
||||
'format' => 'filtered_html',
|
||||
)
|
||||
),
|
||||
'revision_log' => $this->randomString(),
|
||||
));
|
||||
|
||||
// Create 2 users, the only difference being the ability to use in-place
|
||||
// editing
|
||||
$basic_permissions = array('access content', 'create article content', 'edit any article content', 'use text format filtered_html', 'access contextual links');
|
||||
$this->authorUser = $this->drupalCreateUser($basic_permissions);
|
||||
$this->editorUser = $this->drupalCreateUser(array_merge($basic_permissions, array('access in-place editing')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the loading of Quick Edit when a user doesn't have access to it.
|
||||
*/
|
||||
public function testUserWithoutPermission() {
|
||||
$this->drupalLogin($this->authorUser);
|
||||
$this->drupalGet('node/1');
|
||||
|
||||
// Library and in-place editors.
|
||||
$this->assertNoRaw('core/modules/quickedit/js/quickedit.js', 'Quick Edit library not loaded.');
|
||||
$this->assertNoRaw('core/modules/quickedit/js/editors/formEditor.js', "'form' in-place editor not loaded.");
|
||||
|
||||
// HTML annotation must always exist (to not break the render cache).
|
||||
$this->assertRaw('data-quickedit-entity-id="node/1"');
|
||||
$this->assertRaw('data-quickedit-field-id="node/1/body/en/full"');
|
||||
|
||||
// Retrieving the metadata should result in an empty 403 response.
|
||||
$post = array('fields[0]' => 'node/1/body/en/full');
|
||||
$response = $this->drupalPostWithFormat(Url::fromRoute('quickedit.metadata'), 'json', $post);
|
||||
$this->assertIdentical('{"message":""}', $response);
|
||||
$this->assertResponse(403);
|
||||
|
||||
// Quick Edit's JavaScript would SearchRankingTestnever hit these endpoints if the metadata
|
||||
// was empty as above, but we need to make sure that malicious users aren't
|
||||
// able to use any of the other endpoints either.
|
||||
$post = array('editors[0]' => 'form') + $this->getAjaxPageStatePostData();
|
||||
$response = $this->drupalPost('quickedit/attachments', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
|
||||
$this->assertIdentical('{}', $response);
|
||||
$this->assertResponse(403);
|
||||
$post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData();
|
||||
$response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', '', $post, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
|
||||
$this->assertIdentical('{}', $response);
|
||||
$this->assertResponse(403);
|
||||
$edit = array();
|
||||
$edit['form_id'] = 'quickedit_field_form';
|
||||
$edit['form_token'] = 'xIOzMjuc-PULKsRn_KxFn7xzNk5Bx7XKXLfQfw1qOnA';
|
||||
$edit['form_build_id'] = 'form-kVmovBpyX-SJfTT5kY0pjTV35TV-znor--a64dEnMR8';
|
||||
$edit['body[0][summary]'] = '';
|
||||
$edit['body[0][value]'] = '<p>Malicious content.</p>';
|
||||
$edit['body[0][format]'] = 'filtered_html';
|
||||
$edit['op'] = t('Save');
|
||||
$response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', '', $edit, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
|
||||
$this->assertIdentical('{}', $response);
|
||||
$this->assertResponse(403);
|
||||
$post = array('nocssjs' => 'true');
|
||||
$response = $this->drupalPostWithFormat('quickedit/entity/' . 'node/1', 'json', $post);
|
||||
$this->assertIdentical('{"message":""}', $response);
|
||||
$this->assertResponse(403);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the loading of Quick Edit when a user does have access to it.
|
||||
*
|
||||
* Also ensures lazy loading of in-place editors works.
|
||||
*/
|
||||
public function testUserWithPermission() {
|
||||
$this->drupalLogin($this->editorUser);
|
||||
$this->drupalGet('node/1');
|
||||
|
||||
// Library and in-place editors.
|
||||
$settings = $this->getDrupalSettings();
|
||||
$libraries = explode(',', $settings['ajaxPageState']['libraries']);
|
||||
$this->assertTrue(in_array('quickedit/quickedit', $libraries), 'Quick Edit library loaded.');
|
||||
$this->assertFalse(in_array('quickedit/quickedit.inPlaceEditor.form', $libraries), "'form' in-place editor not loaded.");
|
||||
|
||||
// HTML annotation must always exist (to not break the render cache).
|
||||
$this->assertRaw('data-quickedit-entity-id="node/1"');
|
||||
$this->assertRaw('data-quickedit-field-id="node/1/body/en/full"');
|
||||
|
||||
// There should be only one revision so far.
|
||||
$node = Node::load(1);
|
||||
$vids = \Drupal::entityManager()->getStorage('node')->revisionIds($node);
|
||||
$this->assertIdentical(1, count($vids), 'The node has only one revision.');
|
||||
$original_log = $node->revision_log->value;
|
||||
|
||||
// Retrieving the metadata should result in a 200 JSON response.
|
||||
$htmlPageDrupalSettings = $this->drupalSettings;
|
||||
$post = array('fields[0]' => 'node/1/body/en/full');
|
||||
$response = $this->drupalPostWithFormat('quickedit/metadata', 'json', $post);
|
||||
$this->assertResponse(200);
|
||||
$expected = array(
|
||||
'node/1/body/en/full' => array(
|
||||
'label' => 'Body',
|
||||
'access' => TRUE,
|
||||
'editor' => 'form',
|
||||
'aria' => 'Entity node 1, field Body',
|
||||
)
|
||||
);
|
||||
$this->assertIdentical(Json::decode($response), $expected, 'The metadata HTTP request answers with the correct JSON response.');
|
||||
// Restore drupalSettings to build the next requests; simpletest wipes them
|
||||
// after a JSON response.
|
||||
$this->drupalSettings = $htmlPageDrupalSettings;
|
||||
|
||||
// Retrieving the attachments should result in a 200 response, containing:
|
||||
// 1. a settings command with useless metadata: AjaxController is dumb
|
||||
// 2. an insert command that loads the required in-place editors
|
||||
$post = array('editors[0]' => 'form') + $this->getAjaxPageStatePostData();
|
||||
$response = $this->drupalPost('quickedit/attachments', 'application/vnd.drupal-ajax', $post);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(2, count($ajax_commands), 'The attachments HTTP request results in two AJAX commands.');
|
||||
// First command: settings.
|
||||
$this->assertIdentical('settings', $ajax_commands[0]['command'], 'The first AJAX command is a settings command.');
|
||||
// Second command: insert libraries into DOM.
|
||||
$this->assertIdentical('insert', $ajax_commands[1]['command'], 'The second AJAX command is an append command.');
|
||||
$this->assertTrue(in_array('quickedit/quickedit.inPlaceEditor.form', explode(',', $ajax_commands[0]['settings']['ajaxPageState']['libraries'])), 'The quickedit.inPlaceEditor.form library is loaded.');
|
||||
|
||||
// Retrieving the form for this field should result in a 200 response,
|
||||
// containing only a quickeditFieldForm command.
|
||||
$post = array('nocssjs' => 'true', 'reset' => 'true') + $this->getAjaxPageStatePostData();
|
||||
$response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', 'application/vnd.drupal-ajax', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
|
||||
$this->assertIdentical('quickeditFieldForm', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldForm command.');
|
||||
$this->assertIdentical('<form ', Unicode::substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
|
||||
|
||||
// Prepare form values for submission. drupalPostAjaxForm() is not suitable
|
||||
// for handling pages with JSON responses, so we need our own solution here.
|
||||
$form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
|
||||
$this->assertTrue($form_tokens_found, 'Form tokens found in output.');
|
||||
|
||||
if ($form_tokens_found) {
|
||||
$edit = array(
|
||||
'body[0][summary]' => '',
|
||||
'body[0][value]' => '<p>Fine thanks.</p>',
|
||||
'body[0][format]' => 'filtered_html',
|
||||
'op' => t('Save'),
|
||||
);
|
||||
$post = array(
|
||||
'form_id' => 'quickedit_field_form',
|
||||
'form_token' => $token_match[1],
|
||||
'form_build_id' => $build_id_match[1],
|
||||
);
|
||||
$post += $edit + $this->getAjaxPageStatePostData();
|
||||
|
||||
// Submit field form and check response. This should store the updated
|
||||
// entity in PrivateTempStore on the server.
|
||||
$response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', 'application/vnd.drupal-ajax', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
|
||||
$this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
|
||||
$this->assertTrue(strpos($ajax_commands[0]['data'], 'Fine thanks.'), 'Form value saved and printed back.');
|
||||
$this->assertIdentical($ajax_commands[0]['other_view_modes'], array(), 'Field was not rendered in any other view mode.');
|
||||
|
||||
// Ensure the text on the original node did not change yet.
|
||||
$this->drupalGet('node/1');
|
||||
$this->assertText('How are you?');
|
||||
|
||||
// Save the entity by moving the PrivateTempStore values to entity storage.
|
||||
$post = array('nocssjs' => 'true');
|
||||
$response = $this->drupalPostWithFormat('quickedit/entity/' . 'node/1', 'json', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(1, count($ajax_commands), 'The entity submission HTTP request results in one AJAX command.');
|
||||
$this->assertIdentical('quickeditEntitySaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditEntitySaved command.');
|
||||
$this->assertIdentical($ajax_commands[0]['data']['entity_type'], 'node', 'Saved entity is of type node.');
|
||||
$this->assertIdentical($ajax_commands[0]['data']['entity_id'], '1', 'Entity id is 1.');
|
||||
|
||||
// Ensure the text on the original node did change.
|
||||
$this->drupalGet('node/1');
|
||||
$this->assertText('Fine thanks.');
|
||||
|
||||
// Ensure no new revision was created and the log message is unchanged.
|
||||
$node = Node::load(1);
|
||||
$vids = \Drupal::entityManager()->getStorage('node')->revisionIds($node);
|
||||
$this->assertIdentical(1, count($vids), 'The node has only one revision.');
|
||||
$this->assertIdentical($original_log, $node->revision_log->value, 'The revision log message is unchanged.');
|
||||
|
||||
// Now configure this node type to create new revisions automatically,
|
||||
// then again retrieve the field form, fill it, submit it (so it ends up
|
||||
// in PrivateTempStore) and then save the entity. Now there should be two
|
||||
// revisions.
|
||||
$node_type = NodeType::load('article');
|
||||
$node_type->setNewRevision(TRUE);
|
||||
$node_type->save();
|
||||
|
||||
// Retrieve field form.
|
||||
$post = array('nocssjs' => 'true', 'reset' => 'true');
|
||||
$response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', 'application/vnd.drupal-ajax', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
|
||||
$this->assertIdentical('quickeditFieldForm', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldForm command.');
|
||||
$this->assertIdentical('<form ', Unicode::substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
|
||||
|
||||
// Submit field form.
|
||||
preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match);
|
||||
preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
|
||||
$edit['body[0][value]'] = '<p>kthxbye</p>';
|
||||
$post = array(
|
||||
'form_id' => 'quickedit_field_form',
|
||||
'form_token' => $token_match[1],
|
||||
'form_build_id' => $build_id_match[1],
|
||||
);
|
||||
$post += $edit + $this->getAjaxPageStatePostData();
|
||||
$response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', 'application/vnd.drupal-ajax', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
|
||||
$this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is an quickeditFieldFormSaved command.');
|
||||
$this->assertTrue(strpos($ajax_commands[0]['data'], 'kthxbye'), 'Form value saved and printed back.');
|
||||
|
||||
// Save the entity.
|
||||
$post = array('nocssjs' => 'true');
|
||||
$response = $this->drupalPostWithFormat('quickedit/entity/' . 'node/1', 'json', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(1, count($ajax_commands));
|
||||
$this->assertIdentical('quickeditEntitySaved', $ajax_commands[0]['command'], 'The first AJAX command is an quickeditEntitySaved command.');
|
||||
$this->assertEqual($ajax_commands[0]['data'], ['entity_type' => 'node', 'entity_id' => 1], 'Updated entity type and ID returned');
|
||||
|
||||
// Test that a revision was created with the correct log message.
|
||||
$vids = \Drupal::entityManager()->getStorage('node')->revisionIds(Node::load(1));
|
||||
$this->assertIdentical(2, count($vids), 'The node has two revisions.');
|
||||
$revision = node_revision_load($vids[0]);
|
||||
$this->assertIdentical($original_log, $revision->revision_log->value, 'The first revision log message is unchanged.');
|
||||
$revision = node_revision_load($vids[1]);
|
||||
$this->assertIdentical('Updated the <em class="placeholder">Body</em> field through in-place editing.', $revision->revision_log->value, 'The second revision log message was correctly generated by Quick Edit module.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the loading of Quick Edit for the title base field.
|
||||
*/
|
||||
public function testTitleBaseField() {
|
||||
$this->drupalLogin($this->editorUser);
|
||||
$this->drupalGet('node/1');
|
||||
|
||||
// Ensure that the full page title is actually in-place editable
|
||||
$node = Node::load(1);
|
||||
$elements = $this->xpath('//h1/span[@data-quickedit-field-id="node/1/title/en/full" and normalize-space(text())=:title]', array(':title' => $node->label()));
|
||||
$this->assertTrue(!empty($elements), 'Title with data-quickedit-field-id attribute found.');
|
||||
|
||||
// Retrieving the metadata should result in a 200 JSON response.
|
||||
$htmlPageDrupalSettings = $this->drupalSettings;
|
||||
$post = array('fields[0]' => 'node/1/title/en/full');
|
||||
$response = $this->drupalPostWithFormat('quickedit/metadata', 'json', $post);
|
||||
$this->assertResponse(200);
|
||||
$expected = array(
|
||||
'node/1/title/en/full' => array(
|
||||
'label' => 'Title',
|
||||
'access' => TRUE,
|
||||
'editor' => 'plain_text',
|
||||
'aria' => 'Entity node 1, field Title',
|
||||
)
|
||||
);
|
||||
$this->assertIdentical(Json::decode($response), $expected, 'The metadata HTTP request answers with the correct JSON response.');
|
||||
// Restore drupalSettings to build the next requests; simpletest wipes them
|
||||
// after a JSON response.
|
||||
$this->drupalSettings = $htmlPageDrupalSettings;
|
||||
|
||||
// Retrieving the form for this field should result in a 200 response,
|
||||
// containing only a quickeditFieldForm command.
|
||||
$post = array('nocssjs' => 'true', 'reset' => 'true') + $this->getAjaxPageStatePostData();
|
||||
$response = $this->drupalPost('quickedit/form/' . 'node/1/title/en/full', 'application/vnd.drupal-ajax', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
|
||||
$this->assertIdentical('quickeditFieldForm', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldForm command.');
|
||||
$this->assertIdentical('<form ', Unicode::substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
|
||||
|
||||
// Prepare form values for submission. drupalPostAjaxForm() is not suitable
|
||||
// for handling pages with JSON responses, so we need our own solution
|
||||
// here.
|
||||
$form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
|
||||
$this->assertTrue($form_tokens_found, 'Form tokens found in output.');
|
||||
|
||||
if ($form_tokens_found) {
|
||||
$edit = array(
|
||||
'title[0][value]' => 'Obligatory question',
|
||||
'op' => t('Save'),
|
||||
);
|
||||
$post = array(
|
||||
'form_id' => 'quickedit_field_form',
|
||||
'form_token' => $token_match[1],
|
||||
'form_build_id' => $build_id_match[1],
|
||||
);
|
||||
$post += $edit + $this->getAjaxPageStatePostData();
|
||||
|
||||
// Submit field form and check response. This should store the
|
||||
// updated entity in PrivateTempStore on the server.
|
||||
$response = $this->drupalPost('quickedit/form/' . 'node/1/title/en/full', 'application/vnd.drupal-ajax', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
|
||||
$this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
|
||||
$this->assertTrue(strpos($ajax_commands[0]['data'], 'Obligatory question'), 'Form value saved and printed back.');
|
||||
|
||||
// Ensure the text on the original node did not change yet.
|
||||
$this->drupalGet('node/1');
|
||||
$this->assertNoText('Obligatory question');
|
||||
|
||||
// Save the entity by moving the PrivateTempStore values to entity storage.
|
||||
$post = array('nocssjs' => 'true');
|
||||
$response = $this->drupalPostWithFormat('quickedit/entity/' . 'node/1', 'json', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(1, count($ajax_commands), 'The entity submission HTTP request results in one AJAX command.');
|
||||
$this->assertIdentical('quickeditEntitySaved', $ajax_commands[0]['command'], 'The first AJAX command is n quickeditEntitySaved command.');
|
||||
$this->assertIdentical($ajax_commands[0]['data']['entity_type'], 'node', 'Saved entity is of type node.');
|
||||
$this->assertIdentical($ajax_commands[0]['data']['entity_id'], '1', 'Entity id is 1.');
|
||||
|
||||
// Ensure the text on the original node did change.
|
||||
$this->drupalGet('node/1');
|
||||
$this->assertText('Obligatory question');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Quick Edit doesn't make pseudo fields or computed fields
|
||||
* editable.
|
||||
*/
|
||||
public function testPseudoFields() {
|
||||
\Drupal::service('module_installer')->install(array('quickedit_test'));
|
||||
|
||||
$this->drupalLogin($this->authorUser);
|
||||
$this->drupalGet('node/1');
|
||||
|
||||
// Check that the data- attribute is not added.
|
||||
$this->assertNoRaw('data-quickedit-field-id="node/1/quickedit_test_pseudo_field/en/default"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Quick Edit doesn't make fields rendered with display options
|
||||
* editable.
|
||||
*/
|
||||
public function testDisplayOptions() {
|
||||
$node = Node::load('1');
|
||||
$display_settings = array(
|
||||
'label' => 'inline',
|
||||
);
|
||||
$build = $node->body->view($display_settings);
|
||||
$output = \Drupal::service('renderer')->renderRoot($build);
|
||||
$this->assertFalse(strpos($output, 'data-quickedit-field-id'), 'data-quickedit-field-id attribute not added when rendering field using dynamic display options.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Quick Edit works with custom render pipelines.
|
||||
*/
|
||||
public function testCustomPipeline() {
|
||||
\Drupal::service('module_installer')->install(array('quickedit_test'));
|
||||
|
||||
$custom_render_url = 'quickedit/form/node/1/body/en/quickedit_test-custom-render-data';
|
||||
$this->drupalLogin($this->editorUser);
|
||||
|
||||
// Request editing to render results with the custom render pipeline.
|
||||
$post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData();
|
||||
$response = $this->drupalPost($custom_render_url, 'application/vnd.drupal-ajax', $post);
|
||||
$ajax_commands = Json::decode($response);
|
||||
|
||||
// Prepare form values for submission. drupalPostAJAX() is not suitable for
|
||||
// handling pages with JSON responses, so we need our own solution here.
|
||||
$form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
|
||||
$this->assertTrue($form_tokens_found, 'Form tokens found in output.');
|
||||
|
||||
if ($form_tokens_found) {
|
||||
$post = array(
|
||||
'form_id' => 'quickedit_field_form',
|
||||
'form_token' => $token_match[1],
|
||||
'form_build_id' => $build_id_match[1],
|
||||
'body[0][summary]' => '',
|
||||
'body[0][value]' => '<p>Fine thanks.</p>',
|
||||
'body[0][format]' => 'filtered_html',
|
||||
'op' => t('Save'),
|
||||
);
|
||||
// Assume there is another field on this page, which doesn't use a custom
|
||||
// render pipeline, but the default one, and it uses the "full" view mode.
|
||||
$post += array('other_view_modes[]' => 'full');
|
||||
|
||||
// Submit field form and check response. Should render with the custom
|
||||
// render pipeline.
|
||||
$response = $this->drupalPost($custom_render_url, 'application/vnd.drupal-ajax', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(1, count($ajax_commands), 'The field form HTTP request results in one AJAX command.');
|
||||
$this->assertIdentical('quickeditFieldFormSaved', $ajax_commands[0]['command'], 'The first AJAX command is a quickeditFieldFormSaved command.');
|
||||
$this->assertTrue(strpos($ajax_commands[0]['data'], 'Fine thanks.'), 'Form value saved and printed back.');
|
||||
$this->assertTrue(strpos($ajax_commands[0]['data'], '<div class="quickedit-test-wrapper">') !== FALSE, 'Custom render pipeline used to render the value.');
|
||||
$this->assertIdentical(array_keys($ajax_commands[0]['other_view_modes']), array('full'), 'Field was also rendered in the "full" view mode.');
|
||||
$this->assertTrue(strpos($ajax_commands[0]['other_view_modes']['full'], 'Fine thanks.'), '"full" version of field contains the form value.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests Quick Edit on a node that was concurrently edited on the full node
|
||||
* form.
|
||||
*/
|
||||
public function testConcurrentEdit() {
|
||||
$this->drupalLogin($this->editorUser);
|
||||
|
||||
$post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData();
|
||||
$response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', 'application/vnd.drupal-ajax', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
|
||||
// Prepare form values for submission. drupalPostAJAX() is not suitable for
|
||||
// handling pages with JSON responses, so we need our own solution here.
|
||||
$form_tokens_found = preg_match('/\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match);
|
||||
$this->assertTrue($form_tokens_found, 'Form tokens found in output.');
|
||||
|
||||
if ($form_tokens_found) {
|
||||
$post = array(
|
||||
'nocssjs' => 'true',
|
||||
'form_id' => 'quickedit_field_form',
|
||||
'form_token' => $token_match[1],
|
||||
'form_build_id' => $build_id_match[1],
|
||||
'body[0][summary]' => '',
|
||||
'body[0][value]' => '<p>Fine thanks.</p>',
|
||||
'body[0][format]' => 'filtered_html',
|
||||
'op' => t('Save'),
|
||||
);
|
||||
|
||||
// Save the node on the regular node edit form.
|
||||
$this->drupalPostForm('node/1/edit', array(), t('Save'));
|
||||
// Ensure different save timestamps for field editing.
|
||||
sleep(2);
|
||||
|
||||
// Submit field form and check response. Should throw a validation error
|
||||
// because the node was changed in the meantime.
|
||||
$response = $this->drupalPost('quickedit/form/' . 'node/1/body/en/full', 'application/vnd.drupal-ajax', $post);
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical(2, count($ajax_commands), 'The field form HTTP request results in two AJAX commands.');
|
||||
$this->assertIdentical('quickeditFieldFormValidationErrors', $ajax_commands[1]['command'], 'The second AJAX command is a quickeditFieldFormValidationErrors command.');
|
||||
$this->assertTrue(strpos($ajax_commands[1]['data'], t('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.')), 'Error message returned to user.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Quick Edit's data- attributes are present for content blocks.
|
||||
*/
|
||||
public function testContentBlock() {
|
||||
\Drupal::service('module_installer')->install(array('block_content'));
|
||||
|
||||
// Create and place a content_block block.
|
||||
$block = BlockContent::create([
|
||||
'info' => $this->randomMachineName(),
|
||||
'type' => 'basic',
|
||||
'langcode' => 'en',
|
||||
]);
|
||||
$block->save();
|
||||
$this->drupalPlaceBlock('block_content:' . $block->uuid());
|
||||
|
||||
// Check that the data- attribute is present.
|
||||
$this->drupalGet('');
|
||||
$this->assertRaw('data-quickedit-entity-id="block_content/1"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Quick Edit can handle an image field.
|
||||
*/
|
||||
public function testImageField() {
|
||||
// Add an image field to the content type.
|
||||
FieldStorageConfig::create([
|
||||
'field_name' => 'field_image',
|
||||
'type' => 'image',
|
||||
'entity_type' => 'node',
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'field_name' => 'field_image',
|
||||
'field_type' => 'image',
|
||||
'label' => t('Image'),
|
||||
'entity_type' => 'node',
|
||||
'bundle' => 'article',
|
||||
])->save();
|
||||
entity_get_form_display('node', 'article', 'default')
|
||||
->setComponent('field_image', [
|
||||
'type' => 'image_image',
|
||||
])
|
||||
->save();
|
||||
|
||||
// Add an image to the node.
|
||||
$this->drupalLogin($this->editorUser);
|
||||
$image = $this->drupalGetTestFiles('image')[0];
|
||||
$this->drupalPostForm('node/1/edit', [
|
||||
'files[field_image_0]' => $image->uri,
|
||||
], t('Upload'));
|
||||
$this->drupalPostForm(NULL, [
|
||||
'field_image[0][alt]' => 'Vivamus aliquet elit',
|
||||
], t('Save'));
|
||||
|
||||
// The image field form should load normally.
|
||||
$response = $this->drupalPost('quickedit/form/node/1/field_image/en/full', 'application/vnd.drupal-ajax', ['nocssjs' => 'true'] + $this->getAjaxPageStatePostData());
|
||||
$this->assertResponse(200);
|
||||
$ajax_commands = Json::decode($response);
|
||||
$this->assertIdentical('<form ', Unicode::substr($ajax_commands[0]['data'], 0, 6), 'The quickeditFieldForm command contains a form.');
|
||||
}
|
||||
}
|
108
core/modules/quickedit/src/Tests/QuickEditTestBase.php
Normal file
108
core/modules/quickedit/src/Tests/QuickEditTestBase.php
Normal file
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\quickedit\Tests\QuickEditTestBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\quickedit\Tests;
|
||||
|
||||
use Drupal\simpletest\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Base class for testing Quick Edit functionality.
|
||||
*/
|
||||
abstract class QuickEditTestBase extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('system', 'entity_test', 'field', 'field_test', 'filter', 'user', 'text', 'quickedit', 'entity_reference');
|
||||
|
||||
/**
|
||||
* Bag of created fields.
|
||||
*
|
||||
* Allows easy access to test field names/IDs/objects via:
|
||||
* - $this->fields->{$field_name}_field_storage
|
||||
* - $this->fields->{$field_name}_instance
|
||||
*
|
||||
* @see \Drupal\quickedit\Tests\QuickEditTestBase::createFieldWithStorage()
|
||||
*
|
||||
* @var \ArrayObject
|
||||
*/
|
||||
protected $fields;
|
||||
|
||||
/**
|
||||
* Sets the default field storage backend for fields created during tests.
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->fields = new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS);
|
||||
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('entity_test');
|
||||
$this->installConfig(array('field', 'filter'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a field.
|
||||
*
|
||||
* @param string $field_name
|
||||
* The field name.
|
||||
* @param string $type
|
||||
* The field type.
|
||||
* @param int $cardinality
|
||||
* The field's cardinality.
|
||||
* @param string $label
|
||||
* The field's label (used everywhere: widget label, formatter label).
|
||||
* @param array $field_settings
|
||||
* @param string $widget_type
|
||||
* The widget type.
|
||||
* @param array $widget_settings
|
||||
* The widget settings.
|
||||
* @param string $formatter_type
|
||||
* The formatter type.
|
||||
* @param array $formatter_settings
|
||||
* The formatter settings.
|
||||
*/
|
||||
protected function createFieldWithStorage($field_name, $type, $cardinality, $label, $field_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) {
|
||||
$field_storage = $field_name . '_field_storage';
|
||||
$this->fields->$field_storage = entity_create('field_storage_config', array(
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => $type,
|
||||
'cardinality' => $cardinality,
|
||||
));
|
||||
$this->fields->$field_storage->save();
|
||||
|
||||
$field = $field_name . '_field';
|
||||
$this->fields->$field = entity_create('field_config', array(
|
||||
'field_storage' => $this->fields->$field_storage,
|
||||
'bundle' => 'entity_test',
|
||||
'label' => $label,
|
||||
'description' => $label,
|
||||
'weight' => mt_rand(0, 127),
|
||||
'settings' => $field_settings,
|
||||
));
|
||||
$this->fields->$field->save();
|
||||
|
||||
entity_get_form_display('entity_test', 'entity_test', 'default')
|
||||
->setComponent($field_name, array(
|
||||
'type' => $widget_type,
|
||||
'settings' => $widget_settings,
|
||||
))
|
||||
->save();
|
||||
|
||||
entity_get_display('entity_test', 'entity_test', 'default')
|
||||
->setComponent($field_name, array(
|
||||
'label' => 'above',
|
||||
'type' => $formatter_type,
|
||||
'settings' => $formatter_settings
|
||||
))
|
||||
->save();
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue