You are here

class RouteParametersWebformSourceEntityTest in Webform 8.5

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

Tests the "route_parameters" webform source entity plugin.

@group webform

@coversDefaultClass \Drupal\webform\Plugin\WebformSourceEntity\RouteParametersWebformSourceEntity

Hierarchy

Expanded class hierarchy of RouteParametersWebformSourceEntityTest

File

tests/src/Unit/Plugin/WebformSourceEntity/RouteParametersWebformSourceEntityTest.php, line 18

Namespace

Drupal\Tests\webform\Unit\Plugin\WebformSourceEntity
View source
class RouteParametersWebformSourceEntityTest extends UnitTestCase {

  /**
   * Tests detection of source entity via route parameters.
   *
   * @param array $route_parameters
   *   Route parameters array to inject. You may use the following magic values:
   *   - source_entity: to denote that this parameter should contain source
   *     entity
   *   - another_entity: to denote that this parameter should contain some other
   *     entity.
   * @param array $ignored_types
   *   Array of entity types that may not be source.
   * @param bool $expect_source_entity
   *   Whether we expect the tested method to return the source entity.
   * @param string $assert_message
   *   Assert message to use.
   *
   * @see RouteParametersWebformSourceEntity::getSourceEntity()
   *
   * @dataProvider providerGetCurrentSourceEntity
   */
  public function testGetCurrentSourceEntity(array $route_parameters, array $ignored_types, $expect_source_entity, $assert_message = '') {
    $route_match = $this
      ->getMockBuilder(RouteMatchInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $source_entity = $this
      ->getMockBuilder(EntityInterface::class)
      ->getMock();

    // Process $route_parameters to unwrap it with real objects.
    if (isset($route_parameters['source_entity'])) {
      $route_parameters['source_entity'] = $source_entity;
    }
    if (isset($route_parameters['another_entity'])) {
      $route_parameters['another_entity'] = $this
        ->getMockBuilder(EntityInterface::class)
        ->getMock();
    }
    $route_match
      ->method('getParameters')
      ->willReturn(new ParameterBag($route_parameters));
    $plugin = new RouteParametersWebformSourceEntity([], 'route_parameters', [], $route_match);
    $output = $plugin
      ->getSourceEntity($ignored_types);
    if ($expect_source_entity) {
      $this
        ->assertSame($source_entity, $output, $assert_message);
    }
    else {
      $this
        ->assertNull($output, $assert_message);
    }
  }

  /**
   * Data provider for testGetCurrentSourceEntity().
   *
   * @see testGetCurrentSourceEntity()
   */
  public function providerGetCurrentSourceEntity() {
    $tests[] = [
      [],
      [],
      FALSE,
      'Empty parameters',
    ];
    $tests[] = [
      [
        'source_entity' => 1,
      ],
      [],
      TRUE,
      'Just source entity in the parameters',
    ];
    $tests[] = [
      [
        'source_entity' => 1,
      ],
      [
        'source_entity',
      ],
      FALSE,
      'Source entity in the parameters but forced to ignore',
    ];
    $tests[] = [
      [
        'another_entity' => 1,
        'source_entity' => 1,
        'static_value' => 'static_value',
      ],
      [],
      TRUE,
      'Testing the order in which parameters are scanned',
    ];
    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.
RouteParametersWebformSourceEntityTest::providerGetCurrentSourceEntity public function Data provider for testGetCurrentSourceEntity().
RouteParametersWebformSourceEntityTest::testGetCurrentSourceEntity public function Tests detection of source entity via route parameters.
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