diff options
Diffstat (limited to 'modules/simpletest/tests')
-rw-r--r-- | modules/simpletest/tests/database_test.module | 90 | ||||
-rw-r--r-- | modules/simpletest/tests/database_test.test | 119 |
2 files changed, 209 insertions, 0 deletions
diff --git a/modules/simpletest/tests/database_test.module b/modules/simpletest/tests/database_test.module index 573075078..2c1d22597 100644 --- a/modules/simpletest/tests/database_test.module +++ b/modules/simpletest/tests/database_test.module @@ -48,6 +48,19 @@ function database_test_menu() { 'access callback' => TRUE, 'page callback' => 'database_test_db_query_temporary', ); + $items['database_test/pager_query_even'] = array( + 'access callback' => TRUE, + 'page callback' => 'database_test_even_pager_query', + ); + $items['database_test/pager_query_odd'] = array( + 'access callback' => TRUE, + 'page callback' => 'database_test_odd_pager_query', + ); + $items['database_test/tablesort'] = array( + 'access callback' => TRUE, + 'page callback' => 'database_test_tablesort', + ); + return $items; } @@ -66,3 +79,80 @@ function database_test_db_query_temporary() { )); exit; } + +/** + * Run a pager query and return the results. + * + * This function does care about the page GET parameter, as set by the + * simpletest HTTP call. + */ +function database_test_even_pager_query($limit) { + + $query = db_select('test', 't'); + $query + ->fields('t', array('name')) + ->orderBy('age'); + + // This should result in 2 pages of results. + $query = $query->extend('PagerDefault')->limit($limit); + + $names = $query->execute()->fetchCol(); + + drupal_json(array( + 'names' => $names, + )); + exit; +} + +/** + * Run a pager query and return the results. + * + * This function does care about the page GET parameter, as set by the + * simpletest HTTP call. + */ +function database_test_odd_pager_query($limit) { + + $query = db_select('test_task', 't'); + $query + ->fields('t', array('task')) + ->orderBy('pid'); + + // This should result in 4 pages of results. + $query = $query->extend('PagerDefault')->limit($limit); + + $names = $query->execute()->fetchCol(); + + drupal_json(array( + 'names' => $names, + )); + exit; +} + +/** + * Run a tablesort query and return the results. + * + * This function does care about the page GET parameter, as set by the + * simpletest HTTP call. + */ +function database_test_tablesort() { + $header = array( + 'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'), + 'pid' => array('data' => t('Person ID'), 'field' => 'pid'), + 'task' => array('data' => t('Task'), 'field' => 'task'), + 'priority' => array('data' => t('Priority'), 'field' => 'priority', ), + ); + + $query = db_select('test_task', 't'); + $query + ->fields('t', array('tid', 'pid', 'task', 'priority')); + + $query = $query->extend('TableSort')->setHeader($header); + + // We need all the results at once to check the sort. + $tasks = $query->execute()->fetchAll(); + + drupal_json(array( + 'tasks' => $tasks, + )); + exit; +}
\ No newline at end of file diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index faaeb01ab..09d209bc4 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -1530,6 +1530,125 @@ class DatabaseSelectComplexTestCase extends DatabaseTestCase { } } +class DatabaseSelectPagerDefaultTestCase extends DatabaseTestCase { + + function getInfo() { + return array( + 'name' => t('Pager query tests'), + 'description' => t('Test the pager query extender.'), + 'group' => t('Database'), + ); + } + + /** + * Confirm that a pager query returns the correct results. + * + * Note that we have to make an HTTP request to a test page handler + * because the pager depends on GET parameters. + */ + function testEvenPagerQuery() { + // To keep the test from being too brittle, we determine up front + // what the page count should be dynamically, and pass the control + // information forward to the actual query on the other side of the + // HTTP request. + $limit = 2; + $count = db_query("SELECT COUNT(*) FROM {test}")->fetchField(); + + $correct_number = $limit; + $num_pages = floor($count / $limit); + + // If there is no remainder from rounding, subtract 1 since we index from 0. + if (!($num_pages * $limit < $count)) { + $num_pages--; + } + + for ($page = 0; $page <= $num_pages; ++$page) { + $this->drupalGet('database_test/pager_query_even/' . $limit, array('query' => array('page' => $page))); + $data = json_decode($this->drupalGetContent()); + + if ($page == $num_pages) { + $correct_number = $count - ($limit * $page); + } + + $this->assertEqual(count($data->names), $correct_number, t('Correct number of records returned by pager: @number', array('@number' => $correct_number))); + } + } + + /** + * Confirm that a pager query returns the correct results. + * + * Note that we have to make an HTTP request to a test page handler + * because the pager depends on GET parameters. + */ + function testOddPagerQuery() { + // To keep the test from being too brittle, we determine up front + // what the page count should be dynamically, and pass the control + // information forward to the actual query on the other side of the + // HTTP request. + $limit = 2; + $count = db_query("SELECT COUNT(*) FROM {test_task}")->fetchField(); + + $correct_number = $limit; + $num_pages = floor($count / $limit); + + // If there is no remainder from rounding, subtract 1 since we index from 0. + if (!($num_pages * $limit < $count)) { + $num_pages--; + } + + for ($page = 0; $page <= $num_pages; ++$page) { + $this->drupalGet('database_test/pager_query_odd/' . $limit, array('query' => array('page' => $page))); + $data = json_decode($this->drupalGetContent()); + + if ($page == $num_pages) { + $correct_number = $count - ($limit * $page); + } + + $this->assertEqual(count($data->names), $correct_number, t('Correct number of records returned by pager: @number', array('@number' => $correct_number))); + } + } +} + + +class DatabaseSelectTableSortDefaultTestCase extends DatabaseTestCase { + + function getInfo() { + return array( + 'name' => t('Tablesort query tests'), + 'description' => t('Test the tablesort query extender.'), + 'group' => t('Database'), + ); + } + + /** + * Confirm that a tablesort query returns the correct results. + * + * Note that we have to make an HTTP request to a test page handler + * because the pager depends on GET parameters. + */ + function testTableSortQuery() { + $sorts = array( + array('field' => t('Task ID'), 'sort' => 'desc', 'first' => 'perform at superbowl', 'last' => 'eat'), + array('field' => t('Task ID'), 'sort' => 'asc', 'first' => 'eat', 'last' => 'perform at superbowl'), + array('field' => t('Task'), 'sort' => 'asc', 'first' => 'code', 'last' => 'sleep'), + array('field' => t('Task'), 'sort' => 'desc', 'first' => 'sleep', 'last' => 'code'), + // more elements here + + ); + + foreach ($sorts as $sort) { + $this->drupalGet('database_test/tablesort/', array('query' => array('order' => $sort['field'], 'sort' => $sort['sort']))); + $data = json_decode($this->drupalGetContent()); + + $first = array_shift($data->tasks); + $last = array_pop($data->tasks); + + $this->assertEqual($first->task, $sort['first'], t('Items appear in the correct order.')); + $this->assertEqual($last->task, $sort['last'], t('Items appear in the correct order.')); + } + } +} + /** * Select tagging tests. * |