public function EntityReferenceSelectionSortTest::testSort

Assert sorting by field and property.

File

drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php, line 37
Contains \Drupal\entity_reference\Tests\EntityReferenceSelectionSortTest.

Class

EntityReferenceSelectionSortTest
Tests the Entity Reference Selection plugin.

Namespace

Drupal\entity_reference\Tests

Code

public function testSort() {

  // Add text field to entity, to sort by.
  $field_info = array(
    'field_name' => 'field_text',
    'type' => 'text',
    'entity_types' => array(
      'node',
    ),
  );
  field_create_field($field_info);
  $instance_info = array(
    'label' => 'Text Field',
    'field_name' => 'field_text',
    'entity_type' => 'node',
    'bundle' => 'article',
    'settings' => array(),
    'required' => FALSE,
  );
  field_create_instance($instance_info);

  // Build a fake field instance.
  $field = array(
    'translatable' => FALSE,
    'entity_types' => array(),
    'settings' => array(
      'target_type' => 'node',
    ),
    'field_name' => 'test_field',
    'type' => 'entity_reference',
    'cardinality' => 1,
  );
  $instance = array(
    'settings' => array(
      'handler' => 'default',
      'handler_settings' => array(
        'target_bundles' => array(),
        // Add sorting.
        'sort' => array(
          'field' => 'field_text.value',
          'direction' => 'DESC',
        ),
      ),
    ),
  );

  // Build a set of test data.
  $node_values = array(
    'published1' => array(
      'type' => 'article',
      'status' => 1,
      'title' => 'Node published1 (<&>)',
      'uid' => 1,
      'field_text' => array(
        array(
          'value' => 1,
        ),
      ),
    ),
    'published2' => array(
      'type' => 'article',
      'status' => 1,
      'title' => 'Node published2 (<&>)',
      'uid' => 1,
      'field_text' => array(
        array(
          'value' => 2,
        ),
      ),
    ),
  );
  $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;
  $handler = entity_reference_get_selection_handler($field, $instance);

  // Not only assert the result, but make sure the keys are sorted as
  // expected.
  $result = $handler
    ->getReferencableEntities();
  $expected_result = array(
    $nodes['published2']->nid => $node_labels['published2'],
    $nodes['published1']->nid => $node_labels['published1'],
  );
  $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',
  );
  $handler = entity_reference_get_selection_handler($field, $instance);
  $result = $handler
    ->getReferencableEntities();
  $expected_result = array(
    $nodes['published1']->nid => $node_labels['published1'],
    $nodes['published2']->nid => $node_labels['published2'],
  );
  $this
    ->assertIdentical($result['article'], $expected_result, 'Query sorted by property returned expected values.');
}