You are here

public function RouteParametersWebformSourceEntityTest::testGetCurrentSourceEntity in Webform 6.x

Same name and namespace in other branches
  1. 8.5 tests/src/Unit/Plugin/WebformSourceEntity/RouteParametersWebformSourceEntityTest.php \Drupal\Tests\webform\Unit\Plugin\WebformSourceEntity\RouteParametersWebformSourceEntityTest::testGetCurrentSourceEntity()

Tests detection of source entity via route parameters.

@dataProvider providerGetCurrentSourceEntity

Parameters

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.

array $ignored_types: Array of entity types that may not be source.

bool $expect_source_entity: Whether we expect the tested method to return the source entity.

string $assert_message: Assert message to use.

See also

RouteParametersWebformSourceEntity::getSourceEntity()

File

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

Class

RouteParametersWebformSourceEntityTest
Tests the "route_parameters" webform source entity plugin.

Namespace

Drupal\Tests\webform\Unit\Plugin\WebformSourceEntity

Code

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));

  // Build container.
  $container = new ContainerBuilder();
  $container
    ->set('current_route_match', $route_match);

  /**************************************************************************/
  $plugin = RouteParametersWebformSourceEntity::create($container, [], 'route_parameters', []);
  $output = $plugin
    ->getSourceEntity($ignored_types);
  if ($expect_source_entity) {
    $this
      ->assertSame($source_entity, $output, $assert_message);
  }
  else {
    $this
      ->assertNull($output, $assert_message);
  }
}