diff --git a/config/install/easy_breadcrumb.settings.yml b/config/install/easy_breadcrumb.settings.yml index e9f10fdd733ec82dd62402beb34d2056dc460b08..77eb211c05bf4ac038e5736bb7a49884c7284d05 100644 --- a/config/install/easy_breadcrumb.settings.yml +++ b/config/install/easy_breadcrumb.settings.yml @@ -43,3 +43,9 @@ truncator_length: 100 truncator_dots: true remove_repeated_segments_text_only: false home_segment_validation_skip: false +dependencies: + module: + - easy_breadcrumb + enforced: + module: + - easy_breadcrumb diff --git a/easy_breadcrumb.install b/easy_breadcrumb.install index 25de44d26a075df02fff70b325c4fc7777a3df5d..14749660ebfb1e607edaf3a8ae186b4f2241dbb2 100644 --- a/easy_breadcrumb.install +++ b/easy_breadcrumb.install @@ -5,6 +5,7 @@ * Add installation messages to help users get started and update. */ +use Drupal\Core\Update\UpdateException; use Drupal\easy_breadcrumb\EasyBreadcrumbConstants; /** @@ -151,3 +152,36 @@ function easy_breadcrumb_update_8009() { return t('Breadcrumb segment minimum count has been set to 1 to maintain the existing behavior when rendering breadcrumbs.'); } } + +/** + * Updates dependencies in easy_breadcrumb.settings configuration. + */ +function easy_breadcrumb_update_80010() { + /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */ + $config_factory = \Drupal::service('config.factory'); + + // Load the easy_breadcrumb.settings configuration. + $config = $config_factory->getEditable('easy_breadcrumb.settings'); + + if (!$config) { + throw new UpdateException('Could not load easy_breadcrumb.settings configuration.'); + } + + // Add the dependencies if they are not already set. + $dependencies = $config->get('dependencies') ?: []; + + // Ensure the 'module' dependency is set. + $dependencies['module'] = $dependencies['module'] ?? []; + if (!in_array('easy_breadcrumb', $dependencies['module'], TRUE)) { + $dependencies['module'][] = 'easy_breadcrumb'; + } + + // Ensure the 'enforced' dependencies are set. + $dependencies['enforced']['module'] = $dependencies['enforced']['module'] ?? []; + if (!in_array('easy_breadcrumb', $dependencies['enforced']['module'], TRUE)) { + $dependencies['enforced']['module'][] = 'easy_breadcrumb'; + } + + // Save the updated configuration. + $config->set('dependencies', $dependencies)->save(); +} diff --git a/tests/src/Functional/EasyBreadcrumbInstallUninstallTest.php b/tests/src/Functional/EasyBreadcrumbInstallUninstallTest.php new file mode 100644 index 0000000000000000000000000000000000000000..1f4d8f4c36d31767dc6471e5c37951f2633f7abb --- /dev/null +++ b/tests/src/Functional/EasyBreadcrumbInstallUninstallTest.php @@ -0,0 +1,101 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\easy_breadcrumb\Functional; + +use Drupal\Tests\BrowserTestBase; + +/** + * Tests the installation, uninstallation, and reinstallation of the easy_breadcrumb module. + * + * @group easy_breadcrumb + */ +class EasyBreadcrumbInstallUninstallTest extends BrowserTestBase { + + /** + * The default theme to use during testing. + * + * @var string + */ + protected $defaultTheme = 'stark'; + + /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * {@inheritdoc} + */ + protected static $modules = []; + + /** + * A test user with admin permissions. + * + * @var \Drupal\user\Entity\User + */ + protected $adminUser; + + /** + * {@inheritdoc} + */ + protected $strictConfigSchema = FALSE; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + + // Initialize the module handler service. + $this->moduleHandler = $this->container->get('module_handler'); + + // Create a user with permission to install and uninstall modules. + $this->adminUser = $this->createUser([ + 'administer modules', + 'administer site configuration', + ]); + $this->drupalLogin($this->adminUser); + } + + /** + * Tests the installation, uninstallation, and reinstallation of the module. + */ + public function testInstallUninstallReinstall(): void { + // Step 1: Verify that the module is not installed initially. + $this->assertFalse($this->moduleHandler->moduleExists('easy_breadcrumb'), 'Error: The easy_breadcrumb module is installed initially when it should not be.'); + + // Step 2: Install the module. + $this->container->get('module_installer')->install(['easy_breadcrumb']); + $this->assertTrue($this->moduleHandler->moduleExists('easy_breadcrumb'), 'Error: The easy_breadcrumb module failed to install.'); + + // Step 3: Verify that the module's configuration exists. + $config = $this->config('easy_breadcrumb.settings'); + $this->assertNotNull($config, 'Error: The easy_breadcrumb configuration does not exist after installation.'); + + // Step 4: Uninstall the module. + try { + $this->container->get('module_installer')->uninstall(['easy_breadcrumb']); + drupal_flush_all_caches(); // Clear all caches after uninstallation. + } catch (\Exception $e) { + $this->fail('Uninstallation failed with exception: ' . $e->getMessage()); + } + $this->assertFalse($this->moduleHandler->moduleExists('easy_breadcrumb'), 'Error: The easy_breadcrumb module failed to uninstall.'); + + // Step 5: Verify that the module's configuration is removed. + $config = $this->config('easy_breadcrumb.settings'); + $this->assertNull($config->getRawData(), 'Error: The easy_breadcrumb configuration still exists after uninstallation.'); + + // Step 6: Reinstall the module. + $this->container->get('module_installer')->install(['easy_breadcrumb']); + $this->assertTrue($this->moduleHandler->moduleExists('easy_breadcrumb'), 'Error: The easy_breadcrumb module failed to reinstall.'); + + // Step 7: Verify that the module's configuration exists again. + $config = $this->config('easy_breadcrumb.settings'); + $this->assertNotNull($config, 'Error: The easy_breadcrumb configuration does not exist after reinstallation.'); + } + +}