You are here

protected function UniqueFieldAjaxBase::createRandomData in Unique field ajax 2.x

Helper method to create random data.

Parameters

string $type: Type of random data.

Return value

false|string|string[] Random data.

8 calls to UniqueFieldAjaxBase::createRandomData()
UniqueFieldAjaxBase::createField in tests/src/Functional/UniqueFieldAjaxBase.php
Helper method to create a field to use.
UniqueFieldAjaxBase::createUpdateFieldData in tests/src/Functional/UniqueFieldAjaxBase.php
Helper method to create custom update edit data for fields.
UniqueFieldAjaxTest::testUniqueFieldCustomMessage in tests/src/Functional/UniqueFieldAjaxTest.php
Tests unique field custom message.
UniqueFieldAjaxTest::testUniqueFieldCustomMessageWithLabelToken in tests/src/Functional/UniqueFieldAjaxTest.php
Tests unique field custom message with an added label token.
UniqueFieldAjaxTest::testUniqueFieldCustomMessageWithLinkToken in tests/src/Functional/UniqueFieldAjaxTest.php
Tests unique field custom message with an added link token.

... See full list

File

tests/src/Functional/UniqueFieldAjaxBase.php, line 410

Class

UniqueFieldAjaxBase
The base testing class for unique_field_ajax.

Namespace

Drupal\Tests\unique_field_ajax\Functional

Code

protected function createRandomData(string $type = 'string') {
  $return = '';
  switch ($type) {
    case 'string':
      $return = mb_strtolower($this
        ->randomMachineName());
      break;
    case 'sentence':
      $length = 200;
      $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
      $return = substr(str_shuffle(str_repeat($chars, ceil($length / strlen($chars)))), 1, $length);
      $return = wordwrap($return, rand(3, 10), ' ', TRUE);
      break;
    case 'link':
      $return = 'https://www.' . $this
        ->createRandomData() . '.com/';
      break;
    case 'email':
      $return = $this
        ->createRandomData() . '@' . $this
        ->createRandomData() . '.com';
      break;
    case 'integer':
      try {
        $return = random_int(0, 9999);
      } catch (\Exception $e) {
        die('Could not generate random int');
      }
      break;
    case 'decimal':
      $min = 0;
      $max = 9999;
      $decimals = 2;
      $scale = pow(10, $decimals);
      $return = mt_rand($min * $scale, $max * $scale) / $scale;
      break;
  }
  return $return;
}