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

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

namespace Drupal\entity_reference\Tests;

10
use Drupal\Core\Field\FieldStorageDefinitionInterface;
11
12
13
use Drupal\simpletest\WebTestBase;

/**
14
15
16
 * Tests creating new entity (e.g. taxonomy-term) from an autocomplete widget.
 *
 * @group entity_reference
17
18
19
20
21
 */
class EntityReferenceAutoCreateTest extends WebTestBase {

  public static $modules = array('entity_reference', 'node');

22
  protected function setUp() {
23
24
25
26
27
28
29
30
31
    parent::setUp();

    // Create "referencing" and "referenced" node types.
    $referencing = $this->drupalCreateContentType();
    $this->referencing_type = $referencing->type;

    $referenced = $this->drupalCreateContentType();
    $this->referenced_type = $referenced->type;

32
    entity_create('field_storage_config', array(
33
      'field_name' => 'test_field',
34
      'entity_type' => 'node',
35
36
37
38
39
40
      'translatable' => FALSE,
      'entity_types' => array(),
      'settings' => array(
        'target_type' => 'node',
      ),
      'type' => 'entity_reference',
41
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
42
    ))->save();
43

44
    entity_create('field_config', array(
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
      'label' => 'Entity reference field',
      'field_name' => 'test_field',
      'entity_type' => 'node',
      'bundle' => $referencing->type,
      'settings' => array(
        'handler' => 'default',
        'handler_settings' => array(
          // Reference a single vocabulary.
          'target_bundles' => array(
            $referenced->type,
          ),
          // Enable auto-create.
          'auto_create' => TRUE,
        ),
      ),
60
    ))->save();
61

62
63
64
    entity_get_display('node', $referencing->type, 'default')
      ->setComponent('test_field')
      ->save();
65
66
67
68
69
    entity_get_form_display('node', $referencing->type, 'default')
      ->setComponent('test_field', array(
        'type' => 'entity_reference_autocomplete',
      ))
      ->save();
70
71
72
  }

  /**
73
74
   * Tests that the autocomplete input element appears and the creation of a new
   * entity.
75
76
77
78
79
   */
  public function testAutoCreate() {
    $user1 = $this->drupalCreateUser(array('access content', "create $this->referencing_type content"));
    $this->drupalLogin($user1);

80
81
82
    $this->drupalGet('node/add/' . $this->referencing_type);
    $this->assertFieldByXPath('//input[@id="edit-test-field-0-target-id" and contains(@class, "form-autocomplete")]', NULL, 'The autocomplete input element appears.');

83
    $new_title = $this->randomMachineName();
84
85

    // Assert referenced node does not exist.
86
    $base_query = \Drupal::entityQuery('node');
87
88
89
90
91
92
93
94
95
    $base_query
      ->condition('type', $this->referenced_type)
      ->condition('title', $new_title);

    $query = clone $base_query;
    $result = $query->execute();
    $this->assertFalse($result, 'Referenced node does not exist yet.');

    $edit = array(
96
      'title[0][value]' => $this->randomMachineName(),
97
      'test_field[0][target_id]' => $new_title,
98
    );
99
    $this->drupalPostForm("node/add/$this->referencing_type", $edit, 'Save');
100
101
102
103
104
105

    // Assert referenced node was created.
    $query = clone $base_query;
    $result = $query->execute();
    $this->assertTrue($result, 'Referenced node was created.');
    $referenced_nid = key($result);
106
    $referenced_node = node_load($referenced_nid);
107
108

    // Assert the referenced node is associated with referencing node.
109
    $result = \Drupal::entityQuery('node')
110
111
112
113
114
      ->condition('type', $this->referencing_type)
      ->execute();

    $referencing_nid = key($result);
    $referencing_node = node_load($referencing_nid);
115
    $this->assertEqual($referenced_nid, $referencing_node->test_field->target_id, 'Newly created node is referenced from the referencing node.');
116
117
118
119
120

    // Now try to view the node and check that the referenced node is shown.
    $this->drupalGet('node/' . $referencing_node->id());
    $this->assertText($referencing_node->label(), 'Referencing node label found.');
    $this->assertText($referenced_node->label(), 'Referenced node label found.');
121
122
  }
}