View source
<?php
namespace Drupal\Tests\unique_field_ajax\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
class UniqueFieldAjaxBase extends BrowserTestBase {
protected static $modules = [
'node',
'language',
'language_test',
'field_ui',
'link',
'unique_field_ajax',
];
protected $defaultTheme = 'stark';
protected $contentTypeName = 'node_unique_field_ajax';
protected $contentType;
protected $fieldStorage;
protected $field;
protected $fieldTypes = [
'link' => [
'type' => 'link',
'widget' => 'email_default',
'value' => 'link',
'effect' => 'uri',
'settings' => [],
],
'string' => [
'type' => 'string',
'widget' => 'string_textfield',
'value' => 'string',
'effect' => 'value',
'settings' => [],
],
'email' => [
'type' => 'email',
'widget' => 'email_default',
'value' => 'email',
'effect' => 'value',
'settings' => [],
],
'integer' => [
'type' => 'integer',
'widget' => 'number',
'value' => 'integer',
'effect' => 'value',
'settings' => [],
],
'decimal' => [
'type' => 'decimal',
'widget' => 'number',
'value' => 'decimal',
'effect' => 'value',
'settings' => [],
],
];
protected $translationOptions = [
'es' => 'spanish',
'fr' => 'french',
'de' => 'german',
];
protected function setUp() {
parent::setUp();
$user = $this
->drupalCreateUser([], NULL, TRUE);
$this
->drupalLogin($user);
$this
->createCustomContentType();
}
protected function createCustomContentType() {
$this->contentType = $this
->drupalCreateContentType([
'type' => $this->contentTypeName,
]);
}
protected function createField(string $fieldType, string $widgetType, array $fieldConfigSettings = []) {
$field_name = $this
->createRandomData();
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => $fieldType,
]);
$this->fieldStorage
->save();
$field_config = [
'field_storage' => $this->fieldStorage,
'bundle' => $this->contentTypeName,
'label' => $field_name . '_label',
];
if (!empty($fieldConfigSettings)) {
$field_config['settings'] = $fieldConfigSettings;
}
$this->field = FieldConfig::create($field_config);
$this->field
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', $this->contentTypeName)
->setComponent($field_name, [
'type' => $widgetType,
])
->save();
$display_repository
->getViewDisplay('node', $this->contentTypeName, 'full')
->setComponent($field_name)
->save();
}
protected function itCanSaveField(array $edit, int $nid = NULL, bool $debug = FALSE) : int {
$title = $edit['title[0][value]'];
$method = $this
->getSaveMethod($nid);
$this
->drupalPostForm($method, $edit, t('Save'));
preg_match('|node/(\\d+)|', $this
->getUrl(), $match);
if (!empty($match)) {
$id = $match[1];
if (!$nid) {
$this
->assertSession()
->pageTextContains(t('@contentType @title has been created.', [
'@title' => $title,
'@contentType' => $this->contentTypeName,
]));
}
else {
if ($debug) {
var_dump($this
->getSession()
->getPage()
->getHtml());
}
$this
->assertSession()
->pageTextContains(t('@contentType @title has been updated.', [
'@title' => $title,
'@contentType' => $this->contentTypeName,
]));
}
return (int) $id;
}
else {
var_dump($this
->getUrl());
var_dump($this
->getSession()
->getPage()
->getHtml());
static::fail(t('Could not extract entity id from url'));
}
return -1;
}
protected function itCanUpdateField(array $edit, int $nid, bool $debug = FALSE) : int {
return $this
->itCanSaveField($edit, $nid, $debug);
}
protected function itCannotSaveField(array $edit, string $customMsg = NULL, string $nid = NULL, string $label = NULL) {
$method = $this
->getSaveMethod($nid);
$label_name = $label ?: $this->field
->label();
$this
->drupalPostForm($method, $edit, t('Save'));
if ($customMsg) {
$message = $customMsg;
}
else {
$message = t('The field "@field" has to be unique.', [
'@field' => $label_name,
]);
}
$this
->assertSession()
->pageTextContains($message);
}
protected function itCannotEditField(array $edit, string $nid, string $customMsg = NULL) {
$this
->itCannotSaveField($edit, $customMsg, $nid);
}
protected function getSaveMethod(string $id = NULL) : string {
return !$id ? 'node/add/' . $this->contentTypeName : 'node/' . $id . '/edit';
}
protected function updateThirdPartyFieldSetting(string $key, string $value) {
$this->field
->setThirdPartySetting('unique_field_ajax', $key, $value);
$this->field
->save();
}
protected function updateThirdPartyEntitySetting(string $key, string $value) {
$this->contentType
->setThirdPartySetting('unique_field_ajax', $key, $value);
$this->contentType
->save();
}
protected function createBasicUpdateData(string $title = NULL, string $body = NULL) : array {
$return = [];
$return['title[0][value]'] = $title ?? $this
->randomMachineName();
$return['body[0][value]'] = $body ?? $this
->randomMachineName();
return $return;
}
protected function createUpdateFieldData(string $fieldName, string $value, string $effect, string $language = NULL, string $title = NULL, string $body = NULL) : array {
$return = $this
->createBasicUpdateData($title, $body);
$return["{$fieldName}[0][{$effect}]"] = $this
->createRandomData($value);
if ($language) {
$return['langcode[0][value]'] = $language;
}
return $return;
}
protected function createRandomData(string $type = 'string') {
$return = '';
switch ($type) {
case 'string':
$return = mb_strtolower($this
->randomMachineName());
break;
case 'sentence':
$length = 200;
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$return = substr(str_shuffle(str_repeat($chars, ceil($length / strlen($chars)))), 1, $length);
$return = wordwrap($return, rand(3, 10), ' ', TRUE);
break;
case 'link':
$return = 'https://www.' . $this
->createRandomData() . '.com/';
break;
case 'email':
$return = $this
->createRandomData() . '@' . $this
->createRandomData() . '.com';
break;
case 'integer':
try {
$return = random_int(0, 9999);
} catch (\Exception $e) {
die('Could not generate random int');
}
break;
case 'decimal':
$min = 0;
$max = 9999;
$decimals = 2;
$scale = pow(10, $decimals);
$return = mt_rand($min * $scale, $max * $scale) / $scale;
break;
}
return $return;
}
}