View source
<?php
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;
class QuickEditLoadingTest extends WebTestBase {
public static $modules = array(
'contextual',
'quickedit',
'filter',
'node',
'image',
);
protected $authorUser;
protected $editorUser;
protected function setUp() {
parent::setUp();
$filtered_html_format = entity_create('filter_format', array(
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => array(),
));
$filtered_html_format
->save();
$this
->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
$this
->drupalCreateNode(array(
'type' => 'article',
'body' => array(
0 => array(
'value' => '<p>How are you?</p>',
'format' => 'filtered_html',
),
),
'revision_log' => $this
->randomString(),
));
$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',
)));
}
public function testUserWithoutPermission() {
$this
->drupalLogin($this->authorUser);
$this
->drupalGet('node/1');
$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.");
$this
->assertRaw('data-quickedit-entity-id="node/1"');
$this
->assertRaw('data-quickedit-field-id="node/1/body/en/full"');
$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);
$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);
}
public function testUserWithPermission() {
$this
->drupalLogin($this->editorUser);
$this
->drupalGet('node/1');
$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.");
$this
->assertRaw('data-quickedit-entity-id="node/1"');
$this
->assertRaw('data-quickedit-field-id="node/1/body/en/full"');
$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;
$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',
),
);
$this
->assertIdentical(Json::decode($response), $expected, 'The metadata HTTP request answers with the correct JSON response.');
$this->drupalSettings = $htmlPageDrupalSettings;
$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.');
$this
->assertIdentical('settings', $ajax_commands[0]['command'], 'The first AJAX command is a settings command.');
$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.');
$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.');
$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();
$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.');
$this
->drupalGet('node/1');
$this
->assertText('How are you?');
$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.');
$this
->drupalGet('node/1');
$this
->assertText('Fine thanks.');
$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.');
$node_type = NodeType::load('article');
$node_type
->setNewRevision(TRUE);
$node_type
->save();
$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.');
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.');
$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');
$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.');
}
}
public function testTitleBaseField() {
$this
->drupalLogin($this->editorUser);
$this
->drupalGet('node/1');
$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.');
$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',
),
);
$this
->assertIdentical(Json::decode($response), $expected, 'The metadata HTTP request answers with the correct JSON response.');
$this->drupalSettings = $htmlPageDrupalSettings;
$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.');
$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();
$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.');
$this
->drupalGet('node/1');
$this
->assertNoText('Obligatory question');
$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.');
$this
->drupalGet('node/1');
$this
->assertText('Obligatory question');
}
}
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.');
}
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);
$post = array(
'nocssjs' => 'true',
) + $this
->getAjaxPageStatePostData();
$response = $this
->drupalPost($custom_render_url, 'application/vnd.drupal-ajax', $post);
$ajax_commands = Json::decode($response);
$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'),
);
$post += array(
'other_view_modes[]' => 'full',
);
$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.');
}
}
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);
$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'),
);
$this
->drupalPostForm('node/1/edit', array(), t('Save'));
sleep(2);
$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'], '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.');
}
}
public function testContentBlock() {
\Drupal::service('module_installer')
->install(array(
'block_content',
));
$block = BlockContent::create([
'info' => $this
->randomMachineName(),
'type' => 'basic',
'langcode' => 'en',
]);
$block
->save();
$this
->drupalPlaceBlock('block_content:' . $block
->uuid());
$this
->drupalGet('');
$this
->assertRaw('data-quickedit-entity-id="block_content/1"');
}
public function testImageField() {
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();
$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'));
$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.');
}
}