Skip to content

Commit

Permalink
OPENEUROPA-1561: Refactor behat test classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
imanoleguskiza committed Feb 14, 2019
1 parent d66e81d commit ee1520d
Show file tree
Hide file tree
Showing 8 changed files with 408 additions and 381 deletions.
5 changes: 2 additions & 3 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ default:
- Drupal\DrupalExtension\Context\DrupalContext
- Drupal\DrupalExtension\Context\MessageContext
- Drupal\DrupalExtension\Context\ConfigContext
- Drupal\Tests\oe_multilingual\Behat\DrupalContext
- Drupal\Tests\oe_multilingual\Behat\MinkContext
- Drupal\Tests\oe_multilingual\Behat\InterfaceTranslationContext
- Drupal\oe_multilingual\Tests\Behat\DrupalContext
- Drupal\oe_multilingual\Tests\Behat\MultilingualContext
- OpenEuropa\Behat\TransformationContext:
pages:
English administration home: 'en/admin'
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
},
"autoload": {
"psr-4": {
"Drupal\\oe_multilingual\\": "./src/"
"Drupal\\oe_multilingual\\": "./src/",
"Drupal\\oe_multilingual\\Tests\\Behat\\": "./tests/Behat/"
}
},
"autoload-dev": {
Expand Down
227 changes: 3 additions & 224 deletions tests/Behat/DrupalContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,17 @@

declare(strict_types = 1);

namespace Drupal\Tests\oe_multilingual\Behat;
namespace Drupal\oe_multilingual\Tests\Behat;

use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\DrupalExtension\Context\RawDrupalContext;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\NodeType;
use Drupal\oe_multilingual\Tests\Behat\Traits\ContentManagerTrait;

/**
* Class DrupalContext.
*/
class DrupalContext extends RawDrupalContext {

/**
* The config context.
*
* @var \Drupal\DrupalExtension\Context\ConfigContext
*/
protected $configContext;

/**
* Gathers some other contexts.
*
* @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope
* The before scenario scope.
*
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope) {
$environment = $scope->getEnvironment();
$this->configContext = $environment->getContext('Drupal\DrupalExtension\Context\ConfigContext');
}

/**
* Enable OpenEuropa Multilingual Selection Page module.
*
* @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope
* The Hook scope.
*
* @BeforeScenario @selection-page
*/
public function setupSelectionPage(BeforeScenarioScope $scope): void {
\Drupal::service('module_installer')->install(['oe_multilingual_selection_page']);
}

/**
* Disable OpenEuropa Multilingual Selection Page module.
*
* @param \Behat\Behat\Hook\Scope\AfterScenarioScope $scope
* The Hook scope.
*
* @AfterScenario @selection-page
*/
public function revertSelectionPage(AfterScenarioScope $scope): void {
\Drupal::service('module_installer')->uninstall([
'oe_multilingual_selection_page',
'language_selection_page',
]);
}
use ContentManagerTrait;

/**
* Create content given its type and fields.
Expand All @@ -74,28 +24,6 @@ public function createContent(string $entity_type_label, TableNode $table): void
$this->nodeCreate($node);
}

/**
* Create translation for given content.
*
* @Given the following :language translation for the :entity_type_label with title :title:
*/
public function createTranslation(string $language, string $entity_type_label, string $title, TableNode $table): void {
// Build translation entity.
$values = $this->getContentValues($entity_type_label, $table);
$language = $this->getLanguageIdByName($language);
$translation = \Drupal::entityTypeManager()
->getStorage('node')
->create($values);

// Add the translation to the entity.
$entity = $this->getEntityByLabel('node', $title);
$entity->addTranslation($language, $translation->toArray())->save();

// Make sure URL alias is correctly generated for given translation.
$translation = $entity->getTranslation($language);
\Drupal::service('pathauto.generator')->createEntityAlias($translation, 'insert');
}

/**
* Assert viewing content given its type and title.
*
Expand Down Expand Up @@ -140,153 +68,4 @@ public function iShouldNotSeeTheField(string $field): void {
}
}

/**
* Check that we have the correct language for initial translation.
*
* @param string $title
* Title of node.
*
* @Then The only available translation for :title is in the site's default language
*/
public function assertOnlyDefaultLanguageTranslationExist(string $title): void {
$node = $this->getEntityByLabel('node', $title);
if (!$node) {
throw new \RuntimeException("Node '{$title}' doesn't exist.");
}

$node_translation_languages = $node->getTranslationLanguages();
if (count($node_translation_languages) !== 1) {
throw new \RuntimeException("The node should have only one translation.");
}

$node_language = key($node_translation_languages);
if ($node_language != \Drupal::languageManager()->getDefaultLanguage()->getId()) {
throw new \RuntimeException("Original translation language of the '{$title}' node is not the site's default language.");
}
}

/**
* Sets the default site language.
*
* @param string $name
* The language name.
*
* @Given (I set) the default site language (is) (to) :name
*/
public function theDefaultSiteLanguageIs(string $name): void {
$language = $this->getLanguageIdByName($name);
$this->configContext->setConfig('system.site', 'default_langcode', $language);
}

/**
* Return content fields array suitable for Drupal API.
*
* @param string $entity_type_label
* Content type label.
* @param \Behat\Gherkin\Node\TableNode $table
* TableNode containing a list of fields keyed by their labels.
*
* @return array
* Content fields array.
*/
protected function getContentValues(string $entity_type_label, TableNode $table): array {
$entity_type = $this->getEntityTypeByLabel($entity_type_label);

$values = ['type' => $entity_type];
foreach ($table->getRowsHash() as $field_label => $value) {
$name = $this->getFieldNameByLabel($entity_type, $field_label);
$values[$name] = $value;
}

return $values;
}

/**
* Load an entity by label.
*
* @param string $entity_type_id
* The entity type ID.
* @param string $label
* The label of the entity to load.
*
* @return \Drupal\Core\Entity\ContentEntityInterface
* The loaded entity.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getEntityByLabel(string $entity_type_id, string $label): ContentEntityInterface {
$manager = \Drupal::entityTypeManager();
$label_field = $manager->getDefinition($entity_type_id)->getKey('label');
$entity_list = $manager->getStorage($entity_type_id)->loadByProperties([$label_field => $label]);
return array_shift($entity_list);
}

/**
* Get entity type by its label.
*
* @param string $label
* Content type label.
*
* @return string
* Entity type ID.
*/
protected function getEntityTypeByLabel(string $label): string {
/** @var \Drupal\node\Entity\NodeType[] $entity_types */
$entity_types = NodeType::loadMultiple();
foreach ($entity_types as $entity_type) {
if ($entity_type->label() === $label) {
return $entity_type->id();
}
}

throw new \InvalidArgumentException("Content type '{$label}' not found.");
}

/**
* Get field name by its label.
*
* @param string $entity_type
* Entity type.
* @param string $label
* Field label.
*
* @return string
* Field name.
*/
protected function getFieldNameByLabel(string $entity_type, string $label): string {
if ($label === 'Title') {
return 'title';
}

/** @var \Drupal\Core\Field\FieldConfigBase[] $fields */
$fields = \Drupal::entityManager()->getFieldDefinitions('node', $entity_type);
foreach ($fields as $field) {
if ($field instanceof FieldConfig && $field->label() === $label) {
return $field->getName();
}
}

throw new \InvalidArgumentException("Field '{$label}' not found.");
}

/**
* Get language ID given its name.
*
* @param string $name
* Language name.
*
* @return string
* Language ID.
*/
protected function getLanguageIdByName(string $name): string {
foreach (\Drupal::languageManager()->getLanguages() as $language) {
if ($language->getName() === $name) {
return $language->getId();
}
}

throw new \InvalidArgumentException("Language '{$name}' not found.");
}

}
111 changes: 0 additions & 111 deletions tests/Behat/InterfaceTranslationContext.php

This file was deleted.

Loading

0 comments on commit ee1520d

Please sign in to comment.