class Random
Same name in this branch
- 11.x core/modules/views/src/Plugin/views/sort/Random.php \Drupal\views\Plugin\views\sort\Random
- 11.x core/lib/Drupal/Component/Utility/Random.php \Drupal\Component\Utility\Random
Same name in other branches
- 9 core/modules/views/src/Plugin/views/sort/Random.php \Drupal\views\Plugin\views\sort\Random
- 9 core/lib/Drupal/Component/Utility/Random.php \Drupal\Component\Utility\Random
- 8.9.x core/modules/views/src/Plugin/views/sort/Random.php \Drupal\views\Plugin\views\sort\Random
- 8.9.x core/lib/Drupal/Component/Utility/Random.php \Drupal\Component\Utility\Random
- 10 core/modules/views/src/Plugin/views/sort/Random.php \Drupal\views\Plugin\views\sort\Random
- 10 core/tests/Drupal/TestTools/Random.php \Drupal\TestTools\Random
- 10 core/lib/Drupal/Component/Utility/Random.php \Drupal\Component\Utility\Random
Provides random generator utility static methods.
Hierarchy
- class \Drupal\TestTools\Random
Expanded class hierarchy of Random
20 files declare their use of Random
- ArgumentTransformTermTest.php in core/
modules/ taxonomy/ tests/ src/ Kernel/ Views/ ArgumentTransformTermTest.php - BigPipeResponseAttachmentsProcessorTest.php in core/
modules/ big_pipe/ tests/ src/ Unit/ Render/ BigPipeResponseAttachmentsProcessorTest.php - BlockPluginHasSettingsTrayFormAccessCheckTest.php in core/
modules/ settings_tray/ tests/ src/ Unit/ Access/ BlockPluginHasSettingsTrayFormAccessCheckTest.php - CacheCollectorTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Cache/ CacheCollectorTest.php - CheckpointStorageTest.php in core/
tests/ Drupal/ Tests/ Core/ Config/ Checkpoint/ CheckpointStorageTest.php
6 string references to 'Random'
- RotateImageEffect::submitConfigurationForm in core/
modules/ image/ src/ Plugin/ ImageEffect/ RotateImageEffect.php - SortRandomTest::getBasicRandomView in core/
modules/ views/ tests/ src/ Kernel/ Handler/ SortRandomTest.php - Return a basic view with random ordering.
- TaxonomyViewsFieldAccessTest::testTermFields in core/
modules/ taxonomy/ tests/ src/ Kernel/ Views/ TaxonomyViewsFieldAccessTest.php - Check access for taxonomy fields.
- views.sort.schema.yml in core/
modules/ views/ config/ schema/ views.sort.schema.yml - core/modules/views/config/schema/views.sort.schema.yml
- ViewsViewsHooks::viewsData in core/
modules/ views/ src/ Hook/ ViewsViewsHooks.php - Implements hook_views_data().
File
-
core/
tests/ Drupal/ TestTools/ Random.php, line 12
Namespace
Drupal\TestToolsView source
abstract class Random {
/**
* The random generator.
*/
protected static RandomUtility $randomGenerator;
/**
* Gets the random generator for the utility methods.
*
* @return \Drupal\Component\Utility\Random
* The random generator.
*/
public static function getGenerator() : RandomUtility {
if (!isset(static::$randomGenerator)) {
static::$randomGenerator = new RandomUtility();
}
return static::$randomGenerator;
}
/**
* Generates a pseudo-random string of ASCII characters of codes 32 to 126.
*
* Do not use this method when special characters are not possible (e.g., in
* machine or file names that have already been validated); instead, use
* \Drupal\Tests\RandomGeneratorTrait::randomMachineName(). If $length is
* greater than 3 the random string will include at least one ampersand ('&')
* and at least one greater than ('>') character to ensure coverage for
* special characters and avoid the introduction of random test failures.
*
* @param int $length
* Length of random string to generate.
*
* @return string
* Pseudo-randomly generated unique string including special characters.
*
* @see \Drupal\Component\Utility\Random::string()
*/
public static function string(int $length = 8) : string {
if ($length < 4) {
return static::getGenerator()->string($length, TRUE, [
static::class,
'stringValidate',
]);
}
// To prevent the introduction of random test failures, ensure that the
// returned string contains a character that needs to be escaped in HTML by
// injecting an ampersand into it.
$replacement_pos = intval($length / 2);
// Remove 2 from the length to account for the ampersand and greater than
// characters.
$string = static::getGenerator()->string($length - 2, TRUE, [
static::class,
'stringValidate',
]);
return substr_replace($string, '>&', $replacement_pos, 0);
}
/**
* Callback for random string validation.
*
* @see \Drupal\Component\Utility\Random::string()
*
* @param string $string
* The random string to validate.
*
* @return bool
* TRUE if the random string is valid, FALSE if not.
*/
public static function stringValidate(string $string) : bool {
// Consecutive spaces causes issues for link validation.
if (preg_match('/\\s{2,}/', $string)) {
return FALSE;
}
// Starting or ending with a space means that length might not be what is
// expected.
if (preg_match('/^\\s|\\s$/', $string)) {
return FALSE;
}
return TRUE;
}
/**
* Generates a unique random string containing letters and numbers.
*
* Do not use this method when testing non validated user input. Instead, use
* \Drupal\Tests\RandomGeneratorTrait::randomString().
*
* @param int $length
* Length of random string to generate.
*
* @return string
* Randomly generated unique string.
*
* @see \Drupal\Component\Utility\Random::name()
*/
public static function machineName(int $length = 8) : string {
return static::getGenerator()->machineName($length, TRUE);
}
/**
* Generates a random PHP object.
*
* @param int $size
* The number of random keys to add to the object.
*
* @return object
* The generated object, with the specified number of random keys. Each key
* has a random string value.
*
* @see \Drupal\Component\Utility\Random::object()
*/
public static function object(int $size = 4) : \stdClass {
return static::getGenerator()->object($size);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
Random::$randomGenerator | protected static | property | The random generator. |
Random::getGenerator | public static | function | Gets the random generator for the utility methods. |
Random::machineName | public static | function | Generates a unique random string containing letters and numbers. |
Random::object | public static | function | Generates a random PHP object. |
Random::string | public static | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. |
Random::stringValidate | public static | function | Callback for random string validation. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.