Skip to content

Commit

Permalink
Issue #115: Random User creation for register and login.
Browse files Browse the repository at this point in the history
  • Loading branch information
keopx committed Oct 27, 2021
1 parent c5dcbc4 commit 8e5e6e2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ default:
scroll_offset: -100
- Metadrop\Behat\Context\UrlContext
- Metadrop\Behat\Context\UsersContext
- Metadrop\Behat\Context\UsersRandomContext
- Metadrop\Behat\Context\WaitingContext

filters:
Expand Down
84 changes: 84 additions & 0 deletions src/Behat/Context/UsersRandomContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Metadrop\Behat\Context;

use Behat\Gherkin\Node\TableNode;
use Drupal\DrupalExtension\Context\DrupalContext;

class UsersRandomContext extends DrupalContext {

/**
* Random users generated.
*
* @var array
*/
protected $randomUsers = [];

/**
* Generate random users.
*
* Do not use spaces or special characters.
* Example:
* Given random users:
* | name |
* | debug |
* | email_test2 |
*
* @param \Behat\Gherkin\Node\TableNode $user_names_table
* Names to identify random users.
*
* @Given random users:
*/
public function generateRandomUsers(TableNode $user_names_table) {
$hash = $user_names_table->getHash();
$mail_names = array_map(function ($a) {
return array_pop($a);
}, $hash);
foreach (array_values($mail_names) as $name) {
$prefix = preg_replace("([^\w\_\d])", '', $name);
$uuid = str_replace('-', '', \Drupal::service('uuid')->generate());
$this->randomUsers[$prefix] = [
'email' => $prefix . '+' . $uuid . '@metadrop.net',
'username' => $prefix . '_' . $uuid,
'password' => $prefix . '_' . $uuid,
];
}
}

/**
* Step for random user emails.
*
* @Then I fill in :arg1 with random email :arg2
*/
public function iFillInWithRandomMail($field, $random_mail_name) {
if (isset($this->randomUsers[$random_mail_name])) {
$random_mail = $this->randomUsers[$random_mail_name];
$this->minkContext->fillField($field, $random_mail['email']);
}
}

/**
* Step for random user usernames.
*
* @Then I fill in :arg1 with random username :arg2
*/
public function iFillInWithRandomUserName($field, $random_mail_name) {
if (isset($this->randomUsers[$random_mail_name])) {
$random_mail = $this->randomUsers[$random_mail_name];
$this->minkContext->fillField($field, $random_mail['username']);
}
}

/**
* Step for random user password.
*
* @Then I fill in :arg1 with random password :arg2
*/
public function iFillInWithRandomUserPassword($field, $random_mail_name) {
if (isset($this->randomUsers[$random_mail_name])) {
$random_mail = $this->randomUsers[$random_mail_name];
$this->minkContext->fillField($field, $random_mail['username']);
}
}

}

0 comments on commit 8e5e6e2

Please sign in to comment.