EntityReferenceSelectionSortTest.php 4.03 KB
Newer Older
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
<?php

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

namespace Drupal\entity_reference\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests the Entity Reference Selection plugin.
 */
class EntityReferenceSelectionSortTest extends WebTestBase {

  public static function getInfo() {
    return array(
      'name' => 'Entity Reference handlers sort',
      'description' => 'Test sorting referenced items.',
      'group' => 'Entity Reference',
    );
  }

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

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

    // Create an Article node type.
    $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
  }

  /**
   * Assert sorting by field and property.
   */
  public function testSort() {
    // Add text field to entity, to sort by.
39
    entity_create('field_entity', array(
40 41 42
      'field_name' => 'field_text',
      'type' => 'text',
      'entity_types' => array('node'),
43
    ))->save();
44

45
    entity_create('field_instance', array(
46 47 48 49 50 51
      'label' => 'Text Field',
      'field_name' => 'field_text',
      'entity_type' => 'node',
      'bundle' => 'article',
      'settings' => array(),
      'required' => FALSE,
52
    ))->save();
53 54


55 56
    // Create a field and instance.
    $field = entity_create('field_entity', array(
57 58 59 60 61 62 63 64
      'translatable' => FALSE,
      'entity_types' => array(),
      'settings' => array(
        'target_type' => 'node',
      ),
      'field_name' => 'test_field',
      'type' => 'entity_reference',
      'cardinality' => 1,
65 66 67 68 69 70
    ));
    $field->save();
    $instance = entity_create('field_instance', array(
      'field_name' => 'test_field',
      'entity_type' => 'test_entity',
      'bundle' => 'test_bundle',
71 72 73 74 75 76 77 78 79 80 81
      'settings' => array(
        'handler' => 'default',
        'handler_settings' => array(
          'target_bundles' => array(),
          // Add sorting.
          'sort' => array(
            'field' => 'field_text.value',
            'direction' => 'DESC',
          ),
        ),
      ),
82 83
    ));
    $instance->save();
84 85 86 87 88 89 90 91 92

    // Build a set of test data.
    $node_values = array(
      'published1' => array(
        'type' => 'article',
        'status' => 1,
        'title' => 'Node published1 (<&>)',
        'uid' => 1,
        'field_text' => array(
93 94
          array(
            'value' => 1,
95 96 97 98 99 100 101 102 103
          ),
        ),
      ),
      'published2' => array(
        'type' => 'article',
        'status' => 1,
        'title' => 'Node published2 (<&>)',
        'uid' => 1,
        'field_text' => array(
104 105
          array(
            'value' => 2,
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
          ),
        ),
      ),
    );

    $nodes = array();
    $node_labels = array();
    foreach ($node_values as $key => $values) {
      $node = entity_create('node', $values);
      $node->save();
      $nodes[$key] = $node;
      $node_labels[$key] = check_plain($node->label());
    }

    // Test as a non-admin.
    $normal_user = $this->drupalCreateUser(array('access content'));
    $GLOBALS['user'] = $normal_user;

124
    $handler = entity_reference_get_selection_handler($instance);
125 126 127

    // Not only assert the result, but make sure the keys are sorted as
    // expected.
128
    $result = $handler->getReferenceableEntities();
129
    $expected_result = array(
130 131
      $nodes['published2']->id() => $node_labels['published2'],
      $nodes['published1']->id() => $node_labels['published1'],
132 133 134 135 136 137 138 139
    );
    $this->assertIdentical($result['article'], $expected_result, 'Query sorted by field returned expected values.');

    // Assert sort by property.
    $instance['settings']['handler_settings']['sort'] = array(
      'field' => 'nid',
      'direction' => 'ASC',
    );
140
    $handler = entity_reference_get_selection_handler($instance);
141
    $result = $handler->getReferenceableEntities();
142
    $expected_result = array(
143 144
      $nodes['published1']->id() => $node_labels['published1'],
      $nodes['published2']->id() => $node_labels['published2'],
145 146 147 148
    );
    $this->assertIdentical($result['article'], $expected_result, 'Query sorted by property returned expected values.');
  }
}