You are here

public function RandomTest::testRandomWordValidator in Drupal 9

Tests random word.

@covers ::word

File

core/tests/Drupal/Tests/Component/Utility/RandomTest.php, line 150

Class

RandomTest
Tests random data generation.

Namespace

Drupal\Tests\Component\Utility

Code

public function testRandomWordValidator() {
  $random = new Random();

  // Without a seed, test a different word is returned each time.
  $this->firstStringGenerated = $random
    ->word(5);
  $next_str = $random
    ->word(5);
  $this
    ->assertNotEquals($this->firstStringGenerated, $next_str);

  // With a seed, test the same word is returned each time.
  mt_srand(0);
  $this->firstStringGenerated = $random
    ->word(5);
  mt_srand(0);
  $next_str = $random
    ->word(5);
  $this
    ->assertEquals($this->firstStringGenerated, $next_str);
}