Skip to content

Commit

Permalink
Merge pull request #184 from pfrenssen/languages
Browse files Browse the repository at this point in the history
Add support for testing languages
  • Loading branch information
jhedstrom committed Jul 9, 2015
2 parents f02ab90 + b4c9834 commit f26a57a
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 0 deletions.
21 changes: 21 additions & 0 deletions features/language.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@api @d7 @d8
Feature: Language support
In order to demonstrate the language integration
As a developer for the Behat Extension
I need to provide test cases for the language support

# These test scenarios assume to have a clean installation of the "standard"
# profile and that the "behat_test" module from the "fixtures/" folder is
# enabled on the site.

Scenario: Enable multiple languages
Given the following languages are available:
| languages |
| en |
| fr |
| de |
And I am logged in as a user with the 'administrator' role
When I go to "admin/config/regional/language"
Then I should see "English"
And I should see "French"
And I should see "German"
1 change: 1 addition & 0 deletions fixtures/drupal7/modules/behat_test/behat_test.info
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies[] = entityreference
dependencies[] = features
dependencies[] = link
dependencies[] = list
dependencies[] = locale
dependencies[] = node
dependencies[] = options
dependencies[] = taxonomy
Expand Down
2 changes: 2 additions & 0 deletions fixtures/drupal8/modules/behat_test/behat_test.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ package: Test
version: '8.x-1.x-dev'
core: '8.x'
project: 'behat_test'
dependencies:
- language
23 changes: 23 additions & 0 deletions src/Drupal/DrupalExtension/Context/DrupalContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,29 @@ public function createTerms($vocabulary, TableNode $termsTable) {
}
}

/**
* Creates one or more languages.
*
* @Given the/these (following )languages are available:
*
* Provide language data in the following format:
*
* | langcode |
* | en |
* | fr |
*
* @param TableNode $langcodesTable
* The table listing languages by their ISO code.
*/
public function createLanguages(TableNode $langcodesTable) {
foreach ($langcodesTable->getHash() as $row) {
$language = (object) array(
'langcode' => $row['languages'],
);
$this->languageCreate($language);
}
}

/**
* Pauses the scenario until the user presses a key. Useful when debugging a scenario.
*
Expand Down
42 changes: 42 additions & 0 deletions src/Drupal/DrupalExtension/Context/RawDrupalContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

use Drupal\DrupalDriverManager;

use Drupal\DrupalExtension\Hook\Scope\AfterLanguageEnableScope;
use Drupal\DrupalExtension\Hook\Scope\AfterNodeCreateScope;
use Drupal\DrupalExtension\Hook\Scope\AfterTermCreateScope;
use Drupal\DrupalExtension\Hook\Scope\AfterUserCreateScope;
use Drupal\DrupalExtension\Hook\Scope\BaseEntityScope;
use Drupal\DrupalExtension\Hook\Scope\BeforeLanguageEnableScope;
use Drupal\DrupalExtension\Hook\Scope\BeforeNodeCreateScope;
use Drupal\DrupalExtension\Hook\Scope\BeforeUserCreateScope;
use Drupal\DrupalExtension\Hook\Scope\BeforeTermCreateScope;
Expand Down Expand Up @@ -79,6 +81,13 @@ class RawDrupalContext extends RawMinkContext implements DrupalAwareInterface {
*/
protected $roles = array();

/**
* Keep track of any languages that are created so they can easily be removed.
*
* @var array
*/
protected $languages = array();

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -205,6 +214,19 @@ public function cleanRoles() {
$this->roles = array();
}

/**
* Remove any created languages.
*
* @AfterScenario
*/
public function cleanLanguages() {
// Delete any languages that were created.
foreach ($this->languages as $language) {
$this->getDriver()->languageDelete($language);
unset($this->languages[$language->langcode]);
}
}

/**
* Dispatch scope hooks.
*
Expand Down Expand Up @@ -294,6 +316,26 @@ public function termCreate($term) {
return $saved;
}

/**
* Creates a language.
*
* @param \stdClass $language
* An object with the following properties:
* - langcode: the langcode of the language to create.
*
* @return object|FALSE
* The created language, or FALSE if the language was already created.
*/
public function languageCreate(\stdClass $language) {
$this->dispatchHooks('BeforeLanguageCreateScope', $language);
$language = $this->getDriver()->languageCreate($language);
if ($language) {
$this->dispatchHooks('AfterLanguageCreateScope', $language);
$this->languages[$language->langcode] = $language;
}
return $language;
}

/**
* Log-in the current user.
*/
Expand Down
23 changes: 23 additions & 0 deletions src/Drupal/DrupalExtension/Hook/Scope/AfterLanguageCreateScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @file
* Contains \Drupal\DrupalExtension\Hook\Scope\AfterLanguageCreateScope.
*/
namespace Drupal\DrupalExtension\Hook\Scope;

/**
* Represents a language hook scope.
*/
final class AfterLanguageCreateScope extends LanguageScope {

/**
* Return the scope name.
*
* @return string
*/
public function getName() {
return self::AFTER;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @file
* Contains \Drupal\DrupalExtension\Hook\Scope\BeforeLanguageCreateScope.
*/
namespace Drupal\DrupalExtension\Hook\Scope;

/**
* Represents a language hook scope.
*/
final class BeforeLanguageCreateScope extends LanguageScope {

/**
* Returns the scope name.
*
* @return string
*/
public function getName() {
return self::BEFORE;
}

}
17 changes: 17 additions & 0 deletions src/Drupal/DrupalExtension/Hook/Scope/LanguageScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @file
* Contains \Drupal\DrupalExtension\Hook\Scope\LanguageScope.
*/
namespace Drupal\DrupalExtension\Hook\Scope;

/**
* Represents the LanguageScope object.
*/
abstract class LanguageScope extends BaseEntityScope {

const BEFORE = 'language.create.before';
const AFTER = 'language.create.after';

}

0 comments on commit f26a57a

Please sign in to comment.