diff --git a/behat.yml.dist b/behat.yml.dist index 2fe7945..ec2342d 100644 --- a/behat.yml.dist +++ b/behat.yml.dist @@ -6,6 +6,7 @@ default: contexts: - Drupal\DrupalExtension\Context\MinkContext - Drupal\DrupalExtension\Context\DrupalContext + - Drupal\DrupalExtension\Context\MessageContext - Drupal\Tests\oe_multilingual\Behat\DrupalContext - Drupal\Tests\oe_multilingual\Behat\MinkContext extensions: @@ -18,6 +19,8 @@ default: api_driver: "drupal" drupal: drupal_root: "build" + selectors: &drupal-selectors + success_message_selector: ".messages--status" region_map: "language switcher": "#block-oe-multilingual-language-switcher" "language dialog": "#block-oe-multilingual-language-switcher" diff --git a/composer.json b/composer.json index ff1eca4..51df969 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,9 @@ "drush/drush": "~9.0@stable", "openeuropa/code-review": "^1.0.0-alpha4", "openeuropa/drupal-core-require-dev": "^8.6", - "openeuropa/task-runner": "~1.0", - "phpunit/phpunit": "~6.0" + "openeuropa/task-runner": "~1.0.0-beta2", + "phpunit/phpunit": "~6.0", + "symfony/browser-kit": "~3.0||~4.0" }, "scripts": { "post-install-cmd": "./vendor/bin/run drupal:site-setup", diff --git a/oe_multilingual.services.yml b/oe_multilingual.services.yml index 2e8c606..2499079 100644 --- a/oe_multilingual.services.yml +++ b/oe_multilingual.services.yml @@ -5,3 +5,7 @@ services: oe_multilingual.helper: class: Drupal\oe_multilingual\MultilingualHelper arguments: ['@entity.repository', '@current_route_match', '@language_manager'] + oe_multilingual.content_language_settings: + class: Drupal\oe_multilingual\MultilingualConfigOverride + tags: + - {name: config.factory.override, priority: 5} diff --git a/src/MultilingualConfigOverride.php b/src/MultilingualConfigOverride.php new file mode 100644 index 0000000..d5289da --- /dev/null +++ b/src/MultilingualConfigOverride.php @@ -0,0 +1,57 @@ + 'site_default', + 'language_alterable' => FALSE, + ]; + } + } + + return $overrides; + } + + /** + * {@inheritdoc} + */ + public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION): ?StorableConfigBase { + return NULL; + } + + /** + * {@inheritdoc} + */ + public function getCacheSuffix(): string { + return 'oe_multilingual.language_configs_override'; + } + + /** + * {@inheritdoc} + */ + public function getCacheableMetadata($name): CacheableMetadata { + return new CacheableMetadata(); + } + +} diff --git a/tests/Behat/DrupalContext.php b/tests/Behat/DrupalContext.php index be1c7a1..e5c3d21 100644 --- a/tests/Behat/DrupalContext.php +++ b/tests/Behat/DrupalContext.php @@ -201,4 +201,59 @@ protected function getLanguageIdByName(string $name): string { throw new \InvalidArgumentException("Language '{$name}' not found."); } + /** + * Redirect user to the node creation page. + * + * @param string $content_type_name + * Content type name. + * + * @Given I visit the :content_type_name creation page + */ + public function iAmVisitingTheCreationPage(string $content_type_name): void { + $node_bundle = $this->getEntityTypeByLabel($content_type_name); + $this->visitPath('node/add/' . $node_bundle); + } + + /** + * Check that the field is not present. + * + * @param string $field + * Input id, name or label. + * + * @Then I should not see the field :field + */ + public function iShouldNotSeeTheField(string $field): void { + $element = $this->getSession() + ->getPage() + ->findField($field); + if ($element) { + throw new \RuntimeException("Field '{$field}' is present."); + } + } + + /** + * 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."); + } + } + } diff --git a/tests/features/content-default-language.feature b/tests/features/content-default-language.feature new file mode 100644 index 0000000..2e93e94 --- /dev/null +++ b/tests/features/content-default-language.feature @@ -0,0 +1,17 @@ +@api +Feature: Content initial language. + In order to create content + As a editor + I want to make sure that it is not possible for a content editor to create a node's initial version in any language + other than the site's default language + + Scenario: As an editor, when I create a node I can not select the initial node language + Given I am logged in as a user with the "create oe_demo_translatable_page content" permission + And I visit the "Demo translatable page" creation page + Then I should not see the field "Language" + + When I fill in "Title" with "Title Default value" + And I fill in "Body" with "Body Default value" + And I press "Save" + Then I should see the success message "Demo translatable page Title Default value has been created." + And The only available translation for "Title Default value" is in the site's default language