Skip to content
Snippets Groups Projects
Commit f95cc89f authored by Angie Byron's avatar Angie Byron
Browse files

Issue #2605254 by mikeryan, phenaproxima: LinkItem::generateSamplevalue() does...

Issue #2605254 by mikeryan, phenaproxima: LinkItem::generateSamplevalue() does not respect link_type setting
parent 5fff18f0
No related branches found
No related tags found
No related merge requests found
......@@ -121,25 +121,30 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
// Set of possible top-level domains.
$tlds = array('com', 'net', 'gov', 'org', 'edu', 'biz', 'info');
// Set random length for the domain name.
$domain_length = mt_rand(7, 15);
$random = new Random();
switch ($field_definition->getSetting('title')) {
case DRUPAL_DISABLED:
$values['title'] = '';
break;
case DRUPAL_REQUIRED:
$values['title'] = $random->sentences(4);
break;
case DRUPAL_OPTIONAL:
// In case of optional title, randomize its generation.
$values['title'] = mt_rand(0,1) ? $random->sentences(4) : '';
break;
if ($field_definition->getItemDefinition()->getSetting('link_type') & LinkItemInterface::LINK_EXTERNAL) {
// Set of possible top-level domains.
$tlds = array('com', 'net', 'gov', 'org', 'edu', 'biz', 'info');
// Set random length for the domain name.
$domain_length = mt_rand(7, 15);
switch ($field_definition->getSetting('title')) {
case DRUPAL_DISABLED:
$values['title'] = '';
break;
case DRUPAL_REQUIRED:
$values['title'] = $random->sentences(4);
break;
case DRUPAL_OPTIONAL:
// In case of optional title, randomize its generation.
$values['title'] = mt_rand(0, 1) ? $random->sentences(4) : '';
break;
}
$values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (sizeof($tlds) - 1))];
}
else {
$values['uri'] = 'base:' . $random->name(mt_rand(1, 64));
}
$values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (sizeof($tlds)-1))];
return $values;
}
......
......@@ -10,7 +10,10 @@
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Tests\FieldUnitTestBase;
use Drupal\link\LinkItemInterface;
/**
* Tests the new entity API for the link field type.
......@@ -29,17 +32,40 @@ class LinkItemTest extends FieldUnitTestBase {
protected function setUp() {
parent::setUp();
// Create a link field for validation.
entity_create('field_storage_config', array(
// Create a generic, external, and internal link fields for validation.
FieldStorageConfig::create([
'field_name' => 'field_test',
'entity_type' => 'entity_test',
'type' => 'link',
))->save();
entity_create('field_config', array(
])->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
))->save();
'settings' => ['link_type' => LinkItemInterface::LINK_GENERIC],
])->save();
FieldStorageConfig::create([
'field_name' => 'field_test_external',
'entity_type' => 'entity_test',
'type' => 'link',
])->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_test_external',
'bundle' => 'entity_test',
'settings' => ['link_type' => LinkItemInterface::LINK_EXTERNAL],
])->save();
FieldStorageConfig::create([
'field_name' => 'field_test_internal',
'entity_type' => 'entity_test',
'type' => 'link',
])->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_test_internal',
'bundle' => 'entity_test',
'settings' => ['link_type' => LinkItemInterface::LINK_INTERNAL],
])->save();
}
/**
......@@ -130,9 +156,12 @@ public function testLinkItem() {
$entity->field_test[0] = NULL;
$this->assertNull($entity->field_test[0]->getValue());
// Test the generateSampleValue() method.
// Test the generateSampleValue() method for generic, external, and internal
// link types.
$entity = entity_create('entity_test');
$entity->field_test->generateSampleItems();
$entity->field_test_external->generateSampleItems();
$entity->field_test_internal->generateSampleItems();
$this->entityValidateAndSave($entity);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment