optimizedb.test in OptimizeDB 7
Tests for optimizedb.module.
File
optimizedb.testView source
<?php
/**
* @file
* Tests for optimizedb.module.
*/
/**
* Parent class for Optimizedb tests.
*/
class OptimizedbTestCase extends DrupalWebTestCase {
/**
* A user with permission the settings module.
*
* @var object
*/
protected $web_user;
public function setUp() {
parent::setUp(array(
'optimizedb',
));
$this->web_user = $this
->drupalCreateUser(array(
'administer optimizedb settings',
));
$this
->drupalLogin($this->web_user);
}
}
/**
* Test the module functions.
*/
class OptimizedbFunctionsTestCase extends OptimizedbTestCase {
public static function getInfo() {
return array(
'name' => 'Functions optimizedb.module.',
'description' => 'Test functions optimizedb.module.',
'group' => 'Optimizedb',
);
}
/**
* Sizes tables.
*/
public function testTablesList() {
variable_set('optimizedb_tables_size', 0);
// Function for output all database tables and update their total size.
_optimizedb_tables_list();
$this
->assertNotEqual(variable_get('optimizedb_tables_size'), 0);
}
/**
* Testing module admin page buttons.
*/
public function testButtonsExecutingCommands() {
for ($i = 1; $i <= 10; $i++) {
$this
->createCacheFormItem($i);
}
for ($i = 1; $i <= 5; $i++) {
$this
->createCacheFormItem($i + 10, REQUEST_TIME + 3600);
}
$this
->assertEqual($this
->countCacheFormRows(), 15);
$this
->drupalPost('admin/config/development/optimizedb', array(), t('Clear cache_form table'));
$this
->assertEqual($this
->countCacheFormRows(), 5);
$this
->drupalPost('admin/config/development/optimizedb', array(), t('Clear an entire table cache_form'));
$this
->assertEqual($this
->countCacheFormRows(), 0);
$list_tables = _optimizedb_tables_list();
$count_tables = count($list_tables);
$this
->drupalPost('admin/config/development/optimizedb', array(), t('Optimize tables'));
$this
->assertText(t('Optimized @count tables.', array(
'@count' => $count_tables,
)));
}
/**
* Test _optimizedb_clear_cache_form_table() function.
*/
public function testClearTable() {
$this
->assertEqual($this
->countCacheFormRows(), 0);
$time = REQUEST_TIME;
for ($cid = 1; $cid <= 5; $cid++) {
$cache_time = $time - $cid * 10;
$this
->createCacheFormItem($cid, $cache_time);
}
for ($cid = 6; $cid <= 10; $cid++) {
$cache_time = $time + $cid * 10;
$this
->createCacheFormItem($cid, $cache_time);
}
$this
->assertEqual($this
->countCacheFormRows(), 10);
$count_expire_all = (int) db_select('cache_form')
->condition('expire', $time, '<')
->countQuery()
->execute()
->fetchField();
$this
->assertEqual($count_expire_all, 5);
_optimizedb_clear_cache_form_table(OPTIMIZEDB_CLEAR_EXPIRED_CACHE);
$this
->assertEqual($this
->countCacheFormRows(), 5);
db_truncate('cache_form')
->execute();
for ($cid = 1; $cid <= 5; $cid++) {
$cache_time = $time - $cid * 10;
$this
->createCacheFormItem($cid, $cache_time);
}
_optimizedb_clear_cache_form_table(OPTIMIZEDB_CLEAR_EXPIRED_CACHE);
$this
->assertEqual($this
->countCacheFormRows(), 0);
}
/**
* Test clear cache_form table in optimizedb_cron() function.
*/
public function testCronClearTable() {
variable_set('optimizedb_clear_period', 100);
$this
->createCacheFormItem();
$this
->assertEqual($this
->countCacheFormRows(), 1);
$this
->cronRun();
$this
->assertEqual($this
->countCacheFormRows(), 0);
}
/**
* Testing, when not set period of time clean-up table "cache_form"
* using Cron.
*/
public function testCronClearTablePeriodTimeEmpty() {
variable_set('optimizedb_clear_period_time', 0);
variable_set('optimizedb_clear_period', 100);
$this
->createCacheFormItem();
$this
->assertEqual($this
->countCacheFormRows(), 1);
$this
->cronRun();
$this
->assertEqual($this
->countCacheFormRows(), 0);
}
/**
* Testing, when set period of time clean-up table "cache_form" using Cron.
*/
public function testCronClearTablePeriodTime() {
variable_set('optimizedb_clear_period', 100);
$current_hour = (int) date('H');
foreach (range(0, 23, OPTIMIZEDB_CLEAR_PERIOD_STEP) as $time) {
$period_time_start = $time - OPTIMIZEDB_CLEAR_PERIOD_STEP;
if ($current_hour >= $period_time_start && $current_hour <= $time) {
variable_set('optimizedb_clear_period_time', $time);
}
}
$this
->createCacheFormItem();
$this
->assertEqual($this
->countCacheFormRows(), 1);
$this
->cronRun();
$this
->assertEqual($this
->countCacheFormRows(), 0);
}
/**
* Test notify optimize in optimizedb_cron() function.
*/
public function testCronNotifyOptimize() {
variable_set('optimizedb_optimization_period', 1);
variable_set('optimizedb_last_optimization', REQUEST_TIME - 3600 * 24 * 2);
variable_set('optimizedb_notify_optimize', FALSE);
$this
->cronRun();
$this
->assertTrue(variable_get('optimizedb_notify_optimize', FALSE));
}
/**
* Testing, when user selected limit for delete entries from table cache_form.
*/
public function testCronClearTableWithLimit() {
variable_set('optimizedb_clear_type', 0);
variable_set('optimizedb_clear_period', 100);
variable_set('optimizedb_clear_limit', 1);
$time = REQUEST_TIME - 3600;
for ($i = 1; $i <= 10; ++$i) {
$this
->createCacheFormItem($i, $time);
}
for ($i = 11; $i <= 20; ++$i) {
$this
->createCacheFormItem($i, $time + 3600 * 2);
}
$this
->assertEqual($this
->countCacheFormRows(), 20);
$this
->drupalPost('admin/config/development/optimizedb', array(), t('Clear cache_form table'));
$this
->assertEqual($this
->countCacheFormRows(), 19);
}
/**
* Count rows in cache form table.
*/
protected function countCacheFormRows() {
return (int) db_select('cache_form')
->countQuery()
->execute()
->fetchField();
}
/**
* Create new item in table "cache_form".
*
* @param int $cid
* (optional) Cache ID.
* @param bool|int $cache_time
* (optional) Cache expire time.
*
* @return DatabaseStatementInterface|int
* ID new cache item.
*/
protected function createCacheFormItem($cid = 1, $cache_time = FALSE) {
if (!$cache_time) {
$cache_time = REQUEST_TIME - 3600;
}
return db_insert('cache_form')
->fields(array(
'cid' => $cid,
'expire' => $cache_time,
'created' => $cache_time,
))
->execute();
}
}
/**
* Test the optimizedb_hide_notification() function.
*/
class OptimizedbHideNotificationTestCase extends OptimizedbTestCase {
public static function getInfo() {
return array(
'name' => 'Load page hide notification.',
'description' => 'Test the show page hide notification.',
'group' => 'Optimizedb',
);
}
/**
* Display notification of the need to perform optimization.
*/
public function testHideNotification() {
variable_set('optimizedb_notify_optimize', FALSE);
$this
->drupalGet('admin/config/development/optimizedb/hide');
$this
->assertText(t('Alerts are not available.'));
variable_set('optimizedb_notify_optimize', TRUE);
$this
->drupalGet('admin/config/development/optimizedb/hide');
$this
->assertNoText(t('Alerts are not available.'));
$notify_optimize = variable_get('optimizedb_notify_optimize');
$this
->assertFalse($notify_optimize);
}
}
/**
* Testing the performance of operations on tables.
*
* @link admin/config/development/optimizedb/list_tables @endlink
*/
class OptimizedbListTablesOperationExecuteTestCase extends OptimizedbTestCase {
public static function getInfo() {
return array(
'name' => 'Performing operations on tables.',
'description' => 'Test the function sampling tables.',
'group' => 'Optimizedb',
);
}
/**
* Performing operations on tables.
*/
public function testListTablesOperationExecute() {
$this
->drupalPost('admin/config/development/optimizedb/list_tables', array(), t('Check tables'));
$this
->assertText(t('To execute, you must select at least one table from the list.'));
// Output all database tables.
$tables = _optimizedb_tables_list();
$table_name = key($tables);
$edit = array();
// Selected first table in list.
$edit['tables[' . $table_name . ']'] = $table_name;
$this
->drupalPost('admin/config/development/optimizedb/list_tables', $edit, t('Check tables'));
$this
->assertText(t('The operation completed successfully.'));
}
}
Classes
Name | Description |
---|---|
OptimizedbFunctionsTestCase | Test the module functions. |
OptimizedbHideNotificationTestCase | Test the optimizedb_hide_notification() function. |
OptimizedbListTablesOperationExecuteTestCase | Testing the performance of operations on tables. |
OptimizedbTestCase | Parent class for Optimizedb tests. |