EntityReferenceAutoCreateTest.php 3.82 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\Language\Language;
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
36
37
use Drupal\simpletest\WebTestBase;

/**
 * Tests the Entity Reference auto-creation feature.
 */
class EntityReferenceAutoCreateTest extends WebTestBase {

  public static function getInfo() {
    return array(
      'name' => 'Entity Reference auto-create',
      'description' => 'Tests creating new entity (e.g. taxonomy-term) from an autocomplete widget.',
      'group' => 'Entity Reference',
    );
  }

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

  function setUp() {
    parent::setUp();

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

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

38
    entity_create('field_entity', array(
39
40
41
42
43
44
45
46
      'translatable' => FALSE,
      'entity_types' => array(),
      'settings' => array(
        'target_type' => 'node',
      ),
      'field_name' => 'test_field',
      'type' => 'entity_reference',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
47
    ))->save();
48

49
    entity_create('field_instance', array(
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
      '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,
        ),
      ),
65
    ))->save();
66

67
68
69
    entity_get_display('node', $referencing->type, 'default')
      ->setComponent('test_field')
      ->save();
70
71
72
73
74
    entity_get_form_display('node', $referencing->type, 'default')
      ->setComponent('test_field', array(
        'type' => 'entity_reference_autocomplete',
      ))
      ->save();
75
76
77
78
79
80
81
82
83
84
85
86
  }

  /**
   * Assert creation on a new entity.
   */
  public function testAutoCreate() {
    $user1 = $this->drupalCreateUser(array('access content', "create $this->referencing_type content"));
    $this->drupalLogin($user1);

    $new_title = $this->randomName();

    // Assert referenced node does not exist.
87
    $base_query = \Drupal::entityQuery('node');
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    $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(
      'title' => $this->randomName(),
      'test_field[und][0][target_id]' => $new_title,
    );
    $this->drupalPost("node/add/$this->referencing_type", $edit, 'Save');

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

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

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

    // 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.');
122
123
  }
}