SelectionTest.php 2.48 KB
Newer Older
1 2 3 4
<?php

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

8
namespace Drupal\entity_reference\Tests\Views;
9 10 11 12 13 14 15 16 17 18 19 20

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(
21
      'name' => 'Entity Reference: Selection handler',
22
      'description' => 'Tests entity reference selection handler provided by Views.',
23
      'group' => 'Views module integration',
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    );
  }

  /**
   * 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) {
39
      $nodes[$node->type][$node->id()] = $node->label();
40 41
    }

42 43
    // Create a field and instance.
    $field = entity_create('field_entity', array(
44 45 46 47 48 49 50 51
      'translatable' => FALSE,
      'entity_types' => array(),
      'settings' => array(
        'target_type' => 'node',
      ),
      'field_name' => 'test_field',
      'type' => 'entity_reference',
      'cardinality' => '1',
52 53 54 55 56 57
    ));
    $field->save();
    $instance = entity_create('field_instance', array(
      'field_name' => 'test_field',
      'entity_type' => 'test_entity',
      'bundle' => 'test_bundle',
58 59 60 61 62 63 64 65 66 67 68
      'settings' => array(
        'handler' => 'views',
        'handler_settings' => array(
          'target_bundles' => array(),
          'view' => array(
            'view_name' => 'test_entity_reference',
            'display_name' => 'entity_reference_1',
            'arguments' => array(),
          ),
        ),
      ),
69 70
    ));
    $instance->save();
71 72

    // Get values from selection handler.
73
    $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($instance);
74
    $result = $handler->getReferenceableEntities();
75 76 77 78 79 80 81 82 83 84 85 86 87 88

    $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.');
  }
}