summaryrefslogtreecommitdiffstats
path: root/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Views/SelectionTest.php
blob: cad4bd8e8d3606b6511c04749d69b3eef29303cc (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

/**
 * @file
 * Contains \Drupal\entity_reference\Tests\Views\SelectionTest.
 */

namespace Drupal\entity_reference\Tests\Views;

use Drupal\simpletest\WebTestBase;

/**
 * Tests entity reference selection handler.
 */
class SelectionTest extends WebTestBase {

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

  public static function getInfo() {
    return array(
      'name' => 'Entity Reference: Selection handler',
      'description' => 'Tests entity reference selection handler provided by Views.',
      'group' => 'Views module integration',
    );
  }

  /**
   * Tests the selection handler.
   */
  public function testSelectionHandler() {
    // Create nodes.
    $type = $this->drupalCreateContentType()->type;
    $node1 = $this->drupalCreateNode(array('type' => $type));
    $node2 = $this->drupalCreateNode(array('type' => $type));
    $node3 = $this->drupalCreateNode();

    $nodes = array();
    foreach (array($node1, $node2, $node3) as $node) {
      $nodes[$node->type][$node->id()] = $node->label();
    }

    // Create a field and instance.
    $field = entity_create('field_entity', array(
      'translatable' => FALSE,
      'entity_types' => array(),
      'settings' => array(
        'target_type' => 'node',
      ),
      'field_name' => 'test_field',
      'type' => 'entity_reference',
      'cardinality' => '1',
    ));
    $field->save();
    $instance = entity_create('field_instance', array(
      'field_name' => 'test_field',
      'entity_type' => 'test_entity',
      'bundle' => 'test_bundle',
      'settings' => array(
        'handler' => 'views',
        'handler_settings' => array(
          'target_bundles' => array(),
          'view' => array(
            'view_name' => 'test_entity_reference',
            'display_name' => 'entity_reference_1',
            'arguments' => array(),
          ),
        ),
      ),
    ));
    $instance->save();

    // Get values from selection handler.
    $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($instance);
    $result = $handler->getReferenceableEntities();

    $success = FALSE;
    foreach ($result as $node_type => $values) {
      foreach ($values as $nid => $label) {
        if (!$success = $nodes[$node_type][$nid] == trim(strip_tags($label))) {
          // There was some error, so break.
          break;
        }
      }
    }

    $this->assertTrue($success, 'Views selection handler returned expected values.');
  }
}