View source
<?php
namespace Drupal\Tests\unique_field_ajax\Functional;
class UniqueFieldAjaxTest extends UniqueFieldAjaxBase {
public function testUniqueFieldNotEnabledNoIssues() {
foreach ($this->fieldTypes as $field_type) {
$this
->createField($field_type['type'], $field_type['widget'], $field_type['settings']);
$field_name = $this->fieldStorage
->getName();
$this
->updateThirdPartyFieldSetting('unique', FALSE);
$edit = $this
->createUpdateFieldData($field_name, $field_type['value'], $field_type['effect']);
$this
->itCanSaveField($edit);
$this
->itCanSaveField($edit);
$this
->itCanSaveField($edit);
$this
->itCanSaveField($edit);
}
}
public function testUniqueFieldEnabledRequiredUniqueness() {
foreach ($this->fieldTypes as $field_type) {
$this
->createField($field_type['type'], $field_type['widget'], $field_type['settings']);
$field_name = $this->fieldStorage
->getName();
$this
->updateThirdPartyFieldSetting('unique', TRUE);
$edit = $this
->createUpdateFieldData($field_name, $field_type['value'], $field_type['effect']);
$this
->itCanSaveField($edit);
$this
->itCannotSaveField($edit);
}
}
public function testUniqueFieldAllowsSavingSameField() {
foreach ($this->fieldTypes as $field_type) {
$this
->createField($field_type['type'], $field_type['widget'], $field_type['settings']);
$field_name = $this->fieldStorage
->getName();
$this
->updateThirdPartyFieldSetting('unique', TRUE);
$edit = $this
->createUpdateFieldData($field_name, $field_type['value'], $field_type['effect']);
$id = $this
->itCanSaveField($edit);
$this
->itCanUpdateField($edit, $id);
$this
->itCanUpdateField($edit, $id);
}
}
public function testUniqueTitleNotEnabledNoIssues() {
$this
->updateThirdPartyEntitySetting('unique', FALSE);
$edit = $this
->createBasicUpdateData();
$this
->itCanSaveField($edit);
$this
->itCanSaveField($edit);
$this
->itCanSaveField($edit);
$this
->itCanSaveField($edit);
}
public function testUniqueTitleEnabledRequiredUniqueness() {
$this
->updateThirdPartyEntitySetting('unique', TRUE);
$edit = $this
->createBasicUpdateData();
$this
->itCanSaveField($edit);
$this
->itCannotSaveField($edit, NULL, NULL, 'title');
}
public function testUniqueTitleAllowsSavingSameField() {
$this
->updateThirdPartyEntitySetting('unique', TRUE);
$edit = $this
->createBasicUpdateData();
$id = $this
->itCanSaveField($edit);
$this
->itCanUpdateField($edit, $id);
$this
->itCanUpdateField($edit, $id);
}
public function testUniqueFieldCustomMessage() {
foreach ($this->fieldTypes as $field_type) {
$this
->createField($field_type['type'], $field_type['widget'], $field_type['settings']);
$field_name = $this->fieldStorage
->getName();
$msg = $this
->createRandomData('sentence');
$this
->updateThirdPartyFieldSetting('unique', TRUE);
$this
->updateThirdPartyFieldSetting('message', $msg);
$edit = $this
->createUpdateFieldData($field_name, $field_type['value'], $field_type['effect']);
$this
->itCanSaveField($edit);
$this
->itCannotSaveField($edit, $msg);
$this
->updateThirdPartyFieldSetting('unique', TRUE);
$this
->updateThirdPartyFieldSetting('message', '');
$edit = $this
->createUpdateFieldData($field_name, $field_type['value'], $field_type['effect']);
$this
->itCanSaveField($edit);
$this
->itCannotSaveField($edit);
}
}
public function testUniqueFieldCustomMessageWithLabelToken() {
foreach ($this->fieldTypes as $field_type) {
$this
->createField($field_type['type'], $field_type['widget'], $field_type['settings']);
$field_name = $this->fieldStorage
->getName();
$msg = $this
->createRandomData('sentence');
$msg .= " Here is my field label: %label ";
$msg .= $this
->createRandomData('sentence');
$this
->updateThirdPartyFieldSetting('unique', TRUE);
$this
->updateThirdPartyFieldSetting('message', $msg);
$edit = $this
->createUpdateFieldData($field_name, $field_type['value'], $field_type['effect']);
$this
->itCanSaveField($edit);
$msg = str_replace('%label', $field_name . "_label", $msg);
$this
->itCannotSaveField($edit, $msg);
}
}
public function testUniqueFieldCustomMessageWithLinkToken() {
$num = 5;
foreach ($this->fieldTypes as $field_type) {
$this
->createField($field_type['type'], $field_type['widget'], $field_type['settings']);
$field_name = $this->fieldStorage
->getName();
$msg = $this
->createRandomData('sentence');
$msg .= " Here is my field link: %link";
$this
->updateThirdPartyFieldSetting('unique', TRUE);
$this
->updateThirdPartyFieldSetting('message', $msg);
$edits = [];
for ($i = 0; $i < $num; $i++) {
$edits[$i] = $this
->createUpdateFieldData($field_name, $field_type['value'], $field_type['effect']);
$this
->itCanSaveField($edits[$i]);
}
$edit = $edits[array_rand($edits)];
$msg = str_replace('%link', $edit['title[0][value]'], $msg);
$this
->itCannotSaveField($edit, $msg);
}
}
public function testUniqueTitleCustomMessage() {
$msg = $this
->createRandomData('sentence');
$this
->updateThirdPartyEntitySetting('unique', TRUE);
$this
->updateThirdPartyEntitySetting('message', $msg);
$edit = $this
->createBasicUpdateData();
$this
->itCanSaveField($edit);
$this
->itCannotSaveField($edit, $msg, NULL, 'title');
$this
->updateThirdPartyEntitySetting('unique', TRUE);
$this
->updateThirdPartyEntitySetting('message', '');
$edit = $this
->createBasicUpdateData();
$this
->itCanSaveField($edit);
$this
->itCannotSaveField($edit, NULL, NULL, 'title');
}
public function testUniqueTitleCustomMessageWithLabelToken() {
$msg = $this
->createRandomData('sentence');
$msg .= " Here is my title label: %label ";
$msg .= $this
->createRandomData('sentence');
$this
->updateThirdPartyEntitySetting('unique', TRUE);
$this
->updateThirdPartyEntitySetting('message', $msg);
$edit = $this
->createBasicUpdateData();
$this
->itCanSaveField($edit);
$msg = str_replace('%label', "title", $msg);
$this
->itCannotSaveField($edit, $msg, NULL, 'title');
}
public function testUniqueTitleCustomMessageWithLinkToken() {
$num = 5;
$msg = $this
->createRandomData('sentence');
$msg .= " Here is my title link: %link";
$this
->updateThirdPartyEntitySetting('unique', TRUE);
$this
->updateThirdPartyEntitySetting('message', $msg);
$edits = [];
for ($i = 0; $i < $num; $i++) {
$edits[$i] = $this
->createBasicUpdateData();
$this
->itCanSaveField($edits[$i]);
}
$edit = $edits[array_rand($edits)];
$msg = str_replace('%link', $edit['title[0][value]'], $msg);
$this
->itCannotSaveField($edit, $msg, NULL, 'title');
}
}