EntityReferenceIntegrationTest.php 7.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

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

namespace Drupal\entity_reference\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests various Entity reference UI components.
14 15
 *
 * @group entity_reference
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 */
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
   */
38
  protected $fieldName;
39 40 41 42 43 44

  /**
   * Modules to enable.
   *
   * @var array
   */
45
  public static $modules = array('config_test', 'entity_test', 'entity_reference', 'options');
46 47 48 49

  /**
   * {@inheritdoc}
   */
50
  protected function setUp() {
51 52 53 54 55 56 57 58
    parent::setUp();

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

  /**
59
   * Tests the entity reference field with all its supported field widgets.
60
   */
61
  public function testSupportedEntityTypesAndWidgets() {
62
    $user_id = mt_rand(128, 256);
63
    foreach ($this->getTestEntities() as $referenced_entities) {
64
      $this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId();
65 66

      // Create an Entity reference field.
67
      entity_reference_create_instance($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), 'default', array(), 2);
68 69 70 71

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

72 73 74 75 76
      $user_id++;
      entity_create('user', array(
        'uid' => $user_id,
        'name' => $this->randomString(),
      ))->save();
77
      $entity_name = $this->randomMachineName();
78
      $edit = array(
79 80
        'name' => $entity_name,
        'user_id' => $user_id,
81
        $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
      );
      $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();

99
      $entity_name = $this->randomMachineName();
100
      $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
      $user_id++;
      entity_create('user', array(
        'uid' => $user_id,
        'name' => $this->randomString(),
      ))->save();
108
      $edit = array(
109 110
        'name' => $entity_name,
        'user_id' => $user_id,
111 112 113 114 115 116 117 118 119 120
        $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);
121 122 123 124

      // 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.
125 126
      // Also exclude the special author reference widgets.
      $exclude = array('entity_reference_autocomplete', 'entity_reference_autocomplete_tags', 'route_based_autocomplete', 'author_autocomplete');
127 128
      $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
      $supported_widgets = \Drupal::service('plugin.manager.field.widget')->getOptions('entity_reference');
129
      $supported_widget_types = array_diff(array_keys($supported_widgets), $exclude);
130 131 132 133 134 135 136 137 138

      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);
      }
139 140 141 142 143 144 145 146 147 148 149 150
    }
  }

  /**
   * 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) {
151 152 153
    $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)));
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

    $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() {
171
    $config_entity_1 = entity_create('config_test', array('id' => $this->randomMachineName(), 'label' => $this->randomMachineName()));
172
    $config_entity_1->save();
173
    $config_entity_2 = entity_create('config_test', array('id' => $this->randomMachineName(), 'label' => $this->randomMachineName()));
174 175
    $config_entity_2->save();

176
    $content_entity_1 = entity_create('entity_test', array('name' => $this->randomMachineName()));
177
    $content_entity_1->save();
178
    $content_entity_2 = entity_create('entity_test', array('name' => $this->randomMachineName()));
179 180 181 182 183 184 185 186 187 188 189 190
    $content_entity_2->save();

    return array(
      'config' => array(
        $config_entity_1,
        $config_entity_2,
      ),
      'content' => array(
        $content_entity_1,
        $content_entity_2,
      ),
    );
191 192 193
  }

}