RegressionTest.php 2.88 KB
Newer Older
1 2
<?php

3
namespace Drupal\KernelTests\Core\Database;
4

5 6
use Drupal\Core\Database\Database;

7
/**
8 9 10
 * Regression tests cases for the database layer.
 *
 * @group Database
11 12 13
 */
class RegressionTest extends DatabaseTestBase {

14 15 16 17 18
  /**
   * Modules to enable.
   *
   * @var array
   */
19
  public static $modules = ['node', 'user'];
20

21
  /**
22
   * Ensures that non-ASCII UTF-8 data is stored in the database properly.
23
   */
24
  public function testRegression_310447() {
25
    // That's a 255 character UTF-8 string.
26
    $job = str_repeat("é", 255);
27
    db_insert('test')
28
      ->fields([
29
        'name' => $this->randomMachineName(),
30
        'age' => 20,
31
        'job' => $job,
32
      ])->execute();
33

34
    $from_database = db_query('SELECT job FROM {test} WHERE job = :job', [':job' => $job])->fetchField();
35
    $this->assertSame($job, $from_database, 'The database handles UTF-8 characters cleanly.');
36 37 38
  }

  /**
39
   * Tests the db_table_exists() function.
40 41 42 43
   *
   * @group legacy
   *
   * @expectedDeprecation db_table_exists() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use $injected_database->schema()->tableExists($table) instead. See https://www.drupal.org/node/2947929.
44
   */
45
  public function testDBTableExists() {
46 47
    $this->assertSame(TRUE, db_table_exists('test'), 'Returns true for existent table.');
    $this->assertSame(FALSE, db_table_exists('nosuchtable'), 'Returns false for nonexistent table.');
48 49 50
  }

  /**
51
   * Tests the db_field_exists() function.
52
   */
53
  public function testDBFieldExists() {
54 55
    $this->assertSame(TRUE, db_field_exists('test', 'name'), 'Returns true for existent column.');
    $this->assertSame(FALSE, db_field_exists('test', 'nosuchcolumn'), 'Returns false for nonexistent column.');
56 57 58
  }

  /**
59
   * Tests the db_index_exists() function.
60
   */
61
  public function testDBIndexExists() {
62 63
    $this->assertSame(TRUE, db_index_exists('test', 'ages'), 'Returns true for existent index.');
    $this->assertSame(FALSE, db_index_exists('test', 'nosuchindex'), 'Returns false for nonexistent index.');
64
  }
65

66 67 68 69 70 71 72 73 74 75 76 77
  /**
   * Tests the db_set_active() function.
   *
   * @group legacy
   *
   * @expectedDeprecation db_set_active() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\Database::setActiveConnection() instead. See https://www.drupal.org/node/2944084.
   */
  public function testDBIsActive() {
    $get_active_db = Database::getConnection()->getKey();
    $this->assert(db_set_active($get_active_db), 'Database connection is active');
  }

78 79 80 81 82 83 84 85 86 87 88
  /**
   * Tests the db_drop_table() function.
   *
   * @group legacy
   *
   * @expectedDeprecation db_drop_table() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\Database::getConnection()->schema()->dropTable() instead. See https://www.drupal.org/node/2987737
   */
  public function testDbDropTable() {
    $this->assertFalse(db_drop_table('temp_test_table'));
  }

89
}