EntityReferenceIntegrationTest.php 7.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?php

/**
 * @file
 * Contains \Drupal\entity_reference\Tests\EntityReferenceIntegrationTest.
 */

namespace Drupal\entity_reference\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests various Entity reference UI components.
 */
class EntityReferenceIntegrationTest extends WebTestBase {

  /**
   * The entity type used in this test.
   *
   * @var string
   */
  protected $entityType = 'entity_test';

  /**
   * The bundle used in this test.
   *
   * @var string
   */
  protected $bundle = 'entity_test';

  /**
   * The name of the field used in this test.
   *
   * @var string
   */
36
  protected $fieldName;
37 38 39 40 41 42

  /**
   * Modules to enable.
   *
   * @var array
   */
43
  public static $modules = array('config_test', 'entity_test', 'entity_reference', 'options');
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

  public static function getInfo() {
    return array(
      'name' => 'Entity reference components (widgets, formatters, etc.)',
      'description' => 'Tests for various Entity reference components.',
      'group' => 'Entity Reference',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    // Create a test user.
    $web_user = $this->drupalCreateUser(array('administer entity_test content'));
    $this->drupalLogin($web_user);
  }

  /**
65
   * Tests the entity reference field with all its supported field widgets.
66
   */
67 68
  public function testSupportedEntityTypesAndWidgets() {
    foreach ($this->getTestEntities() as $referenced_entities) {
69
      $this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId();
70 71

      // Create an Entity reference field.
72
      entity_reference_create_instance($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), 'default', array(), 2);
73 74 75 76 77 78 79 80 81

      // Test the default 'entity_reference_autocomplete' widget.
      entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save();

      $entity_name = $this->randomName();
      $edit = array(
        'name' => $entity_name,
        'user_id' => mt_rand(0, 128),
        $this->fieldName . '[0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')',
82 83
        // Test an input of the entity label without a ' (entity_id)' suffix.
        $this->fieldName . '[1][target_id]' => $referenced_entities[1]->label(),
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
      );
      $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
      $this->assertFieldValues($entity_name, $referenced_entities);

      // Try to post the form again with no modification and check if the field
      // values remain the same.
      $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
      $this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
      $this->assertFieldValues($entity_name, $referenced_entities);

      // Test the 'entity_reference_autocomplete_tags' widget.
      entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array(
        'type' => 'entity_reference_autocomplete_tags',
      ))->save();

      $entity_name = $this->randomName();
      $target_id = $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')';
101 102
      // Test an input of the entity label without a ' (entity_id)' suffix.
      $target_id .= ', ' . $referenced_entities[1]->label();
103 104 105 106 107 108 109 110 111 112 113 114 115
      $edit = array(
        'name' => $entity_name,
        'user_id' => mt_rand(0, 128),
        $this->fieldName . '[target_id]' => $target_id,
      );
      $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
      $this->assertFieldValues($entity_name, $referenced_entities);

      // Try to post the form again with no modification and check if the field
      // values remain the same.
      $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
      $this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
      $this->assertFieldValues($entity_name, $referenced_entities);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

      // Test all the other widgets supported by the entity reference field.
      // Since we don't know the form structure for these widgets, just test
      // that editing and saving an already created entity works.
      $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
      $supported_widgets = \Drupal::service('plugin.manager.field.widget')->getOptions('entity_reference');
      $supported_widget_types = array_diff(array_keys($supported_widgets), array('entity_reference_autocomplete', 'entity_reference_autocomplete_tags'));

      foreach ($supported_widget_types as $widget_type) {
        entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array(
          'type' => $widget_type,
        ))->save();

        $this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
        $this->assertFieldValues($entity_name, $referenced_entities);
      }
132 133 134 135 136 137 138 139 140 141 142 143
    }
  }

  /**
   * Asserts that the reference field values are correct.
   *
   * @param string $entity_name
   *   The name of the test entity.
   * @param \Drupal\Core\Entity\EntityInterface[] $referenced_entities
   *   An array of referenced entities.
   */
  protected function assertFieldValues($entity_name, $referenced_entities) {
144 145 146
    $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));

    $this->assertTrue($entity, format_string('%entity_type: Entity found in the database.', array('%entity_type' => $this->entityType)));
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

    $this->assertEqual($entity->{$this->fieldName}->target_id, $referenced_entities[0]->id());
    $this->assertEqual($entity->{$this->fieldName}->entity->id(), $referenced_entities[0]->id());
    $this->assertEqual($entity->{$this->fieldName}->entity->label(), $referenced_entities[0]->label());

    $this->assertEqual($entity->{$this->fieldName}[1]->target_id, $referenced_entities[1]->id());
    $this->assertEqual($entity->{$this->fieldName}[1]->entity->id(), $referenced_entities[1]->id());
    $this->assertEqual($entity->{$this->fieldName}[1]->entity->label(), $referenced_entities[1]->label());
  }

  /**
   * Creates two content and two config test entities.
   *
   * @return array
   *   An array of entity objects.
   */
  protected function getTestEntities() {
    $config_entity_1 = entity_create('config_test', array('id' => $this->randomName(), 'label' => $this->randomName()));
    $config_entity_1->save();
    $config_entity_2 = entity_create('config_test', array('id' => $this->randomName(), 'label' => $this->randomName()));
    $config_entity_2->save();

    $content_entity_1 = entity_create('entity_test', array('name' => $this->randomName()));
    $content_entity_1->save();
    $content_entity_2 = entity_create('entity_test', array('name' => $this->randomName()));
    $content_entity_2->save();

    return array(
      'config' => array(
        $config_entity_1,
        $config_entity_2,
      ),
      'content' => array(
        $content_entity_1,
        $content_entity_2,
      ),
    );
184 185 186
  }

}