DatabaseLegacyTest.php 26.8 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\KernelTests\Core\Database;

5
use Drupal\Core\Database\Query\Condition;
6
use Drupal\Core\Database\Query\Delete;
7
use Drupal\Core\Database\Query\Insert;
8
use Drupal\Core\Database\Query\Merge;
9
use Drupal\Core\Database\Query\Select;
10
use Drupal\Core\Database\Query\Truncate;
11
use Drupal\Core\Database\Query\Update;
12
use Drupal\Core\Database\StatementInterface;
13
use Drupal\Core\Database\Transaction;
14
use Drupal\Core\Database\Database;
15
use Drupal\Core\Database\DatabaseException;
16

17 18 19 20 21 22 23
/**
 * Deprecation tests cases for the database layer.
 *
 * @group legacy
 */
class DatabaseLegacyTest extends DatabaseTestBase {

24 25 26 27 28 29 30
  /**
   * The modules to enable.
   *
   * @var array
   */
  public static $modules = ['database_test', 'system'];

31 32 33
  /**
   * Tests deprecation of the db_and() function.
   *
34
   * @expectedDeprecation db_and() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Create a \Drupal\Core\Database\Query\Condition object, specifying an AND conjunction: new Condition('AND'), instead. See https://www.drupal.org/node/2993033
35 36 37 38 39 40 41 42
   */
  public function testDbAnd() {
    $this->assertInstanceOf(Condition::class, db_and());
  }

  /**
   * Tests deprecation of the db_condition() function.
   *
43
   * @expectedDeprecation db_condition() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Create a \Drupal\Core\Database\Query\Condition object, specifying the desired conjunction: new Condition($conjunction), instead. See https://www.drupal.org/node/2993033
44 45 46 47 48 49 50 51
   */
  public function testDbCondition() {
    $this->assertInstanceOf(Condition::class, db_condition('AND'));
  }

  /**
   * Tests deprecation of the db_or() function.
   *
52
   * @expectedDeprecation db_or() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Create a \Drupal\Core\Database\Query\Condition object, specifying an OR conjunction: new Condition('OR'), instead. See https://www.drupal.org/node/2993033
53 54 55 56 57 58 59 60
   */
  public function testDbOr() {
    $this->assertInstanceOf(Condition::class, db_or());
  }

  /**
   * Tests deprecation of the db_xor() function.
   *
61
   * @expectedDeprecation db_xor() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Create a \Drupal\Core\Database\Query\Condition object, specifying a XOR conjunction: new Condition('XOR'), instead. See https://www.drupal.org/node/2993033
62 63 64 65 66
   */
  public function testDbXor() {
    $this->assertInstanceOf(Condition::class, db_xor());
  }

67 68 69
  /**
   * Tests the db_table_exists() function.
   *
70
   * @expectedDeprecation db_table_exists() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Use $injected_database->schema()->tableExists($table) instead. See https://www.drupal.org/node/2993033
71 72 73 74 75
   */
  public function testDbTableExists() {
    $this->assertTrue(db_table_exists('test'));
  }

76 77 78
  /**
   * Tests the db_find_tables() function.
   *
79
   * @expectedDeprecation db_find_tables() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Use $injected_database->schema()->findTables($table_expression) instead. See https://www.drupal.org/node/2993033
80 81 82 83 84 85 86 87 88
   */
  public function testDbFindTables() {
    $expected = [
      'test_people' => 'test_people',
      'test_people_copy' => 'test_people_copy',
    ];
    $this->assertEquals($expected, db_find_tables('test_people%'));
  }

89 90 91
  /**
   * Tests the db_set_active() function.
   *
92
   * @expectedDeprecation db_set_active() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Use \Drupal\Core\Database\Database::setActiveConnection() instead. See https://www.drupal.org/node/2993033
93 94 95 96 97 98 99 100 101
   */
  public function testDbSetActive() {
    $get_active_db = $this->connection->getKey();
    $this->assert(db_set_active($get_active_db), 'Database connection is active');
  }

