View source
<?php
namespace Drupal\system\Tests\Entity\Element;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
use Drupal\user\Entity\User;
class EntityAutocompleteElementFormTest extends EntityUnitTestBase implements FormInterface {
protected $testUser;
protected $testAutocreateUser;
protected $referencedEntities;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', [
'router',
'key_value_expire',
]);
\Drupal::service('router.builder')
->rebuild();
$this->testUser = User::create(array(
'name' => 'foobar1',
'mail' => 'foobar1@example.com',
));
$this->testUser
->save();
\Drupal::service('current_user')
->setAccount($this->testUser);
$this->testAutocreateUser = User::create(array(
'name' => 'foobar2',
'mail' => 'foobar2@example.com',
));
$this->testAutocreateUser
->save();
for ($i = 1; $i < 3; $i++) {
$entity = EntityTest::create(array(
'name' => $this
->randomMachineName(),
));
$entity
->save();
$this->referencedEntities[] = $entity;
}
}
public function getFormId() {
return 'test_entity_autocomplete';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['single'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
);
$form['single_autocreate'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#autocreate' => array(
'bundle' => 'entity_test',
),
);
$form['single_autocreate_specific_uid'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#autocreate' => array(
'bundle' => 'entity_test',
'uid' => $this->testAutocreateUser
->id(),
),
);
$form['tags'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#tags' => TRUE,
);
$form['tags_autocreate'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#tags' => TRUE,
'#autocreate' => array(
'bundle' => 'entity_test',
),
);
$form['tags_autocreate_specific_uid'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#tags' => TRUE,
'#autocreate' => array(
'bundle' => 'entity_test',
'uid' => $this->testAutocreateUser
->id(),
),
);
$form['single_no_validate'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#validate_reference' => FALSE,
);
$form['single_autocreate_no_validate'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#validate_reference' => FALSE,
'#autocreate' => array(
'bundle' => 'entity_test',
),
);
$form['single_access'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#default_value' => $this->referencedEntities[0],
);
$form['tags_access'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
'#tags' => TRUE,
'#default_value' => array(
$this->referencedEntities[0],
$this->referencedEntities[1],
),
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function testValidEntityAutocompleteElement() {
$form_state = (new FormState())
->setValues([
'single' => $this
->getAutocompleteInput($this->referencedEntities[0]),
'single_autocreate' => 'single - autocreated entity label',
'single_autocreate_specific_uid' => 'single - autocreated entity label with specific uid',
'tags' => $this
->getAutocompleteInput($this->referencedEntities[0]) . ', ' . $this
->getAutocompleteInput($this->referencedEntities[1]),
'tags_autocreate' => $this
->getAutocompleteInput($this->referencedEntities[0]) . ', tags - autocreated entity label, ' . $this
->getAutocompleteInput($this->referencedEntities[1]),
'tags_autocreate_specific_uid' => $this
->getAutocompleteInput($this->referencedEntities[0]) . ', tags - autocreated entity label with specific uid, ' . $this
->getAutocompleteInput($this->referencedEntities[1]),
]);
$form_builder = $this->container
->get('form_builder');
$form_builder
->submitForm($this, $form_state);
$this
->assertEqual(count($form_state
->getErrors()), 0);
$this
->assertEqual($form_state
->getValue('single'), $this->referencedEntities[0]
->id());
$value = $form_state
->getValue('single_autocreate');
$this
->assertEqual($value['entity']
->label(), 'single - autocreated entity label');
$this
->assertEqual($value['entity']
->bundle(), 'entity_test');
$this
->assertEqual($value['entity']
->getOwnerId(), $this->testUser
->id());
$value = $form_state
->getValue('single_autocreate_specific_uid');
$this
->assertEqual($value['entity']
->label(), 'single - autocreated entity label with specific uid');
$this
->assertEqual($value['entity']
->bundle(), 'entity_test');
$this
->assertEqual($value['entity']
->getOwnerId(), $this->testAutocreateUser
->id());
$expected = array(
array(
'target_id' => $this->referencedEntities[0]
->id(),
),
array(
'target_id' => $this->referencedEntities[1]
->id(),
),
);
$this
->assertEqual($form_state
->getValue('tags'), $expected);
$value = $form_state
->getValue('tags_autocreate');
$this
->assertEqual($value[0]['target_id'], $this->referencedEntities[0]
->id());
$this
->assertTrue(!isset($value[1]['target_id']));
$this
->assertEqual($value[1]['entity']
->label(), 'tags - autocreated entity label');
$this
->assertEqual($value[1]['entity']
->getOwnerId(), $this->testUser
->id());
$this
->assertEqual($value[2]['target_id'], $this->referencedEntities[1]
->id());
$value = $form_state
->getValue('tags_autocreate_specific_uid');
$this
->assertEqual($value[0]['target_id'], $this->referencedEntities[0]
->id());
$this
->assertTrue(!isset($value[1]['target_id']));
$this
->assertEqual($value[1]['entity']
->label(), 'tags - autocreated entity label with specific uid');
$this
->assertEqual($value[1]['entity']
->getOwnerId(), $this->testAutocreateUser
->id());
$this
->assertEqual($value[2]['target_id'], $this->referencedEntities[1]
->id());
}
public function testInvalidEntityAutocompleteElement() {
$form_builder = $this->container
->get('form_builder');
$form_state = (new FormState())
->setValues([
'single' => 'single - non-existent label',
]);
$form_builder
->submitForm($this, $form_state);
$this
->assertEqual(count($form_state
->getErrors()), 1);
$this
->assertEqual($form_state
->getErrors()['single'], t('There are no entities matching "%value".', array(
'%value' => 'single - non-existent label',
)));
$form_state = (new FormState())
->setValues([
'single' => 'single - non-existent label (42)',
]);
$form_builder
->submitForm($this, $form_state);
$this
->assertEqual(count($form_state
->getErrors()), 1);
$this
->assertEqual($form_state
->getErrors()['single'], t('The referenced entity (%type: %id) does not exist.', array(
'%type' => 'entity_test',
'%id' => 42,
)));
$form_state = (new FormState())
->setValues([
'single_no_validate' => 'single - non-existent label',
'single_autocreate_no_validate' => 'single - autocreate non-existent label',
]);
$form_builder
->submitForm($this, $form_state);
$this
->assertEqual(count($form_state
->getErrors()), 1);
$this
->assertEqual($form_state
->getErrors()['single_no_validate'], t('There are no entities matching "%value".', array(
'%value' => 'single - non-existent label',
)));
$form_state = (new FormState())
->setValues([
'single_no_validate' => 'single - non-existent label (42)',
'single_autocreate_no_validate' => 'single - autocreate non-existent label (43)',
]);
$form_builder
->submitForm($this, $form_state);
$this
->assertEqual(count($form_state
->getErrors()), 0);
}
public function testEntityAutocompleteAccess() {
$form_builder = $this->container
->get('form_builder');
$form = $form_builder
->getForm($this);
$expected = $this->referencedEntities[0]
->label() . ' (' . $this->referencedEntities[0]
->id() . ')';
$this
->assertEqual($form['single_access']['#value'], $expected);
$expected .= ', ' . $this->referencedEntities[1]
->label() . ' (' . $this->referencedEntities[1]
->id() . ')';
$this
->assertEqual($form['tags_access']['#value'], $expected);
\Drupal::currentUser()
->setAccount($this
->createUser(array(), array()));
$form = $form_builder
->getForm($this);
$expected = t('- Restricted access -') . ' (' . $this->referencedEntities[0]
->id() . ')';
$this
->assertEqual($form['single_access']['#value'], $expected);
$expected .= ', ' . t('- Restricted access -') . ' (' . $this->referencedEntities[1]
->id() . ')';
$this
->assertEqual($form['tags_access']['#value'], $expected);
}
protected function getAutocompleteInput(EntityInterface $entity) {
return EntityAutocomplete::getEntityLabels(array(
$entity,
));
}
}