test_support-1.0.x-dev/tests/src/Kernel/Installs/InstallModuleTest.php

tests/src/Kernel/Installs/InstallModuleTest.php
<?php

namespace Drupal\Tests\test_support\Kernel\Installs;

use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Tests\test_support\Kernel\Base\EnableModuleKernelTestBase;

class InstallModuleTest extends EnableModuleKernelTestBase
{
    public function module(): string
    {
        return 'test_support_entity_install';
    }

    /**
     * @test
     *
     * The TestInstallEntity declares a base field definition of type "text".
     * Since this plugin definition exists in the "text" module then the
     * install will fail when installing the entity schema definition.
     * The "text" module must be declared as a dependency.
     */
    public function module_and_entity_schema_install_fails_without_correct_dependencies(): void
    {
        $this->enableModules([
            $this->module(),
        ]);

        try {
            $this->installEntitySchema('test_install_entity');

            $this->fail('The entity schema should fail to install. Read the test comment for more information');
        } catch (PluginNotFoundException $exception) {
            $this->assertTrue(
                str_contains($exception->getMessage(), 'The "text" plugin does not exist')
            );
        }

        $this->enableModuleWithDependencies($this->module());

        try {
            $this->installEntitySchema('test_install_entity');
        } catch (\Exception $exception) {
            $this->convertExceptionToFailMessage($exception);
        }
    }

    // Would be nice if we could provide a more informative failure messages, something the developer can act on
    private function convertExceptionToFailMessage(\Exception $exception): void
    {
        $additionalMessage = '';

        if ($exception instanceof PluginNotFoundException) {
            $additionalMessage = 'You may be missing a dependency on your module';
        }

        $this->fail($additionalMessage . '. ' . $exception->getMessage());
    }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc