EntityReferenceItemTest.php 6.66 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

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

namespace Drupal\entity_reference\Tests;

10 11
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
12
use Drupal\Core\Language\LanguageInterface;
13
use Drupal\field\Tests\FieldUnitTestBase;
14 15 16

/**
 * Tests the new entity API for the entity reference field type.
17 18
 *
 * @group entity_reference
19
 */
20
class EntityReferenceItemTest extends FieldUnitTestBase {
21 22

  /**
23
   * Modules to install.
24 25 26
   *
   * @var array
   */
27
  public static $modules = array('entity_reference', 'taxonomy', 'options', 'text', 'filter');
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42
  /**
   * The taxonomy vocabulary to test with.
   *
   * @var \Drupal\taxonomy\VocabularyInterface
   */
  protected $vocabulary;

  /**
   * The taxonomy term to test with.
   *
   * @var \Drupal\taxonomy\TermInterface
   */
  protected $term;

43 44 45
  /**
   * Sets up the test.
   */
46
  protected function setUp() {
47 48
    parent::setUp();

49
    $this->installEntitySchema('taxonomy_term');
50

51
    $this->vocabulary = entity_create('taxonomy_vocabulary', array(
52 53
      'name' => $this->randomMachineName(),
      'vid' => drupal_strtolower($this->randomMachineName()),
54
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
55
    ));
56
    $this->vocabulary->save();
57 58

    $this->term = entity_create('taxonomy_term', array(
59
      'name' => $this->randomMachineName(),
60
      'vid' => $this->vocabulary->id(),
61
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
62 63 64
    ));
    $this->term->save();

65
    // Use the util to create an instance.
66 67
    entity_reference_create_instance('entity_test', 'entity_test', 'field_test_taxonomy_term', 'Test content entity reference', 'taxonomy_term');
    entity_reference_create_instance('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary');
68 69 70
  }

  /**
71
   * Tests the entity reference field type for referencing content entities.
72
   */
73
  public function testContentEntityReferenceItem() {
74
    $tid = $this->term->id();
75 76

    // Just being able to create the entity like this verifies a lot of code.
77
    $entity = entity_create('entity_test');
78
    $entity->field_test_taxonomy_term->target_id = $tid;
79
    $entity->name->value = $this->randomMachineName();
80 81
    $entity->save();

82
    $entity = entity_load('entity_test', $entity->id());
83 84 85
    $this->assertTrue($entity->field_test_taxonomy_term instanceof FieldItemListInterface, 'Field implements interface.');
    $this->assertTrue($entity->field_test_taxonomy_term[0] instanceof FieldItemInterface, 'Field item implements interface.');
    $this->assertEqual($entity->field_test_taxonomy_term->target_id, $tid);
86
    $this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $this->term->getName());
87 88
    $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $tid);
    $this->assertEqual($entity->field_test_taxonomy_term->entity->uuid(), $this->term->uuid());
89 90

    // Change the name of the term via the reference.
91
    $new_name = $this->randomMachineName();
92
    $entity->field_test_taxonomy_term->entity->setName($new_name);
93
    $entity->field_test_taxonomy_term->entity->save();
94
    // Verify it is the correct name.
95
    $term = entity_load('taxonomy_term', $tid);
96
    $this->assertEqual($term->getName(), $new_name);
97

98 99
    // Make sure the computed term reflects updates to the term id.
    $term2 = entity_create('taxonomy_term', array(
100
      'name' => $this->randomMachineName(),
101
      'vid' => $this->term->bundle(),
102
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
103 104
    ));
    $term2->save();
105

106 107
    $entity->field_test_taxonomy_term->target_id = $term2->id();
    $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term2->id());
108
    $this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term2->getName());
109 110 111 112

    // Delete terms so we have nothing to reference and try again
    $term->delete();
    $term2->delete();
113
    $entity = entity_create('entity_test', array('name' => $this->randomMachineName()));
114
    $entity->save();
115 116 117 118 119 120

    // Test the generateSampleValue() method.
    $entity = entity_create('entity_test');
    $entity->field_test_taxonomy_term->generateSampleItems();
    $entity->field_test_taxonomy_vocabulary->generateSampleItems();
    $this->entityValidateAndSave($entity);
121
  }
122 123

  /**
124
   * Tests the entity reference field type for referencing config entities.
125
   */
126 127 128 129
  public function testConfigEntityReferenceItem() {
    $referenced_entity_id = $this->vocabulary->id();

    // Just being able to create the entity like this verifies a lot of code.
130
    $entity = entity_create('entity_test');
131
    $entity->field_test_taxonomy_vocabulary->target_id = $referenced_entity_id;
132
    $entity->name->value = $this->randomMachineName();
133 134 135 136 137 138 139 140 141 142 143
    $entity->save();

    $entity = entity_load('entity_test', $entity->id());
    $this->assertTrue($entity->field_test_taxonomy_vocabulary instanceof FieldItemListInterface, 'Field implements interface.');
    $this->assertTrue($entity->field_test_taxonomy_vocabulary[0] instanceof FieldItemInterface, 'Field item implements interface.');
    $this->assertEqual($entity->field_test_taxonomy_vocabulary->target_id, $referenced_entity_id);
    $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->name, $this->vocabulary->name);
    $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->id(), $referenced_entity_id);
    $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->uuid(), $this->vocabulary->uuid());

    // Change the name of the term via the reference.
144
    $new_name = $this->randomMachineName();
145 146 147
    $entity->field_test_taxonomy_vocabulary->entity->name = $new_name;
    $entity->field_test_taxonomy_vocabulary->entity->save();
    // Verify it is the correct name.
148 149
    $vocabulary = entity_load('taxonomy_vocabulary', $referenced_entity_id);
    $this->assertEqual($vocabulary->name, $new_name);
150 151 152

    // Make sure the computed term reflects updates to the term id.
    $vocabulary2 = entity_create('taxonomy_vocabulary', array(
153 154
      'name' => $this->randomMachineName(),
      'vid' => drupal_strtolower($this->randomMachineName()),
155
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
156 157 158 159 160 161 162 163 164 165
    ));
    $vocabulary2->save();

    $entity->field_test_taxonomy_vocabulary->target_id = $vocabulary2->id();
    $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->id(), $vocabulary2->id());
    $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->name, $vocabulary2->name);

    // Delete terms so we have nothing to reference and try again
    $this->vocabulary->delete();
    $vocabulary2->delete();
166
    $entity = entity_create('entity_test', array('name' => $this->randomMachineName()));
167
    $entity->save();
168
  }
169

170
}