  /**
   * Tests the db_drop_table() function.
   *
102
   * @expectedDeprecation db_drop_table() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Use \Drupal\Core\Database\Database::getConnection()->schema()->dropTable() instead. See https://www.drupal.org/node/2993033
103 104 105 106 107
   */
  public function testDbDropTable() {
    $this->assertFalse(db_drop_table('temp_test_table'));
  }

108 109 110
  /**
   * Tests deprecation of the db_next_id() function.
   *
111
   * @expectedDeprecation db_next_id() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call nextId() on it. For example, $injected_database->nextId($existing_id). See https://www.drupal.org/node/2993033
112 113 114 115 116 117
   */
  public function testDbNextId() {
    $this->installSchema('system', 'sequences');
    $this->assertEquals(1001, db_next_id(1000));
  }

118 119 120
  /**
   * Tests the db_change_field() function is deprecated.
   *
121
   * @expectedDeprecation db_change_field() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call changeField() on it. For example, $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new). See https://www.drupal.org/node/2993033
122 123 124 125 126 127 128 129 130 131 132 133 134
   */
  public function testDbChangeField() {
    $spec = [
      'description' => "A new person's name",
      'type' => 'varchar_ascii',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
      'binary' => TRUE,
    ];
    db_change_field('test', 'name', 'nosuchcolumn', $spec);
  }

135 136 137
  /**
   * Tests deprecation of the db_field_set_default() function.
   *
138
   * @expectedDeprecation db_field_set_default() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call changeField() on it, passing a full field specification. For example, $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new). See https://www.drupal.org/node/2993033
139 140 141 142 143 144 145 146 147
   * @expectedDeprecation fieldSetDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035
   */
  public function testDbFieldSetDefault() {
    db_field_set_default('test', 'job', 'baz');
  }

  /**
   * Tests deprecation of the db_field_set_no_default() function.
   *
148
   * @expectedDeprecation db_field_set_no_default() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call changeField() on it, passing a full field specification. For example, $injected_database->schema()->changeField($table, $field, $field_new, $spec, $keys_new). See https://www.drupal.org/node/2993033
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
   * @expectedDeprecation fieldSetNoDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035
   */
  public function testDbFieldSetNoDefault() {
    db_field_set_no_default('test_null', 'age');
  }

  /**
   * Tests Schema::fieldSetDefault and Schema::fieldSetNoDefault.
   *
   * @expectedDeprecation fieldSetDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035
   * @expectedDeprecation fieldSetNoDefault() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Instead, call ::changeField() passing a full field specification. See https://www.drupal.org/node/2999035
   */
  public function testSchemaFieldDefaultChange() {
    // Create a table.
    $table_specification = [
      'description' => 'Schema table description.',
      'fields' => [
        'id'  => [
          'type' => 'int',
          'default' => NULL,
        ],
        'test_field'  => [
          'type' => 'int',
          'not null' => TRUE,
          'description' => 'Test field',
        ],
      ],
    ];
    $this->connection->schema()->createTable('test_table', $table_specification);

    // An insert without a value for the column 'test_field' should fail.
    try {
      $this->connection->insert('test_table')->fields(['id' => 1])->execute();
      $this->fail('Expected DatabaseException, none was thrown.');
    }
    catch (DatabaseException $e) {
      $this->assertEquals(0, $this->connection->select('test_table')->countQuery()->execute()->fetchField());
    }

    // Add a default value to the column.
    $this->connection->schema()->fieldSetDefault('test_table', 'test_field', 0);

    // The insert should now succeed.
    $this->connection->insert('test_table')->fields(['id' => 1])->execute();
    $this->assertEquals(1, $this->connection->select('test_table')->countQuery()->execute()->fetchField());

    // Remove the default.
    $this->connection->schema()->fieldSetNoDefault('test_table', 'test_field');

    // The insert should fail again.
    try {
      $this->connection->insert('test_table')->fields(['id' => 2])->execute();
      $this->fail('Expected DatabaseException, none was thrown.');
    }
    catch (DatabaseException $e) {
      $this->assertEquals(1, $this->connection->select('test_table')->countQuery()->execute()->fetchField());
    }
  }

208 209 210
  /**
   * Tests deprecation of the db_transaction() function.
   *
211
   * @expectedDeprecation db_transaction is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call startTransaction() on it. For example, $injected_database->startTransaction($name). See https://www.drupal.org/node/2993033
212 213 214 215 216
   */
  public function testDbTransaction() {
    $this->assertInstanceOf(Transaction::class, db_transaction());
  }

217 218 219
  /**
   * Tests the db_close() function.
   *
220
   * @expectedDeprecation db_close() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Use \Drupal\Core\Database\Database::closeConnection() instead. See https://www.drupal.org/node/2993033
221 222 223 224 225 226 227
   */
  public function testDbClose() {
    $this->assertTrue(Database::isActiveConnection(), 'Database connection is active');
    db_close();
    $this->assertFalse(Database::isActiveConnection(), 'Database connection is not active');
  }

228 229 230
  /**
   * Tests deprecation of the db_add_field() function.
   *
231
   * @expectedDeprecation db_add_field() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call addField() on it. For example, $injected_database->schema()->addField($table, $field, $spec, $keys_new). See https://www.drupal.org/node/2993033
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
   */
  public function testDbAddField() {
    $this->assertFalse($this->connection->schema()->fieldExists('test', 'anint'));
    db_add_field('test', 'anint', [
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
      'description' => 'Added int column.',
    ]);
    $this->assertTrue($this->connection->schema()->fieldExists('test', 'anint'));
  }

