PrefixInfoTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Drupal\KernelTests\Core\Database;
  3. use Drupal\Core\Database\Database;
  4. /**
  5. * Tests that the prefix info for a database schema is correct.
  6. *
  7. * @group Database
  8. */
  9. class PrefixInfoTest extends DatabaseTestBase {
  10. /**
  11. * Tests that DatabaseSchema::getPrefixInfo() returns the right database.
  12. *
  13. * We are testing if the return array of the method
  14. * \Drupal\Core\Database\Driver\mysql\Schema::getPrefixInfo(). This return
  15. * array is a keyed array with info about amongst other things the database.
  16. * The other two by Drupal core supported databases do not have this variable
  17. * set in the return array.
  18. */
  19. public function testGetPrefixInfo() {
  20. $connection_info = Database::getConnectionInfo('default');
  21. if ($connection_info['default']['driver'] == 'mysql') {
  22. // Copy the default connection info to the 'extra' key.
  23. Database::addConnectionInfo('extra', 'default', $connection_info['default']);
  24. $db1_connection = Database::getConnection('default', 'default');
  25. $db1_schema = $db1_connection->schema();
  26. $db2_connection = Database::getConnection('default', 'extra');
  27. // Get the prefix info for the first databse.
  28. $method = new \ReflectionMethod($db1_schema, 'getPrefixInfo');
  29. $method->setAccessible(TRUE);
  30. $db1_info = $method->invoke($db1_schema);
  31. // We change the database after opening the connection, so as to prevent
  32. // connecting to a non-existent database.
  33. $reflection = new \ReflectionObject($db2_connection);
  34. $property = $reflection->getProperty('connectionOptions');
  35. $property->setAccessible(TRUE);
  36. $connection_info['default']['database'] = 'foobar';
  37. $property->setValue($db2_connection, $connection_info['default']);
  38. // For testing purposes, we also change the database info.
  39. $reflection_class = new \ReflectionClass('Drupal\Core\Database\Database');
  40. $property = $reflection_class->getProperty('databaseInfo');
  41. $property->setAccessible(TRUE);
  42. $info = $property->getValue();
  43. $info['extra']['default']['database'] = 'foobar';
  44. $property->setValue(NULL, $info);
  45. $extra_info = Database::getConnectionInfo('extra');
  46. $this->assertSame($extra_info['default']['database'], 'foobar');
  47. $db2_schema = $db2_connection->schema();
  48. $db2_info = $method->invoke($db2_schema);
  49. $this->assertNotSame($db2_info['database'], $db1_info['database'], 'Each target connection has a different database.');
  50. $this->assertSame($db2_info['database'], 'foobar', 'The new profile has a different database.');
  51. Database::removeConnection('extra');
  52. }
  53. }
  54. }