diff --git a/src/BehatDrushHelper.php b/src/BehatDrushHelper.php index cefe319..61b5503 100644 --- a/src/BehatDrushHelper.php +++ b/src/BehatDrushHelper.php @@ -104,7 +104,7 @@ public static function DisplaySearchResults($test_id) { foreach ($logs as $feature => $steps) { BehatDrushHelper::coolLog($feature); - foreach ($steps as $step) { + foreach ($steps as $delta => $step) { if ($step['status'] == 'pass') { BehatDrushHelper::coolLog($step['step'], 'green', 1); } diff --git a/src/BehatTestsAbstract.php b/src/BehatTestsAbstract.php index 20b274c..07787e5 100644 --- a/src/BehatTestsAbstract.php +++ b/src/BehatTestsAbstract.php @@ -29,7 +29,7 @@ class BehatTestsAbstract extends BrowserTestBase { * * @var array */ - public static $modules = ['behat']; + public static $modules = ['behat', 'node']; /** * @var array diff --git a/src/Features/comment_crud.feature b/src/Features/comment_crud.feature index ae80fad..4a7595e 100644 --- a/src/Features/comment_crud.feature +++ b/src/Features/comment_crud.feature @@ -1,4 +1,6 @@ Feature: Comment crud. - Scenario: Testing the login form. - Given I visit "user" + @comment + Scenario: Testing comment creation. + Given I login as user "@user-name" + And I create a node diff --git a/src/FeaturesTraits/BasicTrait.php b/src/FeaturesTraits/BasicTrait.php index d6b21c0..09a8176 100644 --- a/src/FeaturesTraits/BasicTrait.php +++ b/src/FeaturesTraits/BasicTrait.php @@ -4,42 +4,5 @@ trait BasicTrait { - /** - * @Given /^I fill in "([^"]*)" with "([^"]*)"$/ - */ - public function iFillInWith($name, $value) { - $this->assertSession()->fieldExists($name); - $this->edit[$name] = $value; - } - - /** - * @Given /^I press "([^"]*)"$/ - */ - public function iPress($element) { - $button = $this->assertSession()->buttonExists($element); - - if ($button->getAttribute('type') == 'submit') { - // This is a submit element. Call the submit form method. - $this->submitForm($this->edit, $element); - } - else { - // Normal button. Press it. - $button->press(); - } - } - - /** - * @Given /^I should see "([^"]*)"$/ - */ - public function iShouldSee($text) { - $this->assertSession()->pageTextContains($text); - } - - /** - * @Given /^I visit "([^"]*)"$/ - */ - public function iVisit($url) { - $this->drupalGet($url); - } } diff --git a/src/Plugin/FeatureContext/FeatureContextBase.php b/src/Plugin/FeatureContext/FeatureContextBase.php index eee0f15..641a3b3 100644 --- a/src/Plugin/FeatureContext/FeatureContextBase.php +++ b/src/Plugin/FeatureContext/FeatureContextBase.php @@ -7,7 +7,9 @@ use Behat\Gherkin\Node\ScenarioInterface; use Drupal\behat\BehatTestsAbstract; -use Drupal\behat\FeaturesTraits\BasicTrait; +use Drupal\node\Entity\Node; +use Drupal\node\Entity\NodeType; +use Drupal\user\Entity\User; /** * @FeatureContext( @@ -20,7 +22,17 @@ */ class FeatureContextBase extends BehatTestsAbstract { - use BasicTrait; + /** + * @var User + * + * The user object. + */ + protected $account; + + /** + * @var Node + */ + protected $node; /** * {@inheritdoc} @@ -28,10 +40,113 @@ class FeatureContextBase extends BehatTestsAbstract { public function beforeScenario(ScenarioInterface $scenarioInterface = NULL) { parent::beforeScenario($scenarioInterface); - $account = $this->drupalCreateUser(); + $permissions = []; + if ($tags = $scenarioInterface->getTags()) { + // Keep the permissions for tests with entity. + $tests_permissions = [ + 'comment' => ['post comments'], + 'node' => ['create node'], + 'taxonomy-term' => 'create terms', + ]; + + $entity_feature = $tags[0]; + $permissions = $tests_permissions[$entity_feature]; + } + + $this->account = $this->drupalCreateUser(); $this->placeholders = [ - '@user-name' => $account->label(), - '@user-pass' => $account->passRaw, + '@user-name' => $this->account->label(), + '@user-pass' => $this->account->passRaw, ]; } + + /** + * Creates a node based on default settings. + * + * @param array $settings + * (optional) An associative array of settings for the node, as used in + * entity_create(). Override the defaults by specifying the key and value + * in the array, for example: + * @code + * $this->drupalCreateNode(array( + * 'title' => t('Hello, world!'), + * 'type' => 'article', + * )); + * @endcode + * The following defaults are provided: + * - body: Random string using the default filter format: + * @code + * $settings['body'][0] = array( + * 'value' => $this->randomMachineName(32), + * 'format' => filter_default_format(), + * ); + * @endcode + * - title: Random string. + * - type: 'page'. + * - uid: The currently logged in user, or anonymous. + * + * @return \Drupal\node\NodeInterface + * The created node entity. + */ + protected function drupalCreateNode(array $settings = array()) { + $node = entity_create('node', $settings); + $node->save(); + + return $node; + } + + /** + * @Given /^I fill in "([^"]*)" with "([^"]*)"$/ + */ + public function iFillInWith($name, $value) { + $this->assertSession()->fieldExists($name); + $this->edit[$name] = $value; + } + + /** + * @Given /^I press "([^"]*)"$/ + */ + public function iPress($element) { + $button = $this->assertSession()->buttonExists($element); + + if ($button->getAttribute('type') == 'submit') { + // This is a submit element. Call the submit form method. + $this->submitForm($this->edit, $element); + } + else { + // Normal button. Press it. + $button->press(); + } + } + + /** + * @Given /^I should see "([^"]*)"$/ + */ + public function iShouldSee($text) { + $this->assertSession()->pageTextContains($text); + } + + /** + * @Given /^I visit "([^"]*)"$/ + */ + public function iVisit($url) { + $this->drupalGet($url); + } + + /** + * @Given /^I login as user "([^"]*)"$/ + */ + public function iLogInAsUser($name) { + // todo: handle multiple users in the test. + $this->drupalLogin($this->account); + } + + /** + * @Given /^I create a node$/ + */ + public function iCreateNode() { + NodeType::create(['name' => 'page', 'type' => 'page'])->save(); + Node::create(['type' => 'page', 'title' => 'foo', 'uid' => 1])->save(); +// $this->ivisit($this->node->url()); + } }