Commit a5a24f70 authored by Dries's avatar Dries

Issue #1839134 by Berdir, sun: Convert database tests to (Drupal)UnitTestBase.

parent 143994cc
......@@ -7,64 +7,26 @@
namespace Drupal\system\Tests\Database;
use Drupal\simpletest\WebTestBase;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Tests for databases.
* Base class for databases database tests.
*
* Because all database tests share the same test data, we can centralize that
* here.
*/
abstract class DatabaseTestBase extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('database_test');
abstract class DatabaseTestBase extends DrupalUnitTestBase {
function setUp() {
parent::setUp();
$schema['test'] = drupal_get_schema('test');
$schema['test_people'] = drupal_get_schema('test_people');
$schema['test_one_blob'] = drupal_get_schema('test_one_blob');
$schema['test_two_blobs'] = drupal_get_schema('test_two_blobs');
$schema['test_task'] = drupal_get_schema('test_task');
$this->installTables($schema);
$this->addSampleData();
}
/**
* Sets up several tables needed by a certain test.
*
* @param $schema
* An array of table definitions to install.
*/
function installTables($schema) {
// This ends up being a test for table drop and create, too, which is nice.
foreach ($schema as $name => $data) {
if (db_table_exists($name)) {
db_drop_table($name);
}
db_create_table($name, $data);
}
foreach ($schema as $name => $data) {
$this->assertTrue(db_table_exists($name), format_string('Table @name created successfully.', array('@name' => $name)));
}
$this->enableModules(array('database_test'));
self::addSampleData();
}
/**
* Sets up tables for NULL handling.
*/
function ensureSampleDataNull() {
$schema['test_null'] = drupal_get_schema('test_null');
$this->installTables($schema);
db_insert('test_null')
->fields(array('name', 'age'))
->values(array(
......@@ -84,11 +46,8 @@ function ensureSampleDataNull() {
/**
* Sets up our sample data.
*
* These are added using db_query(), since we're not trying to test the
* INSERT operations here, just populate.
*/
function addSampleData() {
static function addSampleData() {
// We need the IDs, so we can't use a multi-insert here.
$john = db_insert('test')
->fields(array(
......
<?php
/**
* @file
* Definition of Drupal\system\Tests\Database\DatabaseWebTestBase.
*/
namespace Drupal\system\Tests\Database;
use Drupal\simpletest\WebTestBase;
/**
* Base class for databases database tests.
*/
abstract class DatabaseWebTestBase extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('database_test');
function setUp() {
parent::setUp();
DatabaseTestBase::addSampleData();
}
}
......@@ -9,12 +9,12 @@
use Drupal\Core\Database\StatementEmpty;
use Drupal\Core\Database\StatementInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\simpletest\UnitTestBase;
/**
* Tests the empty pseudo-statement class.
*/
class EmptyStatementTest extends WebTestBase {
class EmptyStatementTest extends UnitTestBase {
public static function getInfo() {
return array(
'name' => 'Empty statement',
......
......@@ -7,12 +7,19 @@
namespace Drupal\system\Tests\Database;
use Drupal\simpletest\WebTestBase;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Checks the sequences API.
*/
class NextIdTest extends WebTestBase {
class NextIdTest extends DrupalUnitTestBase {
/**
* The modules to enable.
* @var array
*/
public static $modules = array('system');
public static function getInfo() {
return array(
'name' => 'Sequences API',
......@@ -21,6 +28,11 @@ public static function getInfo() {
);
}
public function setUp() {
parent::setUp();
$this->installSchema('system', 'sequences');
}
/**
* Tests that the sequences API works.
*/
......
......@@ -48,7 +48,7 @@ function testRegression_310447() {
* Tests the db_table_exists() function.
*/
function testDBTableExists() {
$this->assertIdentical(TRUE, db_table_exists('node'), 'Returns true for existent table.');
$this->assertIdentical(TRUE, db_table_exists('test'), 'Returns true for existent table.');
$this->assertIdentical(FALSE, db_table_exists('nosuchtable'), 'Returns false for nonexistent table.');
}
......@@ -56,15 +56,15 @@ function testDBTableExists() {
* Tests the db_field_exists() function.
*/
function testDBFieldExists() {
$this->assertIdentical(TRUE, db_field_exists('node', 'nid'), 'Returns true for existent column.');
$this->assertIdentical(FALSE, db_field_exists('node', 'nosuchcolumn'), 'Returns false for nonexistent column.');
$this->assertIdentical(TRUE, db_field_exists('test', 'name'), 'Returns true for existent column.');
$this->assertIdentical(FALSE, db_field_exists('test', 'nosuchcolumn'), 'Returns false for nonexistent column.');
}
/**
* Tests the db_index_exists() function.
*/
function testDBIndexExists() {
$this->assertIdentical(TRUE, db_index_exists('node', 'node_created'), 'Returns true for existent index.');
$this->assertIdentical(FALSE, db_index_exists('node', 'nosuchindex'), 'Returns false for nonexistent index.');
$this->assertIdentical(TRUE, db_index_exists('test', 'ages'), 'Returns true for existent index.');
$this->assertIdentical(FALSE, db_index_exists('test', 'nosuchindex'), 'Returns false for nonexistent index.');
}
}
......@@ -2,20 +2,20 @@
/**
* @file
* Definition of Drupal\system\Tests\Common\SchemaTest.
* Contains Drupal\system\Tests\Database\SchemaTest.
*/
namespace Drupal\system\Tests\Common;
namespace Drupal\system\Tests\Database;
use Drupal\Core\Database\Database;
use Drupal\simpletest\WebTestBase;
use Drupal\simpletest\UnitTestBase;
use Exception;
/**
* Tests the Schema API.
*/
class SchemaTest extends WebTestBase {
class SchemaTest extends UnitTestBase {
/**
* A global counter for table and field creation.
......
......@@ -337,8 +337,14 @@ function testJoinTwice() {
* Tests that we can join on a query.
*/
function testJoinSubquery() {
$acct = $this->drupalCreateUser();
$this->drupalLogin($acct);
$this->enableModules(array('system'), FALSE);
$this->installSchema('system', 'sequences');
$this->enableModules(array('field', 'user'));
$account = entity_create('user', array(
'name' => $this->randomName(),
'mail' => $this->randomName() . '@example.com',
));
$query = db_select('test_task', 'tt', array('target' => 'slave'));
$query->addExpression('tt.pid + 1', 'abc');
......@@ -349,7 +355,7 @@ function testJoinSubquery() {
$subquery->join('test_one_blob', 'tpb', 'tp.id = tpb.id');
$subquery->join('node', 'n', 'tp.id = n.nid');
$subquery->addTag('node_access');
$subquery->addMetaData('account', $acct);
$subquery->addMetaData('account', $account);
$subquery->addField('tp', 'id');
$subquery->condition('age', 5, '>');
$subquery->condition('age', 500, '<');
......
......@@ -10,7 +10,7 @@
/**
* Tests the pager query select extender.
*/
class SelectPagerDefaultTest extends DatabaseTestBase {
class SelectPagerDefaultTest extends DatabaseWebTestBase {
public static function getInfo() {
return array(
......
......@@ -10,7 +10,7 @@
/**
* Tests the tablesort query extender.
*/
class SelectTableSortDefaultTest extends DatabaseTestBase {
class SelectTableSortDefaultTest extends DatabaseWebTestBase {
public static function getInfo() {
return array(
......
......@@ -10,7 +10,7 @@
/**
* Tests temporary queries.
*/
class TemporaryQueryTest extends DatabaseTestBase {
class TemporaryQueryTest extends DatabaseWebTestBase {
/**
* Modules to enable.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment