Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide some transforms for machine names/random data #293

Closed
djdevin opened this issue Jul 20, 2016 · 3 comments
Closed

provide some transforms for machine names/random data #293

djdevin opened this issue Jul 20, 2016 · 3 comments

Comments

@djdevin
Copy link

djdevin commented Jul 20, 2016

We have to work with machine and user names a lot in Drupal, so when writing 1000s of tests they'll eventually conflict. Sometimes we copy/paste tests to test slightly different scenarios and have to change all the specific references. "testuserC2456_S1" starts to get old after a while and in most cases, we don't care what that user is called, however we do need to refer back to it.

Tag1 did something in a really old custom context https://github.com/tag1consulting/behat-drupalextension

[random] - a random 8 character string, every time [random] is used in the script, a new random string is generated.
[random:1], [random:2], - a way to reference back to used random strings, so that you can put a random string on a form, and then check that it appears somewhere later. [random:1] refers to the last random value, [random:2] refers to the second to last random value.

Maybe drupalextension could provide something similar? Or does it already?

These would be great as it would take a lot of load off of testwriting.

For now I wrote a very simple transform but it could be extended with a static random string cache.

  /**
   * @Transform /^rand:machine_name$/
   */
  public function makeRandomMachineName($string) {
    return uniqid();
  }
@djdevin
Copy link
Author

djdevin commented Jul 20, 2016

Actually here it is, it's the same format as Drupal random tokens (at least in 7), with a name argument appended.

Example:

[random:hash:md5:stored_name]
[random:hash:sha256]
[random:number]
[random:number:myNumber]
<?php
  /**
   * @Transform /^\[random:hash:(\w+):(\w+)\]$/
   * @Transform /^\[random:hash:(\w+)\]$/
   * @Transform /^\[random:(number):(\w+)\]$/
   * @Transform /^\[random:(number)\]$/
   */
  public function makeRandomMachineName($hash_type, $name = NULL) {
    static $randoms = array();

    if ($name && isset($randoms[$name])) {
      return $randoms[$name];
    }

    if ($hash_type == 'number') {
      $hash = rand(0, 2147483647);
    }
    else {
      $hash = hash($hash_type, drupal_random_key());
    }

    if ($name) {
      $randoms[$name] = $hash;
    }

    return $hash;
  }

@jhedstrom
Copy link
Owner

Interesting idea. I could see this coming in quite handy (usernames in-particular are tricky if created via a form since they have to be manually deleted in an after scenario if the same username is used more than once).

I think this could go into a new context that only handles transforms.

@jhedstrom jhedstrom added this to the 4.0 release milestone Jul 20, 2016
@jhedstrom
Copy link
Owner

I'm working on a project now that uses <foo> syntax for random strings. These work in isolation (eg, as a node title in a pipe table), or in a string (<foo>@example.com). I'm not tied to the <> bit, but perhaps something similar here?

jhedstrom added a commit that referenced this issue Mar 27, 2018
```
Given I am viewing an "Article" with the title "<title>"
Then I should see the header "<title>"
```

When the `RandomContext` is included, then `<title>` will be replaced
with a random string during the scenario. It will have a consistent
value throughout a given scenario, but a new value in subsequent
scenarios.

- Fixes #293
jhedstrom added a commit that referenced this issue Mar 27, 2018
```
Given I am viewing an "Article" with the title "<title>"
Then I should see the header "<title>"
```

When the `RandomContext` is included, then `<title>` will be replaced
with a random string during the scenario. It will have a consistent
value throughout a given scenario, but a new value in subsequent
scenarios.

- Fixes #293
jhedstrom added a commit that referenced this issue Mar 28, 2018
```
Given I am viewing an "Article" with the title "<title>"
Then I should see the header "<title>"
```

When the `RandomContext` is included, then `<title>` will be replaced
with a random string during the scenario. It will have a consistent
value throughout a given scenario, but a new value in subsequent
scenarios.

- Fixes #293
jhedstrom added a commit that referenced this issue Apr 13, 2018
```
Given I am viewing an "Article" with the title "<title>"
Then I should see the header "<title>"
```

When the `RandomContext` is included, then `<title>` will be replaced
with a random string during the scenario. It will have a consistent
value throughout a given scenario, but a new value in subsequent
scenarios.

- Fixes #293
jhedstrom added a commit that referenced this issue Apr 17, 2018
```
Given I am viewing an "Article" with the title "<title>"
Then I should see the header "<title>"
```

When the `RandomContext` is included, then `<title>` will be replaced
with a random string during the scenario. It will have a consistent
value throughout a given scenario, but a new value in subsequent
scenarios.

- Fixes #293
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants