You are here

public function QueryStringWebformSourceEntityTest::testGetCurrentSourceEntity in Webform 6.x

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

Tests detection of source entity via query string.

@dataProvider providerGetCurrentSourceEntity

Parameters

array $options: see ::providerGetCurrentSourceEntity.

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

string $assert_message: Assert message to use.

See also

QueryStringWebformSourceEntity::getSourceEntity()

File

tests/src/Unit/Plugin/WebformSourceEntity/QueryStringWebformSourceEntityTest.php, line 42

Class

QueryStringWebformSourceEntityTest
Tests the "query_string" webform source entity plugin.

Namespace

Drupal\Tests\webform\Unit\Plugin\WebformSourceEntity

Code

public function testGetCurrentSourceEntity(array $options, $expect_source_entity, $assert_message = '') {
  $options += [
    // Value for the setting 'form_prepopulate_source_entity' of the webform.
    'webform_settings_prepopulate_source_entity' => TRUE,
    // Source entity type.
    'source_entity_type' => 'node',
    // Source entity id.
    'source_entity_id' => 1,
    // Access result return by source entity 'view' operation.
    'source_entity_view_access_result' => TRUE,
    // Whether source entity has a populate webform field.
    'source_entity_has_webform_field' => TRUE,
    // Whether the source entity has translation.
    'source_entity_has_translation' => TRUE,
    // Source entity type return by request query string.
    'request_query_source_entity_type' => 'node',
    // Whether webform should be included in route object.
    'route_match_get_parameter_webform' => TRUE,
    // Array of entity types that may not be source.
    'ignored_types' => [],
  ];

  /**************************************************************************/
  $webform = $this
    ->getMockWebform($options);
  list($source_entity, $source_entity_translation) = $this
    ->getMockSourceEntity($options, $webform);

  // Mock source entity storage.
  $source_entity_storage = $this
    ->getMockBuilder(EntityStorageInterface::class)
    ->getMock();
  $source_entity_storage
    ->method('load')
    ->willReturnMap([
    [
      $options['source_entity_id'],
      $source_entity,
    ],
  ]);

  // Move entity type manager which returns the mock source entity storage.
  $entity_type_manager = $this
    ->getMockBuilder(EntityTypeManagerInterface::class)
    ->disableOriginalConstructor()
    ->getMock();
  $entity_type_manager
    ->method('hasDefinition')
    ->willReturnMap([
    [
      $options['source_entity_type'],
      TRUE,
    ],
  ]);
  $entity_type_manager
    ->method('getStorage')
    ->willReturnMap([
    [
      $options['source_entity_type'],
      $source_entity_storage,
    ],
  ]);

  // Mock route match.
  $route_match = $this
    ->getMockBuilder(RouteMatchInterface::class)
    ->disableOriginalConstructor()
    ->getMock();
  $route_match
    ->method('getParameter')
    ->willReturnMap([
    [
      'webform',
      $options['route_match_get_parameter_webform'] ? $webform : NULL,
    ],
  ]);

  // Mock request stack.
  $request_stack = $this
    ->getMockBuilder(RequestStack::class)
    ->disableOriginalConstructor()
    ->getMock();
  $request_stack
    ->method('getCurrentRequest')
    ->will($this
    ->returnValue(new Request([
    'source_entity_type' => $options['request_query_source_entity_type'],
    'source_entity_id' => $options['source_entity_id'],
  ])));

  // Move entity reference manager.
  $webform_entity_reference_manager = $this
    ->getMockBuilder(WebformEntityReferenceManagerInterface::class)
    ->disableOriginalConstructor()
    ->getMock();
  $webform_entity_reference_manager
    ->method('getFieldNames')
    ->willReturnMap([
    [
      $source_entity,
      [
        'webform_field_name',
      ],
    ],
    [
      $source_entity_translation,
      [
        'webform_field_name',
      ],
    ],
  ]);

  // Mock language manager.
  $language_manager = $this
    ->getMockBuilder(LanguageManagerInterface::class)
    ->disableOriginalConstructor()
    ->getMock();
  $language_manager
    ->method('getCurrentLanguage')
    ->willReturn(new Language([
    'id' => 'es',
  ]));

  // Build container.
  $container = new ContainerBuilder();
  $container
    ->set('entity_type.manager', $entity_type_manager);
  $container
    ->set('current_route_match', $route_match);
  $container
    ->set('request_stack', $request_stack);
  $container
    ->set('language_manager', $language_manager);
  $container
    ->set('webform.entity_reference_manager', $webform_entity_reference_manager);

  /**************************************************************************/

  // Create QueryStringWebformSourceEntity plugin instance.
  $plugin = QueryStringWebformSourceEntity::create($container, [], 'query_string', []);
  $output = $plugin
    ->getSourceEntity($options['ignored_types']);
  if ($expect_source_entity) {
    $this
      ->assertSame($options['source_entity_has_translation'] ? $source_entity_translation : $source_entity, $output, $assert_message);
  }
  else {
    $this
      ->assertNull($output, $assert_message);
  }
}