You are here

public function RestfulListTestCase::testSort in RESTful 7.2

Same name and namespace in other branches
  1. 7 tests/RestfulListTestCase.test \RestfulListTestCase::testSort()

Test the sorting of entities.

File

tests/RestfulListTestCase.test, line 39
Contains RestfulListTestCase.

Class

RestfulListTestCase
Class RestfulListTestCase.

Code

public function testSort() {

  // Allow the anonymous users access to the author field.
  user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
    'access user profiles' => TRUE,
  ));
  $user1 = $this
    ->drupalCreateUser();
  $user2 = $this
    ->drupalCreateUser();
  $settings = array(
    'type' => 'article',
  );
  $info = array(
    'abc' => $user1->uid,
    'xyz' => $user1->uid,
    'efg' => $user2->uid,
  );
  $nodes = array();
  foreach ($info as $title => $uid) {
    $settings['title'] = $title;
    $settings['uid'] = $uid;
    $node = $this
      ->drupalCreateNode($settings);
    $nodes[$title] = $node->nid;
  }

  // Add unpublished node, to confirm it is not listed.
  $settings['status'] = NODE_NOT_PUBLISHED;
  $this
    ->drupalCreateNode($settings);
  $resource_manager = restful()
    ->getResourceManager();
  $handler = $resource_manager
    ->getPlugin('articles:1.0');
  $query['fields'] = 'id,label';
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $handler
    ->setPath('');

  // No sorting (default sorting).
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $result = $result['data'];
  $expected_result = array(
    array(
      'id' => $nodes['abc'],
      'label' => 'abc',
    ),
    array(
      'id' => $nodes['xyz'],
      'label' => 'xyz',
    ),
    array(
      'id' => $nodes['efg'],
      'label' => 'efg',
    ),
  );
  $this
    ->assertEqual($result, $expected_result, 'No sorting (default sorting).');

  // Sort by ID descending.
  $query['sort'] = '-id';
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $result = $result['data'];
  $expected_result = array(
    array(
      'id' => $nodes['efg'],
      'label' => 'efg',
    ),
    array(
      'id' => $nodes['xyz'],
      'label' => 'xyz',
    ),
    array(
      'id' => $nodes['abc'],
      'label' => 'abc',
    ),
  );
  $this
    ->assertEqual($result, $expected_result, 'Sort by ID descending.');

  // Sort by label ascending.
  $query['sort'] = 'label';
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $result = $result['data'];
  $expected_result = array(
    array(
      'id' => $nodes['abc'],
      'label' => 'abc',
    ),
    array(
      'id' => $nodes['efg'],
      'label' => 'efg',
    ),
    array(
      'id' => $nodes['xyz'],
      'label' => 'xyz',
    ),
  );
  $this
    ->assertEqual($result, $expected_result, 'Sort by label ascending.');

  // Sort by label and by ID. For that we add another node titled "abc".
  $settings = array(
    'type' => 'article',
    'title' => 'abc',
  );
  $node = $this
    ->drupalCreateNode($settings);
  $query['sort'] = 'label,id';
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $result = $result['data'];
  $expected_result = array(
    array(
      'id' => $nodes['abc'],
      'label' => 'abc',
    ),
    array(
      'id' => $node->nid,
      'label' => 'abc',
    ),
    array(
      'id' => $nodes['efg'],
      'label' => 'efg',
    ),
    array(
      'id' => $nodes['xyz'],
      'label' => 'xyz',
    ),
  );
  $this
    ->assertEqual($result, $expected_result, 'Sort by ID and by label.');

  // Test default sorting from plugin definition; by label, then by reverse
  // id.
  $handler = $resource_manager
    ->getPlugin('test_articles:1.1');
  unset($query['sort']);
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $result = $result['data'];
  $expected_result = array(
    array(
      'id' => $node->nid,
      'label' => 'abc',
      'self' => $handler
        ->versionedUrl($node->nid),
    ),
    array(
      'id' => $nodes['abc'],
      'label' => 'abc',
      'self' => $handler
        ->versionedUrl($nodes['abc']),
    ),
    array(
      'id' => $nodes['efg'],
      'label' => 'efg',
      'self' => $handler
        ->versionedUrl($nodes['efg']),
    ),
    array(
      'id' => $nodes['xyz'],
      'label' => 'xyz',
      'self' => $handler
        ->versionedUrl($nodes['xyz']),
    ),
  );
  $this
    ->assertEqual($result, $expected_result, 'Default sort by ID and by label.');

  // Test that the default sort can be overridden.
  $query['sort'] = 'id';
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $handler
    ->setPath('');
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $result = $result['data'];
  $expected_result = array(
    array(
      'id' => $nodes['abc'],
      'label' => 'abc',
      'self' => $handler
        ->versionedUrl($nodes['abc']),
    ),
    array(
      'id' => $nodes['xyz'],
      'label' => 'xyz',
      'self' => $handler
        ->versionedUrl($nodes['xyz']),
    ),
    array(
      'id' => $nodes['efg'],
      'label' => 'efg',
      'self' => $handler
        ->versionedUrl($nodes['efg']),
    ),
    array(
      'id' => $node->nid,
      'label' => 'abc',
      'self' => $handler
        ->versionedUrl($node->nid),
    ),
  );
  $this
    ->assertEqual($result, $expected_result, 'Sort by ID, overriding default sort.');

  // Illegal sort property.
  $query['sort'] = 'wrong_key';
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $handler
    ->setPath('');
  try {
    restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json');
    $this
      ->fail('Illegal sort property used.');
  } catch (BadRequestException $e) {
    $this
      ->pass('Exception thrown on illegal sort property.');
  }

  // Illegal sort property, descending.
  $query['sort'] = '-wrong_key';
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $handler
    ->setPath('');
  try {
    restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json');
    $this
      ->fail('Illegal sort property, descending, used.');
  } catch (BadRequestException $e) {
    $this
      ->pass('Exception thrown on illegal sort property, descending.');
  }

  // Test valid sort with sort params disabled.
  $resource_manager
    ->clearPluginCache($handler
    ->getPluginId());
  $plugin_definition = $handler
    ->getPluginDefinition();
  $plugin_definition['dataProvider']['urlParams'] = array(
    'filter' => TRUE,
    'sort' => FALSE,
    'range' => TRUE,
  );
  $handler
    ->setPluginDefinition($plugin_definition);

  // Re-instantiate the data provider.
  $handler
    ->setDataProvider(NULL);
  $query['sort'] = 'label,id';
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $handler
    ->setPath('');
  try {
    restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json');
    $this
      ->fail('Exception not raised for disabled sort parameter.');
  } catch (UnprocessableEntityException $e) {
    $this
      ->pass('Exception raised for disabled sort parameter.');
  }

  // Test the range overrides.
  unset($query['sort']);
  $query['range'] = 2;
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $handler
    ->setPath('');
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $result = $result['data'];
  $this
    ->assertEqual(count($result), $query['range'], 'Range parameter overridden correctly');

  // Test the max cap in the the annotation configuration.
  $resource_manager
    ->clearPluginCache($handler
    ->getPluginId());
  $plugin_definition = $handler
    ->getPluginDefinition();
  $plugin_definition['range'] = 1;
  $handler
    ->setPluginDefinition($plugin_definition);
  $query['range'] = 2;
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->doGet('', $query), 'json'));
  $this
    ->assertEqual(count($result['data']), $query['range'], 'Range is limited to the configured maximum.');

  // Test invalid range.
  $query['range'] = $this
    ->randomName();
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $handler
    ->setPath('');
  try {
    restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json');
    $this
      ->fail('Exception not raised on invalid range parameter.');
  } catch (BadRequestException $e) {
    $this
      ->pass('Exception raised on invalid range parameter.');
  }

  // Test valid range with range params disabled.
  $resource_manager
    ->clearPluginCache($handler
    ->getPluginId());
  $plugin_definition = $handler
    ->getPluginDefinition();
  $plugin_definition['dataProvider']['urlParams'] = array(
    'filter' => TRUE,
    'sort' => TRUE,
    'range' => FALSE,
  );

  // Unset the previously added range.
  unset($plugin_definition['range']);
  $handler
    ->setPluginDefinition($plugin_definition);

  // Re-instantiate the data provider.
  $handler
    ->setDataProvider(NULL);
  $query['range'] = 2;
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $handler
    ->setPath('');
  try {
    restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json');
    $this
      ->fail('Exception not raised for disabled range parameter.');
  } catch (UnprocessableEntityException $e) {
    $this
      ->pass('Exception raised for disabled range parameter.');
  }

  // Sort by an entity metadata wrapper property that is different from the
  // DB column name.
  $handler = $resource_manager
    ->getPlugin('articles:1.5');
  $query = array(
    'sort' => '-user',
  );
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $handler
    ->setPath('');
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $result = $result['data'];
  $this
    ->assertEqual($result[0]['user']['id'], 3, 'List sorted by the "author" entity metadata wrapper property, which maps to the "uid" DB column name.');
  $query = array(
    'sort' => 'static',
  );
  $handler
    ->setRequest(Request::create('', $query, RequestInterface::METHOD_GET));
  $handler
    ->setPath('');
  try {
    restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json');
    $this
      ->fail('Exception not thrown for invalid sort.');
  } catch (BadRequestException $e) {
    $this
      ->pass('Exception thrown for invalid sort.');
  }
}