You are here

class WebformOptionsHelperTest in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Unit/Utility/WebformOptionsHelperTest.php \Drupal\Tests\webform\Unit\Utility\WebformOptionsHelperTest

Tests webform options utility.

@group webform

@coversDefaultClass \Drupal\webform\Utility\WebformOptionsHelper

Hierarchy

Expanded class hierarchy of WebformOptionsHelperTest

File

tests/src/Unit/Utility/WebformOptionsHelperTest.php, line 15

Namespace

Drupal\Tests\webform\Unit\Utility
View source
class WebformOptionsHelperTest extends UnitTestCase {

  /**
   * Tests WebformOptionsHelper::hasOption().
   *
   * @param string $value
   *   The value to run through WebformOptionsHelper::hasOption().
   * @param array $options
   *   The array to run through WebformOptionsHelper::hasOption().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see WebformOptionsHelper::hasOption()
   *
   * @dataProvider providerHasOption
   */
  public function testHasOption($value, array $options, $expected) {
    $result = WebformOptionsHelper::hasOption($value, $options);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testHasOption().
   *
   * @see testHasOption()
   */
  public function providerHasOption() {
    $tests[] = [
      'value',
      [
        'value' => 'text',
      ],
      TRUE,
    ];
    $tests[] = [
      'value',
      [],
      FALSE,
    ];
    $tests[] = [
      3,
      [
        1 => 'One',
        2 => 'Two',
        'optgroup' => [
          3 => 'Three',
        ],
      ],
      TRUE,
    ];
    $tests[] = [
      '3',
      [
        1 => 'One',
        2 => 'Two',
        'optgroup' => [
          3 => 'Three',
        ],
      ],
      TRUE,
    ];
    $tests[] = [
      'optgroup',
      [
        1 => 'One',
        2 => 'Two',
        'optgroup' => [
          3 => 'Three',
        ],
      ],
      FALSE,
    ];
    return $tests;
  }

  /**
   * Tests WebformOptionsHelper::getOptionsText().
   *
   * @param array $values
   *   The array to run through WebformOptionsHelper::getOptionsText().
   * @param array $options
   *   The array to run through WebformOptionsHelper::getOptionsText().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see WebformOptionsHelper::getOptionsText()
   *
   * @dataProvider providerGetOptionsText
   */
  public function testGetOptionsText(array $values, array $options, $expected) {
    $result = WebformOptionsHelper::getOptionsText($values, $options);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testGetOptionsText().
   *
   * @see testGetOptionsText()
   */
  public function providerGetOptionsText() {
    $tests[] = [
      [
        'value',
      ],
      [
        'value' => 'text',
      ],
      [
        'text',
      ],
    ];
    $tests[] = [
      [
        1,
        3,
      ],
      [
        1 => 'One',
        2 => 'Two',
        'optgroup' => [
          3 => 'Three',
        ],
      ],
      [
        'One',
        'Three',
      ],
    ];
    $tests[] = [
      [
        2,
      ],
      [
        'optgroup1' => [
          1 => 'One',
        ],
        'optgroup2' => [
          2 => 'Two',
        ],
      ],
      [
        'Two',
      ],
    ];
    return $tests;
  }

  /**
   * Tests WebformOptionsHelper::convertOptionsToString().
   *
   * @param array $options
   *   The array to run through WebformOptionsHelper::range().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see WebformOptionsHelper::convertOptionsToString()
   *
   * @dataProvider providerConvertOptionsToString
   */
  public function testConvertOptionsToString(array $options, $expected) {
    $result = WebformOptionsHelper::convertOptionsToString($options);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testConvertOptionsToString().
   *
   * @see testConvertOptionsToString()
   */
  public function providerConvertOptionsToString() {
    $tests[] = [
      [
        99 => 99,
      ],
      [
        '99' => 99,
      ],
    ];
    $tests[] = [
      [
        99.11 => 99,
      ],
      [
        '99' => 99,
      ],
    ];
    $tests[] = [
      [
        TRUE => 99,
      ],
      [
        '1' => 99,
      ],
    ];
    return $tests;
  }

  /**
   * Tests WebformOptionsHelper::range().
   *
   * @param array $element
   *   The array to run through WebformOptionsHelper::range().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see WebformOptionsHelper::range()
   *
   * @dataProvider providerRange
   */
  public function testRange(array $element, $expected) {
    $element += [
      '#min' => 1,
      '#max' => 100,
      '#step' => 1,
      '#pad_length' => NULL,
      '#pad_str' => 0,
    ];
    $result = WebformOptionsHelper::range($element['#min'], $element['#max'], $element['#step'], $element['#pad_length'], $element['#pad_str']);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testRange().
   *
   * @see testRange()
   */
  public function providerRange() {
    $tests[] = [
      [
        '#min' => 1,
        '#max' => 3,
      ],
      [
        1 => 1,
        2 => 2,
        3 => 3,
      ],
    ];
    $tests[] = [
      [
        '#min' => 0,
        '#max' => 6,
        '#step' => 2,
      ],
      [
        0 => 0,
        2 => 2,
        4 => 4,
        6 => 6,
      ],
    ];
    $tests[] = [
      [
        '#min' => 'A',
        '#max' => 'C',
      ],
      [
        'A' => 'A',
        'B' => 'B',
        'C' => 'C',
      ],
    ];
    $tests[] = [
      [
        '#min' => 'a',
        '#max' => 'c',
      ],
      [
        'a' => 'a',
        'b' => 'b',
        'c' => 'c',
      ],
    ];
    $tests[] = [
      [
        '#min' => 1,
        '#max' => 3,
        '#step' => 1,
        '#pad_length' => 2,
        '#pad_str' => 0,
      ],
      [
        '01' => '01',
        '02' => '02',
        '03' => '03',
      ],
    ];
    return $tests;
  }

  /**
   * Tests WebformOptionsHelper::encodeConfig().
   *
   * @param array $options
   *   The options array to run through WebformOptionsHelper::encodeConfig().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see WebformOptionsHelper::encodeConfig()
   *
   * @dataProvider providerEncodeConfig
   */
  public function testEncodeConfig(array $options, $expected) {
    $result = WebformOptionsHelper::encodeConfig($options);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testEncodeConfig().
   *
   * @see testEncodeConfig()
   */
  public function providerEncodeConfig() {
    $tests[] = [
      [
        'one' => 1,
        'two' => 2,
        'with.period' => 'with period',
      ],
      [
        [
          'value' => 'one',
          'text' => 1,
        ],
        [
          'value' => 'two',
          'text' => 2,
        ],
        [
          'value' => 'with.period',
          'text' => 'with period',
        ],
      ],
    ];
    return $tests;
  }

  /**
   * Tests WebformOptionsHelper::decodeConfig().
   *
   * @param array $options
   *   The options array to run through WebformOptionsHelper::decodeConfig().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see WebformOptionsHelper::decodeConfig()
   *
   * @dataProvider providerDecodeConfig
   */
  public function testDecodeConfig(array $options, $expected) {
    $result = WebformOptionsHelper::decodeConfig($options);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testDecodeConfig().
   *
   * @see testDecodeConfig()
   */
  public function providerDecodeConfig() {
    $tests[] = [
      [
        [
          'value' => 'one',
          'text' => 1,
        ],
        [
          'value' => 'two',
          'text' => 2,
        ],
        [
          'value' => 'with.period',
          'text' => 'with period',
        ],
      ],
      [
        'one' => 1,
        'two' => 2,
        'with.period' => 'with period',
      ],
    ];
    return $tests;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340
WebformOptionsHelperTest::providerConvertOptionsToString public function Data provider for testConvertOptionsToString().
WebformOptionsHelperTest::providerDecodeConfig public function Data provider for testDecodeConfig().
WebformOptionsHelperTest::providerEncodeConfig public function Data provider for testEncodeConfig().
WebformOptionsHelperTest::providerGetOptionsText public function Data provider for testGetOptionsText().
WebformOptionsHelperTest::providerHasOption public function Data provider for testHasOption().
WebformOptionsHelperTest::providerRange public function Data provider for testRange().
WebformOptionsHelperTest::testConvertOptionsToString public function Tests WebformOptionsHelper::convertOptionsToString().
WebformOptionsHelperTest::testDecodeConfig public function Tests WebformOptionsHelper::decodeConfig().
WebformOptionsHelperTest::testEncodeConfig public function Tests WebformOptionsHelper::encodeConfig().
WebformOptionsHelperTest::testGetOptionsText public function Tests WebformOptionsHelper::getOptionsText().
WebformOptionsHelperTest::testHasOption public function Tests WebformOptionsHelper::hasOption().
WebformOptionsHelperTest::testRange public function Tests WebformOptionsHelper::range().