views_handler_filter_numeric.test in Views (for Drupal 7) 7.3
Definition of ViewsHandlerFilterNumericTest.
File
tests/handlers/views_handler_filter_numeric.testView source
<?php
/**
* @file
* Definition of ViewsHandlerFilterNumericTest.
*/
/**
* Tests the numeric filter handler.
*/
class ViewsHandlerFilterNumericTest extends ViewsSqlTest {
var $column_map = array();
public static function getInfo() {
return array(
'name' => 'Filter: Numeric',
'description' => 'Tests the numeric filter handler',
'group' => 'Views Handlers',
);
}
/**
* {@inheritdoc}
*/
protected function dataSet() {
$data_set = parent::dataSet();
$data_set[] = array(
'name' => 'Charles',
'age' => NULL,
'job' => 'Bassist',
'created' => gmmktime(6, 30, 10, 1, 1, 2001),
);
return $data_set;
}
/**
* {@inheritdoc}
*/
protected function schemaDefinition() {
$schema = parent::schemaDefinition();
$schema['views_test']['fields']['age']['not null'] = FALSE;
$schema['views_test']['indexes'] = array();
return $schema;
}
/**
* {@inheritdoc}
*/
public function setUp(array $modules = array()) {
parent::setUp($modules);
$this->column_map = array(
'views_test_name' => 'name',
'views_test_age' => 'age',
);
}
function viewsData() {
$data = parent::viewsData();
$data['views_test']['age']['filter']['allow empty'] = TRUE;
$data['views_test']['id']['filter']['allow empty'] = FALSE;
return $data;
}
public function testFilterNumericSimple() {
$view = $this
->getBasicView();
// Change the filtering.
$view->display['default']->handler
->override_option('filters', array(
'age' => array(
'id' => 'age',
'table' => 'views_test',
'field' => 'age',
'relationship' => 'none',
'operator' => '=',
'value' => array(
'value' => 28,
),
),
));
$this
->executeView($view);
$resultset = array(
array(
'name' => 'Ringo',
'age' => 28,
),
);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map);
}
public function testFilterNumericExposedGroupedSimple() {
$filters = $this
->getGroupedExposedFilters();
$view = $this
->getBasicPageView();
// Filter: Age, Operator: =, Value: 28
$filters['age']['group_info']['default_group'] = 1;
$view
->set_display('page_1');
$view->display['page_1']->handler
->override_option('filters', $filters);
$this
->executeView($view);
$resultset = array(
array(
'name' => 'Ringo',
'age' => 28,
),
);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map);
}
public function testFilterNumericBetween() {
$view = $this
->getBasicView();
// Change the filtering.
$view->display['default']->handler
->override_option('filters', array(
'age' => array(
'id' => 'age',
'table' => 'views_test',
'field' => 'age',
'relationship' => 'none',
'operator' => 'between',
'value' => array(
'min' => 26,
'max' => 29,
),
),
));
$this
->executeView($view);
$resultset = array(
array(
'name' => 'George',
'age' => 27,
),
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Paul',
'age' => 26,
),
);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map);
// Test not between.
$view
->delete();
$view = $this
->getBasicView();
// Change the filtering.
$view->display['default']->handler
->override_option('filters', array(
'age' => array(
'id' => 'age',
'table' => 'views_test',
'field' => 'age',
'relationship' => 'none',
'operator' => 'not between',
'value' => array(
'min' => 26,
'max' => 29,
),
),
));
$this
->executeView($view);
$resultset = array(
array(
'name' => 'John',
'age' => 25,
),
array(
'name' => 'Paul',
'age' => 26,
),
array(
'name' => 'Meredith',
'age' => 30,
),
);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map);
}
public function testFilterNumericExposedGroupedBetween() {
$filters = $this
->getGroupedExposedFilters();
$view = $this
->getBasicPageView();
// Filter: Age, Operator: between, Value: 26 and 29
$filters['age']['group_info']['default_group'] = 2;
$view
->set_display('page_1');
$view->display['page_1']->handler
->override_option('filters', $filters);
$this
->executeView($view);
$resultset = array(
array(
'name' => 'George',
'age' => 27,
),
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Paul',
'age' => 26,
),
);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map);
}
public function testFilterNumericExposedGroupedNotBetween() {
$filters = $this
->getGroupedExposedFilters();
$view = $this
->getBasicPageView();
// Filter: Age, Operator: not between, Value: 26 and 29
$filters['age']['group_info']['default_group'] = 3;
$view
->set_display('page_1');
$view->display['page_1']->handler
->override_option('filters', $filters);
$this
->executeView($view);
$resultset = array(
array(
'name' => 'John',
'age' => 25,
),
array(
'name' => 'Paul',
'age' => 26,
),
array(
'name' => 'Meredith',
'age' => 30,
),
);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map);
}
public function testFilterNumericEmpty() {
$view = $this
->getBasicView();
// Change the filtering.
$view->display['default']->handler
->override_option('filters', array(
'age' => array(
'id' => 'age',
'table' => 'views_test',
'field' => 'age',
'relationship' => 'none',
'operator' => 'empty',
),
));
$this
->executeView($view);
$resultset = array(
array(
'name' => 'Charles',
'age' => NULL,
),
);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map);
$view
->delete();
$view = $this
->getBasicView();
// Change the filtering.
$view->display['default']->handler
->override_option('filters', array(
'age' => array(
'id' => 'age',
'table' => 'views_test',
'field' => 'age',
'relationship' => 'none',
'operator' => 'not empty',
),
));
$this
->executeView($view);
$resultset = array(
array(
'name' => 'John',
'age' => 25,
),
array(
'name' => 'George',
'age' => 27,
),
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Paul',
'age' => 26,
),
array(
'name' => 'Meredith',
'age' => 30,
),
);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map);
}
public function testFilterNumericExposedGroupedEmpty() {
$filters = $this
->getGroupedExposedFilters();
$view = $this
->getBasicPageView();
// Filter: Age, Operator: empty, Value.
$filters['age']['group_info']['default_group'] = 4;
$view
->set_display('page_1');
$view->display['page_1']->handler
->override_option('filters', $filters);
$this
->executeView($view);
$resultset = array(
array(
'name' => 'Charles',
'age' => NULL,
),
);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map);
}
public function testFilterNumericExposedGroupedNotEmpty() {
$filters = $this
->getGroupedExposedFilters();
$view = $this
->getBasicPageView();
// Filter: Age, Operator: empty, Value.
$filters['age']['group_info']['default_group'] = 5;
$view
->set_display('page_1');
$view->display['page_1']->handler
->override_option('filters', $filters);
$this
->executeView($view);
$resultset = array(
array(
'name' => 'John',
'age' => 25,
),
array(
'name' => 'George',
'age' => 27,
),
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Paul',
'age' => 26,
),
array(
'name' => 'Meredith',
'age' => 30,
),
);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map);
}
/**
* Tests the limit operators functionality.
*/
public function testFilterNumericExposedLimitOperators() {
$filters = $this
->getGroupedExposedFilters();
$view = $this
->getBasicView();
$available_operators = array(
'<',
'>',
'between',
);
$filters['age']['expose'] += array(
'limit_operators' => TRUE,
'available_operators' => drupal_map_assoc($available_operators),
);
$view->display['default']->handler
->override_option('filters', $filters);
$this
->executeView($view);
$form = array();
$form_state = array();
$view->filter['age']
->operator_form($form, $form_state);
$operator = $form['operator'];
$this
->assertTrue(in_array($operator['#default_value'], $available_operators), 'Default value operator found in list of available operators.');
foreach ($available_operators as $available_operator) {
$this
->assertTrue($operator['#options'][$available_operator], format_string('@operator found in options', array(
'@operator' => $available_operator,
)));
}
}
/**
* Tests exposed numeric filter with exposed operator.
*/
public function testFilterNumericExposedOperator() {
$this
->applyFilterNumericExposedOperator('=', array(
'value' => '27',
), array(
array(
'name' => 'George',
'age' => 27,
),
));
$this
->applyFilterNumericExposedOperator('<', array(
'value' => '27',
), array(
array(
'name' => 'John',
'age' => 25,
),
array(
'name' => 'Paul',
'age' => 26,
),
));
$this
->applyFilterNumericExposedOperator('<=', array(
'value' => '27',
), array(
array(
'name' => 'John',
'age' => 25,
),
array(
'name' => 'George',
'age' => 27,
),
array(
'name' => 'Paul',
'age' => 26,
),
));
$this
->applyFilterNumericExposedOperator('!=', array(
'value' => '27',
), array(
array(
'name' => 'John',
'age' => 25,
),
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Paul',
'age' => 26,
),
array(
'name' => 'Meredith',
'age' => 30,
),
));
$this
->applyFilterNumericExposedOperator('>=', array(
'value' => '27',
), array(
array(
'name' => 'George',
'age' => 27,
),
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Meredith',
'age' => 30,
),
));
$this
->applyFilterNumericExposedOperator('>', array(
'value' => '27',
), array(
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Meredith',
'age' => 30,
),
));
$this
->applyFilterNumericExposedOperator('between', array(
'min' => '28',
'max' => '31',
), array(
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Meredith',
'age' => 30,
),
));
$this
->applyFilterNumericExposedOperator('not between', array(
'min' => '28',
'max' => '31',
), array(
array(
'name' => 'John',
'age' => 25,
),
array(
'name' => 'George',
'age' => 27,
),
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Paul',
'age' => 26,
),
));
$this
->applyFilterNumericExposedOperator('empty', array(), array(
array(
'name' => 'Charles',
'age' => NULL,
),
));
$this
->applyFilterNumericExposedOperator('not empty', array(), array(
array(
'name' => 'John',
'age' => 25,
),
array(
'name' => 'George',
'age' => 27,
),
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Paul',
'age' => 26,
),
array(
'name' => 'Meredith',
'age' => 30,
),
));
$this
->applyFilterNumericExposedOperator('regular_expression', array(
'value' => '^(0|[1-9][0-9]*)$',
), array(
array(
'name' => 'John',
'age' => 25,
),
array(
'name' => 'George',
'age' => 27,
),
array(
'name' => 'Ringo',
'age' => 28,
),
array(
'name' => 'Paul',
'age' => 26,
),
array(
'name' => 'Meredith',
'age' => 30,
),
));
$this
->applyFilterNumericExposedOperator('not_regular_expression', array(
'value' => '^(0|[1-9][0-9]*)$',
), array());
}
/**
* Tests exposed numeric filter with an individual exposed operator.
*
* @param string $operator
* Operator to test.
* @param array $value
* Filter value to use in exposed input. Keys might be 'value', 'min' or
* 'max'. If one of those keys doesn't exist, an empty string is used as the
* key's value.
* @param array $resultset
* The expected result set.
*/
protected function applyFilterNumericExposedOperator($operator, array $value, array $resultset) {
$exposed_input = array(
'age' => $value += array(
'value' => '',
'min' => '',
'max' => '',
),
'age_op' => $operator,
);
$filters = array(
'age' => array(
'id' => 'age',
'table' => 'views_test',
'field' => 'age',
'relationship' => 'none',
'exposed' => TRUE,
'expose' => array(
'operator' => 'age_op',
'label' => 'age',
'identifier' => 'age',
'use_operator' => TRUE,
),
),
);
$view = $this
->getBasicPageView();
$view
->set_display('page_1');
$view->display['page_1']->handler
->override_option('filters', $filters);
$view
->set_exposed_input($exposed_input);
$this
->executeView($view);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map, 'Identical result set for ' . $operator . ' with untouched values.');
$view
->destroy();
// Min, max and value fields are shown/hidden only via JS, so they might
// still be set from a previous operation. Assert that this doesn't change
// the expected result set.
$exposed_input['age'] += array(
'value' => '25',
'min' => '28',
'max' => '30',
);
$view = $this
->getBasicPageView();
$view
->set_display('page_1');
$view->display['page_1']->handler
->override_option('filters', $filters);
$view
->set_exposed_input($exposed_input);
$this
->executeView($view);
$this
->assertIdenticalResultset($view, $resultset, $this->column_map, 'Identical result set for ' . $operator . ' with leftover values from previous operation.');
}
public function testAllowEmpty() {
$view = $this
->getBasicView();
$view->display['default']->handler
->override_option('filters', array(
'id' => array(
'id' => 'id',
'table' => 'views_test',
'field' => 'id',
'relationship' => 'none',
),
'age' => array(
'id' => 'age',
'table' => 'views_test',
'field' => 'age',
'relationship' => 'none',
),
));
$view
->set_display('default');
$view
->init_handlers();
$id_operators = $view->filter['id']
->operators();
$age_operators = $view->filter['age']
->operators();
$this
->assertFalse(isset($id_operators['empty']));
$this
->assertFalse(isset($id_operators['not empty']));
$this
->assertTrue(isset($age_operators['empty']));
$this
->assertTrue(isset($age_operators['not empty']));
}
protected function getGroupedExposedFilters() {
$filters = array(
'age' => array(
'id' => 'age',
'table' => 'views_test',
'field' => 'age',
'relationship' => 'none',
'exposed' => TRUE,
'expose' => array(
'operator' => 'age_op',
'label' => 'age',
'identifier' => 'age',
),
'is_grouped' => TRUE,
'group_info' => array(
'label' => 'age',
'identifier' => 'age',
'default_group' => 'All',
'group_items' => array(
1 => array(
'title' => 'Age is 28',
'operator' => '=',
'value' => array(
'value' => 28,
),
),
2 => array(
'title' => 'Age is between 26 and 29',
'operator' => 'between',
'value' => array(
'min' => 26,
'max' => 29,
),
),
3 => array(
'title' => 'Age is not between 26 and 29',
'operator' => 'not between',
'value' => array(
'min' => 26,
'max' => 29,
),
),
4 => array(
'title' => 'Age is empty',
'operator' => 'empty',
),
5 => array(
'title' => 'Age is not empty',
'operator' => 'not empty',
),
),
),
),
);
return $filters;
}
}
Classes
Name | Description |
---|---|
ViewsHandlerFilterNumericTest | Tests the numeric filter handler. |