  /**
   * Tests deprecation of the db_drop_field() function.
   *
247
   * @expectedDeprecation db_drop_field() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call dropField() on it. For example, $injected_database->schema()->dropField($table, $field). See https://www.drupal.org/node/2993033
248 249 250 251 252 253 254
   */
  public function testDbDropField() {
    $this->assertTrue($this->connection->schema()->fieldExists('test', 'age'));
    $this->assertTrue(db_drop_field('test', 'age'));
    $this->assertFalse($this->connection->schema()->fieldExists('test', 'age'));
  }

255 256 257
  /**
   * Tests deprecation of the db_field_exists() function.
   *
258
   * @expectedDeprecation db_field_exists() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call fieldExists() on it. For example, $injected_database->schema()->fieldExists($table, $field). See https://www.drupal.org/node/2993033
259 260 261 262 263
   */
  public function testDbFieldExists() {
    $this->assertTrue(db_field_exists('test', 'age'));
  }

264 265 266
  /**
   * Tests deprecation of the db_field_names() function.
   *
267
   * @expectedDeprecation db_field_names() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call fieldNames() on it. For example, $injected_database->schema()->fieldNames($fields). See https://www.drupal.org/node/2993033
268 269 270 271 272
   */
  public function testDbFieldNames() {
    $this->assertSame(['test_field'], db_field_names(['test_field']));
  }

273 274 275
  /**
   * Tests deprecation of the db_create_table() function.
   *
276
   * @expectedDeprecation db_create_table() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call createTable() on it. For example, $injected_database->schema()->createTable($name, $table). See https://www.drupal.org/node/2993033
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
   */
  public function testDbCreateTable() {
    $name = 'test_create_table';
    $table = [
      'fields' => [
        'id' => [
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ],
      ],
      'primary key' => ['id'],
    ];
    db_create_table($name, $table);
    $this->assertTrue($this->connection->schema()->tableExists($name));
  }

294 295 296
  /**
   * Tests deprecation of the db_merge() function.
   *
297
   * @expectedDeprecation db_merge() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call merge() on it. For example, $injected_database->merge($table, $options). See https://www.drupal.org/node/2993033
298 299
   */
  public function testDbMerge() {
300
    $this->assertInstanceOf(Merge::class, db_merge('test'));
301 302
  }

303 304 305
  /**
   * Tests deprecation of the db_driver() function.
   *
306
   * @expectedDeprecation db_driver() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call driver() on it. For example, $injected_database->driver($string). See https://www.drupal.org/node/2993033
307 308 309 310 311 312 313 314
   */
  public function testDbDriver() {
    $this->assertNotNull(db_driver());
  }

  /**
   * Tests deprecation of the db_escape_field() function.
   *
315
   * @expectedDeprecation db_escape_field() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call escapeField() on it. For example, $injected_database->escapeField($field). See https://www.drupal.org/node/2993033
316 317 318 319 320
   */
  public function testDbEscapeField() {
    $this->assertNotNull(db_escape_field('test'));
  }

321 322 323
  /**
   * Tests deprecation of the db_like() function.
   *
324
   * @expectedDeprecation db_like() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call escapeLike() on it. For example, $injected_database->escapeLike($string). See https://www.drupal.org/node/2993033
325 326 327 328 329
   */
  public function testDbLike() {
    $this->assertSame('test\%', db_like('test%'));
  }

330 331 332
  /**
   * Tests deprecation of the db_escape_table() function.
   *
333
   * @expectedDeprecation db_escape_table() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call escapeTable() on it. For example, $injected_database->escapeTable($table). See https://www.drupal.org/node/2993033
334 335 336 337 338 339 340 341
   */
  public function testDbEscapeTable() {
    $this->assertNotNull(db_escape_table('test'));
  }

  /**
   * Tests deprecation of the db_rename_table() function.
   *
342
   * @expectedDeprecation db_rename_table() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call renameTable() on it. For example, $injected_database->schema()->renameTable($table, $new_name). See https://www.drupal.org/node/2993033
343 344 345
   */
  public function testDbRenameTable() {
    $this->assertTrue($this->connection->schema()->tableExists('test'));
346
    db_rename_table('test', 'test_rename');
347 348 349 350 351 352
    $this->assertTrue($this->connection->schema()->tableExists('test_rename'));
  }

  /**
   * Tests deprecation of the db_drop_index() function.
   *
353
   * @expectedDeprecation db_drop_index() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call dropIndex() on it. For example, $injected_database->schema()->dropIndex($table, $name). See https://www.drupal.org/node/2993033
354 355 356 357 358
   */
  public function testDbDropIndex() {
    $this->assertFalse(db_drop_index('test', 'no_such_index'));
  }

359 360 361
  /**
   * Tests deprecation of the db_index_exists() function.
   *
362
   * @expectedDeprecation db_index_exists() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call indexExists() on it. For example, $injected_database->schema()->indexExists($table, $name). See https://www.drupal.org/node/2993033
363 364 365 366 367
   */
  public function testDbIndexExists() {
    $this->assertFalse(db_index_exists('test', 'no_such_index'));
  }

368 369 370
  /**
   * Tests deprecation of the db_drop_unique_key() function.
   *
371
   * @expectedDeprecation db_drop_unique_key() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call dropUniqueKey() on it. For example, $injected_database->schema()->dropUniqueKey($table, $name). See https://www.drupal.org/node/2993033
372 373 374 375 376 377 378 379
   */
  public function testDbDropUniqueKey() {
    $this->assertTrue(db_drop_unique_key('test', 'name'));
  }

  /**
   * Tests deprecation of the db_add_unique_key() function.
   *
380
   * @expectedDeprecation db_add_unique_key() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call addUniqueKey() on it. For example, $injected_database->schema()->addUniqueKey($table, $name, $fields). See https://www.drupal.org/node/2993033
381 382 383 384 385 386 387 388
   */
  public function testDbAddUniqueKey() {
    db_add_unique_key('test', 'age', ['age']);
  }

  /**
   * Tests deprecation of the db_drop_primary_key() function.
   *
389
   * @expectedDeprecation db_drop_primary_key() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call dropPrimaryKey() on it. For example, $injected_database->schema()->dropPrimaryKey($table). See https://www.drupal.org/node/2993033
390 391 392 393 394 395 396 397
   */
  public function testDbDropPrimaryKey() {
    $this->assertTrue(db_drop_primary_key('test_people'));
  }

  /**
   * Tests deprecation of the db_add_primary_key() function.
   *
398
   * @expectedDeprecation db_add_primary_key() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call addPrimaryKey() on it. For example, $injected_database->schema()->addPrimaryKey($table, $fields). See https://www.drupal.org/node/2993033
399 400 401 402 403 404
   */
  public function testDbAddPrimaryKey() {
    $this->connection->schema()->dropPrimaryKey('test_people');
    db_add_primary_key('test_people', ['job']);
  }

405 406 407
  /**
   * Tests the db_update() function.
   *
408
   * @expectedDeprecation db_update() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call call update() on it. For example, $injected_database->update($table, $options). See https://www.drupal.org/node/2993033
409 410 411 412 413
   */
  public function testDbUpdate() {
    $this->assertInstanceOf(Update::class, db_update('test'));
  }

414 415 416 417 418 419 420 421 422
  /**
   * Tests the db_query() function.
   *
   * @expectedDeprecation db_query() is deprecated in drupal:8.0.0. It will be removed before drupal:9.0.0. Instead, get a database connection injected into your service from the container and call query() on it. For example, $injected_database->query($query, $args, $options). See https://www.drupal.org/node/2993033
   */
  public function testDbQuery() {
    $this->assertInstanceOf(StatementInterface::class, db_query('SELECT name FROM {test} WHERE name = :name', [':name' => "John"]));
  }

423 424 425
  /**
   * Tests deprecation of the db_delete() function.
   *
426
   * @expectedDeprecation db_delete is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call delete() on it. For example, $injected_database->delete($table, $options). See https://www.drupal.org/node/2993033
427 428 429 430 431
   */
  public function testDbDelete() {
    $this->assertInstanceOf(Delete::class, db_delete('test'));
  }

432 433 434
  /**
   * Tests deprecation of the db_truncate() function.
   *
435
   * @expectedDeprecation db_truncate() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call truncate() on it. For example, $injected_database->truncate($table, $options). See https://www.drupal.org/node/2993033
436 437 438 439 440
   */
  public function testDbTruncate() {
    $this->assertInstanceOf(Truncate::class, db_truncate('test'));
  }

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  /**
   * Tests deprecation of the $options 'target' key in Connection::query.
   *
   * @expectedDeprecation Passing a 'target' key to \Drupal\Core\Database\Connection::query $options argument is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, use \Drupal\Core\Database\Database::getConnection($target)->query(). See https://www.drupal.org/node/2993033.
   */
  public function testDbOptionsTarget() {
    $this->assertNotNull($this->connection->query('SELECT * FROM {test}', [], ['target' => 'bar']));
  }

