You are here

public static function TestBase::generatePermutations in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/TestBase.php \Drupal\simpletest\TestBase::generatePermutations()

Converts a list of possible parameters into a stack of permutations.

Takes a list of parameters containing possible values, and converts all of them into a list of items containing every possible permutation.

Example:

$parameters = array(
  'one' => array(
    0,
    1,
  ),
  'two' => array(
    2,
    3,
  ),
);
$permutations = TestBase::generatePermutations($parameters);

// Result:
$permutations == array(
  array(
    'one' => 0,
    'two' => 2,
  ),
  array(
    'one' => 1,
    'two' => 2,
  ),
  array(
    'one' => 0,
    'two' => 3,
  ),
  array(
    'one' => 1,
    'two' => 3,
  ),
);

Parameters

$parameters: An associative array of parameters, keyed by parameter name, and whose values are arrays of parameter values.

Return value

A list of permutations, which is an array of arrays. Each inner array contains the full list of parameters that have been passed, but with a single value only.

5 calls to TestBase::generatePermutations()
CommentCSSTest::testCommentClasses in core/modules/comment/src/Tests/CommentCSSTest.php
Tests CSS classes on comments.
CommentFieldAccessTest::testAccessToAdministrativeFields in core/modules/comment/src/Tests/CommentFieldAccessTest.php
Test permissions on comment fields.
CommentLinkBuilderTest::getLinkCombinations in core/modules/comment/tests/src/Unit/CommentLinkBuilderTest.php
Data provider for ::testCommentLinkBuilder.
NodeRevisionPermissionsTest::testNodeRevisionAccessAnyType in core/modules/node/src/Tests/NodeRevisionPermissionsTest.php
Tests general revision access permissions.
NodeRevisionPermissionsTest::testNodeRevisionAccessPerType in core/modules/node/src/Tests/NodeRevisionPermissionsTest.php
Tests revision access permissions for a specific content type.

File

core/modules/simpletest/src/TestBase.php, line 1533
Contains \Drupal\simpletest\TestBase.

Class

TestBase
Base class for Drupal tests.

Namespace

Drupal\simpletest

Code

public static function generatePermutations($parameters) {
  $all_permutations = array(
    array(),
  );
  foreach ($parameters as $parameter => $values) {
    $new_permutations = array();

    // Iterate over all values of the parameter.
    foreach ($values as $value) {

      // Iterate over all existing permutations.
      foreach ($all_permutations as $permutation) {

        // Add the new parameter value to existing permutations.
        $new_permutations[] = $permutation + array(
          $parameter => $value,
        );
      }
    }

    // Replace the old permutations with the new permutations.
    $all_permutations = $new_permutations;
  }
  return $all_permutations;
}