jhedstrom / drupalextension

An integration layer between Behat, Mink Extension, and Drupal.
GNU General Public License v2.0
209 stars 192 forks source link

Drupal finder exception causes error - Cannot locate Drupal on a standalone behat installation. #658

Open MiroslavRusev opened 5 months ago

MiroslavRusev commented 5 months ago

https://github.com/jhedstrom/drupalextension/blob/22d69d8c6929e49c4736b147bf2f73e92e4fedb9/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php#L73

I am currently using the region map of the Drupal Extension in a behat, which is installed without Drupal. The behat.yml config looks like: Drupal\DrupalExtension: region_map: region1: ".clss"

I've tried adding blackbox:~ but it doesn't do anything and I think it's because of Line 86 in the same file (DrupalExtension.php). The problem is the exception is thrown before reading the configuration, so there is no way to start Behat with DrupalExtension if no drupal is installed on the same machine. This problem was not present in previous versions as there was no usage of DrupalFinder() in that file. https://github.com/jhedstrom/drupalextension/blob/4.2/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php#L64

MiroslavRusev commented 5 months ago

v 4.2.0 - image

v5 - image

MiroslavRusev commented 5 months ago

I can't open a PR, but I think this should be the fix:


public function load(ContainerBuilder $container, array $config)
  {
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/config'));
        $loader->load('services.yml');
        $container->setParameter('drupal.drupal.default_driver', $config['default_driver']);

        $this->loadParameters($container, $config);
        if (!isset($config['blackbox'])) {
            // Workaround a bug in BrowserKitDriver that wrongly considers as text
            // of the page, pieces of texts inside the <head> section.
            // @see https://github.com/minkphp/MinkBrowserKitDriver/issues/153
            // @see https://www.drupal.org/project/drupal/issues/3175718
            $drupalFinder = new DrupalFinder();
            if (!$drupalFinder->locateRoot(getcwd())) {
                throw new \RuntimeException('Cannot locate Drupal');
            }
            $drupalRoot = $drupalFinder->getDrupalRoot();
            require_once($drupalRoot . '/core/tests/Drupal/Tests/DocumentElement.php');
            class_alias('\Drupal\Tests\DocumentElement', '\Behat\Mink\Element\DocumentElement', true);
        }

        // Setup any drivers if requested.
        $this->loadBlackbox($loader, $config);
        $this->loadDrupal($loader, $container, $config);
        $this->loadDrush($loader, $container, $config);
    }