  /**
   * Tests deprecation of the $options 'target' key in Select.
   *
   * @expectedDeprecation Passing a 'target' key to \Drupal\Core\Database\Connection::query $options argument is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, use \Drupal\Core\Database\Database::getConnection($target)->query(). See https://www.drupal.org/node/2993033.
   */
  public function testDbOptionsTargetInSelect() {
    $this->assertNotNull($this->connection->select('test', 't', ['target' => 'bar'])->fields('t')->execute());
  }

459 460 461
  /**
   * Tests deprecation of the db_query_temporary() function.
   *
462
   * @expectedDeprecation db_query_temporary() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call queryTemporary() on it. For example, $injected_database->queryTemporary($query, $args, $options). See https://www.drupal.org/node/2993033
463 464 465 466 467 468 469 470
   */
  public function testDbQueryTemporary() {
    $expected = $this->connection->select('test')->countQuery()->execute()->fetchField();
    $name = db_query_temporary('SELECT name FROM {test}');
    $count = $this->connection->select($name)->countQuery()->execute()->fetchField();
    $this->assertSame($expected, $count);
  }

471 472 473
  /**
   * Tests deprecation of the db_query_range() function.
   *
474
   * @expectedDeprecation db_query_range() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call queryRange() on it. For example, $injected_database->queryRange($query, $from, $count, $args, $options). See https://www.drupal.org/node/2993033
475 476 477 478 479 480
   */
  public function testDbQueryRange() {
    $count = count(db_query_range('SELECT name FROM {test}', 1, 3)->fetchAll());
    $this->assertSame(3, $count);
  }

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  /**
   * Tests deprecation of the db_add_index() function.
   *
   * @expectedDeprecation db_add_index() is deprecated in Drupal 8.0.x and will be removed in Drupal 9.0.0. Instead, get a database connection injected into your service from the container, get its schema driver, and call addIndex() on it. For example, $injected_database->schema()->addIndex($table, $name, $fields, $spec). See https://www.drupal.org/node/2993033
   */
  public function testDbAddIndex() {
    $table_specification = [
      'fields' => [
        'age' => [
          'description' => "The person's age",
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ],
      ],
    ];
    $this->assertNull(db_add_index('test', 'test', ['age'], $table_specification));
  }

501 502 503
  /**
   * Tests the db_insert() function.
   *
504
   * @expectedDeprecation db_insert() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call insert() on it. For example, $injected_database->insert($table, $options). See https://www.drupal.org/node/2993033
505 506 507 508 509
   */
  public function testDbInsert() {
    $this->assertInstanceOf(Insert::class, db_insert('test'));
  }

510 511 512
  /**
   * Tests the db_select() function.
   *
513
   * @expectedDeprecation db_select() is deprecated in drupal:8.0.0. It will be removed from drupal:9.0.0. Instead, get a database connection injected into your service from the container and call select() on it. For example, $injected_database->db_select($table, $alias, $options). See https://www.drupal.org/node/2993033
514 515 516 517 518
   */
  public function testDbSelect() {
    $this->assertInstanceOf(Select::class, db_select('test'));
  }

519 520 521
  /**
   * Tests the db_ignore_replica() function.
   *
522
   * @expectedDeprecation db_ignore_replica() is deprecated in drupal:8.7.0. It will be removed from drupal:9.0.0. Use \Drupal\Core\Database\ReplicaKillSwitch::trigger() instead. See https://www.drupal.org/node/2997500
523 524 525 526 527 528 529 530 531 532
   */
  public function testDbIgnoreReplica() {
    $connection = Database::getConnectionInfo('default');
    Database::addConnectionInfo('default', 'replica', $connection['default']);
    db_ignore_replica();
    /** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */
    $session = \Drupal::service('session');
    $this->assertTrue($session->has('ignore_replica_server'));
  }

533 534 535 536 537 538 539 540 541 542 543 544 545
  /**
   * Tests the _db_get_target() function.
   *
   * @expectedDeprecation _db_get_target() is deprecated in drupal:8.8.0. Will be removed before drupal:9.0.0. See https://www.drupal.org/node/2993033
   */
  public function testDbGetTarget() {
    $op1 = $op2 = ['target' => 'replica'];
    $this->assertEquals('replica', _db_get_target($op1));
    $this->assertEquals('default', _db_get_target($op2, FALSE));
    $this->assertEmpty($op1);
    $this->assertEmpty($op2);
  }

546
}