function RestfulListTestCase::testFilter in RESTful 7
Same name and namespace in other branches
- 7.2 tests/RestfulListTestCase.test \RestfulListTestCase::testFilter()
Test filtering.
File
- tests/
RestfulListTestCase.test, line 287 - Contains RestfulListTestCase
Class
- RestfulListTestCase
- @file Contains RestfulListTestCase
Code
function testFilter() {
$user1 = $this
->drupalCreateUser();
$user2 = $this
->drupalCreateUser();
$this
->addIntegerFields();
$info = array(
array(
'title' => 'abc',
'integer_single' => 1,
'integer_multiple' => array(
1,
2,
3,
),
'uid' => $user1->uid,
),
array(
'title' => 'another abc',
'integer_single' => 5,
'integer_multiple' => array(
3,
4,
5,
),
'uid' => $user1->uid,
),
array(
'title' => 'last',
'integer_single' => NULL,
'integer_multiple' => array(),
'uid' => $user2->uid,
),
);
$nodes = array();
// We set the 'created' property of the node in order to test the operator
// 'BETWEEN' on a property.
$nodes_created = array(
'abc' => strtotime('june 1 1971'),
'another abc' => strtotime('june 1 1986'),
'last' => strtotime('june 1 1996'),
);
foreach ($info as $row) {
$title = $row['title'];
$settings = array(
'type' => 'article',
'title' => $title,
);
$settings['integer_single'][LANGUAGE_NONE][0]['value'] = $row['integer_single'];
foreach ($row['integer_multiple'] as $key => $value) {
$settings['integer_multiple'][LANGUAGE_NONE][$key]['value'] = $value;
}
$settings['uid'] = $row['uid'];
// Setting the 'created' time of the node.
$settings['created'] = $nodes_created[$title];
$node = $this
->drupalCreateNode($settings);
$nodes[$title] = $node->nid;
}
$handler = restful_get_restful_handler('test_articles', 1, 2);
$fields = array(
'id',
'label',
'integer_single',
'intger_multiple',
);
$request['fields'] = implode(',', $fields);
// Single value property.
$request['filter'] = array(
'label' => 'abc',
);
$result = $handler
->get('', $request);
$this
->assertEqual($result[0]['id'], $nodes['abc'], 'Filter list by single value property.');
// Assert count is correct.
$formatter_handler = restful_get_formatter_handler('hal_json', $handler);
$output = $formatter_handler
->prepare($result);
$this
->assertEqual($output['count'], 1, '"count" property is correct.');
// Single value field.
$request['filter'] = array(
'integer_single' => '1',
);
$result = $handler
->get('', $request);
$this
->assertEqual($result[0]['id'], $nodes['abc'], 'Filter list by Single value field.');
// LIKE operator.
$request['filter'] = array(
'label' => array(
'value' => '%nothe%',
'operator' => 'LIKE',
),
);
$result = $handler
->get('', $request);
$this
->assertEqual($result[0]['id'], $nodes['another abc'], 'Filter list using LIKE operator.');
// STARTS_WITH operator.
$request['filter'] = array(
'label' => array(
'value' => 'las',
'operator' => 'STARTS_WITH',
),
);
$result = $handler
->get('', $request);
$this
->assertEqual($result[0]['id'], $nodes['last'], 'Filter list using STARTS_WITH operator.');
// CONTAINS operator.
$request['filter'] = array(
'label' => array(
'value' => 'bc',
'operator' => 'CONTAINS',
),
);
$result = $handler
->get('', $request);
// Sort the results before checking it.
usort($result, function ($a, $b) {
return strcmp($a['label'], $b['label']);
});
$this
->assertEqual($result[0]['id'], $nodes['abc'], 'Filter list using CONTAINS operator.');
$this
->assertEqual($result[1]['id'], $nodes['another abc'], 'Filter list using CONTAINS operator.');
// Multiple value field.
$request['filter'] = array(
'integer_multiple' => array(
'value' => array(
4,
5,
),
'operator' => 'BETWEEN',
),
);
$result = $handler
->get('', $request);
$this
->assertEqual($result[0]['id'], $nodes['another abc'], 'Filter list by multiple value field.');
// Invalid key.
$request['filter'] = array(
'invalid_key' => '3',
);
try {
$handler
->get('', $request);
$this
->fail('No exception was thrown on invalid key for filter.');
} catch (\RestfulBadRequestException $e) {
$this
->pass('Correct exception was thrown on invalid key for filter.');
} catch (\Exception $e) {
$this
->fail('Incorrect exception was thrown on invalid key for filter.');
}
// Assert filtering doesn't apply for non-list request
// (e.g. /api/v1.0/articles/1?filter[label]=foo), as this might be called from
// a formatter plugin, after RESTful's error handling has finished.
$query = array(
'filter' => array(
'invalid-key' => 'foo',
),
);
$result = $this
->httpRequest('api/v1.0/articles/1', \RestfulInterface::GET, $query);
$this
->assertEqual($result['code'], '200', 'Invalid filter key was ignored on non-list query.');
// Test the 'BETWEEN' operator on the 'created' property.
$request = array(
'filter' => array(
'created' => array(
'value' => array(
0,
$nodes_created['abc'],
),
'operator' => 'BETWEEN',
),
),
);
$result = $handler
->get('', $request);
$this
->assertEqual(count($result), 1);
$request['filter']['created']['value'] = array(
$nodes_created['abc'],
$nodes_created['last'] - 1,
);
$result = $handler
->get('', $request);
$this
->assertEqual(count($result), 2);
$request['filter']['created']['value'] = array(
$nodes_created['abc'],
);
try {
$handler
->get('', $request);
$this
->fail('Exception not raised when using one value on operator BETWEEN (on a property).');
} catch (\PDOException $e) {
$this
->pass('Exception raised when using one value on operator BETWEEN (on a property).');
}
// Test multiple filters on the same field.
$request = array(
'filter' => array(
'integer_single' => array(
'value' => array(
1,
4,
),
'operator' => array(
'>',
'<>',
),
),
),
);
$result = $handler
->get('', $request);
$this
->assertEqual($nodes['another abc'], $result[0]['id']);
$request = array(
'filter' => array(
'integer_single' => array(
'value' => array(
1,
5,
),
'operator' => array(
'>',
'<>',
),
),
),
);
$result = $handler
->get('', $request);
$this
->assertEqual($result, array());
$request = array(
'filter' => array(
'integer_multiple' => array(
'value' => array(
3,
4,
),
),
),
);
$result = $handler
->get('', $request);
$this
->assertEqual($nodes['another abc'], $result[0]['id']);
// Test valid filter with filter params disabled.
$handler
->setPluginKey('url_params', array(
'filter' => FALSE,
'sort' => TRUE,
'range' => TRUE,
));
$request['filter'] = array(
'label' => 'abc',
);
try {
$handler
->get('', $request);
$this
->fail('Exception not raised for disabled filter parameter.');
} catch (\RestfulBadRequestException $e) {
$this
->pass('Exception raised for disabled filter parameter.');
}
// Filter by an entity metadata wrapper property that is different from the
// DB column name.
$handler = restful_get_restful_handler('articles', 1, 5);
$request = array(
'filter' => array(
'user' => array(
'value' => $user1->uid,
),
),
);
$result = $handler
->get('', $request);
$this
->assertEqual(count($result), 2, 'List filtered by the "author" entity metadata wrapper property, which maps to the "uid" DB column name.');
// Filter by an entity metadata wrapper property that is different from the
// DB column name using the 'IN' operator.
$request = array(
'filter' => array(
'user' => array(
'value' => array(
$user1->uid,
$user2->uid,
),
'operator' => 'IN',
),
),
);
$result = $handler
->get('', $request);
$this
->assertEqual(count($result), 3, 'List filtered by the "author" entity metadata wrapper property, which maps to the "uid" DB column name, using the "IN" operator.');
// Filter by an entity metadata wrapper property that is different from the
// DB column name using the 'NOT IN' operator.
$request = array(
'filter' => array(
'user' => array(
'value' => array(
$user1->uid,
$user2->uid,
),
'operator' => 'NOT IN',
),
),
);
$result = $handler
->get('', $request);
$this
->assertEqual(count($result), 0, 'List filtered by the "author" entity metadata wrapper property, which maps to the "uid" DB column name, using the "NOT IN" operator.');
// Filter by an entity metadata wrapper property that is different from the
// DB column name using the 'NOT IN' operator.
$request = array(
'filter' => array(
'user' => array(
'value' => array(
$user2->uid,
),
'operator' => 'NOT IN',
),
),
);
$result = $handler
->get('', $request);
$this
->assertEqual(count($result), 2, 'List filtered by the "author" entity metadata wrapper property, which maps to the "uid" DB column name, using the "NOT IN" operator.');
// Filter by an entity metadata wrapper property that is different from the
// DB column name using the 'NOT IN' operator.
$request = array(
'filter' => array(
'user' => array(
'value' => array(),
'operator' => 'NOT IN',
),
),
);
$result = $handler
->get('', $request);
$this
->assertEqual(count($result), 3, 'List filtered by the "author" entity metadata wrapper property, which maps to the "uid" DB column name, using the "NOT IN" operator.');
// Filter by an entity metadata wrapper property that is different from the
// DB column name using the 'IN' operator.
$request = array(
'filter' => array(
'user' => array(
'value' => array(),
'operator' => 'IN',
),
),
);
$result = $handler
->get('', $request);
$this
->assertEqual(count($result), 0, 'List filtered by the "author" entity metadata wrapper property, which maps to the "uid" DB column name, using the "IN" operator.');
$request = array(
'filter' => array(
'static' => array(
'value' => 0,
),
),
);
try {
$handler
->get('', $request);
$this
->fail('Exception not thrown for invalid filter.');
} catch (\RestfulBadRequestException $e) {
$this
->pass('Exception thrown for invalid filter.');
}
}