diff --git a/core/modules/behat/behat.install b/core/modules/behat/behat.install index ee01d1e..882861d 100644 --- a/core/modules/behat/behat.install +++ b/core/modules/behat/behat.install @@ -8,7 +8,7 @@ function behat_install() { global $base_url; - $behat_conf = Yaml::parse(DRUPAL_ROOT . '/' . \Drupal::moduleHandler()->getModule('behat')->getPath() . '/config/behat.yml.dist'); + $behat_conf = Yaml::parse(\Drupal::root() . '/' . \Drupal::moduleHandler()->getModule('behat')->getPath() . '/config/behat.yml.dist'); $behat_conf['default']['extensions']['Behat\MinkExtension']['base_url'] = $base_url; $behat_conf['default']['extensions']['Drupal\DrupalExtension']['drupal']['drupal_root'] = $base_url; @@ -17,7 +17,7 @@ function behat_install() { $destination = 'public://behat.yml'; if (file_unmanaged_save_data($yaml, $destination, FILE_EXISTS_RENAME) === FALSE) { - drupal_set_message(t('An error occurred while writing to %destination. You have to manually copy core/modules/behat/config/behat.yml.dist as @destination and make the appropriate changes.', ['%destination' => $destination]), 'error'); + drupal_set_message(t('An error occurred while writing to %destination. You have to manually copy core/modules/behat/config/behat.yml.dist as %destination and make the appropriate changes.', ['%destination' => $destination]), 'error'); } else { drupal_set_message(t('behat.yml has been written to the public files directory.')); diff --git a/core/modules/behat/behat.module b/core/modules/behat/behat.module index a864ae9..1cd9739 100644 --- a/core/modules/behat/behat.module +++ b/core/modules/behat/behat.module @@ -4,3 +4,18 @@ * @file * Provides validation testing with Behat. */ + +use Drupal\Core\Routing\RouteMatchInterface; + +/** + * Implements hook_help(). + */ +function behat_help($route_name, RouteMatchInterface $route_match) { + switch ($route_name) { + case 'help.page.behat': + $output = ''; + $output .= '

' . t('About') . '

'; + $output .= '

' . t('The Behat module provides support for running validation testing with Behat.') . '

'; + return $output; + } +} diff --git a/core/modules/behat/includes/behat.inc b/core/modules/behat/includes/behat.inc index 2521037..e46f9a6 100644 --- a/core/modules/behat/includes/behat.inc +++ b/core/modules/behat/includes/behat.inc @@ -17,8 +17,7 @@ * An array of the modules that were discovered. */ function behat_discover_modules() { - $module_handler = \Drupal::moduleHandler(); - $modules = $module_handler->getModuleList(); + $modules = \Drupal::moduleHandler()->getModuleList(); $discovered = []; foreach ($modules as $module) { @@ -94,9 +93,7 @@ function behat_discover_module_features(Extension $module) { * The system path for the module's Behat features directory. */ function behat_module_features_path(Extension $module) { - $system_path = DRUPAL_ROOT . '/' . $module->getPath() . '/tests/features'; - - return $system_path; + return \Drupal::root() . '/' . $module->getPath() . '/tests/features'; } /** @@ -112,10 +109,10 @@ function behat_module_features_path(Extension $module) { */ function behat_module_bootstrap_path($module) { if (file_exists($module->getPath() . '/tests/features/bootstrap/FeatureContext.php')) { - $system_path = DRUPAL_ROOT . '/' . $module->getPath() . '/tests/features/bootstrap'; + $system_path = \Drupal::root() . '/' . $module->getPath() . '/tests/features/bootstrap'; } else { - $system_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'behat') . '/tests/features/bootstrap'; + $system_path = \Drupal::root() . '/' . drupal_get_path('module', 'behat') . '/tests/features/bootstrap'; } return $system_path; @@ -136,8 +133,7 @@ function behat_register_feature_scenarios(FeatureNode $feature) { $data['feature'] = $feature->getTitle(); $data['location'] = $feature_location . ':' . $scenario->getLine(); $data['module'] = $feature->drupalModule; - $entity = entity_create('behat_scenario', $data); - $entity->save(); + Scenario::create($data)->save(); } // @todo return somesthing here, particularly on failure. @@ -180,7 +176,7 @@ function behat_get_parser() { * The absolute system path. */ function behat_convert_relative_to_absolute_path($relative_path) { - return DRUPAL_ROOT . '/' . $relative_path; + return \Drupal::root() . '/' . $relative_path; } /** @@ -193,7 +189,7 @@ function behat_convert_relative_to_absolute_path($relative_path) { * The relative path. */ function behat_convert_absolute_to_relative_path($absolute_path) { - return str_replace(DRUPAL_ROOT . '/', '', $absolute_path); + return str_replace(\Drupal::root() . '/', '', $absolute_path); } /** @@ -206,11 +202,12 @@ function behat_convert_absolute_to_relative_path($absolute_path) { * TRUE if the module has behat scenarios registered with behat. */ function behat_module_is_registered(Extension $module) { - $query = 'SELECT bsid FROM {behat_scenario} WHERE module = :module LIMIT 1'; - $result = db_query($query, [':module' => $module->getName()]); - $record = $result->fetchObject(); + $count = \Drupal::entityQuery('behat_scenario') + ->condition('module', $module->getName()) + ->count() + ->execute(); - return (boolean) $record; + return (bool) $count; } /** @@ -224,9 +221,14 @@ function behat_module_is_registered(Extension $module) { * by entitiy id. */ function behat_get_module_scenario_registrations(Extension $module) { - $query = 'SELECT bsid, location FROM {behat_scenario} WHERE module = :module'; - $result = db_query($query, [':module' => $module->getName()]); - $registrations = $result->fetchAllKeyed(0, 1); + $ids = \Drupal::entityQuery('behat_scenario') + ->condition('module', $module->getName()) + ->execute(); + + $registrations = []; + foreach (Scenario::loadMultiple($ids) as $id => $scenario) { + $registrations[$id] = $scenario->get('location'); + } return $registrations; } @@ -250,20 +252,17 @@ function behat_deregister_scenario($bsid) { function behat_deregister_module_scenarios(Extension $module) { $scenario_registrations = behat_get_module_scenario_registrations($module); $entity_ids = array_keys($scenario_registrations); - - entity_delete_multiple('behat_scenario', $entity_ids); + \Drupal::entityManager()->getStorage('behat_scenario')->delete($entity_ids); } /** * Gets environmental parameters for Behat. * * @return array - * An associateive array of environmental Behat parameters. + * An associative array of environmental Behat parameters. */ function behat_get_env_params() { - $behat_params = parse_url(getenv('BEHAT_PARAMS')); - - return $behat_params; + return parse_url(getenv('BEHAT_PARAMS')); } /** @@ -289,7 +288,9 @@ function behat_set_env_param($key, $value) { } $behat_params_value = UrlHelper::buildQuery($behat_params); - return putenv("BEHAT_PARAMS=$behat_params_value"); + if (!putenv("BEHAT_PARAMS=$behat_params_value")) { + throw new \Exception('Cannot set environment variable BEHAT_PARAMS.'); + } } /** @@ -322,7 +323,7 @@ function behat_execute_tests(array $scenarios = [], array $tags = [], array $for $tags = behat_tags_argument_build($tags); $formats = behat_format_arguments_build($formats); $config = behat_config_argument_build(); - $binary_path = DRUPAL_ROOT . '/core/vendor/bin/behat'; + $binary_path = \Drupal::root() . '/core/vendor/bin/behat'; // Load all scenarios if none have been specified. if (!$scenarios) { @@ -331,7 +332,7 @@ function behat_execute_tests(array $scenarios = [], array $tags = [], array $for // Execute each scenario separately. foreach ($scenarios as $scenario) { - $scenario_location = DRUPAL_ROOT . '/' . $scenario->location->value; + $scenario_location = \Drupal::root() . '/' . $scenario->location->value; $module = $modules[$scenario->module->value]; $features_path = behat_module_features_path($module); diff --git a/core/modules/behat/src/Entity/Scenario.php b/core/modules/behat/src/Entity/Scenario.php index 5627e18..959e23a 100644 --- a/core/modules/behat/src/Entity/Scenario.php +++ b/core/modules/behat/src/Entity/Scenario.php @@ -42,7 +42,6 @@ class Scenario extends ContentEntityBase { * {@inheritdoc} */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { - $fields['bsid'] = BaseFieldDefinition::create('integer') ->setLabel(t('Behat Scenario ID')) ->setDescription(t('The Behat scenario ID.')) @@ -51,7 +50,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['blid'] = BaseFieldDefinition::create('integer') ->setLabel(t('Behat Log ID')) - ->setDescription(t('The Behat log id the most recent run for this scenario. NULL if database logging is disabled.')) + ->setDescription(t('The Behat log ID the most recent run for this scenario. NULL if database logging is disabled.')) ->setSetting('unsigned', TRUE); $fields['uuid'] = BaseFieldDefinition::create('uuid') diff --git a/core/modules/behat/src/FeatureContext.php b/core/modules/behat/src/FeatureContext.php index 6c733eb..eebd038 100644 --- a/core/modules/behat/src/FeatureContext.php +++ b/core/modules/behat/src/FeatureContext.php @@ -12,6 +12,7 @@ use Behat\Behat\Tester\Exception\PendingException; use Behat\Gherkin\Node\PyStringNode; use Behat\Gherkin\Node\TableNode; +use Drupal\node\Entity\Node; class FeatureContext extends DrupalContext { @@ -148,56 +149,21 @@ public function iAmViewingTheTheme($expected_theme) { } /** - * Returns the most recently created node. - * - * @return object - * The most recently created node. - */ - public function getLastCreatedNode() { - return $this->getLastCreatedEntity("node"); - } - - /** - * Returns the current, relative path. - * - * Simply using Drupal's current_path() or $_GET['q'] does not work. - * - * @return string - * The path. - */ - public function getCurrentPath() { - $url = $this->getSession()->getCurrentUrl(); - $parsed_url = parse_url($url); - $path = trim($parsed_url['path'], '/'); - - return $path; - } - - /** * Returns node currently being viewed. Assumes /node/[nid] URL. * - * Using path-based loaders, like menu_load_object(), will not work. - * - * @return object + * @return \Drupal\node\Entity\Node * The currently viewed node. * - * @throws Exception + * @throws \Exception */ public function getNodeFromUrl() { + $route_match = \Drupal::routeMatch(); - $path = $this->getCurrentPath(); - $system_path = drupal_lookup_path('source', $path); - if (!$system_path) { - $system_path = $path; - } - $menu_item = menu_get_item($system_path); - if ($menu_item['path'] == 'node/%') { - $node = node_load($menu_item['original_map'][1]); + if ($route_match->getRouteObject()->getPath() != '/node/{node}') { + throw new \Exception(sprintf("Node could not be loaded from URL '%s'", $this->getSession()->getCurrentUrl())); } - else { - throw new \Exception(sprintf("Node could not be loaded from URL '%s'", $path)); - } - return $node; + + return $route_match->getParameter('node'); } } diff --git a/core/modules/behat/src/ScenarioStorage.php b/core/modules/behat/src/ScenarioStorage.php index b8a3b19..befdbdf 100644 --- a/core/modules/behat/src/ScenarioStorage.php +++ b/core/modules/behat/src/ScenarioStorage.php @@ -10,15 +10,12 @@ use Drupal\Core\Entity\Sql\SqlContentEntityStorage; /** - * Defines a Controller class for scenario entities. + * Defines a storage handler for scenario entities. */ class ScenarioStorage extends SqlContentEntityStorage { /** * {@inheritdoc} - * - * We don't need to store behat scenario entities as they will be - * automatically recreated during next install. */ public function hasData() { return FALSE; diff --git a/core/scripts/run-behat-tests.sh b/core/scripts/run-behat-tests.sh index af66018..79c140b 100644 --- a/core/scripts/run-behat-tests.sh +++ b/core/scripts/run-behat-tests.sh @@ -14,7 +14,7 @@ // Change directories so that drupal_realpath() in behat_execute_tests() returns // the correct path. // @todo Refactor so that this hack isn't necessary. -chdir(DRUPAL_ROOT); +chdir(\Drupal::root()); // Discover and execute Behat tests. behat_discover_modules();