function RestfulListTestCase::testSort in RESTful 7
Same name and namespace in other branches
- 7.2 tests/RestfulListTestCase.test \RestfulListTestCase::testSort()
Test the sorting of entities.
File
- tests/
RestfulListTestCase.test, line 25 - Contains RestfulListTestCase
Class
- RestfulListTestCase
- @file Contains RestfulListTestCase
Code
function testSort() {
$user1 = $this
->drupalCreateUser();
$user2 = $this
->drupalCreateUser();
$settings = array(
'type' => 'article',
);
$info = array(
'abc' => $user1->uid,
'xyz' => $user1->uid,
'efg' => $user2->uid,
);
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);
$handler = restful_get_restful_handler('articles');
$request['fields'] = 'id,label';
// No sorting (default sorting).
$result = $handler
->get('', $request);
$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.
$request['sort'] = '-id';
$result = $handler
->get('', $request);
$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.
$request['sort'] = 'label';
$result = $handler
->get('', $request);
$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);
$request['sort'] = 'label,id';
$result = $handler
->get('', $request);
$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 = restful_get_restful_handler('test_articles', 1, 1);
unset($request['sort']);
$result = $handler
->get('', $request);
$expected_result = array(
array(
'id' => $node->nid,
'label' => 'abc',
),
array(
'id' => $nodes['abc'],
'label' => 'abc',
),
array(
'id' => $nodes['efg'],
'label' => 'efg',
),
array(
'id' => $nodes['xyz'],
'label' => 'xyz',
),
);
$this
->assertEqual($result, $expected_result, 'Default sort by ID and by label.');
// Test that the default sort can be overridden.
$request['sort'] = 'id';
$result = $handler
->get('', $request);
$expected_result = array(
array(
'id' => $nodes['abc'],
'label' => 'abc',
),
array(
'id' => $nodes['xyz'],
'label' => 'xyz',
),
array(
'id' => $nodes['efg'],
'label' => 'efg',
),
array(
'id' => $node->nid,
'label' => 'abc',
),
);
$this
->assertEqual($result, $expected_result, 'Sort by ID, overriding default sort.');
// Illegal sort property.
$request['sort'] = 'wrong_key';
try {
$handler
->get('', $request);
$this
->fail('Illegal sort property used.');
} catch (RestfulBadRequestException $e) {
$this
->pass('Exception thrown on illegal sort property.');
}
// Illegal sort property, descending.
$request['sort'] = '-wrong_key';
try {
$handler
->get('', $request);
$this
->fail('Illegal sort property, descending, used.');
} catch (RestfulBadRequestException $e) {
$this
->pass('Exception thrown on illegal sort property, descending.');
}
// Test valid sort with sort params disabled.
$handler
->setPluginKey('url_params', array(
'filter' => TRUE,
'sort' => FALSE,
'range' => TRUE,
));
$request['sort'] = 'label,id';
try {
$handler
->get('', $request);
$this
->fail('Exception not raised for disabled sort parameter.');
} catch (\RestfulBadRequestException $e) {
$this
->pass('Exception raised for disabled sort parameter.');
}
// Test the range overrides.
unset($request['sort']);
$request['range'] = 2;
$result = $handler
->get('', $request);
$this
->assertEqual(count($result), $request['range'], 'Range parameter overridden correctly');
// Test invalid range.
$request['range'] = $this
->randomName();
try {
$handler
->get('', $request);
$this
->fail('Exception not raised on invalid range parameter.');
} catch (\RestfulBadRequestException $e) {
$this
->pass('Exception raised on invalid range parameter.');
}
// Test valid range with range params disabled.
$handler
->setPluginKey('url_params', array(
'filter' => TRUE,
'sort' => TRUE,
'range' => FALSE,
));
$request['range'] = 2;
try {
$handler
->get('', $request);
$this
->fail('Exception not raised for disabled range parameter.');
} catch (\RestfulBadRequestException $e) {
$this
->pass('Exception raised for disabled range parameter.');
}
// Test the administrator's content listing.
$role_name = 'administrator';
$handler = restful_get_restful_handler_by_name('per_role_content__1_0:' . $role_name);
// Force the user 1 to be identified.
$handler
->setAccount(user_load(1));
$settings = array(
'type' => 'article',
'title' => $this
->randomName(),
'uid' => 1,
);
$this
->drupalCreateNode($settings);
$request = array(
'fields' => 'roles',
);
$response = $handler
->get('', $request);
foreach ($response as $item) {
$roles = array_map('trim', explode(',', $item['roles']));
$this
->assertTrue(in_array($role_name, $roles), format_string('%role role found in content list.', array(
'%role' => $role_name,
)));
}
// Sort by an entity metadata wrapper property that is different from the
// DB column name.
$handler = restful_get_restful_handler('articles', 1, 5);
$request = array(
'sort' => '-user',
);
$result = $handler
->get('', $request);
$this
->assertEqual($result[0]['user']['id'], 3, 'List sorted by the "author" entity metadata wrapper property, which maps to the "uid" DB column name.');
$request = array(
'sort' => 'static',
);
try {
$handler
->get('', $request);
$this
->fail('Exception not thrown for invalid sort.');
} catch (\RestfulBadRequestException $e) {
$this
->pass('Exception thrown for invalid sort.');
}
}