Projet

Général

Profil

Révision 01dfd3b5

Ajouté par Assos Assos il y a plus de 3 ans

Udpate to 7.77

Voir les différences:

drupal7/modules/simpletest/tests/database_test.test
163 163
        'priority' => 3,
164 164
      ))
165 165
      ->execute();
166

  
167
    db_insert('virtual')
168
      ->fields(array(
169
        'function' => 'Function value 1',
170
      ))
171
      ->execute();
166 172
  }
167 173
}
168 174

  
......
3457 3463
      ->fetchField();
3458 3464
    $this->assertFalse($result, 'SQL injection attempt did not result in a row being inserted in the database table.');
3459 3465
  }
3460

  
3461 3466
}
3462 3467

  
3463 3468
/**
......
4033 4038
  protected $monitor;
4034 4039
  protected $originalCount;
4035 4040

  
4041
  protected $skipTest;
4042

  
4036 4043
  public static function getInfo() {
4037 4044
    return array(
4038 4045
      'name' => 'Connection unit tests',
......
4053 4060
    // @todo Make this test driver-agnostic, or find a proper way to skip it.
4054 4061
    // @see http://drupal.org/node/1273478
4055 4062
    $connection_info = Database::getConnectionInfo('default');
4056
    $this->skipTest = (bool) $connection_info['default']['driver'] != 'mysql';
4063
    $this->skipTest = (bool) ($connection_info['default']['driver'] != 'mysql');
4057 4064
    if ($this->skipTest) {
4058 4065
      // Insert an assertion to prevent Simpletest from interpreting the test
4059 4066
      // as failure.
......
4238 4245
    // Verify that we are back to the original connection count.
4239 4246
    $this->assertNoConnection($id);
4240 4247
  }
4248
}
4249

  
4250
/**
4251
 * Test reserved keyword handling (introduced for MySQL 8+)
4252
*/
4253
class DatabaseReservedKeywordTestCase extends DatabaseTestCase {
4254
  public static function getInfo() {
4255
    return array(
4256
      'name' => 'Reserved Keywords',
4257
      'description' => 'Test handling of reserved keywords.',
4258
      'group' => 'Database',
4259
    );
4260
  }
4261

  
4262
  function setUp() {
4263
    parent::setUp('database_test');
4264
  }
4265

  
4266
  public function testTableNameQuoting() {
4267
    // Test db_query with {table} pattern.
4268
    $record = db_query('SELECT * FROM {system} LIMIT 1')->fetchObject();
4269
    $this->assertTrue(isset($record->filename), 'Successfully queried the {system} table.');
4270

  
4271
    $connection = Database::getConnection()->getConnectionOptions();
4272
    if ($connection['driver'] === 'sqlite') {
4273
      // In SQLite simpletest's prefixed db tables exist in their own schema
4274
      // (e.g. simpletest124904.system), so we cannot test the schema.{table}
4275
      // syntax here as the table name will have the schema name prepended to it
4276
      // when prefixes are processed.
4277
      $this->assert(TRUE, 'Skipping schema.{system} test for SQLite.');
4278
    }
4279
    else {
4280
      $database = $connection['database'];
4281
      // Test db_query with schema.{table} pattern
4282
      db_query('SELECT * FROM ' . $database . '.{system} LIMIT 1')->fetchObject();
4283
      $this->assertTrue(isset($record->filename), 'Successfully queried the schema.{system} table.');
4284
    }
4285
  }
4286

  
4287
  public function testSelectReservedWordTableCount() {
4288
    $rows = db_select('virtual')
4289
      ->countQuery()
4290
      ->execute()
4291
      ->fetchField();
4292
    $this->assertEqual($rows, 1, 'Successful count query on a table with a reserved name.');
4293
  }
4294

  
4295
  public function testSelectReservedWordTableSpecificField() {
4296
    $record = db_select('virtual')
4297
      ->fields('virtual', array('function'))
4298
      ->execute()
4299
      ->fetchAssoc();
4300
    $this->assertEqual($record['function'], 'Function value 1', 'Successfully read a field from a table with a name and column which are reserved words.');
4301
  }
4302

  
4303
  public function testSelectReservedWordTableAllFields() {
4304
    $record = db_select('virtual')
4305
      ->fields('virtual')
4306
      ->execute()
4307
      ->fetchAssoc();
4308
    $this->assertEqual($record['function'], 'Function value 1', 'Successful all_fields query from a table with a name and column which are reserved words.');
4309
  }
4310

  
4311
  public function testSelectReservedWordAliasCount() {
4312
    $rows = db_select('test', 'character')
4313
      ->countQuery()
4314
      ->execute()
4315
      ->fetchField();
4316
    $this->assertEqual($rows, 4, 'Successful count query using an alias which is a reserved word.');
4317
  }
4241 4318

  
4319
  public function testSelectReservedWordAliasSpecificFields() {
4320
    $record = db_select('test', 'high_priority')
4321
      ->fields('high_priority', array('name'))
4322
      ->condition('age', 27)
4323
      ->execute()->fetchAssoc();
4324
    $this->assertEqual($record['name'], 'George', 'Successful query using an alias which is a reserved word.');
4325
  }
4326

  
4327
  public function testSelectReservedWordAliasAllFields() {
4328
    $record = db_select('test', 'high_priority')
4329
      ->fields('high_priority')
4330
      ->condition('age', 27)
4331
      ->execute()->fetchAssoc();
4332
    $this->assertEqual($record['name'], 'George', 'Successful all_fields query using an alias which is a reserved word.');
4333
  }
4334

  
4335
  public function testInsertReservedWordTable() {
4336
    $num_records_before = db_query('SELECT COUNT(*) FROM {virtual}')->fetchField();
4337
    db_insert('virtual')
4338
      ->fields(array(
4339
        'function' => 'Inserted function',
4340
      ))
4341
      ->execute();
4342
    $num_records_after = db_query('SELECT COUNT(*) FROM {virtual}')->fetchField();
4343
    $this->assertIdentical($num_records_before + 1, (int) $num_records_after, 'Successful insert into a table with a name and column which are reserved words.');
4344
  }
4345

  
4346
  public function testDeleteReservedWordTable() {
4347
    $delete = db_delete('virtual')
4348
      ->condition('function', 'Function value 1');
4349
    $num_deleted = $delete->execute();
4350
    $this->assertEqual($num_deleted, 1, "Deleted 1 record from a table with a name and column which are reserved words..");
4351
  }
4352

  
4353
  function testTruncateReservedWordTable() {
4354
    db_truncate('virtual')->execute();
4355
    $num_records_after = db_query("SELECT COUNT(*) FROM {virtual}")->fetchField();
4356
    $this->assertEqual(0, $num_records_after, 'Truncated a table with a reserved name.');
4357
  }
4358

  
4359
  function testUpdateReservedWordTable() {
4360
    $num_updated = db_update('virtual')
4361
      ->fields(array('function' => 'Updated function'))
4362
      ->execute();
4363
    $this->assertIdentical($num_updated, 1, 'Updated 1 record in a table with a name and column which are reserved words.');
4364
  }
4365

  
4366
  function testMergeReservedWordTable() {
4367
    $key = db_query('SELECT id FROM {virtual} LIMIT 1')->fetchField();
4368
    $num_records_before = db_query('SELECT COUNT(*) FROM {virtual}')->fetchField();
4369
    db_merge('virtual')
4370
      ->key(array('id' => $key))
4371
      ->fields(array('function' => 'Merged function'))
4372
      ->execute();
4373
    $num_records_after = db_query('SELECT COUNT(*) FROM {virtual}')->fetchField();
4374
    $this->assertIdentical($num_records_before, $num_records_after, 'Successful merge query on a table with a name and column which are reserved words.');
4375
  }
4376
}
4377

  
4378
/**
4379
 * Test table prefix handling.
4380
*/
4381
class DatabaseTablePrefixTestCase extends DatabaseTestCase {
4382
  public static function getInfo() {
4383
    return array(
4384
      'name' => 'Table prefixes',
4385
      'description' => 'Test handling of table prefixes.',
4386
      'group' => 'Database',
4387
    );
4388
  }
4389

  
4390
  public function testSchemaDotTablePrefixes() {
4391
    // Get a copy of the default connection options.
4392
    $db = Database::getConnection('default', 'default');
4393
    $connection_options = $db->getConnectionOptions();
4394

  
4395
    if ($connection_options['driver'] === 'sqlite') {
4396
      // In SQLite simpletest's prefixed db tables exist in their own schema
4397
      // (e.g. simpletest124904.system), so we cannot test the schema.table
4398
      // prefix syntax here.
4399
      $this->assert(TRUE, 'Skipping schema.table prefixed tables test for SQLite.');
4400
      return;
4401
    }
4402

  
4403
    $db_name = $connection_options['database'];
4404
    // This prefix is usually something like simpletest12345
4405
    $test_prefix = $connection_options['prefix']['default'];
4406

  
4407
    // Set up a new connection with table prefixes in the form "schema.table"
4408
    $prefixed = $connection_options;
4409
    $prefixed['prefix'] = array(
4410
      'default' => $test_prefix,
4411
      'users' => $db_name . '.' . $test_prefix,
4412
      'role' => $db_name . '.' . $test_prefix,
4413
    );
4414
    Database::addConnectionInfo('default', 'prefixed', $prefixed);
4415

  
4416
    // Test that the prefixed database connection can query the prefixed tables.
4417
    $num_users_prefixed = Database::getConnection('prefixed', 'default')->query('SELECT COUNT(1) FROM {users}')->fetchField();
4418
    $this->assertTrue((int) $num_users_prefixed > 0, 'Successfully queried the users table using a schema.table prefix');
4419
    $num_users_default = Database::getConnection('default', 'default')->query('SELECT COUNT(1) FROM {users}')->fetchField();
4420
    $this->assertEqual($num_users_default, $num_users_prefixed, 'Verified results of query using a connection with schema.table prefixed tables');
4421
  }
4242 4422
}

Formats disponibles : Unified diff