diff --git a/core/modules/system/src/Tests/Module/InstallTest.php b/core/tests/Drupal/KernelTests/Core/Module/ModuleInstallerKernelTest.php similarity index 38% rename from core/modules/system/src/Tests/Module/InstallTest.php rename to core/tests/Drupal/KernelTests/Core/Module/ModuleInstallerKernelTest.php index babdd76..603014b 100644 --- a/core/modules/system/src/Tests/Module/InstallTest.php +++ b/core/tests/Drupal/KernelTests/Core/Module/ModuleInstallerKernelTest.php @@ -1,31 +1,50 @@ root . '/core/modules/system/system.module'; + $this->moduleInstaller = $this->container->get('module_installer'); + $this->moduleInstaller->install([ + 'system', + 'user', + 'module_test', + ]); + } + + /** + * Verifies that drupal_get_schema() can be used during module installation. */ public function testGetSchemaAtInstallTime() { - // @see module_test_install() - $value = db_query("SELECT data FROM {module_test}")->fetchField(); - $this->assertIdentical($value, 'varchar'); + $database = $this->container->get('database'); + $query = $database->select('module_test'); + $query->addField('module_test', 'data'); + $value = $query->execute()->fetchField(); + + $this->assertSame($value, 'varchar'); } /** @@ -36,8 +55,12 @@ public function testGetSchemaAtInstallTime() { * be an array. */ public function testEnableUserTwice() { - \Drupal::service('module_installer')->install(array('user'), FALSE); - $this->assertIdentical($this->config('core.extension')->get('module.user'), 0); + $this->moduleInstaller->install(['user'], FALSE); + $this->assertSame(0, $this->config('core.extension')->get('module.user')); + + // To avoid false positives, ensure that a module that does not exist does + // not return exactly zero. + $this->assertNotSame(0, $this->config('core.extension')->get('module.does_not_exist')); } /** @@ -45,21 +68,29 @@ public function testEnableUserTwice() { */ public function testRequiredModuleSchemaVersions() { $version = drupal_get_installed_schema_version('system', TRUE); - $this->assertTrue($version > 0, 'System module version is > 0.'); + $this->assertGreaterThan(0, $version); $version = drupal_get_installed_schema_version('user', TRUE); - $this->assertTrue($version > 0, 'User module version is > 0.'); + $this->assertGreaterThan(0, $version); $post_update_key_value = \Drupal::keyValue('post_update'); $existing_updates = $post_update_key_value->get('existing_updates', []); - $this->assertTrue(in_array('module_test_post_update_test', $existing_updates)); + $this->assertArraySubset(['module_test_post_update_test'], $existing_updates); } /** * Ensures that post update functions are removed on uninstall. */ public function testUninstallPostUpdateFunctions() { - \Drupal::service('module_installer')->uninstall(['module_test']); + // First, to avoid false positives, ensure that the post_update function + // exists while the module is still installed. + $post_update_key_value = \Drupal::keyValue('post_update'); + $existing_updates = $post_update_key_value->get('existing_updates', []); + $this->assertArraySubset(['module_test_post_update_test'], $existing_updates); + + // Uninstall the module. + $this->moduleInstaller->uninstall(['module_test']); + // Ensure the post update function is no longer listed. $post_update_key_value = \Drupal::keyValue('post_update'); $existing_updates = $post_update_key_value->get('existing_updates', []); $this->assertFalse(in_array('module_test_post_update_test', $existing_updates)); @@ -70,24 +101,18 @@ public function testUninstallPostUpdateFunctions() { */ public function testModuleNameLength() { $module_name = 'invalid_module_name_over_the_maximum_allowed_character_length'; - $message = format_string('Exception thrown when enabling module %name with a name length over the allowed maximum', array('%name' => $module_name)); - try { - $this->container->get('module_installer')->install(array($module_name)); - $this->fail($message); - } - catch (ExtensionNameLengthException $e) { - $this->pass($message); - } + $this->setExpectedException(ExtensionNameLengthException::class); + $this->moduleInstaller->install([$module_name]); + } + /** + * Tests that an exception is thrown when a module name is too long. + */ + public function testModuleNameLengthFalse() { // Since for the UI, the submit callback uses FALSE, test that too. - $message = format_string('Exception thrown when enabling as if via the UI the module %name with a name length over the allowed maximum', array('%name' => $module_name)); - try { - $this->container->get('module_installer')->install(array($module_name), FALSE); - $this->fail($message); - } - catch (ExtensionNameLengthException $e) { - $this->pass($message); - } + $module_name = 'invalid_module_name_over_the_maximum_allowed_character_length'; + $this->setExpectedException(ExtensionNameLengthException::class); + $this->moduleInstaller->install([$module_name], FALSE); } }