View source
<?php
namespace Drupal\quickedit\Tests;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
use Drupal\simpletest\WebTestBase;
class QuickEditAutocompleteTermTest extends WebTestBase {
use EntityReferenceTestTrait;
public static $modules = array(
'node',
'taxonomy',
'quickedit',
);
protected $node;
protected $vocabulary;
protected $term1;
protected $term2;
protected $fieldName;
protected $editorUser;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'article',
));
$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',
]);
}
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);
$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'),
);
$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');
$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);
$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));
$post = array(
'nocssjs' => 'true',
);
$response = $this
->drupalPostWithFormat('quickedit/entity/node/' . $this->node
->id(), 'json', $post);
$this
->assertResponse(200);
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertLink($this->term1
->getName());
$this
->assertLink($this->term2
->getName());
$this
->assertLink('new 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(),
'format' => $format
->id(),
'vid' => $this->vocabulary
->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$term
->save();
return $term;
}
}