Verified Commit 7ac5f521 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3185183 by philipnorton42, larowlan, bruno.bicudo, longwave, xjm,...

Issue #3185183 by philipnorton42, larowlan, bruno.bicudo, longwave, xjm, quietone: Refactor ContextualUnitTest to use a data provider
parent 3664a3d4
Loading
Loading
Loading
Loading
+45 −46
Original line number Diff line number Diff line
@@ -5,8 +5,7 @@
use Drupal\KernelTests\KernelTestBase;

/**
 * Tests all edge cases of converting from #contextual_links to ids and vice
 * versa.
 * Tests edge cases for converting between contextual links and IDs.
 *
 * @group contextual
 */
@@ -23,14 +22,13 @@ class ContextualUnitTest extends KernelTestBase {
   * Provides testcases for both test functions.
   *
   * Used in testContextualLinksToId() and testContextualIdToLinks().
   *
   * @return array[]
   *   Test cases.
   */
  public function _contextual_links_id_testcases() {
    // Test branch conditions:
    // - one group.
    // - one dynamic path argument.
    // - no metadata.
    $tests[] = [
      'links' => [
  public function contextualLinksDataProvider(): array {
    $tests['one group, one dynamic path argument, no metadata'] = [
      [
        'node' => [
          'route_parameters' => [
            'node' => '14031991',
@@ -38,33 +36,25 @@ public function _contextual_links_id_testcases() {
          'metadata' => ['langcode' => 'en'],
        ],
      ],
      'id' => 'node:node=14031991:langcode=en',
      'node:node=14031991:langcode=en',
    ];

    // Test branch conditions:
    // - one group.
    // - multiple dynamic path arguments.
    // - no metadata.
    $tests[] = [
      'links' => [
    $tests['one group, multiple dynamic path arguments, no metadata'] = [
      [
        'foo' => [
          'route_parameters' => [
            'bar',
            0 => 'bar',
            'key' => 'baz',
            'qux',
            1 => 'qux',
          ],
          'metadata' => ['langcode' => 'en'],
        ],
      ],
      'id' => 'foo:0=bar&key=baz&1=qux:langcode=en',
      'foo:0=bar&key=baz&1=qux:langcode=en',
    ];

    // Test branch conditions:
    // - one group.
    // - one dynamic path argument.
    // - metadata.
    $tests[] = [
      'links' => [
    $tests['one group, one dynamic path argument, metadata'] = [
      [
        'views_ui_edit' => [
          'route_parameters' => [
            'view' => 'frontpage',
@@ -76,14 +66,11 @@ public function _contextual_links_id_testcases() {
          ],
        ],
      ],
      'id' => 'views_ui_edit:view=frontpage:location=page&display=page_1&langcode=en',
      'views_ui_edit:view=frontpage:location=page&display=page_1&langcode=en',
    ];

    // Test branch conditions:
    // - multiple groups.
    // - multiple dynamic path arguments.
    $tests[] = [
      'links' => [
    $tests['multiple groups, multiple dynamic path arguments'] = [
      [
        'node' => [
          'route_parameters' => [
            'node' => '14031991',
@@ -92,9 +79,9 @@ public function _contextual_links_id_testcases() {
        ],
        'foo' => [
          'route_parameters' => [
            'bar',
            0 => 'bar',
            'key' => 'baz',
            'qux',
            1 => 'qux',
          ],
          'metadata' => ['langcode' => 'en'],
        ],
@@ -103,30 +90,42 @@ public function _contextual_links_id_testcases() {
          'metadata' => ['langcode' => 'en'],
        ],
      ],
      'id' => 'node:node=14031991:langcode=en|foo:0=bar&key=baz&1=qux:langcode=en|edge:0=20011988:langcode=en',
      'node:node=14031991:langcode=en|foo:0=bar&key=baz&1=qux:langcode=en|edge:0=20011988:langcode=en',
    ];

    return $tests;
  }

  /**
   * Tests _contextual_links_to_id().
   * Tests the conversion from contextual links to IDs.
   *
   * @param array $links
   *   The #contextual_links property value array.
   * @param string $id
   *   The serialized representation of the passed links.
   *
   * @covers ::_contextual_links_to_id
   *
   * @dataProvider contextualLinksDataProvider
   */
  public function testContextualLinksToId() {
    $tests = $this->_contextual_links_id_testcases();
    foreach ($tests as $test) {
      $this->assertSame($test['id'], _contextual_links_to_id($test['links']));
    }
  public function testContextualLinksToId(array $links, string $id) {
    $this->assertSame($id, _contextual_links_to_id($links));
  }

  /**
   * Tests _contextual_id_to_links().
   * Tests the conversion from contextual ID to links.
   *
   * @param array $links
   *   The #contextual_links property value array.
   * @param string $id
   *   The serialized representation of the passed links.
   *
   * @covers ::_contextual_id_to_links
   *
   * @dataProvider contextualLinksDataProvider
   */
  public function testContextualIdToLinks() {
    $tests = $this->_contextual_links_id_testcases();
    foreach ($tests as $test) {
      $this->assertSame($test['links'], _contextual_id_to_links($test['id']));
    }
  public function testContextualIdToLinks(array $links, string $id) {
    $this->assertSame($links, _contextual_id_to_links($id));
  }

}